]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/clicmd/admin.py
made age a required argument for the -clean-deleted command
[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         self.parser = parser
33         group = parser.add_argument_group('Admin tasks')
34         objs = group.add_mutually_exclusive_group(required=True)
35         objs.add_argument('--warm', action='store_true',
36                           help='Warm database caches for search and reverse queries')
37         objs.add_argument('--check-database', action='store_true',
38                           help='Check that the database is complete and operational')
39         objs.add_argument('--migrate', action='store_true',
40                           help='Migrate the database to a new software version')
41         objs.add_argument('--analyse-indexing', action='store_true',
42                           help='Print performance analysis of the indexing process')
43         objs.add_argument('--collect-os-info', action="store_true",
44                           help="Generate a report about the host system information")
45         objs.add_argument('--clean-deleted', action='store', metavar='AGE',
46                           help='Clean up deleted relations')
47         group = parser.add_argument_group('Arguments for cache warming')
48         group.add_argument('--search-only', action='store_const', dest='target',
49                            const='search',
50                            help="Only pre-warm tables for search queries")
51         group.add_argument('--reverse-only', action='store_const', dest='target',
52                            const='reverse',
53                            help="Only pre-warm tables for reverse queries")
54         group = parser.add_argument_group('Arguments for index anaysis')
55         mgroup = group.add_mutually_exclusive_group()
56         mgroup.add_argument('--osm-id', type=str,
57                             help='Analyse indexing of the given OSM object')
58         mgroup.add_argument('--place-id', type=int,
59                             help='Analyse indexing of the given Nominatim object')
60         group = parser.add_argument_group('Arguments for cleaning deleted')
61
62
63     def run(self, args: NominatimArgs) -> int:
64         # pylint: disable=too-many-return-statements
65         if args.warm:
66             return self._warm(args)
67
68         if args.check_database:
69             LOG.warning('Checking database')
70             from ..tools import check_database
71             return check_database.check_database(args.config)
72
73         if args.analyse_indexing:
74             LOG.warning('Analysing performance of indexing function')
75             from ..tools import admin
76             admin.analyse_indexing(args.config, osm_id=args.osm_id, place_id=args.place_id)
77             return 0
78
79         if args.migrate:
80             LOG.warning('Checking for necessary database migrations')
81             from ..tools import migration
82             return migration.migrate(args.config, args)
83
84         if args.collect_os_info:
85             LOG.warning("Reporting System Information")
86             from ..tools import collect_os_info
87             collect_os_info.report_system_information(args.config)
88             return 0
89
90         if args.clean_deleted:
91             LOG.warning('Cleaning up deleted relations')
92             from ..tools import admin
93             admin.clean_deleted_relations(args.config, age=args.clean_deleted)
94             return 0
95
96         return 1
97
98
99     def _warm(self, args: NominatimArgs) -> int:
100         LOG.warning('Warming database caches')
101
102         api = napi.NominatimAPI(args.project_dir)
103
104         try:
105             if args.target != 'search':
106                 for _ in range(1000):
107                     api.reverse((random.uniform(-90, 90), random.uniform(-180, 180)),
108                                 address_details=True)
109
110             if args.target != 'reverse':
111                 from ..tokenizer import factory as tokenizer_factory
112
113                 tokenizer = tokenizer_factory.get_tokenizer_for_db(args.config)
114                 with connect(args.config.get_libpq_dsn()) as conn:
115                     if conn.table_exists('search_name'):
116                         words = tokenizer.most_frequent_words(conn, 1000)
117                     else:
118                         words = []
119
120                 for word in words:
121                     api.search(word)
122         finally:
123             api.close()
124
125         return 0