With the decode checker's fixes merged (histogram vector sum625bc39, mic psi5644dbd, the file's geophone range4844135), the event file reads everything Blastware reports. So a live download's row and samples are now the file's: peaks, vector sum, mic -- and the geophone range from the file's own setup block, the setting when the event was recorded. The live 1A is the unit's setting now, which may have changed since; it is the fallback only when the file's range does not read. Measured: K557J1KB.RQ0W (Sensitive waveform) downloaded with its settings unread -- e.g. while monitoring -- stores 0.019 / 0.055 / 0.019 and PVS 0.061, Blastware's report; its .h5 read 0.155 / 0.44 (8x) before. M010HR6K.OA0H (Sensitive histogram) likewise. K441LTON.900 (Normal) stores Blastware's values whatever the unit's current settings say, and its mic is 5.80e-4 psi (Blastware: 106.0 dB(L)). The unit's 0C summary stays the download check, and fills in the channel peaks and vector sum only when the file's range does not read (never seen in DL2) and the unit is not confirmed Normal -- the impossible-vector-sum guard kept there. The mic is the file's always. Closes the range part of open finding #17. Store consequence: new live downloads only; no backfill, no migration. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
501 lines
25 KiB
Python
501 lines
25 KiB
Python
"""Storing a downloaded event: the same store + DB writes every ingest path makes."""
|
|
from __future__ import annotations
|
|
|
|
import datetime
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from device_access_fakes import FakeMiniMateClient, s3_event # noqa: E402
|
|
from sfm.database import SeismoDb # noqa: E402
|
|
from sfm.device_access.models import DownloadedEvent # noqa: E402
|
|
from sfm.event_ingest import IngestError, backfill_series3_event, persist_download # noqa: E402
|
|
from sfm.waveform_store import WaveformStore # noqa: E402
|
|
|
|
THOR_IDF = Path(__file__).parent / "fixtures" / "thor-idf"
|
|
BW_FIXTURES = Path(__file__).parent / "fixtures" / "5-11-26"
|
|
HIST_FIX = Path(__file__).parent / "fixtures" / "minimate-bench-10-26-26-histogram" / "blastware"
|
|
|
|
|
|
@pytest.fixture
|
|
def sdm(tmp_path):
|
|
return WaveformStore(tmp_path / "waveforms"), SeismoDb(tmp_path / "seismo.db")
|
|
|
|
|
|
def s4_download(name, verified=True, record=None):
|
|
return DownloadedEvent(event_id=f"UM20147:055d4a81:{name[8:22]}", serial="UM20147", series=4,
|
|
filename=name, verified=verified, idf_bytes=(THOR_IDF / name).read_bytes(),
|
|
timestamp=datetime.datetime.strptime(name[8:22], "%Y%m%d%H%M%S"),
|
|
record=dict(record or {}))
|
|
|
|
|
|
def _tree(root):
|
|
"""Every file under the store: (size, mtime_ns). Any write shows."""
|
|
return {str(p.relative_to(root)): (p.stat().st_size, p.stat().st_mtime_ns)
|
|
for p in sorted(root.rglob("*")) if p.is_file()}
|
|
|
|
|
|
def _age_files(directory):
|
|
"""Back-date every file, so a rewrite shows in its mtime whatever the clock's grain."""
|
|
for p in directory.iterdir():
|
|
if p.is_file():
|
|
os.utime(p, ns=(1_000_000_000_000_000_000, 1_000_000_000_000_000_000))
|
|
|
|
|
|
def test_a_series4_waveform_is_stored_and_findable(sdm):
|
|
store, db = sdm
|
|
out = persist_download(s4_download("UM20147_20250531135901.IDFW"), store=store, db=db)
|
|
assert out.stored_filename == "UM20147_20250531135901.IDFW" and out.inserted
|
|
assert (store.root / "UM20147" / out.stored_filename).exists()
|
|
row = db.get_event(out.db_event_id)
|
|
assert row["serial"] == "UM20147" and row["device_family"] == "series4"
|
|
assert out.verified is True and out.sha256
|
|
|
|
|
|
def test_a_fresh_series4_download_keeps_the_units_pvs_and_sensor_location(sdm):
|
|
# Without Thor's TXT the file alone gives only the three PPVs; the unit's
|
|
# 0x0C record carries the vector sum (find_twins matches on it) and the location.
|
|
store, db = sdm
|
|
out = persist_download(s4_download("UM20147_20250531135901.IDFW",
|
|
record={"peak_vector_sum_ips": 0.391, "sensor_location": "Loc A"}),
|
|
store=store, db=db)
|
|
row = db.get_event(out.db_event_id)
|
|
assert out.inserted
|
|
assert (row["peak_vector_sum"], row["sensor_location"]) == (0.391, "Loc A")
|
|
assert row["tran_ppv"] is not None # the file's own peaks still stored
|
|
|
|
|
|
def test_the_record_fills_only_what_the_file_left_empty(sdm, monkeypatch):
|
|
store, db = sdm
|
|
real = store.save_imported_idf
|
|
|
|
def with_values(*a, **kw):
|
|
ev, rec = real(*a, **kw)
|
|
ev.peak_values.peak_vector_sum = 1.5
|
|
ev.project_info.sensor_location = "From the file"
|
|
return ev, rec
|
|
monkeypatch.setattr(store, "save_imported_idf", with_values)
|
|
out = persist_download(s4_download("UM20147_20250531135901.IDFW",
|
|
record={"peak_vector_sum_ips": 0.391, "sensor_location": "Loc A"}),
|
|
store=store, db=db)
|
|
row = db.get_event(out.db_event_id)
|
|
assert (row["peak_vector_sum"], row["sensor_location"]) == (1.5, "From the file")
|
|
|
|
|
|
def test_the_record_is_kept_even_when_the_file_gave_no_peaks_or_project(sdm, monkeypatch):
|
|
store, db = sdm
|
|
real = store.save_imported_idf
|
|
|
|
def bare(*a, **kw):
|
|
ev, rec = real(*a, **kw)
|
|
ev.peak_values = ev.project_info = None
|
|
return ev, rec
|
|
monkeypatch.setattr(store, "save_imported_idf", bare)
|
|
out = persist_download(s4_download("UM20147_20250531135901.IDFW",
|
|
record={"peak_vector_sum_ips": 0.391, "sensor_location": "Loc A"}),
|
|
store=store, db=db)
|
|
row = db.get_event(out.db_event_id)
|
|
assert (row["peak_vector_sum"], row["sensor_location"]) == (0.391, "Loc A")
|
|
|
|
|
|
def test_a_series4_histogram_is_stored(sdm):
|
|
store, db = sdm
|
|
out = persist_download(s4_download("UM20147_20250330070110.IDFH", verified=None), store=store, db=db)
|
|
assert out.inserted and out.db_event_id and out.verified is None
|
|
|
|
|
|
def test_storing_the_same_event_twice_stores_nothing_the_second_time(sdm):
|
|
store, db = sdm
|
|
name = "UM20147_20250531135901.IDFW"
|
|
first = persist_download(s4_download(name), store=store, db=db)
|
|
_age_files(store.root / "UM20147")
|
|
row0 = db.get_event(first.db_event_id)
|
|
tree0 = _tree(store.root)
|
|
again = persist_download(s4_download(name), store=store, db=db)
|
|
assert (first.inserted, again.inserted) == (True, False)
|
|
assert again.db_event_id == first.db_event_id and again.detail["already_stored"] is True
|
|
assert db.query_events(serial="UM20147") == [row0]
|
|
assert (store.root / "UM20147" / name).stat().st_mtime_ns == tree0[f"UM20147/{name}"][1]
|
|
assert _tree(store.root) == tree0
|
|
|
|
|
|
# ── an event THOR's watcher (or Blastware's ACH) already stored ───────────────
|
|
# Built exactly as /db/import/idf_file and /db/import/blastware_file build it
|
|
# (sfm/server.py): the store's import call, then insert_events.
|
|
|
|
def watcher_import_idf(store, db, name):
|
|
ev, rec = store.save_imported_idf((THOR_IDF / name).read_bytes(), source_path=Path(name),
|
|
serial_hint=None, idf_report_text=None)
|
|
serial = rec.get("serial") or "UNKNOWN"
|
|
db.insert_events([ev], serial=serial,
|
|
waveform_records={ev._waveform_key.hex(): rec} if ev._waveform_key else None,
|
|
device_family="series4")
|
|
return serial, rec
|
|
|
|
|
|
def watcher_import_bw(store, db, name):
|
|
ev, rec = store.save_imported_bw((BW_FIXTURES / name).read_bytes(), source_path=Path(name),
|
|
serial_hint=None,
|
|
bw_report_text=(BW_FIXTURES / f"{name}.TXT").read_bytes())
|
|
serial = rec.get("serial") or "UNKNOWN"
|
|
db.insert_events([ev], serial=serial,
|
|
waveform_records={ev._waveform_key.hex(): rec if ev._waveform_key else None}
|
|
if ev._waveform_key else None,
|
|
device_family="series3")
|
|
return serial, rec
|
|
|
|
|
|
def test_downloading_an_event_the_watcher_stored_writes_nothing_series4(sdm):
|
|
store, db = sdm
|
|
name = "UM20147_20250531135901.IDFW"
|
|
serial, rec = watcher_import_idf(store, db, name)
|
|
assert serial == "UM20147"
|
|
# Something only the stored copy has; a rewrite of the sidecar would drop it.
|
|
assert store.patch_sidecar(serial, name, extensions={"operator_note": {"text": "checked on site"}})
|
|
_age_files(store.root / serial)
|
|
[row0] = db.query_events(serial=serial)
|
|
sidecar0 = store.sidecar_path_for(serial, name).read_bytes()
|
|
mtime0 = (store.root / serial / name).stat().st_mtime_ns
|
|
tree0 = _tree(store.root)
|
|
|
|
out = persist_download(s4_download(name, record={"peak_vector_sum_ips": 0.391,
|
|
"sensor_location": "Loc A"}),
|
|
store=store, db=db)
|
|
|
|
assert out.inserted is False and out.detail["already_stored"] is True
|
|
assert out.db_event_id == row0["id"]
|
|
assert (out.stored_filename, out.filesize) == (name, rec["filesize"])
|
|
assert db.query_events(serial=serial) == [row0]
|
|
assert store.sidecar_path_for(serial, name).read_bytes() == sidecar0
|
|
assert (store.root / serial / name).stat().st_mtime_ns == mtime0
|
|
assert _tree(store.root) == tree0
|
|
|
|
|
|
class RecordingStore:
|
|
def __init__(self):
|
|
self.saved = []
|
|
|
|
def save(self, ev, serial, a5_frames, *, geo_range="normal", source_kind="sfm-live"):
|
|
self.saved.append((ev, serial, a5_frames, geo_range))
|
|
return {"filename": "T193LQ9K.OE0", "filesize": 8708, "sha256": "ab" * 32}
|
|
|
|
def save_imported_bw(self, bw_bytes, source_path, *, serial_hint=None, bw_report_text=None,
|
|
source_kind="bw-import", geo_range="normal"):
|
|
self.saved.append(("imported", bw_bytes, source_path, serial_hint, source_kind))
|
|
self.geo_range = geo_range
|
|
ev = s3_event(0, "01110000", datetime.datetime(2026, 10, 1, 8, 0, 0))
|
|
return ev, {"filename": source_path.name, "filesize": len(bw_bytes), "sha256": "ab" * 32}
|
|
|
|
|
|
def test_a_series3_download_is_stored_through_the_file_import_path(tmp_path):
|
|
# #8: the same path production's watcher uses; data from the binary, no backfill.
|
|
db = SeismoDb(tmp_path / "seismo.db")
|
|
store = RecordingStore()
|
|
dl = DownloadedEvent(event_id="BE18193:01110000:20261001080000", serial="BE18193", series=3,
|
|
filename="T193LQ9K.OE0", verified=None, bw_bytes=b"BW-FILE",
|
|
event=s3_event(0, "01110000", datetime.datetime(2026, 10, 1, 8, 0, 0)),
|
|
compliance=FakeMiniMateClient().compliance,
|
|
timestamp=datetime.datetime(2026, 10, 1, 8, 0, 0))
|
|
out = persist_download(dl, store=store, db=db)
|
|
assert store.saved == [("imported", b"BW-FILE", Path("T193LQ9K.OE0"), "BE18193", "sfm-live")]
|
|
assert dl.event.sample_rate is None # the live event is not backfilled
|
|
assert db.get_event(out.db_event_id)["device_family"] == "series3"
|
|
assert out.stored_filename == "T193LQ9K.OE0"
|
|
|
|
|
|
def _hist_download(name, key, when):
|
|
return DownloadedEvent(event_id=f"BE9441:{key}:{when:%Y%m%d%H%M%S}", serial="BE9441", series=3,
|
|
filename=name, verified=None, bw_bytes=(HIST_FIX / name).read_bytes(),
|
|
timestamp=when)
|
|
|
|
|
|
def test_a_series3_histogram_download_is_stored_from_its_file(sdm):
|
|
import hashlib, json
|
|
from sfm import event_hdf5
|
|
store, db = sdm
|
|
dl = _hist_download("K441LTON.900", "0111f210", datetime.datetime(2026, 10, 26, 17, 0, 36))
|
|
out = persist_download(dl, store=store, db=db)
|
|
assert out.inserted is True
|
|
assert out.sha256 == hashlib.sha256(dl.bw_bytes).hexdigest() # the file, byte for byte
|
|
plot = event_hdf5.plot_json_from_hdf5(store.hdf5_path_for("BE9441", "K441LTON.900"))
|
|
assert plot["channels"]["Tran"]["values"] # the histogram decoded
|
|
sidecar = json.loads(store.sidecar_path_for("BE9441", "K441LTON.900").read_text())
|
|
assert sidecar["source"]["kind"] == "sfm-live"
|
|
assert db.get_event(out.db_event_id)["sample_rate"] == 4096
|
|
|
|
|
|
def test_the_small_mic_off_histogram_is_stored_with_samples(sdm):
|
|
# Refused on the 2026-10-29 bench; decodes since #47/#49 came in with dev.
|
|
from sfm import event_hdf5
|
|
store, db = sdm
|
|
dl = _hist_download("K441LTOK.QZ0", "01110000", datetime.datetime(2026, 10, 26, 16, 6, 35))
|
|
out = persist_download(dl, store=store, db=db)
|
|
assert out.inserted is True
|
|
plot = event_hdf5.plot_json_from_hdf5(store.hdf5_path_for("BE9441", "K441LTOK.QZ0"))
|
|
assert plot["channels"]["Vert"]["values"]
|
|
|
|
|
|
def test_a_repeat_series3_download_adds_no_row(sdm):
|
|
store, db = sdm
|
|
when = datetime.datetime(2026, 10, 26, 17, 0, 36)
|
|
first = persist_download(_hist_download("K441LTON.900", "0111f210", when), store=store, db=db)
|
|
again = persist_download(_hist_download("K441LTON.900", "0111f210", when), store=store, db=db)
|
|
assert first.inserted is True and again.inserted is False
|
|
assert len(db.query_events(serial="BE9441")) == 1
|
|
|
|
|
|
def test_downloading_an_event_the_watcher_stored_writes_nothing_series3(tmp_path):
|
|
db = SeismoDb(tmp_path / "seismo.db")
|
|
serial, rec = watcher_import_bw(WaveformStore(tmp_path / "waveforms"), db, "M529LL1L.V70")
|
|
[row0] = db.query_events(serial=serial)
|
|
when = datetime.datetime.fromisoformat(row0["timestamp"])
|
|
# The download's own decode differs from the stored row (peaks, file name), so
|
|
# any write would show in the row.
|
|
ev = s3_event(0, row0["waveform_key"], when)
|
|
store = RecordingStore()
|
|
dl = DownloadedEvent(event_id=f"{serial}:{row0['waveform_key']}:{when:%Y%m%d%H%M%S}",
|
|
serial=serial, series=3, filename="M529LL1L.V70", verified=None, event=ev,
|
|
a5_frames=["frame"], compliance=FakeMiniMateClient().compliance,
|
|
timestamp=when)
|
|
out = persist_download(dl, store=store, db=db)
|
|
assert store.saved == []
|
|
assert db.query_events(serial=serial) == [row0]
|
|
assert out.inserted is False and out.detail["already_stored"] is True
|
|
assert (out.db_event_id, out.stored_filename, out.filesize) == (row0["id"], rec["filename"],
|
|
rec["filesize"])
|
|
assert ev.sample_rate is None # not even backfilled
|
|
|
|
|
|
def test_a_series3_download_with_no_file_stores_nothing_and_says_so(tmp_path):
|
|
store = RecordingStore()
|
|
dl = DownloadedEvent(event_id="BE18193:01110000:-", serial="BE18193", series=3,
|
|
filename="T193LQ9K.OE0", verified=None, event=None, a5_frames=["frame"])
|
|
with pytest.raises(IngestError, match="carried no event file; nothing was stored"):
|
|
persist_download(dl, store=store, db=SeismoDb(tmp_path / "seismo.db"))
|
|
assert store.saved == []
|
|
|
|
|
|
def test_backfill_derives_total_samples_only_when_the_decoded_one_is_implausible():
|
|
cc = FakeMiniMateClient().compliance # 1024 sps, 3.0 s
|
|
ev = s3_event(0, "01110000", datetime.datetime(2026, 10, 1))
|
|
ev.total_samples = 40_000
|
|
backfill_series3_event(ev, cc)
|
|
assert ev.total_samples == 3072
|
|
ev.total_samples = 3000
|
|
backfill_series3_event(ev, cc)
|
|
assert ev.total_samples == 3000
|
|
|
|
|
|
def test_backfill_without_settings_changes_nothing():
|
|
ev = s3_event(0, "01110000", datetime.datetime(2026, 10, 1))
|
|
backfill_series3_event(ev, None)
|
|
assert ev.sample_rate is None
|
|
|
|
|
|
# Since 2026-11-02 a live download stores what its own file says -- peaks, vector
|
|
# sum, mic, and the geophone range the event was recorded at -- and the unit's 0C
|
|
# summary is only the download check. The file's range is the setting when it
|
|
# recorded; the live 1A is the unit's setting now, which may have changed since.
|
|
# Only when the file's range does not read (never seen in DL2) does the 1A decide.
|
|
|
|
def _with_unit_peaks(dl, *, tran, vert, long, micl, pvs):
|
|
from minimateplus.models import PeakValues
|
|
dl.event = s3_event(0, dl.event_id.split(":")[1], dl.timestamp)
|
|
dl.event.peak_values = PeakValues(tran=tran, vert=vert, long=long, micl=micl, peak_vector_sum=pvs)
|
|
return dl
|
|
|
|
|
|
def _range(geo_range):
|
|
cc = FakeMiniMateClient().compliance
|
|
cc.geo_range = geo_range
|
|
return cc
|
|
|
|
|
|
GEO_FIX = Path(__file__).parent / "fixtures" / "geo-range-11-01-26"
|
|
WRONG = dict(tran=0.21, vert=0.36, long=0.27, micl=0.0009, pvs=0.40) # believable, wrong; clears the old guard
|
|
|
|
|
|
def _download_from(path, serial):
|
|
from minimateplus.blastware_file import blastware_filename_time
|
|
when = blastware_filename_time(path.name)
|
|
return DownloadedEvent(event_id=f"{serial}:01110000:{when:%Y%m%d%H%M%S}", serial=serial, series=3,
|
|
filename=path.name, verified=None, bw_bytes=path.read_bytes(), timestamp=when)
|
|
|
|
|
|
def _h5_peaks(store, serial, name):
|
|
from sfm import event_hdf5
|
|
plot = event_hdf5.plot_json_from_hdf5(store.hdf5_path_for(serial, name))
|
|
return tuple(max(abs(v) for v in plot["channels"][c]["values"]) for c in ("Tran", "Vert", "Long"))
|
|
|
|
|
|
def _file_range_unread(monkeypatch):
|
|
# A file whose range does not read: the decoder then assumes Normal.
|
|
# K441LTON.900 is a Normal file, so its peaks are what such a decode gives.
|
|
import minimateplus.event_file_io as efi
|
|
real = efi.decode_blastware_bytes
|
|
|
|
def unread(raw, **kw):
|
|
ev = real(raw, **kw)
|
|
ev.geo_range = None
|
|
return ev
|
|
monkeypatch.setattr(efi, "decode_blastware_bytes", unread)
|
|
|
|
|
|
@pytest.mark.parametrize("compliance", [_range(0), _range(1), None],
|
|
ids=["unit-says-normal", "unit-says-sensitive", "settings-unread"])
|
|
def test_a_series3_download_stores_what_its_file_says(sdm, compliance):
|
|
# K441LTON.900 was recorded in Normal range, whatever the unit is set to now.
|
|
store, db = sdm
|
|
dl = _with_unit_peaks(_hist_download("K441LTON.900", "0111f210", datetime.datetime(2026, 10, 26, 17, 0, 36)),
|
|
**WRONG)
|
|
dl.compliance = compliance
|
|
row = db.get_event(persist_download(dl, store=store, db=db).db_event_id)
|
|
assert (row["tran_ppv"], row["vert_ppv"], row["long_ppv"]) == pytest.approx((0.2, 0.385, 0.255), abs=1e-4)
|
|
assert row["peak_vector_sum"] == pytest.approx(0.3852, abs=1e-4) # Blastware: 0.385
|
|
assert row["mic_ppv"] == pytest.approx(5.80e-4, abs=2e-6) # Blastware: 106.0 dB(L)
|
|
assert _h5_peaks(store, "BE9441", "K441LTON.900")[1] == pytest.approx(0.385, abs=0.006)
|
|
|
|
|
|
def test_a_sensitive_waveform_downloaded_with_its_settings_unread_is_blastwares(sdm):
|
|
# e.g. downloaded while monitoring (the 1A carries no settings). The samples
|
|
# were scaled as Normal before -- 8x: Vert read 0.44 in the .h5.
|
|
store, db = sdm
|
|
dl = _download_from(GEO_FIX / "K557J1KB.RQ0W", "BE9557")
|
|
row = db.get_event(persist_download(dl, store=store, db=db).db_event_id)
|
|
assert (row["tran_ppv"], row["vert_ppv"], row["long_ppv"]) == pytest.approx((0.019, 0.055, 0.019), abs=6e-4)
|
|
assert row["peak_vector_sum"] == pytest.approx(0.061, abs=6e-4) # Blastware's report
|
|
assert _h5_peaks(store, "BE9557", "K557J1KB.RQ0W") == pytest.approx((0.019, 0.055, 0.019), abs=7e-4)
|
|
|
|
|
|
def test_a_sensitive_histogram_downloaded_with_its_settings_unread_keeps_its_scale(sdm):
|
|
from minimateplus.event_file_io import decode_blastware_bytes
|
|
store, db = sdm
|
|
path = GEO_FIX / "M010HR6K.OA0H"
|
|
pv = decode_blastware_bytes(path.read_bytes()).peak_values
|
|
persist_download(_download_from(path, "BE11010"), store=store, db=db)
|
|
assert _h5_peaks(store, "BE11010", path.name) == pytest.approx((pv.tran, pv.vert, pv.long), abs=7e-4)
|
|
assert max(_h5_peaks(store, "BE11010", path.name)) < 0.02 # not 8x (Vert would read ~0.07)
|
|
|
|
|
|
def test_when_the_files_range_does_not_read_a_sensitive_units_own_peaks_stand(sdm, monkeypatch):
|
|
_file_range_unread(monkeypatch)
|
|
store, db = sdm
|
|
dl = _with_unit_peaks(_hist_download("K441LTON.900", "0111f210", datetime.datetime(2026, 10, 26, 17, 0, 36)),
|
|
tran=0.025, vert=0.048, long=0.032, micl=0.0009, pvs=0.0481)
|
|
dl.compliance = _range(1)
|
|
row = db.get_event(persist_download(dl, store=store, db=db).db_event_id)
|
|
assert (row["tran_ppv"], row["vert_ppv"], row["long_ppv"]) == pytest.approx((0.025, 0.048, 0.032), abs=1e-6)
|
|
assert row["peak_vector_sum"] == pytest.approx(0.0481, abs=1e-6)
|
|
assert row["mic_ppv"] == pytest.approx(5.80e-4, abs=2e-6) # the mic's scale is not the geophones'
|
|
assert _h5_peaks(store, "BE9441", "K441LTON.900")[1] == pytest.approx(0.385 / 8, abs=7e-4)
|
|
|
|
|
|
def test_when_the_files_range_does_not_read_and_the_unit_says_normal_the_files_peaks_stand(sdm, monkeypatch):
|
|
_file_range_unread(monkeypatch)
|
|
store, db = sdm
|
|
dl = _with_unit_peaks(_hist_download("K441LTON.900", "0111f210", datetime.datetime(2026, 10, 26, 17, 0, 36)),
|
|
**WRONG)
|
|
dl.compliance = _range(0)
|
|
row = db.get_event(persist_download(dl, store=store, db=db).db_event_id)
|
|
assert (row["vert_ppv"], row["peak_vector_sum"]) == pytest.approx((0.385, 0.3852), abs=1e-4)
|
|
|
|
|
|
def test_an_impossible_unit_vector_sum_is_not_kept(sdm, monkeypatch):
|
|
# Bench finding #4: BE9441's 0C read -0.0 for a vector sum whose channels reach
|
|
# 1.08. The unit's values are used only when neither range reads.
|
|
_file_range_unread(monkeypatch)
|
|
store, db = sdm
|
|
dl = _with_unit_peaks(_hist_download("K441LTOK.R10", "011106a0", datetime.datetime(2026, 10, 26, 16, 6, 37)),
|
|
tran=0.25, vert=1.08, long=0.195, micl=0.0001, pvs=-0.0)
|
|
row = db.get_event(persist_download(dl, store=store, db=db).db_event_id)
|
|
assert row["peak_vector_sum"] == pytest.approx(1.0826, abs=1e-3) # the file's own
|
|
|
|
|
|
def test_the_units_range_reaches_the_store(tmp_path):
|
|
store = RecordingStore()
|
|
cc = FakeMiniMateClient().compliance
|
|
cc.geo_range = 1 # Sensitive, 1.25 in/s
|
|
dl = DownloadedEvent(event_id="BE18193:01110000:20261001080000", serial="BE18193", series=3,
|
|
filename="T193LQ9K.OE0", verified=None, bw_bytes=b"BW-FILE", compliance=cc,
|
|
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
|
|
|
|
|
|
def test_a_live_histogram_is_labelled_a_histogram(sdm):
|
|
# #41 (the record type from STRT[18], not the name): a direct download's name,
|
|
# K441LTON.900, carries no type letter, so the bytes decide.
|
|
store, db = sdm
|
|
out = persist_download(_hist_download("K441LTON.900", "0111f210", datetime.datetime(2026, 10, 26, 17, 0, 36)),
|
|
store=store, db=db)
|
|
assert db.get_event(out.db_event_id)["record_type"] == "Histogram"
|
|
|
|
|
|
def _stored_as(store, db, name, fixture="K441LTOK.QZ0"):
|
|
ev, rec = store.save_imported_bw((HIST_FIX / fixture).read_bytes(), source_path=Path(name),
|
|
serial_hint=None)
|
|
db.insert_events([ev], serial="BE9441", waveform_records={ev._waveform_key.hex(): rec},
|
|
device_family="series3")
|
|
|
|
|
|
def test_a_lettered_name_does_not_match_the_other_type_in_the_same_second(sdm):
|
|
# SFM's own names now carry W/H (2026-10-30). In Histogram+Continuous a
|
|
# waveform can trigger in the second a histogram starts; with both letters
|
|
# known they are different events.
|
|
store, db = sdm
|
|
_stored_as(store, db, "K441LTOK.QZ0H")
|
|
assert db.find_event_by_blastware_name("BE9441", "K441LTOK.QZ0W") is None
|
|
assert db.find_event_by_blastware_name("BE9441", "K441LTOK.QZ0H")["blastware_filename"] == "K441LTOK.QZ0H"
|
|
assert db.find_event_by_blastware_name("BE9441", "K441LTOK.QZ0") is not None # a letterless name: no opinion
|
|
|
|
|
|
def test_a_letterless_stored_name_still_matches_a_lettered_download(sdm):
|
|
# Rows stored before 2026-10-30, and Blastware's own direct downloads, have no letter.
|
|
store, db = sdm
|
|
_stored_as(store, db, "K441LTOK.QZ0")
|
|
assert db.find_event_by_blastware_name("BE9441", "K441LTOK.QZ0H")["blastware_filename"] == "K441LTOK.QZ0"
|