mirror of
https://github.com/pablorevilla-meshtastic/meshview.git
synced 2026-03-04 23:27:46 +01:00
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:
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user