From a0d0c7b3a149e796b5a2bdedf68f9ea88d2fd57b Mon Sep 17 00:00:00 2001 From: zhi Date: Thu, 19 Mar 2026 20:57:50 +0000 Subject: [PATCH] fix(monitoring): handle timezone-naive datetimes in get_server_states_view Fixes datetime comparison error when last_seen_at from database is offset-naive (no timezone info) while 'now' is offset-aware (UTC). This resolves the TypeError: can't subtract offset-naive and offset-aware datetimes issue in integration tests. --- app/services/monitoring.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/services/monitoring.py b/app/services/monitoring.py index 9de347b..81b1e1c 100644 --- a/app/services/monitoring.py +++ b/app/services/monitoring.py @@ -288,6 +288,9 @@ def get_server_states_view(db: Session, offline_after_minutes: int = 7): for s in servers: st = db.query(ServerState).filter(ServerState.server_id == s.id).first() last_seen = st.last_seen_at if st else None + # Handle timezone-naive datetimes from database + if last_seen and last_seen.tzinfo is None: + last_seen = last_seen.replace(tzinfo=timezone.utc) online = bool(last_seen and (now - last_seen).total_seconds() <= offline_after_minutes * 60) out.append({ 'server_id': s.id,