Two efforts that landed together because the v2 topic work extends tables the health-telemetry effort added days earlier in the same files/functions, making them impractical to separate cleanly: Health/diagnostics telemetry (schema, Jul 13-17): - New Postgres tables: device_alert_events, device_boot_events, device_ping_samples, device_diagnostics_reports, plus a `source` column on device_logs to distinguish log origins - Query/service layer in pg_mqtt.py and database/__init__.py for inserting and listing this history, plus a "latest metrics" endpoint combining most-recent diagnostics + ping RTT per device - mqtt/router.py gains list endpoints for alert/boot/ping/diagnostics history, consumed by the upcoming Health tab MQTT v2 topic migration (Sep 21): - Heartbeat payload flattened per vesper_mqtt_topic_spec_v2.md, adding rssi/free_heap/state/ok fields - Command replies move to control/ack, device-initiated events to control/reports; mqtt/client.py subscribes to the new topic set and runs a ping_loop (wired up in main.py) for RTT sampling - mqtt/logger.py and pg_mqtt.py updated to parse and persist the new payload shape alongside the legacy fields for backwards compatibility Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
83 lines
3.6 KiB
Python
83 lines
3.6 KiB
Python
"""device health tab — boot events, heartbeat free_heap, ping samples
|
|
|
|
Adds three pieces of schema needed for the device Health tab:
|
|
|
|
1. device_boot_events — structured, insert-only history of the firmware's
|
|
boot_report MQTT event (vesper/{uid}/status/info, type="boot_report").
|
|
Previously this payload was flattened into a single device_logs text line
|
|
with all structured fields (boot_count, crash detail) discarded on arrival.
|
|
This table gives the console a real timeline to query and chart against,
|
|
instead of parsing log strings.
|
|
|
|
2. heartbeats.free_heap — firmware now includes free_heap on every 30s
|
|
heartbeat (previously only available once per boot via boot_report), so
|
|
the console can chart heap trend instead of seeing one point per boot.
|
|
|
|
3. device_ping_samples — backend-computed RTT samples. The firmware's ping
|
|
command now echoes back a caller-supplied timestamp; the backend pings
|
|
each online device on an interval and records (now - echoed_ts) here.
|
|
A dedicated table rather than reusing `commands` because `commands` isn't
|
|
shaped for time-series charting (mixed command types, no fast per-device
|
|
time-range query path) and pruning ping history independently of other
|
|
command history is desirable.
|
|
|
|
Revision ID: f6a7b8c9d0e1
|
|
Revises: e5f6a7b8c9d0
|
|
Create Date: 2026-07-16 00:00:00.000000
|
|
"""
|
|
from typing import Sequence, Union
|
|
import sqlalchemy as sa
|
|
from alembic import op
|
|
|
|
revision: str = "f6a7b8c9d0e1"
|
|
down_revision: Union[str, None] = "e5f6a7b8c9d0"
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
op.create_table(
|
|
"device_boot_events",
|
|
sa.Column("id", sa.BigInteger(), primary_key=True, autoincrement=True),
|
|
sa.Column("device_serial", sa.String(128), nullable=False),
|
|
sa.Column("boot_count", sa.Integer(), nullable=True),
|
|
sa.Column("reset_reason", sa.String(64), nullable=True),
|
|
sa.Column("is_fault", sa.Boolean(), nullable=False, server_default=sa.false()),
|
|
sa.Column("free_heap", sa.Integer(), nullable=True),
|
|
sa.Column("crash_task", sa.String(64), nullable=True),
|
|
sa.Column("crash_pc", sa.BigInteger(), nullable=True),
|
|
sa.Column("crash_exc_cause", sa.Integer(), nullable=True),
|
|
sa.Column("crash_exc_vaddr", sa.BigInteger(), nullable=True),
|
|
sa.Column("occurred_at", sa.DateTime(timezone=True), nullable=False,
|
|
server_default=sa.func.now()),
|
|
)
|
|
op.create_index(
|
|
"idx_device_boot_events_serial_occurred",
|
|
"device_boot_events",
|
|
["device_serial", sa.text("occurred_at DESC")],
|
|
)
|
|
|
|
op.add_column("heartbeats", sa.Column("free_heap", sa.Integer(), nullable=True))
|
|
|
|
op.create_table(
|
|
"device_ping_samples",
|
|
sa.Column("id", sa.BigInteger(), primary_key=True, autoincrement=True),
|
|
sa.Column("device_serial", sa.String(128), nullable=False),
|
|
sa.Column("rtt_ms", sa.Integer(), nullable=False),
|
|
sa.Column("sampled_at", sa.DateTime(timezone=True), nullable=False,
|
|
server_default=sa.func.now()),
|
|
)
|
|
op.create_index(
|
|
"idx_device_ping_samples_serial_sampled",
|
|
"device_ping_samples",
|
|
["device_serial", sa.text("sampled_at DESC")],
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.drop_index("idx_device_ping_samples_serial_sampled", table_name="device_ping_samples")
|
|
op.drop_table("device_ping_samples")
|
|
op.drop_column("heartbeats", "free_heap")
|
|
op.drop_index("idx_device_boot_events_serial_occurred", table_name="device_boot_events")
|
|
op.drop_table("device_boot_events")
|