feat: add waveform download and storage.
This commit is contained in:
+58
-1
@@ -445,8 +445,19 @@ class AchSession:
|
||||
# ── Persist to SQLite DB ─────────────────────────────────────
|
||||
_session_start = datetime.datetime.now()
|
||||
try:
|
||||
# Build waveform blobs for events that have full raw_samples
|
||||
_waveform_blobs = {}
|
||||
for _ev in new_events:
|
||||
if _ev._waveform_key and _ev.raw_samples:
|
||||
_blob = _build_waveform_blob(_ev)
|
||||
if _blob:
|
||||
_waveform_blobs[_ev._waveform_key.hex()] = _blob
|
||||
if _waveform_blobs:
|
||||
log.info(" [DB] waveform blobs prepared: %d", len(_waveform_blobs))
|
||||
|
||||
_ev_ins, _ev_skip = self.db.insert_events(
|
||||
new_events, serial=serial or self.peer, session_id=None
|
||||
new_events, serial=serial or self.peer, session_id=None,
|
||||
waveform_blobs=_waveform_blobs,
|
||||
)
|
||||
_ml_ins, _ml_skip = self.db.insert_monitor_log(
|
||||
new_monitor_entries, session_id=None
|
||||
@@ -599,6 +610,52 @@ def _event_to_dict(e: Event) -> dict:
|
||||
}
|
||||
|
||||
|
||||
def _build_waveform_blob(e: Event) -> Optional[str]:
|
||||
"""
|
||||
Serialise a downloaded event's full waveform data as a JSON string for
|
||||
storage in the DB waveform_blob column.
|
||||
|
||||
Returns the same shape as GET /device/event/{index}/waveform so the
|
||||
waveform viewer can consume either source without modification.
|
||||
Returns None if the event has no raw_samples (e.g. metadata-only download).
|
||||
"""
|
||||
raw = e.raw_samples or {}
|
||||
if not raw:
|
||||
return None
|
||||
|
||||
pv = e.peak_values
|
||||
peak_values = None
|
||||
if pv:
|
||||
peak_values = {
|
||||
"tran": pv.tran,
|
||||
"vert": pv.vert,
|
||||
"long": pv.long,
|
||||
"micl_psi": pv.micl,
|
||||
"peak_vector_sum": pv.peak_vector_sum,
|
||||
}
|
||||
|
||||
ts = e.timestamp
|
||||
timestamp_str = (
|
||||
f"{ts.year:04d}-{ts.month:02d}-{ts.day:02d}T"
|
||||
f"{ts.hour:02d}:{ts.minute:02d}:{ts.second:02d}"
|
||||
if ts else None
|
||||
)
|
||||
|
||||
blob = {
|
||||
"index": e.index,
|
||||
"record_type": e.record_type,
|
||||
"timestamp": timestamp_str,
|
||||
"total_samples": e.total_samples,
|
||||
"pretrig_samples": e.pretrig_samples,
|
||||
"rectime_seconds": e.rectime_seconds,
|
||||
"samples_decoded": len(raw.get("Tran", [])),
|
||||
"sample_rate": e.sample_rate,
|
||||
"peak_values": peak_values,
|
||||
"channels": raw,
|
||||
}
|
||||
return json.dumps(blob)
|
||||
|
||||
|
||||
def _monitor_log_entry_to_dict(e: MonitorLogEntry) -> dict:
|
||||
return {
|
||||
"key": e.key,
|
||||
|
||||
Reference in New Issue
Block a user