]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/clicmd/admin.py
Integrated 'collect_os_info.py' into Nominatim's CLI tool
[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
13 from nominatim.tools.exec_utils import run_legacy_script
14 from nominatim.clicmd.args import NominatimArgs
15
16 # Do not repeat documentation of subcommand classes.
17 # pylint: disable=C0111
18 # Using non-top-level imports to avoid eventually unused imports.
19 # pylint: disable=E0012,C0415
20
21 LOG = logging.getLogger()
22
23
24 class AdminFuncs:
25     """\
26     Analyse and maintain the database.
27     """
28
29     def add_args(self, parser: argparse.ArgumentParser) -> None:
30         group = parser.add_argument_group('Admin tasks')
31         objs = group.add_mutually_exclusive_group(required=True)
32         objs.add_argument('--warm', action='store_true',
33                           help='Warm database caches for search and reverse queries')
34         objs.add_argument('--check-database', action='store_true',
35                           help='Check that the database is complete and operational')
36         objs.add_argument('--migrate', action='store_true',
37                           help='Migrate the database to a new software version')
38         objs.add_argument('--analyse-indexing', action='store_true',
39                           help='Print performance analysis of the indexing process')
40         objs.add_argument('--collect-os-info', action="store_true",
41                           help="Generate a report about the host system information")
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         return 1
83
84     def _warm(self, args: NominatimArgs) -> int:
85         LOG.warning('Warming database caches')
86         params = ['warm.php']
87         if args.target == 'reverse':
88             params.append('--reverse-only')
89         if args.target == 'search':
90             params.append('--search-only')
91         return run_legacy_script(*params, nominatim_env=args)