From 357fb530e288d085a75977f0771da2f070908fa6 Mon Sep 17 00:00:00 2001 From: Mike Weaver Date: Sun, 8 Feb 2026 03:07:46 -0700 Subject: [PATCH] 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. --- meshview/web_api/api.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/meshview/web_api/api.py b/meshview/web_api/api.py index e318ed5..0461bab 100644 --- a/meshview/web_api/api.py +++ b/meshview/web_api/api.py @@ -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"