feat(db): events are identified by unit + time + type (#51)
The events table was UNIQUE(serial, timestamp): one event per unit per second. A unit records a waveform and a histogram in the same second legitimately -- 209 times in prod's 2026-09-30 store once histograms carry Blastware's start -- so the key is now (serial, timestamp, record_type). Agreed with Brian 2026-11-03. - Migration 3 rebuilds the table from its own current definition with only the key swapped, in one transaction: every column, row and id is kept (Terra-View reads rows by id) and the indexes are recreated. Migration 1's check no longer mistakes the new key for a missing one. - The upsert, the estimate guard and the restamp match on the type too. The restamp also re-types a file's own row in place: before v0.33.0 a row's type came from the filename, and a re-import under the file's own type must not add a second row. - An untyped event still refreshes the untyped row at its second (a UNIQUE key treats NULLs as distinct). - scripts/repair_unknown_serials.py checks the new key. No time changes yet: that is the next commit, and they ship together. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ALX2R8pghDaZuti9U7mEE9
This commit is contained in:
1 parent
e8242aaadc
commit
ea2dfc21b7
3 files changed
+193
-23
No files matched your search
@@ -0,0 +1,123 @@
|
||||
"""An event is identified by unit + time + type (#51).
|
||||
|
||||
The events table was unique on (serial, timestamp): one event per unit per
|
||||
second. A unit records a waveform and a histogram in the same second
|
||||
legitimately -- 209 times in prod's 2026-09-30 store once histograms carry
|
||||
Blastware's start -- so the key is now (serial, timestamp, record_type).
|
||||
Agreed with Brian 2026-11-03.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
import os
|
||||
import sqlite3
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from minimateplus.models import Event, PeakValues, Timestamp # noqa: E402
|
||||
from sfm.database import SeismoDb # noqa: E402
|
||||
|
||||
T = "2026-08-25T10:33:29"
|
||||
T_EARLY = "2026-08-25T10:33:28"
|
||||
|
||||
|
||||
def _event(when: str, rtype: str, key: str = "0111646a", pvs: float = 0.5) -> Event:
|
||||
t = datetime.datetime.fromisoformat(when)
|
||||
ev = Event(index=0)
|
||||
ev.timestamp = Timestamp(raw=b"", flag=0x10, year=t.year, unknown_byte=0, month=t.month,
|
||||
day=t.day, hour=t.hour, minute=t.minute, second=t.second)
|
||||
ev._waveform_key = bytes.fromhex(key)
|
||||
ev.record_type = rtype
|
||||
ev.peak_values = PeakValues(tran=0.1, vert=0.2, long=0.3, peak_vector_sum=pvs, micl=None)
|
||||
return ev
|
||||
|
||||
|
||||
def _store(db: SeismoDb, ev: Event, filename: str) -> tuple:
|
||||
return db.insert_events([ev], serial="BE12844", device_family="series3",
|
||||
waveform_records={ev._waveform_key.hex(): {"filename": filename}})
|
||||
|
||||
|
||||
def _rows(db: SeismoDb) -> list:
|
||||
with sqlite3.connect(str(db.db_path)) as c:
|
||||
c.row_factory = sqlite3.Row
|
||||
return [dict(r) for r in c.execute(
|
||||
"SELECT * FROM events ORDER BY timestamp, record_type")]
|
||||
|
||||
|
||||
def _key_sql(db: SeismoDb) -> str:
|
||||
with sqlite3.connect(str(db.db_path)) as c:
|
||||
return c.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name='events'"
|
||||
).fetchone()[0]
|
||||
|
||||
|
||||
def test_a_waveform_and_a_histogram_can_share_a_second(tmp_path):
|
||||
db = SeismoDb(tmp_path / "seismo_relay.db")
|
||||
assert _store(db, _event(T, "Waveform"), "N844LQHB.ZT0W") == (1, 0)
|
||||
assert _store(db, _event(T, "Histogram", key="0111700a"), "N844LQHB.ZT0H") == (1, 0)
|
||||
assert [(r["timestamp"], r["record_type"]) for r in _rows(db)] == [
|
||||
(T, "Histogram"), (T, "Waveform")]
|
||||
|
||||
|
||||
def test_the_same_event_twice_is_still_one_row(tmp_path):
|
||||
db = SeismoDb(tmp_path / "seismo_relay.db")
|
||||
_store(db, _event(T, "Histogram"), "N844LQHB.ZT0H")
|
||||
assert _store(db, _event(T, "Histogram", pvs=0.8), "N844LQHB.ZT0H") == (0, 1)
|
||||
(row,) = _rows(db)
|
||||
assert row["peak_vector_sum"] == 0.8 # the upsert refreshed it
|
||||
|
||||
|
||||
def test_a_histogram_moves_onto_its_waveforms_second(tmp_path):
|
||||
# What the release backfill does: the histogram was stored a second early;
|
||||
# its corrected start is the waveform's second. The row moves in place.
|
||||
db = SeismoDb(tmp_path / "seismo_relay.db")
|
||||
_store(db, _event(T, "Waveform"), "N844LQHB.ZT0W")
|
||||
_store(db, _event(T_EARLY, "Histogram", key="0111700a"), "N844LQHB.ZS0H")
|
||||
(before,) = [r for r in _rows(db) if r["record_type"] == "Histogram"]
|
||||
assert _store(db, _event(T, "Histogram", key="0111700a"), "N844LQHB.ZS0H") == (0, 1)
|
||||
rows = _rows(db)
|
||||
assert [(r["timestamp"], r["record_type"]) for r in rows] == [(T, "Histogram"), (T, "Waveform")]
|
||||
assert [r["id"] for r in rows if r["record_type"] == "Histogram"] == [before["id"]]
|
||||
|
||||
|
||||
def test_a_re_typed_event_is_corrected_in_place(tmp_path):
|
||||
# Before v0.33.0 the type came from the filename; a re-import with the
|
||||
# file's own type must correct that row, not add a second one.
|
||||
db = SeismoDb(tmp_path / "seismo_relay.db")
|
||||
_store(db, _event(T, "Waveform"), "N844LQHB.ZT0H")
|
||||
(before,) = _rows(db)
|
||||
_store(db, _event(T, "Histogram"), "N844LQHB.ZT0H")
|
||||
(row,) = _rows(db)
|
||||
assert (row["id"], row["record_type"]) == (before["id"], "Histogram")
|
||||
|
||||
|
||||
def test_an_old_database_is_migrated_keeping_every_row_and_id(tmp_path):
|
||||
db = SeismoDb(tmp_path / "seismo_relay.db")
|
||||
_store(db, _event(T, "Waveform"), "N844LQHB.ZT0W")
|
||||
_store(db, _event(T_EARLY, "Histogram", key="0111700a"), "N844LQHB.ZS0H")
|
||||
old_rows = _rows(db)
|
||||
with sqlite3.connect(str(db.db_path)) as c: # put the old key back
|
||||
sql = _key_sql(db).replace("UNIQUE(serial, timestamp, record_type)",
|
||||
"UNIQUE(serial, timestamp)")
|
||||
c.executescript(f"""
|
||||
ALTER TABLE events RENAME TO events_new_key;
|
||||
{sql};
|
||||
INSERT INTO events SELECT * FROM events_new_key;
|
||||
DROP TABLE events_new_key;
|
||||
""")
|
||||
assert "UNIQUE(serial, timestamp)" in _key_sql(db)
|
||||
|
||||
db = SeismoDb(tmp_path / "seismo_relay.db") # opening migrates
|
||||
assert "UNIQUE(serial, timestamp, record_type)" in _key_sql(db)
|
||||
assert _rows(db) == old_rows
|
||||
assert _store(db, _event(T, "Histogram", key="0111700b"), "N844LQHC.ZT0H") == (1, 0)
|
||||
with sqlite3.connect(str(db.db_path)) as c:
|
||||
names = {r[0] for r in c.execute(
|
||||
"SELECT name FROM sqlite_master WHERE type='index' AND tbl_name='events'")}
|
||||
assert {"idx_events_serial", "idx_events_timestamp"} <= names
|
||||
|
||||
|
||||
def test_reopening_keeps_the_new_key(tmp_path):
|
||||
SeismoDb(tmp_path / "seismo_relay.db")
|
||||
db = SeismoDb(tmp_path / "seismo_relay.db")
|
||||
assert "UNIQUE(serial, timestamp, record_type)" in _key_sql(db)
|
||||
Reference in new issue
Block a user