sfm/mlg_timeline.py builds, from a unit's stored records: sessions (with how each ended, its events and its trigger level as logged), the gaps between them and what each means (call-in, scheduled off, lost power, stopped by a person, memory full), clock steps (a session starting before the previous one stopped, as at the end of daylight saving), and call-ins: delivered, no_file (a Modem Exit that left no file) and nothing_new (an empty file). monitoring_at() answers monitoring / not_monitoring / unknown, never claiming more than the records show. Tested on real files, including BE8081's weekend: Battery Low Exit Saturday 10:47, back Monday 12:47, two calls that left no file. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ALX2R9LdWawLzUxwewZGA1
151 lines
7.0 KiB
Python
151 lines
7.0 KiB
Python
"""
|
|
Tests for sfm/mlg_timeline.py — a unit's monitoring timeline from its .MLG
|
|
records, and "was it monitoring at T" (spec §4).
|
|
|
|
Each case is a real file; the expected sessions were read off the records by
|
|
hand (docs/superpowers/specs/2026-11-09-mlg-unit-timeline-design.md,
|
|
"How a session appears").
|
|
"""
|
|
|
|
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 sfm.database import SeismoDb
|
|
from sfm.mlg_ingest import ingest_mlg
|
|
from sfm.mlg_timeline import build_timeline, monitoring_at
|
|
from sfm.waveform_store import WaveformStore
|
|
|
|
FIX = Path(__file__).parent / "fixtures" / "mlg-11-9-26"
|
|
UTC = datetime.timezone.utc
|
|
dt = datetime.datetime
|
|
BASE = dt(2026, 1, 1, tzinfo=UTC)
|
|
|
|
|
|
def _timeline(tmp_path, serial, *names):
|
|
db, store = SeismoDb(tmp_path / "t.db"), WaveformStore(tmp_path / "w")
|
|
for i, name in enumerate(names):
|
|
res = ingest_mlg(db, store, name, (FIX / name).read_bytes(),
|
|
file_mtime=BASE + datetime.timedelta(hours=i))
|
|
assert res["status"] == "ok", res
|
|
return build_timeline(db.query_mlg_records(serial), db.query_mlg_files(serial))
|
|
|
|
|
|
def test_sessions_gaps_and_an_event_session(tmp_path):
|
|
tl = _timeline(tmp_path, "BE12844", "BE128443067.MLG", "BE128443068.MLG")
|
|
s = tl["sessions"]
|
|
assert len(s) == 3
|
|
assert (s[0]["start"], s[0]["stop"], s[0]["ended_by"], s[0]["kind"]) == (
|
|
"2026-09-07T18:47:27", "2026-09-07T20:00:00", "Timer Stop", "monitoring")
|
|
assert s[0]["trigger_level"] == "Geo: 0.0500 in/s Mic: 125.0 dB(L)"
|
|
assert (s[1]["start"], s[1]["stop"], s[1]["end_after"], s[1]["end_before"]) == (
|
|
"2026-09-08T06:00:13", None, "2026-09-08T13:38:25", "2026-09-08T13:41:16")
|
|
assert s[1]["running_at_last_report"] is False
|
|
assert [(e["start"], e["stop"], e["trigger_source"]) for e in s[1]["events"]] == [
|
|
("2026-09-08T13:38:22", "2026-09-08T13:38:25", 1)]
|
|
assert (s[2]["start"], s[2]["stop"], s[2]["ended_by"]) == (
|
|
"2026-09-08T13:41:16", "2026-09-08T18:45:00", "Modem Exit")
|
|
g = tl["gaps"]
|
|
assert [(x["start"], x["stop"], x["meaning"]) for x in g] == [
|
|
("2026-09-07T20:00:00", "2026-09-08T06:00:13", "scheduled off"),
|
|
("2026-09-08T13:38:25", "2026-09-08T13:41:16", "unknown")]
|
|
assert g[0]["minutes"] == pytest.approx(600.217, abs=0.01)
|
|
assert g[1]["minutes"] == pytest.approx(2.85, abs=0.01)
|
|
assert tl["clock_steps"] == []
|
|
|
|
|
|
def test_monitoring_at(tmp_path):
|
|
tl = _timeline(tmp_path, "BE12844", "BE128443067.MLG", "BE128443068.MLG")
|
|
m = monitoring_at(tl, dt(2026, 9, 8, 10, 0))
|
|
assert (m["status"], m["ambiguous"], len(m["sessions"])) == ("monitoring", False, 1)
|
|
assert m["trigger_level"] == "Geo: 0.0500 in/s Mic: 125.0 dB(L)"
|
|
assert monitoring_at(tl, dt(2026, 9, 8, 13, 38, 0))["status"] == "monitoring"
|
|
assert monitoring_at(tl, dt(2026, 9, 8, 13, 40, 0))["status"] == "unknown"
|
|
off = monitoring_at(tl, dt(2026, 9, 7, 23, 0))
|
|
assert off["status"] == "not_monitoring" and off["gap"]["meaning"] == "scheduled off"
|
|
assert monitoring_at(tl, dt(2027, 1, 1))["status"] == "unknown"
|
|
assert monitoring_at(tl, dt(2026, 1, 1))["status"] == "unknown"
|
|
|
|
|
|
def test_monitoring_at_boundaries_are_inclusive(tmp_path):
|
|
tl = _timeline(tmp_path, "BE12844", "BE128443067.MLG", "BE128443068.MLG")
|
|
for at in (dt(2026, 9, 7, 18, 47, 27), dt(2026, 9, 7, 20, 0, 0), dt(2026, 9, 8, 6, 0, 13)):
|
|
assert monitoring_at(tl, at)["status"] == "monitoring", at
|
|
|
|
|
|
def test_clock_step_back_is_not_a_negative_gap(tmp_path):
|
|
tl = _timeline(tmp_path, "BE12844", "BE128443155.MLG", "BE128443156.MLG")
|
|
assert [s["start"] for s in tl["sessions"]] == [
|
|
"2026-10-31T18:47:28", "2026-11-01T06:00:13", "2026-11-01T17:47:25"]
|
|
assert all(g["minutes"] >= 0 for g in tl["gaps"])
|
|
assert [g["meaning"] for g in tl["gaps"]] == ["scheduled off"]
|
|
(step,) = tl["clock_steps"]
|
|
assert (step["previous_stop"], step["next_start"]) == ("2026-11-01T18:45:00", "2026-11-01T17:47:25")
|
|
assert step["minutes"] == pytest.approx(-57.583, abs=0.01)
|
|
m = monitoring_at(tl, dt(2026, 11, 1, 18, 0))
|
|
assert (m["status"], m["ambiguous"], len(m["sessions"])) == ("monitoring", True, 2)
|
|
|
|
|
|
def test_weekend_battery_outage_and_missed_calls(tmp_path):
|
|
tl = _timeline(tmp_path, "BE8081", "BE8081321.MLG")
|
|
s = tl["sessions"]
|
|
assert [x["start"] for x in s] == [
|
|
"2019-04-12T17:17:59", "2019-04-12T18:38:14", "2019-04-13T07:05:15", "2019-04-15T12:47:05"]
|
|
assert [x["kind"] for x in s] == ["histogram"] * 4
|
|
assert [x["ended_by"] for x in s] == ["Modem Exit", "Modem Exit", "Battery Low Exit", "Modem Exit"]
|
|
assert s[0]["stop"] == "2019-04-12T18:33:00"
|
|
g = tl["gaps"]
|
|
assert [x["meaning"] for x in g] == ["call-in", "call-in", "lost power"]
|
|
assert [x["minutes"] for x in g] == pytest.approx([5.233, 5.233, 2999.833], abs=0.01)
|
|
assert tl["call_ins"] == [
|
|
{"kind": "no_file", "unit_time": "2019-04-12T18:33:00", "file_time": None,
|
|
"file_name": "BE8081321.MLG"},
|
|
{"kind": "no_file", "unit_time": "2019-04-13T07:00:01", "file_time": None,
|
|
"file_name": "BE8081321.MLG"},
|
|
{"kind": "delivered", "unit_time": "2019-04-15T17:15:00",
|
|
"file_time": "2026-01-01T00:00:00+00:00", "file_name": "BE8081321.MLG"},
|
|
]
|
|
m = monitoring_at(tl, dt(2019, 4, 14, 12, 0))
|
|
assert m["status"] == "not_monitoring" and m["gap"]["meaning"] == "lost power"
|
|
|
|
|
|
def test_histogram_session_keeps_its_waveform_event(tmp_path):
|
|
tl = _timeline(tmp_path, "BE8081", "BE8081.MLG")
|
|
(s,) = tl["sessions"]
|
|
assert (s["start"], s["stop"], s["kind"], s["trigger_level"], s["ended_by"]) == (
|
|
"2018-02-16T15:15:31", "2018-02-16T15:15:49", "histogram", "Histogram", "Modem Exit")
|
|
(e,) = s["events"]
|
|
assert (e["start"], e["trigger_source"], e["trigger_level"]) == (
|
|
"2018-02-16T15:15:47", 2, "Geo: 0.300 in/s")
|
|
|
|
|
|
def test_event_with_an_end_bit_inside_a_histogram(tmp_path):
|
|
tl = _timeline(tmp_path, "BE9441", "BE94412.MLG")
|
|
s = tl["sessions"]
|
|
assert [(x["start"], x["stop"], x["ended_by"], x["kind"]) for x in s] == [
|
|
("2026-10-26T16:06:34", "2026-10-26T16:07:21", "Keyboard Exit", "histogram"),
|
|
("2026-10-26T17:00:36", "2026-10-26T18:53:53", "Remote Exit", "histogram")]
|
|
assert [e["description"] for e in s[0]["events"]] == ["Event Recorded Memory Full Exit"]
|
|
assert [g["meaning"] for g in tl["gaps"]] == ["stopped by a person"]
|
|
|
|
|
|
def test_call_in_kinds(tmp_path):
|
|
tl = _timeline(tmp_path, "BE6907", "BE6907750.MLG", "BE6907751.MLG")
|
|
assert [(c["kind"], c["unit_time"], c["file_name"]) for c in tl["call_ins"]] == [
|
|
("no_file", "2026-08-22T18:30:00", "BE6907750.MLG"),
|
|
("delivered", "2026-08-23T18:30:00", "BE6907750.MLG"),
|
|
("nothing_new", None, "BE6907751.MLG")]
|
|
|
|
|
|
def test_no_data_gives_an_empty_timeline():
|
|
tl = build_timeline([], [])
|
|
assert tl == {"sessions": [], "gaps": [], "clock_steps": [], "call_ins": []}
|
|
assert monitoring_at(tl, dt(2026, 9, 8))["status"] == "unknown"
|