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