]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/clicmd/admin.py
Merge pull request #2186 from lonvia/port-import-to-python
[nominatim.git] / nominatim / clicmd / admin.py
1 """
2 Implementation of the 'admin' subcommand.
3 """
4 import logging
5
6 from ..tools.exec_utils import run_legacy_script
7 from ..db.connection import connect
8
9 # Do not repeat documentation of subcommand classes.
10 # pylint: disable=C0111
11 # Using non-top-level imports to avoid eventually unused imports.
12 # pylint: disable=E0012,C0415
13
14 LOG = logging.getLogger()
15
16 class AdminFuncs:
17     """\
18     Analyse and maintain the database.
19     """
20
21     @staticmethod
22     def add_args(parser):
23         group = parser.add_argument_group('Admin task arguments')
24         group.add_argument('--warm', action='store_true',
25                            help='Warm database caches for search and reverse queries.')
26         group.add_argument('--check-database', action='store_true',
27                            help='Check that the database is complete and operational.')
28         group.add_argument('--analyse-indexing', action='store_true',
29                            help='Print performance analysis of the indexing process.')
30         group = parser.add_argument_group('Arguments for cache warming')
31         group.add_argument('--search-only', action='store_const', dest='target',
32                            const='search',
33                            help="Only pre-warm tables for search queries")
34         group.add_argument('--reverse-only', action='store_const', dest='target',
35                            const='reverse',
36                            help="Only pre-warm tables for reverse queries")
37         group = parser.add_argument_group('Arguments for index anaysis')
38         mgroup = group.add_mutually_exclusive_group()
39         mgroup.add_argument('--osm-id', type=str,
40                             help='Analyse indexing of the given OSM object')
41         mgroup.add_argument('--place-id', type=int,
42                             help='Analyse indexing of the given Nominatim object')
43
44     @staticmethod
45     def run(args):
46         if args.warm:
47             AdminFuncs._warm(args)
48
49         if args.check_database:
50             LOG.warning('Checking database')
51             from ..tools import check_database
52             return check_database.check_database(args.config)
53
54         if args.analyse_indexing:
55             LOG.warning('Analysing performance of indexing function')
56             from ..tools import admin
57             with connect(args.config.get_libpq_dsn()) as conn:
58                 admin.analyse_indexing(conn, osm_id=args.osm_id, place_id=args.place_id)
59
60         return 0
61
62
63     @staticmethod
64     def _warm(args):
65         LOG.warning('Warming database caches')
66         params = ['warm.php']
67         if args.target == 'reverse':
68             params.append('--reverse-only')
69         if args.target == 'search':
70             params.append('--search-only')
71         return run_legacy_script(*params, nominatim_env=args)