拆分 analysis_service:剩余工具函数全部移至 analysis_utils.py
提取 _mgm_hourly_high/_dedupe_forecast_daily/_format_observation_time_local/_parse_local_hour/_parse_utc_datetime/_metar_is_current_local_day/_is_plausible_city_temp。统一 parse_utc_datetime 副本。analysis_service 2082→1966 行。
This commit is contained in:
@@ -6,6 +6,7 @@ Pure helpers: clock arithmetic, bucket labelling, signal packaging.
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from datetime import datetime, timezone, timedelta
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from web.core import _sf
|
||||
@@ -92,3 +93,114 @@ def add_signal(
|
||||
"summary_en": summary_en or summary,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
# ── Time / date helpers ────────────────────────────────────────────────
|
||||
|
||||
def parse_utc_datetime(value: Any) -> Optional[datetime]:
|
||||
raw = str(value or "").strip()
|
||||
if not raw or "T" not in raw:
|
||||
return None
|
||||
try:
|
||||
dt = datetime.fromisoformat(raw.replace("Z", "+00:00"))
|
||||
except Exception:
|
||||
return None
|
||||
if dt.tzinfo is None:
|
||||
dt = dt.replace(tzinfo=timezone.utc)
|
||||
return dt.astimezone(timezone.utc)
|
||||
|
||||
|
||||
def format_observation_time_local(value: Any, utc_offset: int) -> str:
|
||||
raw = str(value or "").strip()
|
||||
if not raw:
|
||||
return ""
|
||||
if "T" in raw:
|
||||
try:
|
||||
dt = datetime.fromisoformat(raw.replace("Z", "+00:00"))
|
||||
if dt.tzinfo is None:
|
||||
dt = dt.replace(tzinfo=timezone.utc)
|
||||
return dt.astimezone(timezone(timedelta(seconds=utc_offset))).strftime("%H:%M")
|
||||
except Exception:
|
||||
pass
|
||||
import re
|
||||
match = re.search(r"(\d{1,2}):(\d{2})", raw)
|
||||
if match:
|
||||
return f"{int(match.group(1)):02d}:{match.group(2)}"
|
||||
return raw[:16]
|
||||
|
||||
|
||||
def parse_local_hour(local_time_str: Optional[str]) -> Optional[int]:
|
||||
if not local_time_str:
|
||||
return None
|
||||
try:
|
||||
parts = str(local_time_str).strip().split(":")
|
||||
hour = int(parts[0])
|
||||
if 0 <= hour <= 23:
|
||||
return hour
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
|
||||
def metar_is_current_local_day(
|
||||
metar: Dict[str, Any],
|
||||
*,
|
||||
local_date: str,
|
||||
utc_offset: int,
|
||||
) -> bool:
|
||||
if not isinstance(metar, dict) or not metar:
|
||||
return False
|
||||
if metar.get("stale_for_today") is True:
|
||||
return False
|
||||
observation_local_date = str(metar.get("observation_local_date") or "").strip()
|
||||
if observation_local_date:
|
||||
return observation_local_date == local_date
|
||||
obs_dt = parse_utc_datetime(metar.get("observation_time"))
|
||||
if obs_dt is None:
|
||||
return True
|
||||
local_dt = obs_dt.astimezone(timezone(timedelta(seconds=utc_offset)))
|
||||
return local_dt.strftime("%Y-%m-%d") == local_date
|
||||
|
||||
|
||||
def is_plausible_city_temp(city: str, value: Any, unit: str = "°C") -> bool:
|
||||
from src.data_collection.city_registry import CITY_REGISTRY
|
||||
|
||||
temp = _sf(value)
|
||||
if temp is None:
|
||||
return False
|
||||
meta = CITY_REGISTRY.get(str(city or "").strip().lower(), {}) or {}
|
||||
min_c = _sf(meta.get("min_plausible_metar_temp_c"))
|
||||
if min_c is None:
|
||||
return True
|
||||
min_value = min_c * 9 / 5 + 32 if str(unit or "").upper().endswith("F") else min_c
|
||||
return temp >= min_value
|
||||
|
||||
|
||||
def dedupe_forecast_daily(rows: Any) -> list:
|
||||
if not isinstance(rows, list):
|
||||
return []
|
||||
seen = set()
|
||||
out = []
|
||||
for row in rows:
|
||||
if not isinstance(row, dict):
|
||||
continue
|
||||
date = str(row.get("date") or "").strip()
|
||||
if not date or date in seen:
|
||||
continue
|
||||
seen.add(date)
|
||||
out.append(row)
|
||||
return out
|
||||
|
||||
|
||||
def mgm_hourly_high(mgm: Dict[str, Any]) -> Optional[float]:
|
||||
hourly = mgm.get("hourly") if isinstance(mgm, dict) else []
|
||||
if not isinstance(hourly, list):
|
||||
return None
|
||||
values = []
|
||||
for row in hourly:
|
||||
if not isinstance(row, dict):
|
||||
continue
|
||||
value = _sf(row.get("temp"))
|
||||
if value is not None:
|
||||
values.append(value)
|
||||
return max(values) if values else None
|
||||
|
||||
Reference in New Issue
Block a user