]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/db/status.py
use group() for regex matches
[nominatim.git] / nominatim / db / status.py
1 """
2 Access and helper functions for the status and status log table.
3 """
4 import datetime as dt
5 import logging
6 import re
7
8 from ..tools.exec_utils import get_url
9 from ..errors import UsageError
10
11 LOG = logging.getLogger()
12 ISODATE_FORMAT = '%Y-%m-%dT%H:%M:%S'
13
14 def compute_database_date(conn):
15     """ Determine the date of the database from the newest object in the
16         data base.
17     """
18     # First, find the node with the highest ID in the database
19     with conn.cursor() as cur:
20         osmid = cur.scalar("SELECT max(osm_id) FROM place WHERE osm_type='N'")
21
22         if osmid is None:
23             LOG.fatal("No data found in the database.")
24             raise UsageError("No data found in the database.")
25
26     LOG.info("Using node id %d for timestamp lookup", osmid)
27     # Get the node from the API to find the timestamp when it was created.
28     node_url = 'https://www.openstreetmap.org/api/0.6/node/{}/1'.format(osmid)
29     data = get_url(node_url)
30
31     match = re.search(r'timestamp="((\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}))Z"', data)
32
33     if match is None:
34         LOG.fatal("The node data downloaded from the API does not contain valid data.\n"
35                   "URL used: %s", node_url)
36         raise UsageError("Bad API data.")
37
38     LOG.debug("Found timestamp %s", match.group(1))
39
40     return dt.datetime.strptime(match.group(1), ISODATE_FORMAT).replace(tzinfo=dt.timezone.utc)
41
42
43 def set_status(conn, date, seq=None, indexed=True):
44     """ Replace the current status with the given status. If date is `None`
45         then only sequence and indexed will be updated as given. Otherwise
46         the whole status is replaced.
47     """
48     assert date is None or date.tzinfo == dt.timezone.utc
49     with conn.cursor() as cur:
50         if date is None:
51             cur.execute("UPDATE import_status set sequence_id = %s, indexed = %s",
52                         (seq, indexed))
53         else:
54             cur.execute("TRUNCATE TABLE import_status")
55             cur.execute("""INSERT INTO import_status (lastimportdate, sequence_id, indexed)
56                            VALUES (%s, %s, %s)""", (date, seq, indexed))
57
58     conn.commit()
59
60
61 def get_status(conn):
62     """ Return the current status as a triple of (date, sequence, indexed).
63         If status has not been set up yet, a triple of None is returned.
64     """
65     with conn.cursor() as cur:
66         cur.execute("SELECT * FROM import_status LIMIT 1")
67         if cur.rowcount < 1:
68             return None, None, None
69
70         row = cur.fetchone()
71         return row['lastimportdate'], row['sequence_id'], row['indexed']
72
73
74 def set_indexed(conn, state):
75     """ Set the indexed flag in the status table to the given state.
76     """
77     with conn.cursor() as cur:
78         cur.execute("UPDATE import_status SET indexed = %s", (state, ))
79     conn.commit()
80
81
82 def log_status(conn, start, event, batchsize=None):
83     """ Write a new status line to the `import_osmosis_log` table.
84     """
85     with conn.cursor() as cur:
86         cur.execute("""INSERT INTO import_osmosis_log
87                        (batchend, batchseq, batchsize, starttime, endtime, event)
88                        SELECT lastimportdate, sequence_id, %s, %s, now(), %s FROM import_status""",
89                     (batchsize, start, event))