Activity and traffic persist across sessions, with housekeeping

The monitor seeds its six-hour activity buckets and its by-kind tallies
from stored observations at session start, so the header pulse, the
dashboard chart, and the traffic panel open mid-story after a restart
instead of counting from zero. A once-per-session sweep prunes
observations past the new history_days retention setting (default 365,
0 = keep forever), keeping the always-recording database bounded.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Jean-Pierre MartineauandClaude Fable 5 committed 2026-07-11 18:22:29 -04:00
1 parent 30498efe0b
commit 3903d996db
4 files changed
+96 -7

No files matched your search

+3
View File
@@ -13,6 +13,9 @@ trace_cooldown_s = 1.0
# 0, 1, or 2 (clamped): 0 = one shot, 2 (default) = up to three tries. Ctrl-R in chat is a
# further manual retry on top of this.
direct_message_soft_retries = 2
# Days of overheard-packet history the recorder keeps; older observations are pruned
# once per session start (0 = keep everything forever).
history_days = 365
# db_path = "C:/Users/you/meshterm/meshterm.db"
# output_dir = "C:/Users/you/meshterm/output"
+7
View File
@@ -100,6 +100,11 @@ class Settings:
(and starts always-on background listening) immediately at launch. When
``False`` the connection is opened lazily — only once monitoring is turned on
or a tool first needs the radio — so launching the menu touches no serial port.
history_days: How many days of overheard-packet history the recorder retains.
Observations older than this are pruned once per session start (the
housekeeping sweep), keeping the database bounded while the dashboard and
Time Machine draw on everything inside the window. ``0`` disables pruning
entirely — history grows forever.
"""
config_dir: Path = field(default_factory=default_config_dir)
@@ -112,6 +117,7 @@ class Settings:
tx_opt_max: int = 28
direct_message_soft_retries: int = 2
connect_on_start: bool = True
history_days: int = 365
def __post_init__(self) -> None:
"""Derive dependent paths that were not explicitly provided."""
@@ -187,4 +193,5 @@ class Settings:
),
),
connect_on_start=bool(data.get("connect_on_start", True)),
history_days=max(0, int(data.get("history_days", 365))),
)
+43
View File
@@ -831,6 +831,49 @@ class Repository:
arrivals.sort(key=lambda t: t[2], reverse=True)
return arrivals
def kind_counts(self, *, since: Optional[datetime] = None) -> dict[str, int]:
"""Stored observation tallies by packet class, optionally windowed.
The persistent seed of the dashboard's traffic panel: what the recorder has
heard across sessions, by kind, so the panel opens populated instead of
counting from zero every launch. Messages and acks are separate event
families (not observations) and are counted live on top of this.
Args:
since: Only observations at or after this time, if given.
Returns:
``kind → count`` for every packet class ever stored (in the window).
"""
sql = "SELECT kind, COUNT(*) AS n FROM observations"
params: list[Any] = []
if since is not None:
sql += " WHERE observed_at >= ?"
params.append(since.isoformat())
sql += " GROUP BY kind"
rows = self._conn.execute(sql, params).fetchall()
return {row["kind"]: row["n"] for row in rows}
def prune_observations(self, older_than: datetime) -> int:
"""Delete observations that aged past the retention window (housekeeping).
The once-per-session sweep that keeps a permanently-recording database
bounded: everything the dashboard and Time Machine read lives inside the
retention window, so rows beyond it are pure weight. Timestamps compare as
strings, like every other ``observed_at`` filter here.
Args:
older_than: Observations strictly before this time are deleted.
Returns:
How many rows were removed (0 when the window has no stale rows).
"""
cur = self._conn.execute(
"DELETE FROM observations WHERE observed_at < ?", (older_than.isoformat(),)
)
self._conn.commit()
return cur.rowcount if cur.rowcount is not None and cur.rowcount > 0 else 0
def node_names(self) -> dict[str, str]:
"""The most recent advertised name per node, across everything ever recorded.
+43 -7
View File
@@ -18,10 +18,12 @@ counters shown live in the menu header.
from __future__ import annotations
import time
from datetime import timedelta
from typing import TYPE_CHECKING, Optional
from ..core.connection import Unsubscribe
from ..core.events import EventKind, MeshEvent
from ..core.models import utcnow
if TYPE_CHECKING:
from ..context import AppContext
@@ -60,13 +62,44 @@ class MonitorService:
# indicator answers "is the mesh alive?", not "any mail?". Pruned as it rolls,
# so it never holds more than the six-hour window plus one closing bucket.
self._activity: dict[int, int] = {}
# Session-long tallies by packet class (advert/telemetry/packet/message/ack),
# fed by the same kind-unfiltered subscription — the dashboard's traffic panel.
# Tallies by packet class (advert/telemetry/packet/message/ack): seeded from
# stored history below, then fed live by the kind-unfiltered subscription —
# the dashboard's traffic panel, persistent across sessions.
self._kind_counts: dict[str, int] = {}
# Housekeeping: age out observations past the retention window once per
# session, so an always-recording database stays bounded (0 = keep forever).
days = getattr(getattr(ctx, "settings", None), "history_days", 0) or 0
if days:
try:
pruned = ctx.repo.prune_observations(utcnow() - timedelta(days=days))
if pruned:
ctx.log.info("history housekeeping: pruned %s observations", pruned)
except Exception as exc: # noqa: BLE001 - housekeeping must never block startup
ctx.log.debug("history housekeeping failed: %s", exc)
# Total observations already in the database when the session began; the live
# "total" is this plus what we capture this session (this process is the only
# writer during an interactive session), avoiding a DB count on every repaint.
self._start_total = ctx.repo.observation_count()
self._seed_from_history()
def _seed_from_history(self) -> None:
"""Warm the activity buckets and kind tallies from stored observations.
The dashboard's persistence: a fresh session opens mid-story — the activity
chart already showing the trailing six hours and the traffic panel its
all-history tallies — instead of an empty chart that only fills while the app
happens to be running. Live events then stack on top (they are *new* rows, so
nothing double-counts). Stored history holds observations only; messages and
acks resume counting from zero each session.
"""
try:
window = timedelta(seconds=ACTIVITY_BUCKET_S * ACTIVITY_BUCKETS)
for obs in self._ctx.repo.recent_observations(since=utcnow() - window):
bucket = int(obs.observed_at.timestamp() // ACTIVITY_BUCKET_S)
self._activity[bucket] = self._activity.get(bucket, 0) + 1
self._kind_counts = dict(self._ctx.repo.kind_counts())
except Exception as exc: # noqa: BLE001 - a cold start is worse than a blank chart
self._ctx.log.debug("monitor: history seed failed: %s", exc)
@property
def active(self) -> bool:
@@ -86,10 +119,11 @@ class MonitorService:
"""All-packet counts per one-minute bucket over the trailing six hours.
Newest first — index 0 is the current minute — the order the chart widgets
expect (they draw "now" at the right edge). Counts everything the hub fans out
this session; time before launch simply reads as silence. Consumers slice
however much of the window fits their chart and treat the rest as history in
reserve.
expect (they draw "now" at the right edge). Seeded from stored observations at
session start and fed live by everything the hub fans out, so the header's
pulse and the dashboard's chart open mid-story after a restart. Consumers
slice however much of the window fits their chart and treat the rest as
history in reserve.
Returns:
:data:`ACTIVITY_BUCKETS` bucket counts.
@@ -98,8 +132,10 @@ class MonitorService:
return tuple(self._activity.get(bucket - i, 0) for i in range(ACTIVITY_BUCKETS))
def kind_counts(self) -> dict[str, int]:
"""Session tallies by packet class: advert/telemetry/packet, message, ack.
"""Tallies by packet class: advert/telemetry/packet, message, ack.
Seeded from stored history at session start and grown live from there, so the
numbers describe everything the recorder retains, not just this session.
Observation classes come from the packet itself (``Observation.kind``);
messages and acks are their own classes. A copy, safe to mutate.
"""