fix(sfm): a download the store already holds is found by its Blastware name, not only its time (#8 review)
persist_download checked 'already stored' on the unit's 0C time only, but a
histogram's row is stored at its file's time, a second earlier on most
histograms (#51): K441LTOK.QZ0 decodes to 16:06:34, its 0C says 16:06:35. So a
live download of an event the watcher had imported missed the check and
re-stored it -- UPSERTing over the watcher's row (Blastware's ZC frequencies and
vector sum lost) and rewriting its sidecar as sfm-live -- and every repeat live
download rewrote the files again.
New SeismoDb.find_event_by_blastware_name(): the row stored under the same
Blastware name (the event's second, per blastware_filename_time; the rule
_restamp_by_file uses), matching a direct name to the call-in copy's W/H name.
Any other name matches nothing. Also two comments from 9492e49 that said the
opposite of the code (the unit's peaks are overlaid; source.kind is a parameter).
Store consequence: none for existing rows; no schema change, no migration.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
1 parent
dbd8a91661
commit
3a1d34ae92
4 files changed
+71
-3
No files matched your search
@@ -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 `<stem>.<AB>0` and
|
||||
Blastware's call-in copy of the same event `<stem>.<AB>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,
|
||||
|
||||
+7
-2
@@ -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
|
||||
|
||||
@@ -317,7 +317,8 @@ class WaveformStore:
|
||||
3. Resolve serial from BW filename (`<P><serial3>...`) or use
|
||||
serial_hint. Falls back to "UNKNOWN".
|
||||
4. Copy the BW bytes verbatim into <root>/<serial>/<filename>.
|
||||
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).
|
||||
|
||||
@@ -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
|
||||
Reference in new issue
Block a user