Fix MTM timestamp comparison in engine loop

This commit is contained in:
Thigazhezhilan J 2026-04-06 21:20:33 +05:30
parent 565de64459
commit 49950d4028

View File

@ -136,19 +136,32 @@ def _get_last_mtm_ts(user_id: str | None = None, run_id: str | None = None):
row = cur.fetchone() row = cur.fetchone()
if not row or row[0] is None: if not row or row[0] is None:
return None return None
return row[0].astimezone().replace(tzinfo=None) value = row[0]
if value.tzinfo is None:
return value.replace(tzinfo=timezone.utc)
return value.astimezone(timezone.utc)
def _normalize_dt(value):
if value is None:
return None
if value.tzinfo is None:
return value.replace(tzinfo=timezone.utc)
return value.astimezone(timezone.utc)
def should_log_mtm(df, now, user_id: str | None = None, run_id: str | None = None): def should_log_mtm(df, now, user_id: str | None = None, run_id: str | None = None):
now_dt = _normalize_dt(now)
if df is None: if df is None:
last_ts = _get_last_mtm_ts(user_id=user_id, run_id=run_id) last_ts = _get_last_mtm_ts(user_id=user_id, run_id=run_id)
if last_ts is None: if last_ts is None:
return True return True
return (now - last_ts).total_seconds() >= MTM_INTERVAL_SECONDS return (now_dt - last_ts).total_seconds() >= MTM_INTERVAL_SECONDS
if getattr(df, "empty", False): if getattr(df, "empty", False):
return True return True
try: try:
last_ts = datetime.fromisoformat(str(df.iloc[-1]["timestamp"])) last_ts = datetime.fromisoformat(str(df.iloc[-1]["timestamp"]))
except Exception: except Exception:
return True return True
return (now - last_ts).total_seconds() >= MTM_INTERVAL_SECONDS last_ts = _normalize_dt(last_ts)
return (now_dt - last_ts).total_seconds() >= MTM_INTERVAL_SECONDS