]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/clicmd/admin.py
add tests for new arm and export Python functions
[nominatim.git] / nominatim / clicmd / admin.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Implementation of the 'admin' subcommand.
9 """
10 import logging
11 import argparse
12 import random
13
14 from nominatim.db.connection import connect
15 from nominatim.clicmd.args import NominatimArgs
16 import nominatim.api as napi
17
18 # Do not repeat documentation of subcommand classes.
19 # pylint: disable=C0111
20 # Using non-top-level imports to avoid eventually unused imports.
21 # pylint: disable=E0012,C0415
22
23 LOG = logging.getLogger()
24
25
26 class AdminFuncs:
27     """\
28     Analyse and maintain the database.
29     """
30
31     def add_args(self, parser: argparse.ArgumentParser) -> None:
32         group = parser.add_argument_group('Admin tasks')
33         objs = group.add_mutually_exclusive_group(required=True)
34         objs.add_argument('--warm', action='store_true',
35                           help='Warm database caches for search and reverse queries')
36         objs.add_argument('--check-database', action='store_true',
37                           help='Check that the database is complete and operational')
38         objs.add_argument('--migrate', action='store_true',
39                           help='Migrate the database to a new software version')
40         objs.add_argument('--analyse-indexing', action='store_true',
41                           help='Print performance analysis of the indexing process')
42         objs.add_argument('--collect-os-info', action="store_true",
43                           help="Generate a report about the host system information")
44         group = parser.add_argument_group('Arguments for cache warming')
45         group.add_argument('--search-only', action='store_const', dest='target',
46                            const='search',
47                            help="Only pre-warm tables for search queries")
48         group.add_argument('--reverse-only', action='store_const', dest='target',
49                            const='reverse',
50                            help="Only pre-warm tables for reverse queries")
51         group = parser.add_argument_group('Arguments for index anaysis')
52         mgroup = group.add_mutually_exclusive_group()
53         mgroup.add_argument('--osm-id', type=str,
54                             help='Analyse indexing of the given OSM object')
55         mgroup.add_argument('--place-id', type=int,
56                             help='Analyse indexing of the given Nominatim object')
57
58     def run(self, args: NominatimArgs) -> int:
59         if args.warm:
60             return self._warm(args)
61
62         if args.check_database:
63             LOG.warning('Checking database')
64             from ..tools import check_database
65             return check_database.check_database(args.config)
66
67         if args.analyse_indexing:
68             LOG.warning('Analysing performance of indexing function')
69             from ..tools import admin
70             admin.analyse_indexing(args.config, osm_id=args.osm_id, place_id=args.place_id)
71             return 0
72
73         if args.migrate:
74             LOG.warning('Checking for necessary database migrations')
75             from ..tools import migration
76             return migration.migrate(args.config, args)
77
78         if args.collect_os_info:
79             LOG.warning("Reporting System Information")
80             from ..tools import collect_os_info
81             collect_os_info.report_system_information(args.config)
82             return 0
83
84         return 1
85
86
87     def _warm(self, args: NominatimArgs) -> int:
88         LOG.warning('Warming database caches')
89
90         api = napi.NominatimAPI(args.project_dir)
91
92         try:
93             if args.target != 'reverse':
94                 for _ in range(1000):
95                     api.reverse((random.uniform(-90, 90), random.uniform(-180, 180)),
96                                 address_details=True)
97
98             if args.target != 'search':
99                 from ..tokenizer import factory as tokenizer_factory
100
101                 tokenizer = tokenizer_factory.get_tokenizer_for_db(args.config)
102                 with connect(args.config.get_libpq_dsn()) as conn:
103                     words = tokenizer.most_frequent_words(conn, 1000)
104
105                 for word in words:
106                     api.search(word)
107         finally:
108             api.close()
109
110         return 0