docs(spec): monitor logs (.MLG) and each unit's monitoring timeline

Design for reading Blastware .MLG files into SFM and deriving each unit's
monitoring timeline (sessions, stop reasons, call-ins), for shot reports
and outage history. Includes the measured MLG format: length-led 292-byte
records with a CRC-16/AUG-CCITT tail (3,329 / 3,329 records).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ALX2R9LdWawLzUxwewZGA1
This commit is contained in:
serversdownandClaude Opus 5.5 committed 2026-11-13 03:33:02 -05:00
1 parent 77a0296496
commit 6f5898fe4e
1 file changed
+332
@@ -0,0 +1,332 @@
# Monitor logs (.MLG) and each unit's monitoring timeline — design
**Date:** 2026-11-09 · **Branch:** `feat/mlg-import` (worktree `~/seismo-relay-mlg`) ·
**Status:** design, for review
## Why this exists
Every Series-3 unit sends a Blastware monitor log, an `.MLG` file, at every call-in, whatever mode
it is recording in. It lists each time the unit started monitoring, each event, and each time it
stopped and why. Event files cannot replace it: a unit in continuous (waveform) mode sends an event
file only when something triggers, so a quiet day leaves no file at all.
Brian named two uses (2026-11-09):
1. **Shot reports.** Every event from every unit on a site goes in the report. A unit that did not
trigger (often the farthest one) is listed as "did not trigger", with its printed `.MLG`
attached as proof it was monitoring at the time of the blast. Example: BE6907 and BE12844
monitored the same job, June to November 2026, both in continuous mode. BE6907 is much farther
away and recorded 10 events to BE12844's 55.
2. **Outage history.** It is good to know when a unit was not recording, for how long, and why. A
unit on solar and battery can die for a weekend and nobody would notice. Most units keep
recording when they cannot call in; only losing the power and then the internal battery stops
them. So this is history seen once the unit calls in again, not a live alarm.
The `.MLG` is also what the watcher's heartbeat sniffs today to show that a unit is still calling
in. SFM holding the files would later let it answer that itself.
## Goals
1. Read a Blastware `.MLG` into its records, with every record's checksum verified.
2. Accept forwarded `.MLG`s through a new import route, storing each record once.
3. Keep each unit's monitoring timeline, derived from its records: when it was recording, how each
session ended, the events inside, and the call-ins. It must answer two questions:
- was unit X monitoring at time T, and with what trigger level?
- when was it not monitoring, for how long, and why?
4. Serve the records and the timeline through `/db/*`, for Terra-View to use later.
5. Bulk-import a directory of `.MLG`s (DL2 holds about 86,000), and cross-check the records against
the event files' footers.
## Non-goals
- **The watcher forwarding `.MLG`s.** That is a change in `../series3-watcher`, Brian's call, and a
separate piece. Until it lands, `.MLG`s reach SFM only through the import route and the bulk tool.
- **Anything in Terra-View:** the timeline view, the shot report, taking "last call-in" from SFM
instead of the heartbeat. Later pieces.
- **Live "missed its call-in" alerts.** A dead unit sends nothing; this timeline shows the gap
once it is back.
- **Series 4.** Thor writes no `.MLG`.
- **Editing the event decoders** (`event_file_io.py`, `decode_check.py`, `histogram_codec.py`,
`waveform_codec.py`). Another session owns them; this project imports from them only.
## Anti-goals (would make it a failure even if it works)
- **Claiming monitoring the data does not show.** A session still open at the last call-in is
"running at the last call-in", never "running now". Where a session's end is not recorded, the
timeline says so and gives the window it lies in.
- **Dropping a damaged file or record silently.** A record that fails its checksum is reported,
never half-read into the timeline.
- **Taking the serial from the file name when the file carries it.** Names are `<serial><counter>`
with nothing between, so `BE11010304` cannot be split by reading it.
- **Mixing clocks.** Record times are the unit's own local clock. File times are the PC's. They are
stored and labelled separately.
## What was measured
Grades: **measured** means checked on the files named; **inferred** means a reading of them that
has not been tested.
**Files examined:** 1,273 `.MLG`s from DL2's `C:\Blastware 10\Event\autocall home` (BE6907 369 and
BE12844 197, from 2026; BE8081 707, 2018–19), in `~/dl2-archive/mlg/`. Also the 9 bench files in
`example-events/`. Analysis scripts used for this section are in the session scratchpad and are
not part of the build.
### The file format (measured on all 1,282 files)
- **Layout:** an 18-byte prefix `10 00 01 80 00 00 "Instantel\0" 07 2c`, then whole 292-byte
records. The size is always `18 + 292·n`.
- **Every record** is `[length uint16 LE = 0x0122 (290)][id 2 bytes] … [CRC 2 bytes]`. Id `0e a0` is
the header record (one, first, carrying the serial). Id `0e 80` is a log record.
- **Checksum:** CRC-16/AUG-CCITT (poly 0x1021, init 0x1D0F, no reflection) over the record's bytes
`[0:288]`, stored little-endian at `[290:292]`. Bytes `[288:290]` are `00 00` and not covered.
This is the rule the Micromate setup blocks use (`micromate.setup_block`). It matches **3,329 of
3,329** records, with no failures.
- The reference (D.4.2) put a CRC at the *start* of each record. That was the previous record's
tail, which is also why every file had 2 unexplained bytes at its end.
- **A log record** (offsets from its length field):
| offset | field |
|---|---|
| `[4:12]` | start: day, month, year **LE**, 0, hour, min, sec |
| `[12:20]` | stop, same format; all zeros = no stop |
| `[20:22]` | monitor-log flag word, uint16 LE: the same word as the event footer's (D.6.14) |
| `[22:24]` | trigger source, uint16 LE: `0x0001`–`0x0008` a channel, `0x0400` a histogram |
| `[24:34]` | serial, null-padded ASCII |
| `[34:…]` | text, null-padded (below) |
- **Text:**
- a "monitoring started" record: `Trigger Level : Geo: 0.0500 in/s Mic: 125.0 dB(L)`, or
`Trigger Level : Histogram`
- an event record: `08`, then the event time in 8 bytes (LE year), then
`Geo: 0.0500 in/s Mic: 125.0 dB(L)`
- a closed session: `Geo: 0.0500 in/s Mic: 125.0 dB(L)`
- **Empty files:** some files are the 18-byte prefix alone, with no records and so no serial inside.
### How Blastware writes them (measured on the 3 DL2 units)
- **One file per call-in**, named `<serial><counter>.MLG`, the counter going up by one each time.
- **No record appears in two files.** Each holds what happened since the previous call-in.
- **A file is written 1–2 minutes after the unit stops for its call-in** ("Modem Exit"). The unit's
clock and DL2's agree to within those 2 minutes.
- ⚠ Dates of files that came through a Windows zip read an hour early from March to October.
Windows converts with the offset in force at zip time, not the one at the file's date. A
robocopy copy keeps the true time.
- **BE6907 writes two empty files 2–3 minutes after nearly every real one** (230 of its 369).
BE12844 and BE8081 never do. Inferred: the unit calls back in and has nothing new.
- **Some files hold more than one Modem Exit:** 38 files (BE8081 33, BE6907 3, BE12844 2). A
file is written just after its *last* Modem Exit. The earlier ones are calls that left no file.
Example: `BE8081321` holds Modem Exits on 2019-04-12 18:33, 04-13 07:00 and 04-15 17:15.
- Inferred: the call failed, or the PC did not take it.
- Either way, this is the "missed call-in" history Brian asked about.
- **The unit's clock steps back an hour at the first call-in after daylight saving ends.** On
2026-11-01 BE12844 ran `06:00:13 → 18:45:00` (unit still on EDT). It called in at 17:46 EST,
and its next session is `17:47:25 → 18:45:00`. BE6907 shows the same. So local times on that day
overlap. Inferred: Blastware sets the unit's clock at the call-in, and in spring the clock
steps forward the same way.
- **Blastware writes the text, not the unit.** The two bench copies of the same records
(`ach-vs-manual/BE11529.MLG`, `BE115292.MLG`) differ only in `Trigger Level: Histogram` vs
`Trigger Level : Histogram`. A record's identity must not include its text or its checksum.
- **Cable or direct downloads are cumulative.** The bench `4-3-26-multi/BE11529.MLG` repeats the
4-1 records. Only the call-in files hold "only what is new".
### How a session appears (measured on the 3 DL2 units)
- **A session with no events** is one closed record: start, stop and why it stopped. BE12844's day:
`06:00:13 → 18:45:00` "Modem Exit" (its call-in), then `18:47 → 20:00` "Timer Stop" (a
work-hours timer).
- **A session with events** is an open "monitoring started" record (flag word `ffff`, no stop),
then one record per event (`0x0001` + the channel). The unit calls in minutes after each event,
and monitoring resumes as a *new* session minutes later. Example, BE12844 on 9-8:
- `06:00:13` started, then an event `13:38:22 → 13:38:25` (Tran)
- a file written about a minute later
- next file: `13:41:16 → 18:45:00` "Modem Exit"
The first session's end is not written anywhere. It lies between the last event's stop and the
next session's start (inferred: the call-in after the event ended it).
- **A histogram session** is a "monitoring started" record at T, then a histogram record from T+1 s
to the stop, carrying the stop reason (`0x0021` = Event Recorded + Modem Exit, source `0x0400`).
- **A histogram + waveform session** also has waveform event records whose start falls inside the
histogram record's span (BE8081, 5 in 2018). Example: histogram `15:15:32 → 15:15:49`, with a
Vert event `15:15:47 → 15:15:49`. Its trigger-level text says only "Histogram". The geophone
level appears only on its event records.
### Cross-check against the event files (measured)
Each "Event Recorded" record was matched to DL2's event files by **stop time + flag word + trigger
source**:
- **BE8081:** 785 / 785 records match an event file, and all 785 of its DL2 event files in that
period have a record. 714 histogram footers start exactly at the session's "monitoring started"
record; the other 71 start where the record does.
- **BE6907 / BE12844:** 25 / 25 event files have a record. The 42 records with no file all stop
after 2026-08-25, where the DL2 copy ends.
- On 2026-08-27 both units triggered at 13:00:42 / 13:00:43, the same blast.
## Design
### 1. The reader (codec library: pure, no I/O beyond the file)
`read_mlg(path)` replaces the stub in `minimateplus/blastware_file.py` and returns a new model
(`MlgFile`, `MlgRecord` in `models.py`). It does not reuse `MonitorLogEntry`, which is the live
0x0A shape. Per record:
- start, stop (None when open), flag word, trigger source
- Blastware's words, from `event_file_io.monitor_log_description()` (imported, not copied)
- the text, plus the trigger level and the event time parsed from it
- whether the checksum passed
Per file: the serial (from the header record; None for an empty file), the records, whether the
file is whole. A file whose size is not `18 + 292·n`, or with a bad prefix, raises. A record that
fails its checksum is returned flagged; the caller decides.
### 2. `write_mlg()` (last, small)
Fix it to write what Blastware writes: little-endian times, the length-led framing and the CRC.
Test: rebuild bench and DL2 files byte for byte from what the reader returns. If the header
record's unread bytes stop that, say so and leave the writer as it is.
### 3. Storage (`sfm/database.py`)
- **The original file is kept, byte for byte,** in the waveform store beside the unit's event
files. Files are tiny (18 bytes to a few KB). It is the shot-report proof, and Blastware can
open and print it. Rows alone cannot rebuild it, because Blastware's text varies.
- **`mlg_files`:** one row per file received:
- serial, file name, size, SHA-256, record count, an empty flag
- received time, and the file's modified time (UTC, when the sender gives it)
- whether it is a call-in file (default yes; a cable download is not)
The same name with the same bytes is a re-send and is skipped. Empty files all share one
hash, so the name keeps them apart. Some names have no counter (`BE8081.MLG` was DL2's first),
so name alone is not enough. This table is the call-in history, and the only trace of an empty
file.
- **`mlg_records`:** one row per log record: serial, start, stop, flag word, trigger source, text,
and the file it first came in. Stored once:
- unique on `(serial, start, stop, flag word, trigger source)`
- ⚠ an open record's stop is NULL, and SQLite treats NULLs as distinct in a UNIQUE constraint.
The key must handle that, for example with an index on `COALESCE(stop, '')`.
- **The existing `monitor_log` table is left alone.** It holds live 0x0A rows from
`bridges/ach_server.py`, which is not in production. A future SFM call-in server would write
`.MLG`-shaped records into `mlg_records` instead.
### 4. The timeline (computed when asked, not stored)
From a unit's records in time order:
- **A closed record** with no event bit is a session `[start, stop]`, ended for its reason.
- **A histogram record** is a session from its "monitoring started" record (or its own start, when
there is none) to its stop, ended for its reason.
- **A "monitoring started" record followed by events** is a session from that start. Its end is
"between the last event's stop and the next session's start".
- **A "monitoring started" record with nothing after it yet** is "running at the last call-in".
Gaps between sessions take the reason the previous session ended with:
| previous session ended with | the gap means |
|---|---|
| Modem Exit | a call-in |
| Timer Stop | scheduled off |
| Battery Low Exit | lost power |
| Keyboard Exit / Remote Exit | stopped by a person |
| Memory Full Exit | memory full |
| none known | unknown |
- **Waveform events inside a histogram session** belong to that session. They are not sessions
of their own.
- **Trigger level is reported as logged:** the session's text, plus any geophone level its event
records carry. It is never borrowed from a neighbouring session. A histogram session with no
events says "Histogram", geophone level not logged.
- **Clock steps:** a session that starts before the previous one stopped is a clock step, not an
overlap. The timeline shows it as such, with no negative gap. Times are the unit's local clock,
as SFM's event times are; `at`, `from_dt` and `to_dt` are in that clock. If `at` falls in a
repeated hour and matches two sessions, `monitoring_at` returns both and says so. The spring
step forward will show as a gap an hour too long on that one day; that is accepted.
**Call-ins** are listed by the call each file came from:
- **Delivered:** a call-in file's last Modem Exit (unit clock), paired with the file (PC clock).
- **No file:** the earlier Modem Exits in the same file. Calls that left no file.
- **Nothing new:** an empty file, at its file time.
Cable-download files are not call-ins.
### 5. Routes (`sfm/server.py`)
- `POST /db/import/mlg_file`: one or more files, an optional serial hint, each file's modified
time when the sender has it, and `call_in` (default true).
- Mirrors `/db/import/blastware_file`: each file gets its own status, and good files are stored
even when another in the same request is not.
- Per file it returns the serial, records inserted and skipped, and whether it was empty.
- A file with any failed checksum, or that is not whole, stores nothing of itself. Its status
names the record.
- `GET /db/mlg_files/{id}`: the original file, for printing or opening in Blastware.
- `GET /db/units/{serial}/timeline?from_dt=&to_dt=`: sessions (start, end or end window, how it
ended, its events, its trigger level), gaps with their meaning, and call-ins.
- `GET /db/units/{serial}/monitoring_at?at=`: `monitoring` / `not_monitoring` / `unknown`, with the
session and records that say so, and the trigger level. This is the shot report's question.
- `GET /db/mlg_records?serial=&from_dt=&to_dt=`: the raw records.
### 6. Serial of an empty file
An empty file holds no serial. Take, in order:
1. the request's serial hint
2. the longest serial SFM already knows (units, events, `.MLG` headers) that the name starts with
3. otherwise store the file with no serial and report it as unresolved
### 7. Tools
- **`scripts/import_mlg_dir.py <dir>`:** imports a directory tree of `.MLG`s straight into the DB,
with a dry run that prints the counts. Same pattern as the backfill scripts. For DL2's ~86,000.
- Every file it reads is a call-in file (it is pointed at the ACH folder's copy).
- It records file times as found, but they matter only for empty files. A zipped copy's times
are an hour early from March to October.
- The full DL2 import should come from a robocopy copy, which keeps true times.
- **`scratch/check_mlg_vs_events.py <mlg dir> <event dir>`:** the cross-check above, in both
directions, for the whole archive.
### 8. Documentation
- Rewrite the reference's D.4 (`docs/instantel_protocol_reference.md`) with the measured framing,
checksum, fields and file behaviour, keeping the old text as a retraction. Mark D.4.3 solved.
The file is CRLF: edit it with the Edit tool.
- 🔑 **For Brian to pass to the decoder session, not edited here:** the event-file footer's
checksum (D.3.3, open since April) is the same rule. AUG-CCITT over the footer record from its
length `00 1a` through byte 24, stored big-endian. It matches **70,041 / 70,041** DL2 files.
Inferred: every record in the event file's tail chain (D.6.13) carries one.
## Testing
TDD. Fixtures go in a dated `tests/fixtures/` directory (force-added if ignored). A small set
covering each shape:
- a closed-records day (BE12844)
- a session with an event (BE12844 #3067–#3068)
- a histogram day (BE8081)
- a Battery Low Exit (BE8081)
- an empty 18-byte file (BE6907)
- a file with calls that left no file (`BE8081321`)
- the clock step back (BE12844 #3155–#3156)
- a histogram + waveform session (`BE8081.MLG`, 2018-02-16)
- the 4 bench files, including the `ach-vs-manual` pair whose text differs
Plus one file with a byte flipped, for the checksum.
The reader, the storage (including that an open record imported twice is stored once), the
timeline cases above and the routes each get tests. The full suite has 16 known failures; read the
failure list before committing.
## Left to the builder
- Model and helper names; the exact JSON shapes of the routes.
- Whether `read_mlg` moves into its own module later.
- What the header record's other bytes hold (all zero on the files seen).
- **Not in this project:** linking a session's events to SFM's stored event ids. Terra-View may
want that later. It needs a matcher, because the events table stores neither the stop time nor
the flag word, and its times moved in 0.32.0 and 0.34.0.
## Later pieces (not this project)
- **The watcher** forwards `.MLG`s with their modified time, and the heartbeat stops sniffing them.
- **Terra-View** shows each unit's timeline, puts "monitoring during the blast" on shot reports,
and takes "last call-in" from SFM.
- **BE6907's extra call-ins:** worth a look at its modem and call-home settings.