Day charts: even-spaced date ticks back, an hour per column at 24 h

Revert the axis ticks to the evenly-thinned dated labels — one per day
where they fit, else a subset keeping both ends, the month printed on the
first tick and each rollover. The packed right-first scheme read worse; the
even-split bars and the honest y-marks stay. _thinned_picks now holds the
shared fit-or-subset choice.

The 24 h window charts an hour per column instead of a day (its name is
"24 h", not "1 day"): a new Repository.hourly_series buckets by UTC clock
hour like daily_activity does by day, _fill_hours fills the quiet hours,
and _hour_ticks labels the axis with local HH:00 times closing on "now".
Both feed the same bar machinery — only the resolution and labels differ.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Jean-Pierre MartineauandClaude Fable 5 committed 2026-07-12 23:13:49 -04:00
1 parent 8d2096767f
commit bc1ba7de4c
3 files changed
+238 -77

No files matched your search

+28
View File
@@ -848,6 +848,34 @@ class Repository:
).fetchall()
return [(row["day"], int(row["pkts"]), int(row["nodes"])) for row in rows]
def hourly_series(self, since: datetime) -> list[tuple[str, int, int]]:
"""Per-clock-hour activity totals since a time, oldest first (the 24 h window feed).
The hour-resolution sibling of :meth:`daily_activity`: the same packet and
distinct-node counts, but grouped on the ISO-8601 ``observed_at`` prefix down
to the hour (``YYYY-MM-DDTHH``, a cheap string slice — position 11 is the
``T``) so a day of history folds into ~24 rows however dense it is. Only hours
with traffic come back; the caller fills the quiet ones (see
:func:`~meshterm.ui.timemachine_screen._fill_hours`) so the axis is real
clock time.
Args:
since: Only observations at or after this time.
Returns:
``(hour, packets, nodes)`` per active hour: ``hour`` as a UTC
``YYYY-MM-DDTHH``, every observation counted, and the distinct identified
nodes heard (``packet`` rows excluded from the node count — no reliable
identity), oldest first.
"""
rows = self._conn.execute(
"SELECT substr(observed_at, 1, 13) AS hour, COUNT(*) AS pkts, "
"COUNT(DISTINCT CASE WHEN kind != 'packet' THEN node END) AS nodes "
"FROM observations WHERE observed_at >= ? GROUP BY hour ORDER BY hour",
(since.isoformat(),),
).fetchall()
return [(row["hour"], int(row["pkts"]), int(row["nodes"])) for row in rows]
def hourly_activity(self, *, since: Optional[datetime] = None) -> list[int]:
"""Observation counts by UTC hour of day (0–23) across the stored history.
+140 -45
View File
@@ -22,7 +22,7 @@ transmits.
from __future__ import annotations
from datetime import date, datetime, timedelta
from datetime import date, datetime, timedelta, timezone
from statistics import median
from typing import TYPE_CHECKING, Callable, Optional
@@ -30,7 +30,7 @@ from rich.console import Group, RenderableType
from rich.text import Text
from ..core.models import utcnow
from .braillechart import GAP, _TICK_GAP, axis_chart, chart_span, timeline_rows
from .braillechart import GAP, axis_chart, chart_span, timeline_rows
from .menus import fit_cells, section_heading
from .theme import snr_style
from .tui.render import render_lines
@@ -368,18 +368,37 @@ def _day_centers(days: int, chars: int) -> list[int]:
]
def _day_ticks(shown: list, chars: int) -> list[tuple[int, str]]:
"""``(cell, label)`` axis ticks under the day bars: as many short dates as fit.
def _thinned_picks(n: int, chars: int) -> list[int]:
"""The day/hour indices to label: all of them, or an evenly spaced subset.
Ticks are claimed in priority order — the newest day first, the oldest day
second, then the rest right to left — each label centred under its own bar
(see :func:`_day_centers`) and keeping at least two blank cells from every
label already placed, so the axis packs as dense as it can and thins from the
middle-left outward when room runs out, never losing either end. Dates stay
compact: the month is written only on the oldest day and on the first of a
month (the calendar anchors), the rest read as bare day numbers, and the
newest bar reads ``today`` when it is, mirroring the node page's closing
``now``.
A dated label is at most ``"Jul 12"`` (6 cells) and an hour label ``"18:00"``
(5), so a couple of cells of clearance means roughly ``chars // 8`` fit. When
the bars outnumber that, keep an even spread of them — first and last always
included, so both ends of the axis stay annotated — rather than crowding every
bar with a label it has no room for.
Args:
n: How many bars the chart draws.
chars: The chart's width in character cells.
Returns:
The bar indices to place ticks under, oldest first.
"""
fit = max(2, chars // 8)
if n <= fit:
return list(range(n))
return sorted({round(i * (n - 1) / (fit - 1)) for i in range(fit)})
def _day_ticks(shown: list, chars: int) -> list[tuple[int, str]]:
"""``(cell, label)`` axis ticks under the day bars: short dates, thinned to fit.
One tick per day where they all fit, else an evenly spaced subset keeping both
ends (see :func:`_thinned_picks`); each label sits under its own bar (see
:func:`_day_centers`). Dates are kept compact — the month is shown only on the
first tick and whenever it rolls over, so most ticks read as a bare day number
— and the newest bar reads ``today`` when it is, mirroring the node page's
closing ``now``.
Args:
shown: The charted days, oldest first, each ``(iso_date, ...)``.
@@ -391,36 +410,59 @@ def _day_ticks(shown: list, chars: int) -> list[tuple[int, str]]:
n = len(shown)
centers = _day_centers(n, chars)
today = utcnow().strftime("%Y-%m-%d")
def label_for(i: int) -> str:
ticks: list[tuple[int, str]] = []
prev_month: Optional[str] = None
for i in _thinned_picks(n, chars):
iso = shown[i][0]
try:
day = datetime.strptime(iso, "%Y-%m-%d")
except ValueError:
return iso
ticks.append((centers[i], iso))
continue
month = day.strftime("%b")
if i == n - 1 and iso == today:
return "today"
if i == 0 or day.day == 1:
return f"{day:%b} {day.day}"
return str(day.day)
label = "today"
elif month != prev_month:
label = f"{month} {day.day}"
else:
label = str(day.day)
prev_month = month
ticks.append((centers[i], label))
return ticks
# Claimed [start, end) label spans — the same centring and spacing arithmetic
# _tick_axis renders with, so everything placed here survives its backstop.
taken: list[tuple[int, int]] = []
def claim(i: int) -> Optional[tuple[int, str]]:
label = label_for(i)
start = max(0, min(chars - len(label), centers[i] - len(label) // 2))
end = start + len(label)
if end > chars or any(
start < e + _TICK_GAP and s < end + _TICK_GAP for s, e in taken
):
return None
taken.append((start, end))
return centers[i], label
def _hour_ticks(shown: list, chars: int) -> list[tuple[int, str]]:
"""``(cell, label)`` axis ticks under the hourly bars: local clock times, thinned to fit.
order = [n - 1, *([0] if n > 1 else []), *range(n - 2, 0, -1)]
return sorted(tick for i in order if (tick := claim(i)) is not None)
The hour-resolution sibling of :func:`_day_ticks` for the 24 h window: one tick
per hour where they fit, else an evenly spaced subset keeping both ends (see
:func:`_thinned_picks`), each label centred under its own bar (see
:func:`_day_centers`). Labels read as local ``HH:00`` clock times — the UTC hour
buckets rotated into the viewer's zone — and the newest bar reads ``now``,
mirroring the node page's closing ``now``.
Args:
shown: The charted hours, oldest first, each ``(hour_iso, ...)`` in UTC
(``YYYY-MM-DDTHH``).
chars: The chart's width in character cells.
Returns:
The ticks to pass to :func:`~meshterm.ui.braillechart.axis_chart`.
"""
n = len(shown)
centers = _day_centers(n, chars)
ticks: list[tuple[int, str]] = []
for i in _thinned_picks(n, chars):
if i == n - 1:
ticks.append((centers[i], "now"))
continue
try:
hour = datetime.strptime(shown[i][0], "%Y-%m-%dT%H").replace(tzinfo=timezone.utc)
except ValueError:
ticks.append((centers[i], shown[i][0]))
continue
ticks.append((centers[i], f"{hour.astimezone():%H:00}"))
return ticks
def _fill_days(
@@ -460,6 +502,48 @@ def _fill_days(
return out
def _fill_hours(
active: list[tuple[str, int, int]], since: datetime, now: datetime
) -> list[tuple[str, int, int]]:
"""Fill an hour series' gaps so a quiet hour shows as an empty bar, not a skip.
The hour-resolution sibling of :func:`_fill_days`, feeding the 24 h window's
charts: :meth:`~meshterm.persistence.repository.Repository.hourly_series` returns
only hours with traffic, so a silent hour would fuse its busy neighbours. This
walks every UTC clock hour of the window and emits ``(iso, 0, 0)`` for the quiet
ones, so the x-axis is real clock time. The range runs from the window's floor
(but never earlier than the first hour recorded — we don't invent emptiness from
before monitoring began) through the current hour.
Args:
active: ``(hour_iso, packets, nodes)`` for hours with activity, oldest first,
each ``hour_iso`` a UTC ``YYYY-MM-DDTHH``.
since: The window's start.
now: The current time (the series ends on its clock hour).
Returns:
``(hour_iso, packets, nodes)`` for every UTC clock hour in range, oldest first.
"""
if not active:
return []
def floor_hour(when: datetime) -> datetime:
return when.astimezone(timezone.utc).replace(minute=0, second=0, microsecond=0)
by_iso = {iso: (packets, nodes) for iso, packets, nodes in active}
first = datetime.strptime(active[0][0], "%Y-%m-%dT%H").replace(tzinfo=timezone.utc)
start = max(first, floor_hour(since))
end = max(start, floor_hour(now))
out: list[tuple[str, int, int]] = []
hour = start
while hour <= end:
iso = hour.strftime("%Y-%m-%dT%H")
packets, nodes = by_iso.get(iso, (0, 0))
out.append((iso, packets, nodes))
hour += timedelta(hours=1)
return out
def _day_columns(values: list[int], chars: int) -> list:
"""Stretch per-day counts into day-wide bars that fill the chart's width exactly.
@@ -511,8 +595,15 @@ def _mesh_sections(
"""
now = utcnow()
since = now - window if window is not None else None
days = _fill_days(ctx.repo.daily_activity(), since, now)
if not days:
# The 24 h window charts an hour per column (its name is "24 h", not "1 day");
# every wider window keeps the calendar-day columns. Both feed the same bar and
# tick machinery — only the bucket resolution and the axis labels differ.
hourly = since is not None and window is not None and window <= timedelta(days=1)
if hourly:
series = _fill_hours(ctx.repo.hourly_series(since), since, now)
else:
series = _fill_days(ctx.repo.daily_activity(), since, now)
if not series:
return [
Text(),
Text("Nothing recorded in this window.", style="muted"),
@@ -532,30 +623,34 @@ def _mesh_sections(
# slice) and shared by every chart, so all their gutters — and thus their left edges —
# line up. The rhythm keeps its own finer width; only the gutter is common.
label_w = max(
len(str(max(d[1] for d in days))),
len(str(max(d[2] for d in days))),
len(str(max(d[1] for d in series))),
len(str(max(d[2] for d in series))),
len(str(max(slots))),
)
chars = max(20, width - 2 * (label_w + 2))
shown = days[-chars * 2 :]
shown = series[-chars * 2 :]
out: list[RenderableType] = []
packets = [d[1] for d in shown]
day_ticks = _day_ticks(shown, chars)
out.append(_heading("Packets per day", "UTC days"))
ticks = _hour_ticks(shown, chars) if hourly else _day_ticks(shown, chars)
pkt_title, node_title = (
("Packets per hour", "Nodes per hour") if hourly
else ("Packets per day", "Nodes per day")
)
out.append(_heading(pkt_title, "local hours" if hourly else "UTC days"))
out.extend(
axis_chart(
timeline_rows(_day_columns(packets, chars), rows=_CHART_ROWS),
max(packets), chars, label_w=label_w, ticks=day_ticks,
max(packets), chars, label_w=label_w, ticks=ticks,
)
)
nodes = [d[2] for d in shown]
out.append(Text())
out.append(_heading("Nodes per day", "distinct nodes heard"))
out.append(_heading(node_title, "distinct nodes heard"))
out.extend(
axis_chart(
timeline_rows(_day_columns(nodes, chars), rows=_CHART_ROWS),
max(nodes), chars, label_w=label_w, ticks=day_ticks,
max(nodes), chars, label_w=label_w, ticks=ticks,
)
)
+70 -32
View File
@@ -19,6 +19,7 @@ from meshterm.ui.timemachine_screen import (
_day_centers,
_day_columns,
_day_ticks,
_hour_ticks,
_mesh_sections,
_node_sections,
_snr_cell_style,
@@ -127,6 +128,28 @@ def test_hourly_activity_groups_by_utc_hour(tmp_path: Path) -> None:
repo.close()
def test_hourly_series_groups_by_clock_hour(tmp_path: Path) -> None:
"""The 24 h feed folds observations into UTC clock-hour buckets: packets and nodes."""
repo = Repository(tmp_path / "hs.db")
run = repo.start_run("monitor", {}, None)
now = utcnow()
base = now.replace(minute=0, second=0, microsecond=0) # top of the current UTC hour
three_h = base - timedelta(hours=3) # top of a busy hour, three back
for node in ("aa" * 6, "bb" * 6):
repo.record_observation(run, Observation(node=node, observed_at=three_h + timedelta(minutes=5)))
repo.record_observation(run, Observation(node="aa" * 6, observed_at=three_h + timedelta(minutes=20)))
repo.record_observation(
run, Observation(node="cc" * 6, kind="packet", path="", observed_at=three_h + timedelta(minutes=40))
)
repo.record_observation(run, Observation(node="aa" * 6, observed_at=base + timedelta(minutes=1)))
series = repo.hourly_series(now - timedelta(days=1))
by_hour = {iso: (pkts, nodes) for iso, pkts, nodes in series}
assert by_hour[three_h.strftime("%Y-%m-%dT%H")] == (4, 2) # 3 adverts + a packet row; 2 nodes
assert by_hour[base.strftime("%Y-%m-%dT%H")] == (1, 1) # just this hour's lone advert
assert series == sorted(series) # oldest first
repo.close()
# --- the math ---------------------------------------------------------------------------
@@ -200,6 +223,22 @@ def test_fill_days_never_invents_days_before_recording_began() -> None:
assert filled[-1][0] == "2026-07-12" # …but still runs through today
def test_fill_hours_shows_gap_hours_as_zero_bars() -> None:
"""A quiet hour between busy ones is emitted with zero counts, not skipped."""
from datetime import datetime, timezone
from meshterm.ui.timemachine_screen import _fill_hours
active = [("2026-07-12T08", 30, 3), ("2026-07-12T11", 20, 2)]
since = datetime(2026, 7, 12, 6, 30, tzinfo=timezone.utc) # floors to 06:00…
now = datetime(2026, 7, 12, 12, 15, tzinfo=timezone.utc) # …but the first hour recorded wins
filled = _fill_hours(active, since, now)
assert [iso for iso, _p, _n in filled] == [f"2026-07-12T{h:02d}" for h in range(8, 13)]
counts = {iso: pkts for iso, pkts, _n in filled}
assert counts["2026-07-12T09"] == 0 and counts["2026-07-12T10"] == 0 # the gap, now visible
assert counts["2026-07-12T08"] == 30 and counts["2026-07-12T11"] == 20 # busy hours intact
def test_day_centers_land_under_each_bar() -> None:
"""A day's centre cell sits within its own bar's span (so a tick points at it)."""
chars = 60
@@ -230,20 +269,30 @@ def test_day_ticks_reprint_the_month_on_a_rollover() -> None:
assert labels == ["Jun 29", "30", "Jul 1", "2"]
def test_day_ticks_prioritise_the_newest_then_the_oldest_day() -> None:
"""With room for almost nothing, the newest day wins, then the oldest.
A chart squeezed to the minimum width still labels its "now" edge first and
its far edge second; whatever interior days fit are claimed right to left, so
the freshest history stays the best annotated.
"""
def test_day_ticks_thin_to_fit_keeping_both_ends() -> None:
"""More days than labels fit: an evenly spaced subset is kept, first and last included."""
days = [(f"2026-06-{d:02d}", 1, 1) for d in range(1, 31)] # 30 days
chars = 20
chars = 40 # ~5 dated labels fit (chars // 8)
ticks = _day_ticks(days, chars)
centers = _day_centers(len(days), chars)
assert 2 <= len(ticks) <= 6
cols = [cell for cell, _ in ticks]
assert centers[-1] in cols # the newest day always keeps its tick
assert centers[0] in cols # …and the oldest is claimed right after it
assert cols[0] == centers[0] and cols[-1] == centers[-1] # both ends survive the thinning
def test_hour_ticks_close_on_now_and_read_clock_times() -> None:
"""Hourly ticks label local HH:00 clock times, thinned to fit, the newest reading 'now'."""
import re as _re
hours = [(f"2026-07-12T{h:02d}", 1, 1) for h in range(24)] # a full 24 hours
chars = 60
ticks = _hour_ticks(hours, chars)
centers = _day_centers(len(hours), chars)
assert ticks[-1] == (centers[-1], "now") # the newest bar closes on 'now'
others = [label for _, label in ticks[:-1]]
assert others and all(_re.fullmatch(r"\d{2}:00", label) for label in others) # HH:00 times
cols = [cell for cell, _ in ticks]
assert cols == sorted(cols) and all(cell in centers for cell in cols) # aligned under bars
def test_day_columns_widths_differ_by_at_most_one_dot_and_interleave() -> None:
@@ -267,28 +316,6 @@ def test_day_columns_widths_differ_by_at_most_one_dot_and_interleave() -> None:
assert any(w == 3 for w in widths[first_wide:last_wide]) # narrow days sit between wide ones
def test_day_ticks_pack_the_axis_keeping_both_ends() -> None:
"""More days than labels fit: the axis packs what it can, ends always included.
Ticks are claimed newest-first, then oldest, then right to left, each keeping
two blank cells from its neighbours — so a crowded axis stays dense (well past
the old evenly-thinned handful) and any dropped days come from the interior.
"""
days = [(f"2026-06-{d:02d}", 1, 1) for d in range(1, 31)] # 30 days
chars = 40
ticks = _day_ticks(days, chars)
centers = _day_centers(len(days), chars)
cols = [cell for cell, _ in ticks]
assert cols[0] == centers[0] and cols[-1] == centers[-1] # both ends survive
assert len(ticks) >= 8 # bare day numbers pack far denser than dated labels
# Every placed label keeps at least two blank cells from the one before it.
spans = []
for cell, label in ticks:
start = max(0, min(chars - len(label), cell - len(label) // 2))
spans.append((start, start + len(label)))
assert all(b_start >= a_end + 2 for (_, a_end), (b_start, _) in zip(spans, spans[1:]))
def test_day_columns_notch_the_boundaries_and_keep_the_first_bar_flush() -> None:
"""Notches sit strictly between days; the first bar starts at the axis itself.
@@ -459,6 +486,17 @@ def test_mesh_page_day_axis_ticks_sit_under_dated_columns(tmp_path: Path) -> Non
repo.close()
def test_mesh_page_24h_window_charts_hours_not_days(tmp_path: Path) -> None:
"""The 24 h window buckets by the hour — its name is '24 h', not '1 day'."""
repo = _seeded_repo(tmp_path)
ctx = SimpleNamespace(repo=repo)
body = _plain(_mesh_sections(ctx, timedelta(days=1), 90))
assert "Packets per hour" in body and "Nodes per hour" in body
assert "local hours" in body
assert "per day" not in body # no day columns at this window (rhythm reads "time of day")
repo.close()
def test_mesh_page_rhythm_left_edge_aligns_with_the_day_charts(tmp_path: Path) -> None:
"""The rhythm shares the day charts' y-axis gutter, so all three left edges line up.