diff --git a/sfm/database.py b/sfm/database.py index e872fb1..9ac430a 100644 --- a/sfm/database.py +++ b/sfm/database.py @@ -808,6 +808,28 @@ class SeismoDb: ).fetchone() return dict(row) if row else None + def find_event_by_blastware_name(self, serial: str, filename: str) -> Optional[dict]: + """The row stored under `filename`'s Blastware name, or None. + + A Blastware name encodes the serial and the event's second + (`blastware_filename_time`), so the same name is the same event -- the + rule `_restamp_by_file` uses. A direct download is `.0` and + Blastware's call-in copy of the same event `.0W` / `H`; all + three forms match. Any other name ("imported.bw") matches nothing. + """ + if blastware_filename_time(filename) is None: + return None + base = filename.upper() + if len(base.rsplit(".", 1)[-1]) == 4: + base = base[:-1] + with self._connect() as conn: + row = conn.execute( + "SELECT * FROM events WHERE serial = ? AND UPPER(blastware_filename) IN (?, ?, ?) " + "ORDER BY rowid LIMIT 1", + (serial, base, base + "W", base + "H"), + ).fetchone() + return dict(row) if row else None + def query_events( self, serial: Optional[str] = None, diff --git a/sfm/event_ingest.py b/sfm/event_ingest.py index c8dbb18..8480d96 100644 --- a/sfm/event_ingest.py +++ b/sfm/event_ingest.py @@ -123,6 +123,11 @@ def persist_download(dl: DownloadedEvent, *, store: Any, db: Any) -> EventDownlo rows = db.query_events(serial=dl.serial, from_dt=dl.timestamp, to_dt=dl.timestamp, limit=1) if rows: return _already_stored(dl, rows[0]) + # A histogram's row is stored at its file's time, a second before the unit's + # on most (#51), so the time above misses it; its Blastware name does not. + row = db.find_event_by_blastware_name(dl.serial, dl.filename) + if row is not None: + return _already_stored(dl, row) if dl.series == 4: ev, rec = store.save_imported_idf(dl.idf_bytes, source_path=Path(dl.filename), serial_hint=dl.serial) @@ -132,8 +137,8 @@ def persist_download(dl: DownloadedEvent, *, store: Any, db: Any) -> EventDownlo else: # The same path production's watcher uses for Blastware's own files: the # event is decoded from the file (samples, histogram, setup-block sample - # rate and record time), not from the live 5A decode. The unit's 0C peaks - # stay what they were -- the download *check* (verify_s3_download). + # rate and record time), not from the live 5A decode. The unit's own 0C + # peaks, mic and vector sum then go over the file's (_overlay_s3_record). if not dl.bw_bytes: raise IngestError(f"{dl.filename}: the download carried no event file; nothing was stored") geo_range = getattr(dl.compliance, "geo_range", None) if dl.compliance else None diff --git a/sfm/waveform_store.py b/sfm/waveform_store.py index 031065c..6408b1e 100644 --- a/sfm/waveform_store.py +++ b/sfm/waveform_store.py @@ -317,7 +317,8 @@ class WaveformStore: 3. Resolve serial from BW filename (`

...`) or use serial_hint. Falls back to "UNKNOWN". 4. Copy the BW bytes verbatim into //. - 5. Write the .sfm.json sidecar with source.kind = "bw-import" + 5. Write the .sfm.json sidecar with source.kind = `source_kind` + ("bw-import"; "sfm-live" for SFM's own downloads) and a5_pickle_filename = None. Does NOT write a .a5.pkl (no A5 source available; byte-for-byte regeneration not possible — the on-disk BW file IS the byte-for-byte source). diff --git a/tests/test_event_ingest.py b/tests/test_event_ingest.py index e79a190..5257762 100644 --- a/tests/test_event_ingest.py +++ b/tests/test_event_ingest.py @@ -335,3 +335,43 @@ def test_the_units_range_reaches_the_store(tmp_path): timestamp=datetime.datetime(2026, 10, 1, 8, 0, 0)) persist_download(dl, store=store, db=SeismoDb(tmp_path / "seismo.db")) assert store.geo_range == 1 + + +# Review finding (2026-10-29): a histogram's row is stored at its file's time, +# a second before the unit's 0C time on most histograms (#51) -- K441LTOK.QZ0 +# decodes to 16:06:34, its 0C says 16:06:35 -- so a pre-check on the 0C time +# alone missed it, and a live download overwrote the watcher's row and sidecar. +# The Blastware name encodes the event's own second and does not miss. + +@pytest.mark.parametrize("stored_as", ["K441LTOK.QZ0", "K441LTOK.QZ0H"]) # direct / call-in name +def test_a_live_histogram_the_watcher_already_stored_writes_nothing(sdm, stored_as): + import json + store, db = sdm + ev, rec = store.save_imported_bw((HIST_FIX / "K441LTOK.QZ0").read_bytes(), + source_path=Path(stored_as), serial_hint=None, + bw_report_text=(HIST_FIX / "K441LTOK_QZ0_ASCII.TXT").read_bytes()) + db.insert_events([ev], serial="BE9441", waveform_records={ev._waveform_key.hex(): rec}, + device_family="series3") # as the import route (server.py) does + [row0] = db.query_events(serial="BE9441") + assert row0["timestamp"] != "2026-10-26T16:06:35" # the case: the row sits a second off + assert row0["blastware_filename"] == stored_as + out = persist_download(_hist_download("K441LTOK.QZ0", "01110000", + datetime.datetime(2026, 10, 26, 16, 6, 35)), store=store, db=db) + assert out.inserted is False and out.detail["already_stored"] is True + assert db.query_events(serial="BE9441") == [row0] + sidecar = json.loads(store.sidecar_path_for("BE9441", stored_as).read_text()) + assert sidecar["source"]["kind"] == "bw-import" + + +def test_a_repeat_live_histogram_a_second_off_its_file_adds_nothing(sdm): + store, db = sdm + when = datetime.datetime(2026, 10, 26, 16, 6, 35) + first = persist_download(_hist_download("K441LTOK.QZ0", "01110000", when), store=store, db=db) + again = persist_download(_hist_download("K441LTOK.QZ0", "01110000", when), store=store, db=db) + assert first.inserted is True and again.inserted is False and again.detail["already_stored"] is True + assert len(db.query_events(serial="BE9441")) == 1 + + +def test_a_name_that_is_not_blastwares_matches_nothing(sdm): + store, db = sdm + assert db.find_event_by_blastware_name("BE9441", "imported.bw") is None