Align stored AMSC payload timestamps
This commit is contained in:
@@ -162,6 +162,47 @@ def test_raw_observation_store_records_latest_observation(tmp_path):
|
|||||||
assert latest["payload"]["temp_c"] == 24.0
|
assert latest["payload"]["temp_c"] == 24.0
|
||||||
|
|
||||||
|
|
||||||
|
def test_observation_collector_aligns_amsc_payload_time_with_record_observed_at(tmp_path):
|
||||||
|
from src.database.db_manager import DBManager
|
||||||
|
from web.observation_collector_service import ObservationCollector
|
||||||
|
from web.services.observation_source_adapters import ObservationRecord
|
||||||
|
|
||||||
|
db = DBManager(str(tmp_path / "polyweather.db"))
|
||||||
|
collector = ObservationCollector(
|
||||||
|
weather=object(),
|
||||||
|
profiles=[],
|
||||||
|
observation_store=db,
|
||||||
|
)
|
||||||
|
|
||||||
|
record = ObservationRecord(
|
||||||
|
source="amsc_awos",
|
||||||
|
city="shanghai",
|
||||||
|
value=25.4,
|
||||||
|
observed_at="2026-06-14T15:43:00+00:00",
|
||||||
|
observed_at_local="2026-06-14 23:43:00",
|
||||||
|
station_code="ZSPD",
|
||||||
|
station_name="Shanghai Pudong",
|
||||||
|
runway="",
|
||||||
|
value_unit="c",
|
||||||
|
source_label="AMSC AWOS Shanghai Pudong (ZSPD)",
|
||||||
|
payload={
|
||||||
|
"source": "amsc_awos",
|
||||||
|
"temp_c": 25.4,
|
||||||
|
"observation_time": "2026-06-14T10:44:00+00:00",
|
||||||
|
"observation_time_local": "2026-06-14 18:44:00",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert collector._store_raw_observations([record]) == 1
|
||||||
|
|
||||||
|
latest = db.get_latest_raw_observation("amsc_awos", "shanghai")
|
||||||
|
|
||||||
|
assert latest is not None
|
||||||
|
assert latest["observed_at"] == "2026-06-14T15:43:00+00:00"
|
||||||
|
assert latest["payload"]["observation_time"] == "2026-06-14T15:43:00+00:00"
|
||||||
|
assert latest["payload"]["observation_time_local"] == "2026-06-14 23:43:00"
|
||||||
|
|
||||||
|
|
||||||
def test_raw_observation_failure_preserves_last_success_and_increments_errors(tmp_path):
|
def test_raw_observation_failure_preserves_last_success_and_increments_errors(tmp_path):
|
||||||
from src.database.db_manager import DBManager
|
from src.database.db_manager import DBManager
|
||||||
|
|
||||||
|
|||||||
@@ -403,6 +403,17 @@ class ObservationCollector:
|
|||||||
exc,
|
exc,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _raw_payload_for_record(record: ObservationRecord) -> dict[str, Any]:
|
||||||
|
payload = dict(record.payload)
|
||||||
|
source = str(record.source or payload.get("source") or "").strip().lower()
|
||||||
|
if source == "amsc_awos":
|
||||||
|
if record.observed_at:
|
||||||
|
payload["observation_time"] = record.observed_at
|
||||||
|
if record.observed_at_local:
|
||||||
|
payload["observation_time_local"] = record.observed_at_local
|
||||||
|
return payload
|
||||||
|
|
||||||
def _store_raw_observations(self, records: Sequence[ObservationRecord]) -> int:
|
def _store_raw_observations(self, records: Sequence[ObservationRecord]) -> int:
|
||||||
store = self.observation_store
|
store = self.observation_store
|
||||||
writer = getattr(store, "append_raw_observation", None)
|
writer = getattr(store, "append_raw_observation", None)
|
||||||
@@ -424,7 +435,7 @@ class ObservationCollector:
|
|||||||
runway=record.runway,
|
runway=record.runway,
|
||||||
value_unit=record.value_unit,
|
value_unit=record.value_unit,
|
||||||
status="ok",
|
status="ok",
|
||||||
payload=dict(record.payload),
|
payload=self._raw_payload_for_record(record),
|
||||||
)
|
)
|
||||||
written_records.append(record)
|
written_records.append(record)
|
||||||
wrote += 1
|
wrote += 1
|
||||||
|
|||||||
Reference in New Issue
Block a user