Add database health check to include last import time

Retrieve the last import time from the database during health check.

This is useful to setup monitoring for meshview instances that are no longer receiving messages.
This commit is contained in:
Mike Weaver
2026-02-08 03:07:46 -07:00
committed by GitHub
parent c454f2ef3a
commit 357fb530e2

View File

@@ -635,8 +635,16 @@ async def health_check(request):
# Check database connectivity
try:
async with database.async_session() as session:
await session.execute(text("SELECT 1"))
result = await session.execute(
select(func.max(PacketModel.import_time_us))
)
last_import_time_us = result.scalar()
health_status["database"] = "connected"
if last_import_time_us is not None:
now_us = int(datetime.datetime.now(datetime.UTC).timestamp() * 1_000_000)
health_status["seconds_since_last_message"] = round(
(now_us - last_import_time_us) / 1_000_000, 1
)
except Exception as e:
logger.error(f"Database health check failed: {e}")
health_status["database"] = "disconnected"