1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2023 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Classes and function releated to status call.
10 from typing import Optional, cast
13 import sqlalchemy as sa
14 from sqlalchemy.ext.asyncio.engine import AsyncConnection
17 from nominatim import version
20 """ Result of a call to the status API.
23 def __init__(self, status: int, msg: str):
26 self.software_version = version.NOMINATIM_VERSION
27 self.data_updated: Optional[dt.datetime] = None
28 self.database_version: Optional[version.NominatimVersion] = None
31 async def _get_database_date(conn: AsyncConnection) -> Optional[dt.datetime]:
32 """ Query the database date.
34 sql = sa.text('SELECT lastimportdate FROM import_status LIMIT 1')
35 result = await conn.execute(sql)
38 return cast(dt.datetime, row[0])
43 async def _get_database_version(conn: AsyncConnection) -> Optional[version.NominatimVersion]:
44 sql = sa.text("""SELECT value FROM nominatim_properties
45 WHERE property = 'database_version'""")
46 result = await conn.execute(sql)
49 return version.parse_version(cast(str, row[0]))
54 async def get_status(conn: AsyncConnection) -> StatusResult:
55 """ Execute a status API call.
57 status = StatusResult(0, 'OK')
59 status.data_updated = await _get_database_date(conn)
60 status.database_version = await _get_database_version(conn)
61 except asyncpg.PostgresError:
62 return StatusResult(700, 'Database connection failed')