mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-03-11 21:34:06 +00:00
implement command line status call in Python
This commit is contained in:
0
nominatim/apicmd/__init__.py
Normal file
0
nominatim/apicmd/__init__.py
Normal file
66
nominatim/apicmd/status.py
Normal file
66
nominatim/apicmd/status.py
Normal file
@@ -0,0 +1,66 @@
|
||||
# SPDX-License-Identifier: GPL-2.0-only
|
||||
#
|
||||
# This file is part of Nominatim. (https://nominatim.org)
|
||||
#
|
||||
# Copyright (C) 2022 by the Nominatim developer community.
|
||||
# For a full list of authors see the git log.
|
||||
"""
|
||||
Classes and function releated to status call.
|
||||
"""
|
||||
from typing import Optional, cast
|
||||
import datetime as dt
|
||||
|
||||
import sqlalchemy as sqla
|
||||
from sqlalchemy.ext.asyncio.engine import AsyncEngine, AsyncConnection
|
||||
import asyncpg
|
||||
|
||||
from nominatim import version
|
||||
|
||||
class StatusResult:
|
||||
""" Result of a call to the status API.
|
||||
"""
|
||||
|
||||
def __init__(self, status: int, msg: str):
|
||||
self.status = status
|
||||
self.message = msg
|
||||
# XXX versions really should stay tuples here
|
||||
self.software_version = version.version_str()
|
||||
self.data_updated: Optional[dt.datetime] = None
|
||||
self.database_version: Optional[str] = None
|
||||
|
||||
|
||||
async def _get_database_date(conn: AsyncConnection) -> Optional[dt.datetime]:
|
||||
""" Query the database date.
|
||||
"""
|
||||
sql = sqla.text('SELECT lastimportdate FROM import_status LIMIT 1')
|
||||
result = await conn.execute(sql)
|
||||
|
||||
for row in result:
|
||||
return cast(dt.datetime, row[0])
|
||||
|
||||
return None
|
||||
|
||||
|
||||
async def _get_database_version(conn: AsyncConnection) -> Optional[str]:
|
||||
sql = sqla.text("""SELECT value FROM nominatim_properties
|
||||
WHERE property = 'database_version'""")
|
||||
result = await conn.execute(sql)
|
||||
|
||||
for row in result:
|
||||
return cast(str, row[0])
|
||||
|
||||
return None
|
||||
|
||||
|
||||
async def get_status(engine: AsyncEngine) -> StatusResult:
|
||||
""" Execute a status API call.
|
||||
"""
|
||||
status = StatusResult(0, 'OK')
|
||||
try:
|
||||
async with engine.begin() as conn:
|
||||
status.data_updated = await _get_database_date(conn)
|
||||
status.database_version = await _get_database_version(conn)
|
||||
except asyncpg.PostgresError as err:
|
||||
return StatusResult(700, str(err))
|
||||
|
||||
return status
|
||||
Reference in New Issue
Block a user