1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 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 sqla
14 from sqlalchemy.ext.asyncio.engine import AsyncEngine, AsyncConnection
17 from nominatim import version
20 """ Result of a call to the status API.
23 def __init__(self, status: int, msg: str):
26 # XXX versions really should stay tuples here
27 self.software_version = version.version_str()
28 self.data_updated: Optional[dt.datetime] = None
29 self.database_version: Optional[str] = None
32 async def _get_database_date(conn: AsyncConnection) -> Optional[dt.datetime]:
33 """ Query the database date.
35 sql = sqla.text('SELECT lastimportdate FROM import_status LIMIT 1')
36 result = await conn.execute(sql)
39 return cast(dt.datetime, row[0])
44 async def _get_database_version(conn: AsyncConnection) -> Optional[str]:
45 sql = sqla.text("""SELECT value FROM nominatim_properties
46 WHERE property = 'database_version'""")
47 result = await conn.execute(sql)
50 return cast(str, row[0])
55 async def get_status(engine: AsyncEngine) -> StatusResult:
56 """ Execute a status API call.
58 status = StatusResult(0, 'OK')
60 async with engine.begin() as conn:
61 status.data_updated = await _get_database_date(conn)
62 status.database_version = await _get_database_version(conn)
63 except asyncpg.PostgresError:
64 return StatusResult(700, 'Database connection failed')