fix(sfm): a download never downgrades a stored event -- an existing row is returned, nothing written

THOR's watcher (and Blastware's ACH) store every event through
/db/import/idf_file and /db/import/blastware_file, with the paired report.
Storing a download of the same event afterwards overwrote that richer copy:
on Series 4 the file, .h5 and sidecar were rewritten (extensions dropped);
on Series 3 the row lost PVS, project strings and ZC frequencies and was
repointed at a second, review-less file set.

persist_download now looks up (serial, timestamp) first.  A row there is
returned as it stands -- inserted false, detail.already_stored true -- with
no store call, no insert_events and no sidecar write.  Never merge.

- DownloadedEvent gains `timestamp` (the unit's own event time) and
  `record` (Series 4's 0x0C fields); both adapters set them.
- The Series-3 `event is None` guard now runs before backfill/save, with a
  message that is true there ("nothing was stored").
- Tests build the watcher's state exactly as the import endpoints do and
  prove the row dict, the sidecar bytes and the file mtimes are unchanged.

No schema change, no migration, no codec change.

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-21 17:35:23 -04:00
1 parent ddb22d9624
commit ef096e3082
9 files changed
+178 -22

No files matched your search

+26 -3
View File
@@ -2,9 +2,13 @@
POST /device/events/download.
Both series end in the two writes every other ingest path makes: the waveform
store (file + sidecar + .h5) and SeismoDb.insert_events(). The database matches
rows on (serial, timestamp), so storing an event already there -- by an earlier
download, or by THOR's watcher -- refreshes the row instead of duplicating it.
store (file + sidecar + .h5) and SeismoDb.insert_events().
An event already stored -- by an earlier download, or by THOR's watcher or
Blastware's ACH -- is returned as it is and NOTHING is written. The watcher's
copy is the richer one (its paired report carries PVS, project strings and ZC
frequencies a bare download does not), so a download never merges into it or
replaces it.
"""
from __future__ import annotations
@@ -51,13 +55,32 @@ def _event_dt(ev: Any) -> Optional[datetime.datetime]:
return None
def _already_stored(dl: DownloadedEvent, row: dict) -> EventDownload:
return EventDownload(
event_id=dl.event_id, serial=dl.serial, series=dl.series, db_event_id=row["id"],
stored_filename=row.get("blastware_filename") or "",
filesize=row.get("blastware_filesize"),
sha256=None, # the events table does not keep one
inserted=False, verified=dl.verified,
detail={**dl.detail, "already_stored": True},
)
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.
if dl.timestamp is not None:
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])
if dl.series == 4:
ev, rec = store.save_imported_idf(dl.idf_bytes, source_path=Path(dl.filename),
serial_hint=dl.serial)
family = "series4"
else:
ev = dl.event
if ev is None:
raise IngestError(f"{dl.filename}: the download carried no event; nothing was stored")
backfill_series3_event(ev, dl.compliance)
geo_range = getattr(dl.compliance, "geo_range", None) if dl.compliance else None
rec = store.save(ev, serial=dl.serial, a5_frames=dl.a5_frames,