feat: implement real-time observation patch normalization and live temperature threshold visualization logic

This commit is contained in:
2569718930@qq.com
2026-05-27 10:17:33 +08:00
parent e6a673e27d
commit 820dabfbf3
23 changed files with 709 additions and 29 deletions
+14
View File
@@ -14,6 +14,14 @@ from web.realtime_patch_schema import EVENT_TYPE
DEFAULT_RETENTION_HOURS = 6
MAX_REPLAY_LIMIT = 2000
TIME_CONTRACT_KEYS = (
"observed_at_utc",
"observed_at_local",
"city_local_date",
"city_timezone",
"city_utc_offset_seconds",
"source_cadence_sec",
)
def _utc_now() -> datetime:
@@ -43,6 +51,10 @@ def _normalize_city_set(cities: Optional[Set[str]]) -> Set[str]:
return {str(city or "").strip().lower() for city in (cities or set()) if str(city or "").strip()}
def _time_contract_from_payload(payload: Dict[str, Any]) -> Dict[str, Any]:
return {key: payload[key] for key in TIME_CONTRACT_KEYS if key in payload}
def _retention_hours_from_env() -> int:
raw = os.getenv("POLYWEATHER_PATCH_EVENT_RETENTION_HOURS", "").strip()
if not raw:
@@ -133,6 +145,7 @@ class RealtimeEventStore:
"city": str(event["city"]),
"source": str(event["source"]),
"obs_time": event.get("obs_time"),
**_time_contract_from_payload(payload),
"ts": int(event.get("ts") or _created_at_to_ms(created_at)),
"payload": payload,
}
@@ -246,6 +259,7 @@ class RealtimeEventStore:
"city": str(row["city"]),
"source": str(row["source"]),
"obs_time": row["obs_time"],
**_time_contract_from_payload(payload),
"ts": _created_at_to_ms(row["created_at"]),
"payload": payload,
}