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).
|
||||
|
||||
Reference in new issue
Block a user