Persist AMSC runway history from collector
This commit is contained in:
@@ -414,6 +414,80 @@ class ObservationCollector:
|
||||
payload["observation_time_local"] = record.observed_at_local
|
||||
return payload
|
||||
|
||||
@staticmethod
|
||||
def _float_or_none(value: Any) -> Optional[float]:
|
||||
try:
|
||||
if value is None or value == "":
|
||||
return None
|
||||
return float(value)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def _int_or_none(value: Any) -> Optional[int]:
|
||||
try:
|
||||
if value is None or value == "":
|
||||
return None
|
||||
return int(float(value))
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
def _store_amsc_runway_observations(
|
||||
self,
|
||||
record: ObservationRecord,
|
||||
payload: dict[str, Any],
|
||||
) -> None:
|
||||
source = str(record.source or payload.get("source") or "").strip().lower()
|
||||
if source != "amsc_awos":
|
||||
return
|
||||
appender = getattr(self.observation_store, "append_runway_obs", None)
|
||||
if not callable(appender):
|
||||
return
|
||||
runway_obs = payload.get("runway_obs")
|
||||
if not isinstance(runway_obs, dict):
|
||||
return
|
||||
point_temperatures = runway_obs.get("point_temperatures")
|
||||
if not isinstance(point_temperatures, list) or not point_temperatures:
|
||||
return
|
||||
icao = str(payload.get("icao") or record.station_code or "").strip().upper()
|
||||
obs_time = str(
|
||||
payload.get("observation_time")
|
||||
or payload.get("observed_at")
|
||||
or record.observed_at
|
||||
or ""
|
||||
).strip()
|
||||
if not icao or not obs_time:
|
||||
return
|
||||
for point in point_temperatures:
|
||||
if not isinstance(point, dict):
|
||||
continue
|
||||
runway = str(point.get("runway") or "").strip().upper()
|
||||
if not runway:
|
||||
continue
|
||||
try:
|
||||
appender(
|
||||
icao=icao,
|
||||
city=record.city,
|
||||
runway=runway,
|
||||
tdz_temp=self._float_or_none(point.get("tdz_temp")),
|
||||
mid_temp=self._float_or_none(point.get("mid_temp")),
|
||||
end_temp=self._float_or_none(point.get("end_temp")),
|
||||
target_runway_max=self._float_or_none(point.get("target_runway_max")),
|
||||
wind_dir=self._int_or_none(point.get("wind_dir")),
|
||||
wind_speed=self._float_or_none(point.get("wind_speed")),
|
||||
rvr=self._int_or_none(point.get("rvr")),
|
||||
mor=self._float_or_none(point.get("mor")),
|
||||
humidity=self._float_or_none(point.get("humidity")),
|
||||
otime_utc=obs_time,
|
||||
)
|
||||
except Exception as exc:
|
||||
logger.debug(
|
||||
"AMSC runway observation write skipped city={} runway={}: {}",
|
||||
record.city,
|
||||
runway,
|
||||
exc,
|
||||
)
|
||||
|
||||
def _store_raw_observations(self, records: Sequence[ObservationRecord]) -> int:
|
||||
store = self.observation_store
|
||||
writer = getattr(store, "append_raw_observation", None)
|
||||
@@ -424,6 +498,7 @@ class ObservationCollector:
|
||||
written_records: list[ObservationRecord] = []
|
||||
for record in records:
|
||||
try:
|
||||
payload = self._raw_payload_for_record(record)
|
||||
writer(
|
||||
source=record.source,
|
||||
city=record.city,
|
||||
@@ -435,8 +510,9 @@ class ObservationCollector:
|
||||
runway=record.runway,
|
||||
value_unit=record.value_unit,
|
||||
status="ok",
|
||||
payload=self._raw_payload_for_record(record),
|
||||
payload=payload,
|
||||
)
|
||||
self._store_amsc_runway_observations(record, payload)
|
||||
written_records.append(record)
|
||||
wrote += 1
|
||||
except Exception as exc:
|
||||
|
||||
Reference in New Issue
Block a user