StatusServer: richer status(), and recording_mode as a validated choice
status() now shows the failed component names (not just the count), a
red/yellow/green state indicator, and a summary of recent request activity
(count, errors, last kind/age/duration) from /stats - previously only
reachable via separate failures()/stats() calls.
recording_mode is now an _AdjustableFSChoice (enum_strs=("all", "throttle",
"sample"), the exact set RecordingSession.__init__ accepts) instead of a
freeform AdjustableFS: rejects an invalid value at set_target_value time,
and exposes enum_strs so it structurally satisfies
eco.elements.protocols.AdjustableEnum for widget-layer dropdown display.
Deliberately not eco.elements.adjustable.AdjustableEnum itself, which
stores an IntEnum's integer rather than the string - storage stays the
plain string the server already parses, no server-side change needed.
Also documents the max_value_elements/max_points_per_channel distinction
inline (per-value-width filter vs. per-channel-length cap over time) -
not obvious from the settings' names alone.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
45be1f3b72
commit
d41fb10a7d
2 files changed
+151
-13
No files matched your search
@@ -36,8 +36,11 @@ from __future__ import annotations
|
||||
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import colorama
|
||||
|
||||
from eco.elements.adjustable import AdjustableFS
|
||||
from eco.elements.assembly import Assembly
|
||||
|
||||
@@ -49,6 +52,36 @@ from .client import StatusServerClient, warn_failed_required
|
||||
_DEFAULT_CONFIG_DIR = "/sf/bernina/code/gac-bernina/eco_cnf_bernina/configuration"
|
||||
|
||||
|
||||
class _AdjustableFSChoice(AdjustableFS):
|
||||
"""An AdjustableFS restricted to a fixed set of string choices.
|
||||
|
||||
Deliberately not `eco.elements.adjustable.AdjustableEnum`: that wraps a
|
||||
base Adjustable and stores the *IntEnum's integer*, not the string
|
||||
itself -- fine for a PV-backed mbbi record, but it would change what
|
||||
lands in this setting's JSON file from the plain string
|
||||
RecordingSession.__init__ already parses (`mode not in ("all",
|
||||
"throttle", "sample")`) to an int, which only this class's own
|
||||
unwrapping would understand. `enum_strs` is enough on its own: per
|
||||
`eco.elements.protocols.AdjustableEnum`, any Adjustable exposing a
|
||||
truthy `enum_strs` structurally qualifies (no subclassing needed) --
|
||||
including getting picked up as a dropdown by widget layers'
|
||||
`_enum_options()` -- so this only adds that attribute plus write-time
|
||||
validation, storage stays a plain string, wire-compatible with the
|
||||
server as it already is.
|
||||
"""
|
||||
|
||||
def __init__(self, *args, enum_strs, **kwargs):
|
||||
self.enum_strs = tuple(enum_strs)
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def set_target_value(self, value, hold=False):
|
||||
if value not in self.enum_strs:
|
||||
raise ValueError(
|
||||
f"{self.name}: {value!r} is not one of {self.enum_strs}"
|
||||
)
|
||||
return super().set_target_value(value, hold=hold)
|
||||
|
||||
|
||||
class StatusServer(Assembly):
|
||||
def __init__(self, base_url: str, name=None, config_dir=None):
|
||||
super().__init__(name=name)
|
||||
@@ -57,9 +90,18 @@ class StatusServer(Assembly):
|
||||
self._gui_proc = None
|
||||
|
||||
cfg = Path(config_dir) if config_dir is not None else Path(_DEFAULT_CONFIG_DIR)
|
||||
# RecordingSession.__init__'s own accepted modes -- see its
|
||||
# docstring (module namespace_store.py) for what each does:
|
||||
# "all" keeps every update (unbounded memory risk, hence
|
||||
# max_points_per_channel); "throttle" drops updates faster than
|
||||
# recording_min_interval per channel but still runs the callback
|
||||
# every time; "sample" only latches a "latest" slot per update and
|
||||
# copies it onto a fixed grid (recording_sample_interval) from a
|
||||
# separate thread, the cheapest per-update cost of the three.
|
||||
self._append(
|
||||
AdjustableFS, str(cfg / "status_server_recording_mode.json"),
|
||||
_AdjustableFSChoice, str(cfg / "status_server_recording_mode.json"),
|
||||
default_value="throttle", name="recording_mode", is_setting=True,
|
||||
enum_strs=("all", "throttle", "sample"),
|
||||
)
|
||||
self._append(
|
||||
AdjustableFS, str(cfg / "status_server_recording_min_interval.json"),
|
||||
@@ -69,15 +111,25 @@ class StatusServer(Assembly):
|
||||
AdjustableFS, str(cfg / "status_server_recording_sample_interval.json"),
|
||||
default_value=0.1, name="recording_sample_interval", is_setting=True,
|
||||
)
|
||||
# "maximum_element_size": the largest value (by element count) a
|
||||
# recording will store - None keeps everything, including
|
||||
# waveforms. See RecordingSession/write_monitor_recording's own
|
||||
# docs for why this is usually the one knob that matters most for
|
||||
# file size.
|
||||
# "maximum_element_size": drops an update *outright* if that one
|
||||
# value's own element count (e.g. an 8000-sample waveform) exceeds
|
||||
# this - a per-VALUE-WIDTH filter, aimed at exactly the waveform
|
||||
# channels that otherwise dominate a recording's file size by an
|
||||
# order of magnitude (see RecordingSession's own docstring: two
|
||||
# digitizer waveforms were 204 MB of a 239 MB file). None keeps
|
||||
# everything, including waveforms.
|
||||
self._append(
|
||||
AdjustableFS, str(cfg / "status_server_recording_max_value_elements.json"),
|
||||
default_value=None, name="recording_max_value_elements", is_setting=True,
|
||||
)
|
||||
# A per-CHANNEL-LENGTH cap instead: how many stored points (across
|
||||
# the whole recording's duration) one channel may accumulate before
|
||||
# further updates for it are dropped - protects against a single
|
||||
# fast/never-idle channel growing unbounded over a long recording
|
||||
# (the OOM incident RecordingSession's docstring mentions). Distinct
|
||||
# axis from recording_max_value_elements above: that one filters by
|
||||
# the size of each individual value, this one by how many values
|
||||
# pile up over time.
|
||||
self._append(
|
||||
AdjustableFS,
|
||||
str(cfg / "status_server_recording_max_points_per_channel.json"),
|
||||
@@ -114,17 +166,49 @@ class StatusServer(Assembly):
|
||||
return self._client.monitor_policy()
|
||||
|
||||
def status(self):
|
||||
"""Print a one-line summary and return the raw /health body - the
|
||||
console equivalent of the GUI's top panel."""
|
||||
"""Print a fuller summary (health + recent request activity) and
|
||||
return the raw /health body - the console equivalent of the GUI's
|
||||
top panel."""
|
||||
h = self.health()
|
||||
state = h.get("state")
|
||||
n_failed = h.get("n_failed") or 0
|
||||
ok = bool(h.get("ready")) and not n_failed
|
||||
color = (
|
||||
colorama.Fore.GREEN if ok
|
||||
else colorama.Fore.RED if not h.get("ready")
|
||||
else colorama.Fore.YELLOW
|
||||
)
|
||||
reset = colorama.Style.RESET_ALL
|
||||
print(
|
||||
f"{self.base_url}: {state} "
|
||||
f"{self.base_url}: {color}{state}{reset} "
|
||||
f"({h.get('n_initialized')}/{h.get('n_target_names')} initialized, "
|
||||
f"{h.get('n_failed')} failed, {h.get('n_monitorable')} monitorable), "
|
||||
f"{n_failed} failed, {h.get('n_monitorable')} monitorable), "
|
||||
f"generation {h.get('generation')}, up {h.get('uptime_s', 0)/60:.1f} min"
|
||||
)
|
||||
failed = h.get("failed_names") or []
|
||||
if failed:
|
||||
shown = ", ".join(failed[:10])
|
||||
more = f" (+{len(failed) - 10} more)" if len(failed) > 10 else ""
|
||||
red = colorama.Fore.RED + colorama.Style.BRIGHT
|
||||
print(f" {red}failed:{reset} {shown}{more}")
|
||||
warn_failed_required(h)
|
||||
|
||||
try:
|
||||
summary = self.stats().get("summary") or {}
|
||||
except Exception:
|
||||
summary = {}
|
||||
if summary.get("n"):
|
||||
last_ago = (
|
||||
time.time() - summary["last_at"] if summary.get("last_at") else None
|
||||
)
|
||||
ago = f"{last_ago:.1f}s ago" if last_ago is not None else "?"
|
||||
errs = f", {summary['n_errors']} errors" if summary.get("n_errors") else ""
|
||||
last_dur = summary.get("last_duration_s")
|
||||
dur = f"{last_dur * 1000:.0f} ms" if last_dur is not None else "?"
|
||||
print(
|
||||
f" requests: {summary['n']} recent{errs}, last "
|
||||
f"'{summary.get('last_kind')}' {ago} ({dur})"
|
||||
)
|
||||
return h
|
||||
|
||||
def __repr__(self):
|
||||
|
||||
@@ -12,6 +12,7 @@ eco_cnf_bernina tree.
|
||||
"""
|
||||
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -30,8 +31,9 @@ def server(monkeypatch, tmp_path):
|
||||
calls.setdefault("health", 0)
|
||||
calls["health"] += 1
|
||||
return {
|
||||
"state": "ready", "n_initialized": 80, "n_target_names": 82,
|
||||
"n_failed": 1, "n_monitorable": 9000, "generation": 3,
|
||||
"state": "ready", "ready": True, "n_initialized": 80,
|
||||
"n_target_names": 82, "n_failed": 1, "failed_names": ["bad_component"],
|
||||
"n_monitorable": 9000, "generation": 3,
|
||||
"uptime_s": 120.0, "failed_required": [],
|
||||
}
|
||||
|
||||
@@ -40,7 +42,13 @@ def server(monkeypatch, tmp_path):
|
||||
|
||||
def stats(self, limit=None, kind=None):
|
||||
calls["stats"] = {"limit": limit, "kind": kind}
|
||||
return {"summary": {}, "recent": []}
|
||||
return {
|
||||
"summary": {
|
||||
"n": 42, "n_errors": 0, "last_at": time.time() - 3.5,
|
||||
"last_kind": "snapshot", "last_duration_s": 0.017,
|
||||
},
|
||||
"recent": [],
|
||||
}
|
||||
|
||||
def monitor_policy(self):
|
||||
return {"max_rate_hz": 10.0, "auto_monitor_default": True}
|
||||
@@ -72,6 +80,19 @@ def test_recording_settings_have_sensible_defaults(server):
|
||||
assert server.recording_max_points_per_channel.get_current_value() == 100_000
|
||||
|
||||
|
||||
def test_recording_mode_exposes_its_choices(server):
|
||||
# structural eco.elements.protocols.AdjustableEnum: any Adjustable with
|
||||
# a truthy enum_strs qualifies, e.g. for widget-layer dropdown display
|
||||
assert server.recording_mode.enum_strs == ("all", "throttle", "sample")
|
||||
|
||||
|
||||
def test_recording_mode_rejects_an_invalid_choice(server):
|
||||
with pytest.raises(ValueError):
|
||||
server.recording_mode.set_target_value("bogus")
|
||||
# rejected before ever touching the backing file
|
||||
assert server.recording_mode.get_current_value() == "throttle"
|
||||
|
||||
|
||||
def test_recording_settings_are_settable(server):
|
||||
# set_target_value(hold=False, the default) starts the write on a
|
||||
# background thread and returns immediately - wait() before asserting,
|
||||
@@ -126,6 +147,39 @@ def test_status_prints_a_summary_and_returns_health(server, capsys):
|
||||
assert "80/82" in out
|
||||
|
||||
|
||||
def test_status_lists_failed_component_names(server, capsys):
|
||||
server.status()
|
||||
out = capsys.readouterr().out
|
||||
assert "failed:" in out
|
||||
assert "bad_component" in out
|
||||
|
||||
|
||||
def test_status_omits_failed_line_when_nothing_failed(server, capsys):
|
||||
server._client.health = lambda: {
|
||||
"state": "ready", "ready": True, "n_initialized": 82, "n_target_names": 82,
|
||||
"n_failed": 0, "failed_names": [], "n_monitorable": 9000, "generation": 3,
|
||||
"uptime_s": 120.0, "failed_required": [],
|
||||
}
|
||||
server.status()
|
||||
out = capsys.readouterr().out
|
||||
assert "failed:" not in out
|
||||
|
||||
|
||||
def test_status_shows_recent_request_activity(server, capsys):
|
||||
server.status()
|
||||
out = capsys.readouterr().out
|
||||
assert "requests:" in out
|
||||
assert "42 recent" in out
|
||||
assert "snapshot" in out
|
||||
|
||||
|
||||
def test_status_omits_request_line_when_no_stats_yet(server, capsys):
|
||||
server._client.stats = lambda limit=None, kind=None: {"summary": {"n": 0}, "recent": []}
|
||||
server.status()
|
||||
out = capsys.readouterr().out
|
||||
assert "requests:" not in out
|
||||
|
||||
|
||||
def test_status_warns_in_red_about_failed_required(server, capsys):
|
||||
server._client.health = lambda: {
|
||||
"state": "ready", "n_initialized": 80, "n_target_names": 82,
|
||||
|
||||
Reference in new issue
Block a user