]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/tools/admin.py
replace add-data function with native Python code
[nominatim.git] / nominatim / tools / admin.py
1 """
2 Functions for database analysis and maintenance.
3 """
4 import logging
5
6 from nominatim.errors import UsageError
7
8 LOG = logging.getLogger()
9
10 def analyse_indexing(conn, osm_id=None, place_id=None):
11     """ Analyse indexing of a single Nominatim object.
12     """
13     with conn.cursor() as cur:
14         if osm_id:
15             osm_type = osm_id[0].upper()
16             if osm_type not in 'NWR' or not osm_id[1:].isdigit():
17                 LOG.fatal('OSM ID must be of form <N|W|R><id>. Got: %s', osm_id)
18                 raise UsageError("OSM ID parameter badly formatted")
19             cur.execute('SELECT place_id FROM placex WHERE osm_type = %s AND osm_id = %s',
20                         (osm_type, osm_id[1:]))
21
22             if cur.rowcount < 1:
23                 LOG.fatal("OSM object %s not found in database.", osm_id)
24                 raise UsageError("OSM object not found")
25
26             place_id = cur.fetchone()[0]
27
28         if place_id is None:
29             LOG.fatal("No OSM object given to index.")
30             raise UsageError("OSM object not found")
31
32         cur.execute("update placex set indexed_status = 2 where place_id = %s",
33                     (place_id, ))
34
35         cur.execute("""SET auto_explain.log_min_duration = '0';
36                        SET auto_explain.log_analyze = 'true';
37                        SET auto_explain.log_nested_statements = 'true';
38                        LOAD 'auto_explain';
39                        SET client_min_messages = LOG;
40                        SET log_min_messages = FATAL""")
41
42         cur.execute("update placex set indexed_status = 0 where place_id = %s",
43                     (place_id, ))
44
45     # we do not want to keep the results
46     conn.rollback()
47
48     for msg in conn.notices:
49         print(msg)