feat(webapp): list and download events by serial, for both series

- Load events with a serial lists the shared EventRef shape as a table: index,
  time, type, PVS and the three peaks (or —), each row with its own Download.
- Download POSTs the row's event_id; SFM checks and stores it.  The status bar
  says what was stored (or that it already was), and the stored event opens
  in the viewer.  A refusal shows the server's detail verbatim.
- eventView() (device-routing block, Node-tested) reads both event shapes for
  the table, the chips and the peaks bar.
- A list loaded without a serial keeps the chips and the per-index Load /
  Save / Download buttons, unchanged; for a serial-loaded list those buttons
  are off with a tooltip pointing at the rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
serversdownandClaude Opus 5.5 committed 2026-10-29 11:54:43 -04:00
1 parent 44d6e754bd
commit 9aae3b09de
2 files changed
+117 -17

No files matched your search

+100 -16
View File
@@ -987,6 +987,12 @@
</div>
<div id="waveform-area" style="flex:1; overflow-y:auto;">
<div class="db-table-wrap" id="serial-events-wrap" style="display:none; margin:12px;">
<table class="db-table">
<thead><tr><th>#</th><th>Time</th><th>Type</th><th>PVS</th><th>Tran</th><th>Vert</th><th>Long</th><th></th></tr></thead>
<tbody id="serial-events-tbody"></tbody>
</table>
</div>
<div id="empty-state">
<svg width="40" height="40" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
<polyline points="22 12 18 12 15 21 9 3 6 12 2 12"/>
@@ -1557,6 +1563,24 @@ function changeSummary(body) {
return `${body.action}: ${body.outcome}, ${body.verified} (audit ${id8})`;
}
// What a chip and the peaks bar show for an event, from either shape: the
// shared EventRef (serial mode) or the legacy event (host/port mode).
function eventView(ev) {
const label = typeof ev.timestamp === 'string' ? ev.timestamp.replace('T', ' ')
: (ev.timestamp && ev.timestamp.display) || `Event ${ev.index}`;
let peaks = null;
if (ev.peaks_ips || ev.peak_vector_sum_ips != null) {
const p = ev.peaks_ips || {};
peaks = { tran: p.tran ?? null, vert: p.vert ?? null, long: p.long ?? null,
pvs: ev.peak_vector_sum_ips ?? null, micPsi: null };
} else if (ev.peak_values) {
const pv = ev.peak_values;
peaks = { tran: pv.tran_in_s ?? null, vert: pv.vert_in_s ?? null, long: pv.long_in_s ?? null,
pvs: pv.peak_vector_sum ?? null, micPsi: pv.micl_psi ?? null };
}
return { label, title: ev.record_type || '', peaks };
}
function downloadSummary(body) {
if (body.inserted === false) return `already stored — ${body.stored_filename}`;
return `stored ${body.stored_filename}, ${body.filesize} bytes, check: ${(body.detail || {}).check}`;
@@ -1570,7 +1594,8 @@ function downloadSummary(body) {
let unitInfo = null;
let eventList = [];
let storageInfo = null; // /device/events/storage_range — cheap, read on connect
let eventsLoaded = false; // the event chain walk is opt-in; see loadEventList()
let eventsLoaded = false;
let eventsBySerial = false; // the list came from the serial-based /device/events (EventRef shape) // the event chain walk is opt-in; see loadEventList()
let currentEvent = 0;
let charts = {};
let geoAdcScale = 6.206;
@@ -1639,7 +1664,7 @@ async function deviceFetch(action, params = {}, opts = {}) {
const body = await r.json().catch(() => ({}));
if (!r.ok) {
const d = body.detail;
const err = new Error(typeof d === 'string' ? d : (d && d.message) || r.statusText);
const err = new Error(typeof d === 'string' ? d : d ? (d.message || JSON.stringify(d)) : r.statusText);
err.status = r.status; err.body = body;
throw err;
}
@@ -1787,11 +1812,17 @@ async function connectUnit() {
function setEventButtonsEnabled() {
const n = eventList.length;
document.getElementById('load-btn').disabled = n === 0;
document.getElementById('save-btn').disabled = n === 0;
document.getElementById('download-btn').disabled = n === 0;
// A list loaded by serial is downloaded row by row (by event_id); these
// per-index buttons belong to the legacy list.
const bySerial = eventsBySerial;
for (const id of ['load-btn', 'save-btn', 'download-btn']) {
const el = document.getElementById(id);
if (el.dataset.legacyTitle === undefined) el.dataset.legacyTitle = el.title;
el.disabled = n === 0 || bySerial;
el.title = bySerial ? 'For a list loaded without a serial — use a row\'s Download' : el.dataset.legacyTitle;
}
document.getElementById('prev-btn').disabled = true;
document.getElementById('next-btn').disabled = n <= 1;
document.getElementById('next-btn').disabled = n <= 1 || bySerial;
}
// ── Event list (opt-in — this is the slow chain walk) ──────────────────────────
@@ -1799,11 +1830,12 @@ async function loadEventList() {
if (!devHost()) { setStatus('Connect to a device first.', 'error'); return; }
const btn = document.getElementById('load-events-btn');
btn.disabled = true;
setStatus('Walking the event chain — this can take a while…', 'loading');
setStatus(serialMode() ? 'Listing events…' : 'Walking the event chain — this can take a while…', 'loading');
try {
const evData = await deviceFetch('events');
eventList = evData.events || [];
eventsLoaded = true;
eventsBySerial = serialMode();
// /device/events re-reads compliance; fold it in.
if (evData.device) unitInfo = { ...unitInfo, ...evData.device };
} catch (e) {
@@ -2390,11 +2422,20 @@ async function writeCallHome() {
function populateEventChips() {
const el = document.getElementById('event-chips');
el.innerHTML = '';
const wrap = document.getElementById('serial-events-wrap');
wrap.style.display = eventsBySerial && eventList.length ? 'block' : 'none';
if (eventsBySerial) {
populateSerialEventTable();
document.getElementById('empty-state').style.display = eventList.length ? 'none' : 'flex';
updatePeaksBar(null);
return;
}
eventList.forEach((ev, i) => {
const chip = document.createElement('button');
chip.className = 'event-chip' + (i === 0 ? ' active' : '');
chip.textContent = ev.timestamp?.display ?? `Event ${ev.index}`;
chip.title = ev.record_type || '';
const view = eventView(ev);
chip.textContent = view.label;
chip.title = view.title;
chip.onclick = () => selectEvent(i);
el.appendChild(chip);
});
@@ -2403,6 +2444,31 @@ function populateEventChips() {
if (eventList.length > 0) updatePeaksBar(eventList[0]);
}
function populateSerialEventTable() {
const tbody = document.getElementById('serial-events-tbody');
tbody.innerHTML = '';
const ips = v => v != null ? v.toFixed(5) : '—';
eventList.forEach((ev, i) => {
const view = eventView(ev);
const pk = view.peaks || {};
const tr = document.createElement('tr');
for (const text of [ev.index, view.label, view.title || '—', ips(pk.pvs), ips(pk.tran), ips(pk.vert), ips(pk.long)]) {
const td = document.createElement('td');
td.textContent = text;
tr.appendChild(td);
}
const td = document.createElement('td');
const btn = document.createElement('button');
btn.className = 'btn btn-ghost';
btn.textContent = '⬇ Download';
btn.title = 'Download this event from the unit, check it and store it; then open it';
btn.onclick = () => downloadEventBySerial(i, btn);
td.appendChild(btn);
tr.appendChild(td);
tbody.appendChild(tr);
});
}
function selectEvent(idx) {
currentEvent = idx;
document.querySelectorAll('.event-chip').forEach((c, i) => c.classList.toggle('active', i === idx));
@@ -2418,16 +2484,17 @@ function stepEvent(delta) {
}
function updatePeaksBar(ev) {
const pv = ev?.peak_values;
const pk = ev ? eventView(ev).peaks : null;
const bar = document.getElementById('peaks-bar');
if (!pv) { bar.classList.remove('visible'); return; }
if (!pk) { bar.classList.remove('visible'); return; }
bar.classList.add('visible');
qs('pk-tran').textContent = pv.tran_in_s != null ? `${pv.tran_in_s.toFixed(5)} in/s` : '—';
qs('pk-vert').textContent = pv.vert_in_s != null ? `${pv.vert_in_s.toFixed(5)} in/s` : '—';
qs('pk-long').textContent = pv.long_in_s != null ? `${pv.long_in_s.toFixed(5)} in/s` : '—';
const micDbl = pv.micl_psi != null && pv.micl_psi > 0 ? 20 * Math.log10(pv.micl_psi / DBL_REF) : null;
const ips = v => v != null ? `${v.toFixed(5)} in/s` : '—';
qs('pk-tran').textContent = ips(pk.tran);
qs('pk-vert').textContent = ips(pk.vert);
qs('pk-long').textContent = ips(pk.long);
const micDbl = pk.micPsi != null && pk.micPsi > 0 ? 20 * Math.log10(pk.micPsi / DBL_REF) : null;
qs('pk-mic').textContent = micDbl != null ? `${micDbl.toFixed(1)} dBL` : '—';
qs('pk-pvs').textContent = pv.peak_vector_sum != null ? `${pv.peak_vector_sum.toFixed(5)} in/s` : '—';
qs('pk-pvs').textContent = ips(pk.pvs);
}
async function loadWaveform() {
@@ -2488,6 +2555,23 @@ async function saveEventToDb() {
}
}
// A list loaded by serial: download one event by its event_id -- SFM checks it
// and stores it -- then open it from the store. An event already stored says so.
async function downloadEventBySerial(i, btn) {
const ev = eventList[i];
btn.disabled = true;
setStatus(`Downloading event #${ev.index} (${eventView(ev).label})…`, 'loading');
try {
const body = await deviceFetch('download', { event_id: ev.event_id });
setStatus(downloadSummary(body), 'ok');
if (body.db_event_id) openSidecarModal(body.db_event_id);
} catch (e) {
setStatus(`Download failed: ${e.message}`, 'error');
} finally {
btn.disabled = false;
}
}
// ── Download the event file to the user's computer ──────────────────────────
//
// Uses a transient anchor + click trick so the browser surfaces its native
+17 -1
View File
@@ -24,7 +24,7 @@ PAGE = Path(__file__).resolve().parent.parent / "sfm" / "sfm_webapp.html"
NODE = shutil.which("node")
pytestmark = pytest.mark.skipif(NODE is None, reason="node is not installed; the page-logic tests need it")
EXPORTS = ["seriesFromSerial", "unitQuery", "deviceUrl", "confirmedSeries", "controlsFor",
EXPORTS = ["seriesFromSerial", "unitQuery", "deviceUrl", "confirmedSeries", "controlsFor", "eventView",
"changeSummary", "downloadSummary"]
@@ -188,3 +188,19 @@ def test_device_url_puts_an_event_index_in_the_path():
assert r[0] == {"method": "GET", "path": "/device/event/3/waveform?host=h&tcp_port=9034"}
assert r[1] == {"method": "GET", "path": "/device/event/0/blastware_file?serial=BE9441&host=h&tcp_port=9034"}
assert "throws" in r[2]
def test_event_view_reads_both_event_shapes():
shared = {"event_id": "BE9441:0111f210:20261026170036", "index": 2, "timestamp": "2026-10-26T17:00:36",
"record_type": "histogram", "peak_vector_sum_ips": 1.083,
"peaks_ips": {"tran": 0.25, "vert": 1.08, "long": 0.195}}
legacy = {"index": 0, "timestamp": {"display": "2026-10-26 16:06:37"}, "record_type": "Waveform",
"peak_values": {"tran_in_s": 0.03, "vert_in_s": 0.095, "long_in_s": 0.02,
"peak_vector_sum": 0.1, "micl_psi": 0.0001}}
bare = {"index": 5}
a, b, c = run(["eventView", [shared]], ["eventView", [legacy]], ["eventView", [bare]])
assert a == {"label": "2026-10-26 17:00:36", "title": "histogram",
"peaks": {"tran": 0.25, "vert": 1.08, "long": 0.195, "pvs": 1.083, "micPsi": None}}
assert b == {"label": "2026-10-26 16:06:37", "title": "Waveform",
"peaks": {"tran": 0.03, "vert": 0.095, "long": 0.02, "pvs": 0.1, "micPsi": 0.0001}}
assert c == {"label": "Event 5", "title": "", "peaks": None}