diff --git a/sfm/event_ingest.py b/sfm/event_ingest.py index a283bd9..5cd2673 100644 --- a/sfm/event_ingest.py +++ b/sfm/event_ingest.py @@ -66,6 +66,29 @@ def _already_stored(dl: DownloadedEvent, row: dict) -> EventDownload: ) +def _overlay_s4_record(ev: Any, record: dict) -> None: + """Fill, where the file left them empty, what the unit's 0x0C said. + + Without Thor's TXT report the IDF file gives only the three PPVs. The + vector sum matters beyond display: find_twins matches on it. Not the mic: + its 0x0C decode reads a denormal (parked). + """ + from minimateplus.models import PeakValues, ProjectInfo + + pvs = record.get("peak_vector_sum_ips") + if pvs is not None: + if ev.peak_values is None: + ev.peak_values = PeakValues() + if ev.peak_values.peak_vector_sum is None: + ev.peak_values.peak_vector_sum = pvs + location = record.get("sensor_location") + if location is not None: + if ev.project_info is None: + ev.project_info = ProjectInfo() + if ev.project_info.sensor_location is None: + ev.project_info.sensor_location = location + + def persist_download(dl: DownloadedEvent, *, store: Any, db: Any) -> EventDownload: # Never merge: a row for (serial, timestamp) means the event is stored, and # whoever stored it first keeps it. No store call, no insert, no sidecar. @@ -76,6 +99,8 @@ def persist_download(dl: DownloadedEvent, *, store: Any, db: Any) -> EventDownlo if dl.series == 4: ev, rec = store.save_imported_idf(dl.idf_bytes, source_path=Path(dl.filename), serial_hint=dl.serial) + if ev is not None: + _overlay_s4_record(ev, dl.record) family = "series4" else: ev = dl.event diff --git a/tests/test_event_ingest.py b/tests/test_event_ingest.py index 0f2ad92..ec4f7a5 100644 --- a/tests/test_event_ingest.py +++ b/tests/test_event_ingest.py @@ -55,6 +55,52 @@ def test_a_series4_waveform_is_stored_and_findable(sdm): assert out.verified is True and out.sha256 +def test_a_fresh_series4_download_keeps_the_units_pvs_and_sensor_location(sdm): + # Without Thor's TXT the file alone gives only the three PPVs; the unit's + # 0x0C record carries the vector sum (find_twins matches on it) and the location. + store, db = sdm + out = persist_download(s4_download("UM20147_20250531135901.IDFW", + record={"peak_vector_sum_ips": 0.391, "sensor_location": "Loc A"}), + store=store, db=db) + row = db.get_event(out.db_event_id) + assert out.inserted + assert (row["peak_vector_sum"], row["sensor_location"]) == (0.391, "Loc A") + assert row["tran_ppv"] is not None # the file's own peaks still stored + + +def test_the_record_fills_only_what_the_file_left_empty(sdm, monkeypatch): + store, db = sdm + real = store.save_imported_idf + + def with_values(*a, **kw): + ev, rec = real(*a, **kw) + ev.peak_values.peak_vector_sum = 1.5 + ev.project_info.sensor_location = "From the file" + return ev, rec + monkeypatch.setattr(store, "save_imported_idf", with_values) + out = persist_download(s4_download("UM20147_20250531135901.IDFW", + record={"peak_vector_sum_ips": 0.391, "sensor_location": "Loc A"}), + store=store, db=db) + row = db.get_event(out.db_event_id) + assert (row["peak_vector_sum"], row["sensor_location"]) == (1.5, "From the file") + + +def test_the_record_is_kept_even_when_the_file_gave_no_peaks_or_project(sdm, monkeypatch): + store, db = sdm + real = store.save_imported_idf + + def bare(*a, **kw): + ev, rec = real(*a, **kw) + ev.peak_values = ev.project_info = None + return ev, rec + monkeypatch.setattr(store, "save_imported_idf", bare) + out = persist_download(s4_download("UM20147_20250531135901.IDFW", + record={"peak_vector_sum_ips": 0.391, "sensor_location": "Loc A"}), + store=store, db=db) + row = db.get_event(out.db_event_id) + assert (row["peak_vector_sum"], row["sensor_location"]) == (0.391, "Loc A") + + def test_a_series4_histogram_is_stored(sdm): store, db = sdm out = persist_download(s4_download("UM20147_20250330070110.IDFH", verified=None), store=store, db=db)