]> git.openstreetmap.org Git - nominatim.git/blob - src/nominatim_db/clicmd/admin.py
remove remaining pylint hints
[nominatim.git] / src / nominatim_db / clicmd / admin.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2024 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 ..errors import UsageError
15 from ..db.connection import connect, table_exists
16 from .args import NominatimArgs
17
18
19 LOG = logging.getLogger()
20
21
22 class AdminFuncs:
23     """\
24     Analyse and maintain the database.
25     """
26
27     def add_args(self, parser: argparse.ArgumentParser) -> None:
28         group = parser.add_argument_group('Admin tasks')
29         objs = group.add_mutually_exclusive_group(required=True)
30         objs.add_argument('--warm', action='store_true',
31                           help='Warm database caches for search and reverse queries')
32         objs.add_argument('--check-database', action='store_true',
33                           help='Check that the database is complete and operational')
34         objs.add_argument('--migrate', action='store_true',
35                           help='Migrate the database to a new software version')
36         objs.add_argument('--analyse-indexing', action='store_true',
37                           help='Print performance analysis of the indexing process')
38         objs.add_argument('--collect-os-info', action="store_true",
39                           help="Generate a report about the host system information")
40         objs.add_argument('--clean-deleted', action='store', metavar='AGE',
41                           help='Clean up deleted relations')
42         group = parser.add_argument_group('Arguments for cache warming')
43         group.add_argument('--search-only', action='store_const', dest='target',
44                            const='search',
45                            help="Only pre-warm tables for search queries")
46         group.add_argument('--reverse-only', action='store_const', dest='target',
47                            const='reverse',
48                            help="Only pre-warm tables for reverse queries")
49         group = parser.add_argument_group('Arguments for index anaysis')
50         mgroup = group.add_mutually_exclusive_group()
51         mgroup.add_argument('--osm-id', type=str,
52                             help='Analyse indexing of the given OSM object')
53         mgroup.add_argument('--place-id', type=int,
54                             help='Analyse indexing of the given Nominatim object')
55
56     def run(self, args: NominatimArgs) -> int:
57         if args.warm:
58             return self._warm(args)
59
60         if args.check_database:
61             LOG.warning('Checking database')
62             from ..tools import check_database
63             return check_database.check_database(args.config)
64
65         if args.analyse_indexing:
66             LOG.warning('Analysing performance of indexing function')
67             from ..tools import admin
68             admin.analyse_indexing(args.config, osm_id=args.osm_id, place_id=args.place_id)
69             return 0
70
71         if args.migrate:
72             LOG.warning('Checking for necessary database migrations')
73             from ..tools import migration
74             return migration.migrate(args.config, args)
75
76         if args.collect_os_info:
77             LOG.warning("Reporting System Information")
78             from ..tools import collect_os_info
79             collect_os_info.report_system_information(args.config)
80             return 0
81
82         if args.clean_deleted:
83             LOG.warning('Cleaning up deleted relations')
84             from ..tools import admin
85             admin.clean_deleted_relations(args.config, age=args.clean_deleted)
86             return 0
87
88         return 1
89
90     def _warm(self, args: NominatimArgs) -> int:
91         try:
92             import nominatim_api as napi
93         except ModuleNotFoundError as exp:
94             raise UsageError("Warming requires nominatim API. "
95                              "Install with 'pip install nominatim-api'.") from exp
96         LOG.warning('Warming database caches')
97
98         api = napi.NominatimAPI(args.project_dir)
99
100         try:
101             if args.target != 'search':
102                 for _ in range(1000):
103                     api.reverse((random.uniform(-90, 90), random.uniform(-180, 180)),
104                                 address_details=True)
105
106             if args.target != 'reverse':
107                 from ..tokenizer import factory as tokenizer_factory
108
109                 tokenizer = tokenizer_factory.get_tokenizer_for_db(args.config)
110                 with connect(args.config.get_libpq_dsn()) as conn:
111                     if table_exists(conn, 'search_name'):
112                         words = tokenizer.most_frequent_words(conn, 1000)
113                     else:
114                         words = []
115
116                 for word in words:
117                     api.search(word)
118         finally:
119             api.close()
120
121         return 0