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:
serversdownandClaude Opus 5.5 committed 2026-10-29 23:56:44 -04:00
1 parent dbd8a91661
commit 3a1d34ae92
4 files changed
+71 -3

No files matched your search

+40
View File
@@ -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