The Clock drift page puts managed nodes first, with column headers
Managed nodes are now the first section and are always listed: a node with no sample in the window shows its newest offset from all history, or "no sample". Each table has a header line under its section heading (NAME / OFFSET / LAST, and OFFSET / # over the histogram, now under "All clocks"). Older offsets do not count in the histogram or title. The CLI lists each managed node too, with null fields when it has none. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bdcdadc50c
commit
d807109093
6 files changed
+216
-73
No files matched your search
+8
-3
@@ -1079,14 +1079,19 @@ by default). `LAST` is the age of the newest sample. `MANAGED` is `yes` when Mes
|
||||
stores the admin password of the node. A channel sender whose name matches no single key
|
||||
has only its name, with no `TYPE` and no `HASH`.
|
||||
|
||||
Each managed node is a row, also when it sent no sample in the window. Its offset is then
|
||||
the newest from all the history, and `LAST` shows how old that sample is. A managed node
|
||||
that never sent a sample is last, with `no sample` and a `LAST` of `never`. A repeater
|
||||
sends no message, so its adverts are its only samples.
|
||||
|
||||
**`--json`** is an array of rows.
|
||||
|
||||
| Field | Type | Meaning |
|
||||
| --- | --- | --- |
|
||||
| `node` | object | The `node` shape. A sender with no key has only `name`. |
|
||||
| `offset_s` | integer | The offset in seconds. A positive value is a clock ahead of this computer. |
|
||||
| `samples` | integer | The number of samples in the window. |
|
||||
| `last_sample_at` | timestamp | When MeshTerm heard the newest sample. |
|
||||
| `offset_s` | integer \| null | The offset in seconds. A positive value is a clock ahead of this computer. `null` for a managed node that never sent a sample. |
|
||||
| `samples` | integer | The number of samples. `0` for a managed node that never sent one. |
|
||||
| `last_sample_at` | timestamp \| null | When MeshTerm heard the newest sample. `null` is the `never` of the plain face. |
|
||||
| `out_of_sync` | boolean | **JSON only.** `true` when the offset is past `clock_tolerance_s`. It is the `in sync` of the plain face, inverted. |
|
||||
| `managed` | boolean | `true` when MeshTerm stores the admin password of the node. |
|
||||
| `keyed` | boolean | **JSON only.** `false` for a channel sender that has only a name. |
|
||||
|
||||
@@ -28,7 +28,7 @@ This page lists each interactive screen with its scripted equivalent, where one
|
||||
| **📊 Dashboard** | The live overview of the mesh. It has a two-hour chart of the activity of all packets with a pulse line, the traffic counts of the session for each packet class, and window RF health next to the live numbers of the radio. The screen updates each second. | none |
|
||||
| **📰 Live feed** | Each packet as it arrives, newest first. A row shows the time, the type of the packet (its parsed payload class), and what the packet is about. For an advert, that is the node. For a channel text, it is the channel. For a direct message or a request, it is sender → recipient. For a trace, it is the tag. The row also shows the reception quality and, for a scoped flood, the region of the flood. Press Enter to open a row in the packet viewer, with its route graph. | none |
|
||||
| **🚨 Watchtower** | A passive watcher of the nodes that you star. It gives a silence alarm when a watched node goes quiet. It warns you when the SNR of a node gets worse, and it tells you when the node returns. It also tells you when a node is heard for the first time. It runs in the background during the whole session. Alerts that you did not acknowledge show as a badge in the header. | none |
|
||||
| **🕒 Clock drift** | How far the clock of each node is from yours. A node with a wrong clock puts wrong times on what it sends, and only its owner can set it. The page has a histogram of the offsets, from the clocks behind yours, through the clocks in sync, to the clocks ahead. Then it lists your managed nodes (the nodes whose admin password MeshTerm stores), and each other node whose clock is out of sync, the largest offset first. Press Enter to open the node page. In a chat, a message from a wrong clock shows the time when it arrived, with a `?` mark. | `meshterm clocks` |
|
||||
| **🕒 Clock drift** | How far the clock of each node is from yours. A node with a wrong clock puts wrong times on what it sends, and only its owner can set it. The page starts with your managed nodes (the nodes whose admin password MeshTerm stores), each one always, because you can set these clocks yourself. Then a histogram of all the offsets, from the clocks behind yours, through the clocks in sync, to the clocks ahead. Then each other node whose clock is out of sync, the largest offset first. Press Enter to open the node page. In a chat, a message from a wrong clock shows the time when it arrived, with a `?` mark. | `meshterm clocks` |
|
||||
| **⏳ Time machine** | Everything that the recorder ever heard, as braille charts: reception volume, the median-SNR band, the rhythm of the hours of the day, packets and nodes for each day, and first-ever arrivals. You can switch the time window between 24h, 7d, 30d, and all time. On the PicoCalc, the windows stop at 30d. You can look at one node or at the whole mesh. | none |
|
||||
| **🎧 Monitor** | The app always records passively. On the command line, this command captures a window of a fixed length in the foreground. It shows each overheard packet as it arrives, and it gives a summary at the end. It transmits nothing. It only listens. | `meshterm monitor --seconds 60` |
|
||||
|
||||
|
||||
+44
-15
@@ -54,8 +54,8 @@ class ClocksTool(Tool):
|
||||
|
||||
tolerance = ctx.preferences.clock_tolerance_s
|
||||
rows = await clock_rows(ctx)
|
||||
off = sum(1 for r in rows if r.offset.out_of_sync(tolerance))
|
||||
summary = {"senders": len(rows), "out_of_sync": off}
|
||||
off = sum(1 for r in rows if r.current and r.out_of_sync(tolerance))
|
||||
summary = {"senders": sum(1 for r in rows if r.current), "out_of_sync": off}
|
||||
if isinstance(ctx.ui, TuiUi):
|
||||
await _open_page(ctx, rows, tolerance)
|
||||
return ToolResult(summary=summary)
|
||||
@@ -86,19 +86,23 @@ class ClocksTool(Tool):
|
||||
|
||||
|
||||
async def clock_rows(ctx: AppContext) -> list[ClockRow]:
|
||||
"""One row for each sender with a clock sample in the window of ``clock_report_days``.
|
||||
"""One row for each sender with a clock sample in the window, and for each managed node.
|
||||
|
||||
A keyed row gets the contact of the device when there is one, and else a contact made
|
||||
from the stored history, so that Enter can open its node page. Our own node is not a
|
||||
row: MeshTerm sets the clock of the companion itself when it connects.
|
||||
|
||||
Each managed node is a row, also with no sample in the window (JP, 2026-11-03). Its
|
||||
offset is then the newest from all the history, which is not *current*, or ``None``
|
||||
when the node never sent a sample.
|
||||
|
||||
Args:
|
||||
ctx: The shared application context.
|
||||
|
||||
Returns:
|
||||
The rows, in no specific order.
|
||||
"""
|
||||
from ..services.clock_offsets import offsets, window_samples
|
||||
from ..services.clock_offsets import node_offset, offsets, window_samples
|
||||
from ..services.trace_runner import make_name_key_resolver
|
||||
from ..ui.clocks_screen import ClockRow
|
||||
|
||||
@@ -145,6 +149,21 @@ async def clock_rows(ctx: AppContext) -> list[ClockRow]:
|
||||
key = contact.public_key or contact.key_prefix
|
||||
managed = admin is not None and admin.get(contact) is not None
|
||||
rows.append(ClockRow(offset, name, key, contact.node_type, managed, contact))
|
||||
|
||||
# The managed nodes that sent no sample in the window: the newest offset from all the
|
||||
# history, or none.
|
||||
listed = {r.offset.who for r in rows if r.offset is not None and r.offset.keyed}
|
||||
for contact in contacts if admin is not None else ():
|
||||
node_id = (contact.public_key or contact.key_prefix or "").lower().removeprefix("0x")[:12]
|
||||
if not node_id or node_id in listed or node_id == self_id:
|
||||
continue
|
||||
if admin.get(contact) is None:
|
||||
continue
|
||||
older = node_offset(ctx.repo, node_id, name=contact.name, key_of=key_of)
|
||||
key = contact.public_key or contact.key_prefix
|
||||
rows.append(
|
||||
ClockRow(older, contact.name, key, contact.node_type, True, contact, current=False)
|
||||
)
|
||||
return rows
|
||||
|
||||
|
||||
@@ -173,7 +192,9 @@ def _listing(rows: list[ClockRow], tolerance: float, prefix_bytes: int = 0) -> L
|
||||
|
||||
``OFFSET`` is the offset in words (``3h 12m behind``, or ``in sync`` within the
|
||||
tolerance). The document gives ``offset_s``, signed, a positive value for a clock
|
||||
ahead of ours. A sender with no key has only its name, and ``keyed`` is false.
|
||||
ahead of ours. A sender with no key has only its name, and ``keyed`` is false. A managed
|
||||
node that never sent a sample is a row too, last: ``no sample``, and ``null`` in the
|
||||
document.
|
||||
|
||||
Args:
|
||||
rows: The senders.
|
||||
@@ -185,27 +206,35 @@ def _listing(rows: list[ClockRow], tolerance: float, prefix_bytes: int = 0) -> L
|
||||
from ..ui.report import Column, Lane, Listing
|
||||
from ..ui.widgets import format_offset
|
||||
|
||||
def words(seconds: float) -> str:
|
||||
def words(seconds: float | None) -> str:
|
||||
if seconds is None:
|
||||
return "no sample"
|
||||
return "in sync" if abs(seconds) <= tolerance else format_offset(seconds)
|
||||
|
||||
def order(row: ClockRow) -> tuple:
|
||||
return (row.offset is None, -abs(row.offset.offset_s) if row.offset else 0, row.label)
|
||||
|
||||
table = []
|
||||
for row in sorted(rows, key=lambda r: -abs(r.offset.offset_s)):
|
||||
for row in sorted(rows, key=order):
|
||||
offset = row.offset
|
||||
keyed = offset is None or offset.keyed
|
||||
key = (row.contact.public_key or "").lower() if row.contact else ""
|
||||
known = key or (row.offset.who if row.offset.keyed else "")
|
||||
known = key or (offset.who if offset is not None and keyed else "")
|
||||
known = known or ((row.contact.key_prefix or "").lower() if row.contact else "")
|
||||
table.append(
|
||||
{
|
||||
"node": fields.NodeRef(
|
||||
name=row.name or (None if row.offset.keyed else row.offset.who),
|
||||
name=row.name or (None if keyed else row.label),
|
||||
key=key or None,
|
||||
hash=known[: prefix_bytes * 2] or None,
|
||||
type=NODE_TYPE_LABELS.get(row.node_type),
|
||||
),
|
||||
"offset_s": round(row.offset.offset_s),
|
||||
"samples": row.offset.samples,
|
||||
"last_sample_at": row.offset.last_at,
|
||||
"out_of_sync": row.offset.out_of_sync(tolerance),
|
||||
"offset_s": round(offset.offset_s) if offset is not None else None,
|
||||
"samples": offset.samples if offset is not None else 0,
|
||||
"last_sample_at": offset.last_at if offset is not None else None,
|
||||
"out_of_sync": row.out_of_sync(tolerance),
|
||||
"managed": row.managed,
|
||||
"keyed": row.offset.keyed,
|
||||
"keyed": keyed,
|
||||
}
|
||||
)
|
||||
return Listing(
|
||||
@@ -218,7 +247,7 @@ def _listing(rows: list[ClockRow], tolerance: float, prefix_bytes: int = 0) -> L
|
||||
json=lambda v: v,
|
||||
),
|
||||
fields.integer("samples", "SAMPLES"),
|
||||
fields.when("last_sample_at", "LAST"),
|
||||
fields.when("last_sample_at", "LAST", absent="never"),
|
||||
fields.hidden("out_of_sync"),
|
||||
fields.flag("managed", "MANAGED"),
|
||||
fields.hidden("keyed"),
|
||||
|
||||
+125
-52
@@ -2,23 +2,27 @@
|
||||
"""The Clock drift page: how far the clock of each node is from ours.
|
||||
|
||||
The page reads the clock samples of the last ``clock_report_days`` (refer to
|
||||
:mod:`~meshterm.services.clock_offsets`) and shows them in three parts:
|
||||
:mod:`~meshterm.services.clock_offsets`) and shows them in four sections:
|
||||
|
||||
* **A histogram** of the offsets, in log steps on two sides: the clocks behind ours at the
|
||||
top, the clocks in sync in the centre, and the clocks ahead at the bottom (JP,
|
||||
2026-11-02). A clock can be wrong by seconds or by years, so one linear axis cannot
|
||||
show the two. The centre row is as wide as the ``clock_tolerance_s`` preference. Each bar
|
||||
is a :func:`~meshterm.ui.braillechart.meter`, the only bar of the app.
|
||||
* **The nodes**, in two sections. *Managed nodes* (the nodes whose admin password
|
||||
MeshTerm stores) are first, each one also when its clock is correct, because these are
|
||||
the clocks that the user can set. Then *Out of sync*: each other node past the
|
||||
tolerance, the largest offset first. Enter opens the node page.
|
||||
* **The senders with no key**: the channel senders whose name resolves to no single key.
|
||||
Their samples count in the histogram, and their names are page text under their own
|
||||
heading, in the grey of an unknown node. There is no node page to open for a name.
|
||||
* **Managed nodes** (the nodes whose admin password MeshTerm stores) come first, each one
|
||||
always, also when its clock is correct, because these are the clocks that the user can
|
||||
set (JP, 2026-11-03). A managed node with no sample in the window shows its newest offset
|
||||
from all the history, or ``no sample``. A repeater sends no message, so its adverts are
|
||||
its only samples, and a new install has none yet.
|
||||
* **All clocks**: a histogram of the offsets, in log steps on two sides. The clocks behind
|
||||
ours are at the top, the clocks in sync in the centre, and the clocks ahead at the
|
||||
bottom (JP, 2026-11-02). A clock can be wrong by seconds or by years, so one linear axis
|
||||
cannot show the two. The centre row is as wide as the ``clock_tolerance_s`` preference.
|
||||
Each bar is a :func:`~meshterm.ui.braillechart.meter`, the only bar of the app.
|
||||
* **Out of sync**: each other node past the tolerance, the largest offset first. Enter
|
||||
opens the node page.
|
||||
* **Senders with no key**: the channel senders whose name resolves to no single key. Their
|
||||
samples count in the histogram, and their names are page text in the grey of an unknown
|
||||
node. There is no node page to open for a name.
|
||||
|
||||
When most clocks are off in the same direction, the fault is probably ours. A note at the
|
||||
top then says that the clock of this computer can be wrong.
|
||||
Each section has a header line under its heading, which pins with the heading while the
|
||||
section scrolls. When most clocks are off in the same direction, the fault is probably
|
||||
ours. A note at the top then says that the clock of this computer can be wrong.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -45,13 +49,17 @@ if TYPE_CHECKING:
|
||||
#: An edge at or under the tolerance is dropped, so a large tolerance gives fewer steps.
|
||||
_EDGES_S = (3600, 86400, 365 * 86400)
|
||||
|
||||
#: The cells of the label lane of the histogram (``1d–1y behind``).
|
||||
#: The cells of the label lane of the histogram (``1d-1y behind``).
|
||||
_BUCKET_LABEL_W = 14
|
||||
|
||||
#: The cells of the offset lane of a row (``11h 59m behind``) and of its age lane (``52w``).
|
||||
_OFFSET_W = 15
|
||||
_AGE_W = 4
|
||||
|
||||
#: The cells of the pointer column of a row, and of its type mark with its gap.
|
||||
_POINTER_W = 2
|
||||
_MARK_W = 2
|
||||
|
||||
#: The widest that a name lane gets. The filter measures a row at an unbounded width.
|
||||
_NAME_MAX_W = 24
|
||||
|
||||
@@ -70,20 +78,36 @@ class ClockRow:
|
||||
"""One sender on the Clock drift page.
|
||||
|
||||
Attributes:
|
||||
offset: Its clock offset (refer to :class:`~meshterm.services.clock_offsets.ClockOffset`).
|
||||
offset: Its clock offset (refer to :class:`~meshterm.services.clock_offsets.ClockOffset`),
|
||||
or ``None`` for a managed node that never sent a sample.
|
||||
name: The name to show, or ``None`` when nothing names the node.
|
||||
key: The key (or key prefix) that gives the hue of the name, or ``None``.
|
||||
node_type: The advert type of the node, if known.
|
||||
managed: Whether MeshTerm stores the admin password of the node.
|
||||
contact: What Enter opens, or ``None`` for a sender with no key.
|
||||
current: Whether the offset comes from a sample in the window. Only these count in
|
||||
the histogram and in the title. A managed node with no recent sample shows an
|
||||
older offset, which is not current.
|
||||
"""
|
||||
|
||||
offset: ClockOffset
|
||||
offset: ClockOffset | None
|
||||
name: str | None
|
||||
key: str | None
|
||||
node_type: int | None
|
||||
managed: bool
|
||||
contact: Contact | None
|
||||
current: bool = True
|
||||
|
||||
@property
|
||||
def label(self) -> str:
|
||||
"""The name to show, or the id that stands in for it."""
|
||||
if self.name:
|
||||
return self.name
|
||||
return self.offset.who if self.offset is not None else "unknown"
|
||||
|
||||
def out_of_sync(self, tolerance_s: float) -> bool:
|
||||
"""Whether the clock is past the tolerance. A node with no offset is not."""
|
||||
return self.offset is not None and self.offset.out_of_sync(tolerance_s)
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
@@ -91,7 +115,7 @@ class Bucket:
|
||||
"""One step of the histogram.
|
||||
|
||||
Attributes:
|
||||
label: The words of the step (``1h–1d behind``, ``in sync ±5m``).
|
||||
label: The words of the step (``1h-1d behind``, ``in sync ≤5m``).
|
||||
low: The smallest offset in the step, in seconds (``None`` for no limit).
|
||||
high: The largest offset in the step, in seconds (``None`` for no limit).
|
||||
in_sync: Whether this is the centre step.
|
||||
@@ -150,11 +174,17 @@ def buckets(tolerance_s: float) -> list[Bucket]:
|
||||
return [*behind, centre, *ahead]
|
||||
|
||||
|
||||
def _current(rows: Sequence[ClockRow]) -> list[ClockRow]:
|
||||
"""The rows whose offset comes from a sample in the window."""
|
||||
return [r for r in rows if r.current and r.offset is not None]
|
||||
|
||||
|
||||
def histogram(rows: Sequence[ClockRow], tolerance_s: float) -> list[tuple[Bucket, int]]:
|
||||
"""Count the senders in each step of :func:`buckets`."""
|
||||
"""Count the senders with a current offset in each step of :func:`buckets`."""
|
||||
steps = buckets(tolerance_s)
|
||||
counts = [0] * len(steps)
|
||||
for row in rows:
|
||||
for row in _current(rows):
|
||||
assert row.offset is not None
|
||||
for i, step in enumerate(steps):
|
||||
if step.holds(row.offset.offset_s):
|
||||
counts[i] += 1
|
||||
@@ -172,7 +202,7 @@ def own_clock_note(rows: Sequence[ClockRow], tolerance_s: float) -> str | None:
|
||||
Returns:
|
||||
The note, or ``None`` when the clocks do not agree against ours.
|
||||
"""
|
||||
keyed = [r.offset.offset_s for r in rows if r.offset.keyed]
|
||||
keyed = [r.offset.offset_s for r in _current(rows) if r.offset and r.offset.keyed]
|
||||
if len(keyed) < _OWN_CLOCK_MIN_NODES:
|
||||
return None
|
||||
median = statistics.median(keyed)
|
||||
@@ -186,6 +216,31 @@ def own_clock_note(rows: Sequence[ClockRow], tolerance_s: float) -> str | None:
|
||||
return f"⚠ most clocks are {size} {direction} this computer — its own clock may be wrong"
|
||||
|
||||
|
||||
def _name_w(row_width: int) -> int:
|
||||
"""The width of the name lane, for a row of ``row_width`` cells (after the pointer)."""
|
||||
return max(4, min(_NAME_MAX_W, row_width - _MARK_W - 2 - _OFFSET_W - 2 - _AGE_W))
|
||||
|
||||
|
||||
def _node_header(width: int) -> Text:
|
||||
"""The header line of a list of nodes: ``NAME``, ``OFFSET``, and ``LAST``.
|
||||
|
||||
A separator gets the full width, and a row gets it less the pointer column. Thus the
|
||||
header pads the pointer and the type mark itself, and each label is above its lane.
|
||||
"""
|
||||
text = Text(" " * (_POINTER_W + _MARK_W), style="muted", no_wrap=True, overflow="ellipsis")
|
||||
text.append(fit_cells("NAME", _name_w(width - _POINTER_W)))
|
||||
text.append(" " + fit_cells("OFFSET", _OFFSET_W, align="right"))
|
||||
text.append(" " + fit_cells("LAST", _AGE_W, align="right"))
|
||||
return text
|
||||
|
||||
|
||||
def _histogram_header(peak: int) -> Text:
|
||||
"""The header line of the histogram: the step, and ``#`` over the counts."""
|
||||
text = Text(fit_cells("OFFSET", _BUCKET_LABEL_W), style="muted", no_wrap=True)
|
||||
text.append(" " + f"{'#':>{len(str(peak))}}")
|
||||
return text
|
||||
|
||||
|
||||
def _bar_row(step: Bucket, count: int, peak: int, width: int) -> Text:
|
||||
"""One line of the histogram: the label, the count, and the bar.
|
||||
|
||||
@@ -204,80 +259,98 @@ def _bar_row(step: Bucket, count: int, peak: int, width: int) -> Text:
|
||||
return text
|
||||
|
||||
|
||||
def _offset_cells(row: ClockRow, tolerance_s: float, text: Text) -> None:
|
||||
"""Append the offset lane and the age lane of a row to ``text``."""
|
||||
text.append(" ")
|
||||
if row.offset is None:
|
||||
text.append(fit_cells("no sample", _OFFSET_W, align="right"), style="muted")
|
||||
text.append(" " + fit_cells("—", _AGE_W, align="right"), style="faint")
|
||||
return
|
||||
if row.out_of_sync(tolerance_s):
|
||||
words = format_offset(row.offset.offset_s)
|
||||
text.append(fit_cells(words, _OFFSET_W, align="right"), style="warn")
|
||||
else:
|
||||
text.append(fit_cells("in sync", _OFFSET_W, align="right"))
|
||||
age = format_age(age_seconds(row.offset.last_at))
|
||||
text.append(" " + fit_cells(age, _AGE_W, align="right"), style="muted")
|
||||
|
||||
|
||||
def _row_text(row: ClockRow, tolerance_s: float, width: int) -> Text:
|
||||
"""One node: its type mark, its name in its hue, its offset, and the age of its sample."""
|
||||
glyph, glyph_style = NODE_GLYPHS.get(row.node_type, DEFAULT_GLYPH)
|
||||
name_w = max(4, min(_NAME_MAX_W, width - 2 - 2 - _OFFSET_W - 2 - _AGE_W))
|
||||
text = Text(no_wrap=True, overflow="ellipsis")
|
||||
text.append(f"{glyph} ", style=glyph_style)
|
||||
# A node that nothing names shows its id in the grey of an unknown node: a bare hash
|
||||
# that stands in for a name never takes a hue.
|
||||
hue = name_style(row.name, row.key) if row.name else "node.unknown"
|
||||
text.append(fit_cells(row.name or row.offset.who, name_w), style=hue)
|
||||
text.append(" ")
|
||||
if row.offset.out_of_sync(tolerance_s):
|
||||
text.append(fit_cells(format_offset(row.offset.offset_s), _OFFSET_W, align="right"), "warn")
|
||||
else:
|
||||
text.append(fit_cells("in sync", _OFFSET_W, align="right"))
|
||||
text.append(" ")
|
||||
age = format_age(age_seconds(row.offset.last_at))
|
||||
text.append(fit_cells(age, _AGE_W, align="right"), style="muted")
|
||||
text.append(fit_cells(row.label, _name_w(width)), style=hue)
|
||||
_offset_cells(row, tolerance_s, text)
|
||||
return text
|
||||
|
||||
|
||||
def _name_line(row: ClockRow, tolerance_s: float, width: int) -> Text:
|
||||
"""One sender with no key: page text under the row column, in the unknown grey."""
|
||||
name_w = max(4, min(_NAME_MAX_W, width - 4 - 2 - _OFFSET_W - 2 - _AGE_W))
|
||||
text = Text(no_wrap=True, overflow="ellipsis")
|
||||
text.append(" ") # the pointer and the type mark of a row: no node, no mark
|
||||
text.append(fit_cells(row.offset.who, name_w), style="node.unknown")
|
||||
text.append(" ")
|
||||
text.append(fit_cells(format_offset(row.offset.offset_s), _OFFSET_W, align="right"), "warn")
|
||||
text.append(" ")
|
||||
age = format_age(age_seconds(row.offset.last_at))
|
||||
text.append(fit_cells(age, _AGE_W, align="right"), style="muted")
|
||||
text = Text(" " * (_POINTER_W + _MARK_W), no_wrap=True, overflow="ellipsis")
|
||||
text.append(fit_cells(row.label, _name_w(width - _POINTER_W)), style="node.unknown")
|
||||
_offset_cells(row, tolerance_s, text)
|
||||
return text
|
||||
|
||||
|
||||
def _by_size(rows: Sequence[ClockRow]) -> list[ClockRow]:
|
||||
"""The largest offset first."""
|
||||
return sorted(rows, key=lambda r: -abs(r.offset.offset_s))
|
||||
"""The largest offset first, then the nodes with no offset, by name."""
|
||||
return sorted(
|
||||
rows,
|
||||
key=lambda r: (r.offset is None, -abs(r.offset.offset_s) if r.offset else 0, r.label),
|
||||
)
|
||||
|
||||
|
||||
def clock_drift_screen(rows: Sequence[ClockRow], *, tolerance_s: float) -> SelectScreen:
|
||||
"""Build the Clock drift page from its rows.
|
||||
|
||||
Args:
|
||||
rows: Each sender with a clock sample in the window, keyed or not.
|
||||
rows: Each sender with a clock sample in the window, keyed or not, and each managed
|
||||
node.
|
||||
tolerance_s: The ``clock_tolerance_s`` preference.
|
||||
|
||||
Returns:
|
||||
The screen. It resolves with the :class:`~meshterm.core.models.Contact` of the
|
||||
chosen row.
|
||||
"""
|
||||
off = [r for r in rows if r.offset.out_of_sync(tolerance_s)]
|
||||
title = f"Clock drift · {len(rows)} nodes · {len(off)} out of sync"
|
||||
current = _current(rows)
|
||||
off = [r for r in current if r.out_of_sync(tolerance_s)]
|
||||
title = f"Clock drift · {len(current)} nodes · {len(off)} out of sync"
|
||||
items: list = []
|
||||
note = own_clock_note(rows, tolerance_s)
|
||||
if note is not None:
|
||||
items.append(Separator(note, style="warn"))
|
||||
|
||||
def node_rows(group: Sequence[ClockRow]) -> list:
|
||||
return [Choice(lambda w, r=r: _row_text(r, tolerance_s, w), r.contact) for r in group]
|
||||
|
||||
managed = _by_size([r for r in rows if r.managed and r.contact is not None])
|
||||
if managed:
|
||||
items.append(section_heading("Managed nodes"))
|
||||
items.append(Separator(_node_header))
|
||||
items += node_rows(managed)
|
||||
|
||||
counted = histogram(rows, tolerance_s)
|
||||
peak = max((count for _step, count in counted), default=0) or 1
|
||||
items.append(section_heading("All clocks"))
|
||||
items.append(Separator(lambda _w: _histogram_header(peak)))
|
||||
for step, count in counted:
|
||||
items.append(Separator(lambda w, s=step, c=count: _bar_row(s, c, peak, w)))
|
||||
|
||||
managed = _by_size([r for r in rows if r.managed and r.contact is not None])
|
||||
others = _by_size([r for r in off if not r.managed and r.contact is not None])
|
||||
nameless = _by_size([r for r in off if not r.offset.keyed])
|
||||
if managed:
|
||||
items.append(section_heading("Managed nodes"))
|
||||
items += [Choice(lambda w, r=r: _row_text(r, tolerance_s, w), r.contact) for r in managed]
|
||||
items.append(section_heading("Out of sync"))
|
||||
if others:
|
||||
items += [Choice(lambda w, r=r: _row_text(r, tolerance_s, w), r.contact) for r in others]
|
||||
items.append(Separator(_node_header))
|
||||
items += node_rows(others)
|
||||
else:
|
||||
items.append(Separator("no other clock is out of sync", style="muted"))
|
||||
|
||||
nameless = _by_size([r for r in off if r.offset is not None and not r.offset.keyed])
|
||||
if nameless:
|
||||
items.append(section_heading("Senders with no key"))
|
||||
items.append(Separator(_node_header))
|
||||
items += [Separator(lambda w, r=r: _name_line(r, tolerance_s, w)) for r in nameless]
|
||||
return SelectScreen(title, items, footer_hint=_HINT)
|
||||
+35
-2
@@ -92,6 +92,32 @@ def test_the_page_lists_managed_nodes_first_then_the_clocks_out_of_sync() -> Non
|
||||
assert "15m behind" in text
|
||||
|
||||
|
||||
def test_managed_nodes_come_first_with_a_header_and_no_sample_rows() -> None:
|
||||
"""The managed section is the first one, and a managed node with no sample is listed.
|
||||
|
||||
Each table has a header line under its heading. An older offset of a managed node does
|
||||
not count in the histogram or the title, because it is not from the window.
|
||||
"""
|
||||
from meshterm.services.clock_offsets import ClockOffset
|
||||
|
||||
old = ClockOffset("ab" * 6, True, -7200, 2, utcnow() - timedelta(days=30))
|
||||
stale = ClockRow(
|
||||
old, "Old", "ab" * 32, None, True, Contact(name="Old", public_key="ab" * 32), current=False
|
||||
)
|
||||
never = ClockRow(None, "New", "cd" * 32, None, True, Contact(name="New", public_key="cd" * 32))
|
||||
screen = clock_drift_screen([stale, never, _row("Slow", -3 * 3600)], tolerance_s=TOLERANCE)
|
||||
assert screen.title == "Clock drift · 1 nodes · 1 out of sync"
|
||||
lines = plain("\n".join(screen.render_body(72))).splitlines()
|
||||
assert lines[0].strip() == "── Managed nodes ──"
|
||||
assert lines[1].split() == ["NAME", "OFFSET", "LAST"]
|
||||
assert any("New" in line and "no sample" in line for line in lines)
|
||||
assert any("Old" in line and "2h behind" in line and "4w" in line for line in lines)
|
||||
text = "\n".join(lines)
|
||||
assert text.index("Managed nodes") < text.index("All clocks") < text.index("Out of sync")
|
||||
counted = dict((b.label, n) for b, n in histogram([stale, never], TOLERANCE))
|
||||
assert sum(counted.values()) == 0
|
||||
|
||||
|
||||
def test_a_page_with_nothing_out_of_sync_says_so() -> None:
|
||||
"""The Out of sync section always has its heading, and an empty state when it is empty."""
|
||||
screen = clock_drift_screen([_row("Fine", 20)], tolerance_s=TOLERANCE)
|
||||
@@ -145,12 +171,19 @@ def test_clock_rows_name_their_nodes_and_mark_the_managed_ones(tmp_path: Path) -
|
||||
)
|
||||
)
|
||||
)
|
||||
quiet = Contact(name="Quiet", public_key="ef" * 32, node_type=NODE_TYPE_REPEATER)
|
||||
admin = AdminStore(tmp_path / "admin.json")
|
||||
admin.remember(repeater, "secret")
|
||||
rows = asyncio.run(clock_rows(_Ctx(repo, [repeater], admin)))
|
||||
by_name = {r.name or r.offset.who: r for r in rows}
|
||||
admin.remember(quiet, "secret")
|
||||
rows = asyncio.run(clock_rows(_Ctx(repo, [repeater, quiet], admin)))
|
||||
by_name = {r.label: r for r in rows}
|
||||
assert by_name["Hilltop"].managed and by_name["Hilltop"].contact is repeater
|
||||
stranger = by_name["cd" * 6]
|
||||
assert not stranger.managed and stranger.contact is not None # a page to open
|
||||
assert stranger.contact.key_prefix == "cd" * 6
|
||||
# A managed node that never sent a sample still has its row, with no offset.
|
||||
assert by_name["Quiet"].managed and by_name["Quiet"].offset is None
|
||||
assert not by_name["Quiet"].current
|
||||
listing = _listing(rows, TOLERANCE)
|
||||
assert listing.rows[-1]["offset_s"] is None and listing.rows[-1]["managed"]
|
||||
repo.close()
|
||||
@@ -1793,6 +1793,9 @@ def _clock_drift(cols: int, rows: int) -> Screen:
|
||||
row("Raphael's T-Deck", "34" * 32, 2 * 3600),
|
||||
row("Bob", "", -900, keyed=False),
|
||||
]
|
||||
# A managed node that never sent a sample: its row says so, in the same lanes.
|
||||
quiet = Contact(name="New Repeater", public_key="56" * 32, node_type=NODE_TYPE_REPEATER)
|
||||
rows_.append(ClockRow(None, quiet.name, quiet.public_key, NODE_TYPE_REPEATER, True, quiet))
|
||||
return clock_drift_screen(rows_, tolerance_s=300)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user