diff --git a/docs/requirements/015-rates-time-range.md b/docs/requirements/015-rates-time-range.md
index 06b65c9..f960fcd 100644
--- a/docs/requirements/015-rates-time-range.md
+++ b/docs/requirements/015-rates-time-range.md
@@ -38,8 +38,9 @@ control instead of three.
lacks room the chip shows as many as fit and `+n`. Rejected: a limit of 3
or 5.
6. **Persistence**: unit and chip metrics persist in the hub settings
- `rates` without a passkey, as today; the window stays page state, 1 h
- live on load, as today.
+ `rates` without a passkey, as today; the window stays page state, 24 h
+ live on load, as today (corrected 2026-11-17: the draft named 1 h, but
+ the page loads 24 h).
7. **Timing**: implemented after every other open skills requirement.
## Approach
@@ -96,7 +97,7 @@ flowchart TD
the window's token sum divided by 7.
- [ ] With 9 metrics chosen the chip shows every one at 1920 and `+n` at
390, and the header never grows wider than the viewport.
-- [ ] Unit and metrics survive a reload; the window returns to 1 h live.
+- [ ] Unit and metrics survive a reload; the window returns to 24 h live.
### Tests
diff --git a/orchestrator/kvb_orchestrator/rates.py b/orchestrator/kvb_orchestrator/rates.py
index e0308f0..efd40ab 100644
--- a/orchestrator/kvb_orchestrator/rates.py
+++ b/orchestrator/kvb_orchestrator/rates.py
@@ -1,6 +1,5 @@
from __future__ import annotations
-import math
from collections.abc import Iterable
from pathlib import Path
@@ -17,11 +16,10 @@ GAUGES = ("cpu", "ram", "vram", "swap", "disk")
METRICS = (*COUNTS, *GAUGES)
HOST = ("cpu", "ram", "swap")
TOKEN_FIELDS = ("input", "output", "cache_read", "cache_write")
-FINEST = {"tokens": HOUR}
+FINEST = {"tasks": 0, "commits": 0, "krs": 0, "tokens": HOUR}
INTERVAL = 60
DONE = "done"
DIGITS = 4
-TOTAL = "total"
NUMBERS = (int, float)
LIMIT = 1e18
@@ -99,87 +97,32 @@ def share(piece: Piece, begin: float, now: float) -> float:
return amount * inside / (end - start) if inside > 0 else 0.0
-def sums(pieces: list[Piece], now: float, finest: float) -> dict[str, float | None]:
+def counted(pieces: list[Piece], window: tuple[float, float], first: float | None, finest: float) -> dict[str, dict]:
"""Args:
- pieces: (start, end, amount) per event; start None for one without
- a time, end equal to start for an instant.
- now: the end of every window.
- finest: the shortest window the pieces resolve; a shorter one holds
- no event of its own.
+ pieces: (start, end, amount) per event of one kind; start None for
+ one without a time, end equal to start for an instant.
+ window: (begin, end) the rates cover, in epoch seconds.
+ first: the first time the records of the kind hold, None without any.
+ finest: the shortest span the kind's pieces resolve.
Returns:
- Per period the amount inside its window, None below finest; total
- holds every piece.
+ Per period {value, approx, covered}: covered the seconds of the
+ window from first on; total the amount inside the window, every
+ other period that amount per its seconds of covered; value None
+ where no record covers the window; approx where the window is
+ shorter than finest, and for every period but total where it starts
+ before first.
"""
- found: dict[str, float | None] = {}
- for name, seconds in PERIODS:
- if seconds is None:
- found[name] = sum(piece[2] for piece in pieces)
- else:
- found[name] = None if seconds < finest else sum(share(piece, now - seconds, now) for piece in pieces)
- return found
-
-
-def lengths(now: float, since: float | None) -> dict[str, float | None]:
- total = now - since if since is not None and now > since else None
- return {name: total if seconds is None else float(seconds) for name, seconds in PERIODS}
-
-
-def held(now: float, since: float | None, begins: float | None) -> dict[str, float | None]:
- """Args:
- now: the end of every window.
- since: the first time any record holds.
- begins: the first time the records of one kind hold, None when they
- reach back as far as any record.
-
- Returns:
- Per period the seconds now - begins where other records reach
- further back into its window than this kind's, else None.
- """
- if since is None or begins is None:
- return dict.fromkeys(PERIOD_NAMES)
- return {name: now - begins if begins > (since if seconds is None else max(now - seconds, since)) else None for name, seconds in PERIODS}
-
-
-def estimate(own: dict[str, float | None], length: dict[str, float | None], partial: dict[str, float | None], index: int) -> tuple[float, str | None]:
- """Returns:
- (value, source) of the period at index in PERIODS: its own amount
- when partial holds no seconds for it and it holds any or is total;
- its own amount scaled from the partial seconds to the part of its
- window any record covers when it holds any, source total; else the
- shortest longer window that holds any, scaled to the period's length
- (at most its whole amount), source that window, or total when
- partial holds seconds for that window and those scale it; 0 without
- any.
- """
- name, seconds = PERIODS[index]
- value = own[name]
- if partial[name] is None and (value or seconds is None):
- return value or 0.0, None
- if value and partial[name]:
- return value * min(length[name], length[TOTAL]) / partial[name], TOTAL
- for other, _ in PERIODS[index + 1:]:
- if own[other] and length[other]:
- covered = partial[other] or length[other]
- return own[other] * min(1.0, (seconds or covered) / covered), TOTAL if partial[other] else other
- return 0.0, None
-
-
-def extrapolated(own: dict[str, float | None], length: dict[str, float | None], partial: dict[str, float | None]) -> dict[str, dict]:
- """Args:
- own: sums() of one kind.
- length: lengths() of the windows.
- partial: held() of the kind.
-
- Returns:
- Per period {value, approx, source} of estimate(); approx whenever it
- names a source.
- """
- found = {}
- for index, name in enumerate(PERIOD_NAMES):
- value, source = estimate(own, length, partial, index)
- found[name] = {"value": precise(value), "approx": source is not None, "source": source}
- return found
+ begin, end = window
+ covered = end - max(begin, first) if first is not None else 0.0
+ if covered <= 0:
+ return {name: {"value": None, "approx": False, "covered": None} for name in PERIOD_NAMES}
+ amount = sum(share(piece, begin, end) for piece in pieces)
+ short, early = end - begin < finest, begin < first
+ return {
+ name: {"value": precise(amount if seconds is None else amount * seconds / covered), "approx": short or (early and seconds is not None), "covered": covered}
+ for name, seconds in PERIODS
+ }
def precise(value: float) -> float:
@@ -207,10 +150,6 @@ def blank() -> dict:
return {**{key: [0.0, 0.0] for key in HOST}, "vram": [0.0, 0.0, 0.0], "disks": {}}
-def copied(tally: dict) -> dict:
- return {**{key: list(tally[key]) for key in (*HOST, "vram")}, "disks": {mount: list(cell) for mount, cell in tally["disks"].items()}}
-
-
def add_vram(cell: list[float], gpus: object, weight: float) -> None:
used = total = 0.0
for gpu in gpus if type(gpus) is list else ():
@@ -258,30 +197,16 @@ def add(tally: dict, row: dict, part: float) -> None:
add_disks(tally["disks"], row.get("disks"), weight)
-def tallies(items: list[Placed], begins: list[float]) -> list[dict]:
- """Args:
- items: placed() of every sample row, newest first.
- begins: the start of every window, latest first; every window ends
- at the newest row or later.
-
- Returns:
- Per window the add() of every row inside it, a row whose bucket
- straddles its start by the share inside, in one pass over items.
+def tally(items: list[Placed], window: tuple[float, float]) -> dict:
+ """Returns:
+ The add() of every placed() row inside the window, a row whose
+ bucket straddles either end by its share inside.
"""
- widest = max((item[1] for item in items), default=0.0)
- running, index, found = blank(), 0, []
- for begin in begins:
- while index < len(items) and items[index][0] >= begin:
- add(running, items[index][2], 1.0)
- index += 1
- tally = copied(running)
- cursor = index
- while cursor < len(items) and items[cursor][0] > begin - widest:
- t, width, row = items[cursor]
- if t + width > begin:
- add(tally, row, (t + width - begin) / width)
- cursor += 1
- found.append(tally)
+ found = blank()
+ for t, width, row in items:
+ part = share((t, t + width, 1.0), *window)
+ if part:
+ add(found, row, part)
return found
@@ -311,40 +236,38 @@ def gauges(tally: dict) -> dict[str, dict]:
found = {key: {"value": rounded(mean(tally[key], 1), 1)} for key in HOST}
found["vram"] = {"value": rounded(mean(tally["vram"], 1), 1), "used": rounded(mean(tally["vram"], 2), 0)}
found["disk"] = fullest(tally["disks"])
- return {key: {**entry, "approx": False, "source": None} for key, entry in found.items()}
+ return {key: {**entry, "approx": False} for key, entry in found.items()}
-def rated(council: Path, kept: Path, now: float) -> dict:
+def rated(council: Path, kept: Path, now: float, window: tuple[float, float]) -> dict:
"""Args:
council: the council directory holding the register and the score
journal, read through scores.journal().
kept: the directory holding the host sampler's resources folder.
- now: the end of every window in epoch seconds.
+ now: the present in epoch seconds; the current token hour ends here.
+ window: (begin, end) the rates cover, end at most now.
Returns:
- {"now", "since", "periods", "rates"}: since the first time any record
- holds; periods [{name, seconds}], total without seconds; rates per
- period per metric {value, approx, source}: the done tasks, the
- commits, the key results met and the tokens inside the window ending
- now, extrapolated() where it holds none or, for tokens, where it
- reaches back before the first token row, and the gauges() of the
- samples inside it; total spans every record.
+ {"now", "since", "from", "to", "periods", "rates"}: since the first
+ time any record holds; periods [{name, seconds}], total without
+ seconds; rates per period per metric: counted() of the done tasks,
+ the commits, the key results met and the tokens, each kind covered
+ from since on, tokens from their first row, and the gauges() of the
+ samples inside the window, the same in every period.
"""
tasks = read_register(council / REGISTER)
folder = kept / RESOURCES
- items = sorted(filter(None, map(placed, rows_between(folder / SAMPLES, 0, now, 0))), key=lambda item: item[0], reverse=True)
- counted = {
+ items = list(filter(None, map(placed, rows_between(folder / SAMPLES, 0, now, 0))))
+ pieces = {
"tasks": points(task_times(tasks)),
"commits": points(commit_times(journal(council))),
"krs": points(kr_times(tasks)),
"tokens": token_pieces(rows_between(folder / TOKENS, 0, now, HOUR), now),
}
- starts = {kind: [piece[0] for piece in pieces if piece[0] is not None] for kind, pieces in counted.items()}
- times = [*(t for found in starts.values() for t in found), *(item[0] for item in items)]
- since = min(times) if times else None
- length = lengths(now, since)
- begins = {"tokens": min(starts["tokens"], default=None)}
- counts = {kind: extrapolated(sums(pieces, now, FINEST.get(kind, 0)), length, held(now, since, begins.get(kind))) for kind, pieces in counted.items()}
- measured = tallies(items, [-math.inf if seconds is None else now - seconds for _, seconds in PERIODS])
- shown = {name: {**{kind: counts[kind][name] for kind in COUNTS}, **gauges(tally)} for name, tally in zip(PERIOD_NAMES, measured, strict=True)}
- return {"now": now, "since": since, "periods": [{"name": name, "seconds": seconds} for name, seconds in PERIODS], "rates": shown}
+ starts = {kind: [piece[0] for piece in found if piece[0] is not None] for kind, found in pieces.items()}
+ since = min([*(t for found in starts.values() for t in found), *(item[0] for item in items)], default=None)
+ firsts = {**dict.fromkeys(COUNTS, since), "tokens": min(starts["tokens"], default=None)}
+ counts = {kind: counted(found, window, firsts[kind], FINEST[kind]) for kind, found in pieces.items()}
+ measured = gauges(tally(items, window))
+ shown = {name: {**{kind: counts[kind][name] for kind in COUNTS}, **measured} for name in PERIOD_NAMES}
+ return {"now": now, "since": since, "from": window[0], "to": window[1], "periods": [{"name": name, "seconds": seconds} for name, seconds in PERIODS], "rates": shown}
diff --git a/orchestrator/kvb_orchestrator/settings.py b/orchestrator/kvb_orchestrator/settings.py
index e140b82..a8e85db 100644
--- a/orchestrator/kvb_orchestrator/settings.py
+++ b/orchestrator/kvb_orchestrator/settings.py
@@ -50,7 +50,7 @@ SETTINGS = {
"bubbles": False,
"rates": {"period": "hour", "metrics": ["tasks", "tokens", "cpu"]},
}
-RATE_CHIP = (2, 3)
+RATE_CHIP = (2, 9)
SPACE_AXES = ("none", "time", "spawn")
SPACE_LIMITS = {
"graph": {"yaw": (-180.0, 180.0), "pitch": (-90.0, 90.0), "zoom": (0.25, 4.0), "pan_x": (-2000.0, 2000.0), "pan_y": (-2000.0, 2000.0)},
diff --git a/orchestrator/templates/index.html.j2 b/orchestrator/templates/index.html.j2
index a86f92b..3e92753 100644
--- a/orchestrator/templates/index.html.j2
+++ b/orchestrator/templates/index.html.j2
@@ -466,7 +466,6 @@ pre.notes { margin: 0; padding: 8px 10px; background: var(--sunken); border-radi
diff --git a/orchestrator/templates/page-boot.js b/orchestrator/templates/page-boot.js
index 924733c..b64c127 100644
--- a/orchestrator/templates/page-boot.js
+++ b/orchestrator/templates/page-boot.js
@@ -48,7 +48,7 @@
syncBubbles();
syncMusic();
syncRadio();
- timeBar();
+ ratesBar();
notifyLabel();
announce();
measure();
diff --git a/orchestrator/templates/page-constants.js b/orchestrator/templates/page-constants.js
index a47f11c..f5a8a30 100644
--- a/orchestrator/templates/page-constants.js
+++ b/orchestrator/templates/page-constants.js
@@ -3,7 +3,7 @@
const ACTIVE_BEAT = 900;
const DAY = 86400;
const HUMAN = "Operator";
- const PRESETS = [[3600, "1 h"], [21600, "6 h"], [DAY, "24 h"], [7 * DAY, "7 d"], [0, "all"], [-1, "custom"]];
+ const PRESETS = [[3600, "1 h"], [21600, "6 h"], [DAY, "24 h"], [7 * DAY, "7 d"], [0, "all"]];
const PARTS = [["user", "User"], ["assistant", "Assistant"], ["tool_call", "Tool call"], ["tool_result", "Tool result"], ["system", "System"]];
const PART_TONES = {user: "info", assistant: "ok", tool_call: "violet", tool_result: "teal", system: "neutral"};
const KIND_TONES = {radio: "teal", review: "info", ask: "warn", decision: "violet", operator: "bad", register: "neutral", rename: "info", cron: "teal", rule: "ok", approval: "ok", run: "info", transcript: "neutral"};
diff --git a/orchestrator/templates/page-dom.js b/orchestrator/templates/page-dom.js
index 178e6df..d150a89 100644
--- a/orchestrator/templates/page-dom.js
+++ b/orchestrator/templates/page-dom.js
@@ -50,7 +50,7 @@
const counter = (value) => h("span", {class: "count"}, String(value));
const meta = (pairs) => h("dl", {class: "meta"}, pairs.map(([label, value, title]) => h("div", {}, h("dt", {}, label), h("dd", {}, h("code", {title: title ? revealed(title) : null}, revealed(dash(value)))))));
const emptyState = (title, text) => h("div", {class: "empty"}, h("div", {class: "empty-mark", "aria-hidden": "true"}), h("h2", {}, title), h("p", {}, text));
- const live = () => ui.preset !== -1 && ui.asOf === null;
+ const live = () => ui.asOf === null;
const alarmRows = () => state.radio.filter((row) => (row.class === "mayday" || row.class === "pan-pan") && row.ts >= state.alarm_since).reverse();
const keyOf = (item) => `${item.kind}:${item.id}`;
const visibleInbox = () => state.inbox.filter((item) => ui.snoozed || !item.until);
diff --git a/orchestrator/templates/page-filters.js b/orchestrator/templates/page-filters.js
index 37fe5d6..2c462d0 100644
--- a/orchestrator/templates/page-filters.js
+++ b/orchestrator/templates/page-filters.js
@@ -23,9 +23,6 @@
}
function windowKey() {
- if (ui.preset === -1) {
- return `custom:${ui.from}:${ui.to}`;
- }
return `${ui.preset}:${ui.asOf === null ? "live" : ui.asOf}`;
}
diff --git a/orchestrator/templates/page-rates.js b/orchestrator/templates/page-rates.js
index fae5b68..486ad30 100644
--- a/orchestrator/templates/page-rates.js
+++ b/orchestrator/templates/page-rates.js
@@ -1,4 +1,4 @@
- const RATES = {poll: 60, fewest: 2, most: 3};
+ const RATES = {poll: 60, fewest: 2, most: 9};
const RATE_PERIODS = [
["minute", "min", "per minute"],
["hour", "h", "per hour"],
@@ -6,7 +6,7 @@
["week", "wk", "per week"],
["month", "mo", "per month of 30 days"],
["year", "yr", "per year of 365 days"],
- ["total", "all", "in total since records begin"],
+ ["total", "all", "in the whole window"],
];
const RATE_METRICS = [
["tasks", "bi-check2-square", "Tasks done", "count"],
@@ -20,7 +20,7 @@
["disk", "bi-hdd", "Disk", "disk"],
];
const BYTE_UNITS = ["B", "KiB", "MiB", "GiB", "TiB", "PiB"];
- const rateView = {data: null, dom: null, busy: false, at: -Infinity};
+ const rateView = {data: null, dom: null, ticket: 0, at: -Infinity};
const periodOf = (id) => RATE_PERIODS.find(([name]) => name === id) || RATE_PERIODS[1];
const metricOf = (id) => RATE_METRICS.find(([name]) => name === id);
@@ -43,7 +43,7 @@
* @param entry - one metric of one period from /api/rates, or null before the first answer.
* @param kind - the metric's kind: count, percent, vram or disk.
* @param short - the period's short name a count is per, "" for none.
- * @returns the cell text: ≈ before an extrapolated count, n/a for a gauge without a sample.
+ * @returns the cell text: ≈ before an estimated count, n/a without a record or sample in the window.
*/
function rateText(entry, kind, short) {
if (!entry) {
@@ -65,10 +65,11 @@
return `${label} ${per}: loading`;
}
if (entry.value === null) {
- return `${label} ${per}: n/a, no sample in this window`;
+ return `${label} ${per}: n/a, ${kind === "count" ? "no record of this kind reaches into the window" : "no sample in the window"}`;
}
- const source = entry.source === "total" ? "all records of this kind" : `the last ${entry.source}`;
- const scaled = entry.approx ? `; ≈ none in this window or its records begin later, scaled from ${source}` : "";
+ const length = rateView.data.to - rateView.data.from;
+ const covered = entry.covered !== null && entry.covered < length - 1;
+ const scaled = !entry.approx ? "" : covered ? `; ≈ its records cover only the last ${spanName(entry.covered)} of the window, so the rate divides by that part` : "; ≈ the window is shorter than the hour tokens are counted in";
const where = id === "disk" ? ` on ${entry.mount}, ${bytesText(entry.used)} of ${bytesText(entry.total)}` : "";
const mean = kind === "count" ? "" : " on average";
return `${label} ${per}: ${rateText(entry, kind, "")}${mean}${where}${scaled}`;
@@ -76,18 +77,19 @@
function ratesBar() {
const host = document.getElementById("rates");
- const period = h("select", {class: "field rates-period", ...named("Rate period", "Rate period: the window the chip counts over"), on: {change: (event) => chooseRates({...settings.rates, period: event.target.value})}},
- RATE_PERIODS.map(([id, short, per]) => h("option", {value: id, title: per}, short)));
const chip = h("summary", {class: "rates-chip"});
+ const units = h("div", {class: "segmented rates-units", role: "group", "aria-label": "Rate unit"},
+ RATE_PERIODS.map(([id, short, per]) => h("button", {type: "button", "data-period": id, title: per, on: {click: () => chooseRates({...settings.rates, period: id})}}, short)));
const body = h("tbody", {});
const head = h("tr", {}, h("th", {scope: "col"}, "Metric"), h("th", {scope: "col", title: "Shown in the chip"}, "Chip"), RATE_PERIODS.map(([id, short, per]) => h("th", {scope: "col", "data-period": id, title: per}, short)));
const box = h("div", {class: "menu-box end rates-box", role: "group", "aria-label": "Rates per period"},
- h("h3", {}, "Rates per period"),
+ h("h3", {}, "Rates per period"), ...timeSection(), h("h3", {}, "Unit"), units,
h("div", {class: "rates-scroll"}, h("table", {class: "rates-table"}, h("thead", {}, head), body)),
- h("p", {class: "muted"}, `≈ means an estimate: none in the window, so the shortest longer window that holds any, scaled down, or a window reaching back before the records of its kind begin, scaled from those records. n/a means no sample in the window. Total counts since records begin; the gauges average over the window. The chip shows ${RATES.fewest} to ${RATES.most} metrics.`));
+ h("p", {class: "muted"}, `Every column rates the chosen window: a count is its sum per unit, all the sum itself; a gauge averages over the window. ≈ means the records of a kind begin inside the window, so the rate divides by the part they cover, or the window is shorter than the hour tokens are counted in. n/a means no record or sample in the window. The chip shows ${RATES.fewest} to ${RATES.most} metrics.`));
const menu = h("details", {class: "menu rates-menu"}, chip, box);
- host.replaceChildren(period, menu);
- rateView.dom = {period, chip, body, menu};
+ host.replaceChildren(menu);
+ rateView.dom = {chip, units, body, menu};
+ window.addEventListener("resize", () => fitChip(chip));
}
function rateCell(metric, period, chosen) {
@@ -105,6 +107,23 @@
return h("tr", {"data-metric": id}, h("th", {scope: "row"}, icon(name), ` ${label}`), h("td", {}, pick), RATE_PERIODS.map((period) => rateCell(metric, period, chosen)));
}
+ function fitChip(chip) {
+ const shown = [...chip.querySelectorAll(".rate")];
+ const more = chip.querySelector(".rate-more");
+ if (!more) {
+ return;
+ }
+ shown.forEach((node) => {
+ node.hidden = false;
+ });
+ more.hidden = true;
+ for (let kept = shown.length; kept > 1 && chip.scrollWidth > chip.clientWidth; kept -= 1) {
+ shown[kept - 1].hidden = true;
+ more.hidden = false;
+ more.textContent = `+${shown.length - kept + 1}`;
+ }
+ }
+
function drawRates() {
const dom = rateView.dom;
if (!dom) {
@@ -114,11 +133,16 @@
const period = periodOf(chosen.period);
const short = period[0] === "total" ? "" : period[1];
const entries = chosen.metrics.map((id) => [metricOf(id), rateView.data ? rateView.data.rates[period[0]][id] : null]);
- dom.period.value = period[0];
- dom.chip.replaceChildren(...entries.map(([metric, entry]) => h("span", {class: "rate", "data-metric": metric[0], "data-approx": String(Boolean(entry && entry.approx)), title: rateHint(entry, metric, period)},
- icon(metric[1]), h("span", {class: "rate-value"}, rateText(entry, metric[3], metric[3] === "count" ? short : "")))), caret());
- dom.chip.setAttribute("aria-label", `Rates ${period[2]}: ${entries.map(([metric, entry]) => `${metric[2]} ${rateText(entry, metric[3], "")}`).join(", ")}`);
- dom.chip.title = "Rates: every metric per period";
+ for (const button of dom.units.children) {
+ button.setAttribute("aria-pressed", String(button.dataset.period === period[0]));
+ }
+ dom.chip.replaceChildren(h("span", {class: "rate-window"}, `${windowName()} · ${short ? `/${short}` : "all"}`),
+ ...entries.map(([metric, entry]) => h("span", {class: "rate", "data-metric": metric[0], "data-approx": String(Boolean(entry && entry.approx)), title: rateHint(entry, metric, period)},
+ icon(metric[1]), h("span", {class: "rate-value"}, rateText(entry, metric[3], metric[3] === "count" ? short : "")))),
+ h("span", {class: "rate-more", hidden: true}), caret());
+ dom.chip.setAttribute("aria-label", `Rates ${period[2]} over ${windowName()}: ${entries.map(([metric, entry]) => `${metric[2]} ${rateText(entry, metric[3], "")}`).join(", ")}`);
+ dom.chip.title = "Rates per period: time range, replay, unit and every metric";
+ fitChip(dom.chip);
const focused = document.activeElement instanceof Element ? document.activeElement.closest("#rates tr[data-metric]") : null;
dom.body.replaceChildren(...RATE_METRICS.map((metric) => rateRow(metric, chosen)));
const again = focused ? dom.body.querySelector(`tr[data-metric="${focused.dataset.metric}"] input`) : null;
@@ -134,26 +158,22 @@
}
async function fetchRates() {
- rateView.busy = true;
+ rateView.ticket += 1;
+ const ticket = rateView.ticket;
+ rateView.at = nowSeconds();
try {
- const response = await fetch("/api/rates", {cache: "no-store"});
- if (response.ok) {
+ const response = await fetch(`/api/rates?${windowQuery()}`, {cache: "no-store"});
+ if (response.ok && ticket === rateView.ticket) {
rateView.data = await response.json();
}
} catch {
ui.offline = true;
}
- rateView.at = nowSeconds();
- rateView.busy = false;
drawRates();
}
function pollRates() {
- if (!rateView.dom) {
- ratesBar();
- drawRates();
- }
- if (!document.hidden && !rateView.busy && nowSeconds() - rateView.at >= RATES.poll) {
+ if (!document.hidden && nowSeconds() - rateView.at >= RATES.poll) {
fetchRates();
}
}
diff --git a/orchestrator/templates/page-resources.js b/orchestrator/templates/page-resources.js
index e676a09..142bb83 100644
--- a/orchestrator/templates/page-resources.js
+++ b/orchestrator/templates/page-resources.js
@@ -8,7 +8,7 @@
};
const RESOURCE_BURN = {done: "Done", scope: "Scope"};
const resourceView = {data: null, dom: null, busy: false, key: "", at: 0, timer: 0, series: {}};
- const resourceKey = () => JSON.stringify([ui.preset, ui.from, ui.to, ui.asOf]);
+ const resourceKey = () => JSON.stringify([ui.preset, ui.asOf]);
const resourcesStale = () => resourceKey() !== resourceView.key || nowSeconds() - resourceView.at >= RESOURCE.stale;
const compactCount = Resources.compact;
const resourceLabel = (line) => (line.kind === "vram" ? `${line.name} VRAM` : line.name);
diff --git a/orchestrator/templates/page-state.js b/orchestrator/templates/page-state.js
index 1b90ade..10ca995 100644
--- a/orchestrator/templates/page-state.js
+++ b/orchestrator/templates/page-state.js
@@ -32,7 +32,7 @@
let state = JSON.parse(document.getElementById("state").textContent);
const KINDS = Object.entries(state.kinds);
- const ui = {screen: "stream", views: {agents: "orbit", artifacts: "tree", tasks: "list"}, preset: DAY, from: null, to: null, asOf: null, playing: false, busy: false, offline: false, agent: null, order: null, spot: false, nightOff: new Set(), cursor: 0, selected: new Set(), snoozed: false, review: null, file: 0};
+ const ui = {screen: "stream", views: {agents: "orbit", artifacts: "tree", tasks: "list"}, preset: DAY, asOf: null, playing: false, busy: false, offline: false, agent: null, order: null, spot: false, nightOff: new Set(), cursor: 0, selected: new Set(), snoozed: false, review: null, file: 0};
const filter = {view: "fuer-dich", saved: "", kinds: new Set(state.views["fuer-dich"].kinds), waiting: true, q: "", agent: "", session: "", sub: "", parts: new Set(PARTS.map(([id]) => id))};
const stream = {key: null, events: [], entries: [], last: 0, sessions: {}, busy: false, again: false, dom: null, error: ""};
const tree = {data: null, model: null, busy: false, q: "", open: new Set(), dom: null};
diff --git a/orchestrator/templates/page-time.js b/orchestrator/templates/page-time.js
index c91c5c1..b0681b5 100644
--- a/orchestrator/templates/page-time.js
+++ b/orchestrator/templates/page-time.js
@@ -1,7 +1,7 @@
+ const SPAN_STEPS = 1000;
+ const SHORTEST = 60;
+
function windowQuery() {
- if (ui.preset === -1) {
- return `from=${Math.floor(ui.from)}&to=${Math.ceil(ui.to)}`;
- }
const end = ui.asOf === null ? nowSeconds() : ui.asOf;
const begin = ui.preset === 0 ? 0 : end - ui.preset;
return `from=${Math.floor(begin)}${ui.asOf === null ? "" : `&to=${Math.ceil(end)}`}`;
@@ -12,9 +12,34 @@
return new Date(date.getTime() - date.getTimezoneOffset() * 60000).toISOString().slice(0, 16);
};
+ function spanName(seconds) {
+ if (seconds >= DAY) {
+ return `${Math.round(seconds / DAY)} d`;
+ }
+ return seconds >= 3600 ? `${Math.round(seconds / 3600)} h` : `${Math.max(Math.round(seconds / 60), 1)} min`;
+ }
+
+ function windowName() {
+ const preset = PRESETS.find(([seconds]) => seconds === ui.preset);
+ return `${preset ? preset[1] : spanName(ui.preset)}${ui.asOf === null ? "" : " · past"}`;
+ }
+
+ /**
+ * @param {number} begin the window's start in epoch seconds.
+ * @param {number} end its end; at or after now the window is live and keeps its length.
+ */
+ function setWindow(begin, end) {
+ stopReplay();
+ const now = nowSeconds();
+ const stop = Math.min(end, now);
+ ui.preset = Math.max(Math.round(stop - begin), SHORTEST);
+ ui.asOf = end >= now ? null : stop;
+ reload();
+ }
+
function customRange() {
- const from = h("input", {type: "datetime-local", "aria-label": "From", value: localInput(ui.from === null ? state.since : ui.from)});
- const to = h("input", {type: "datetime-local", "aria-label": "To", value: localInput(ui.to === null ? state.until : ui.to)});
+ const from = h("input", {type: "datetime-local", "aria-label": "From", value: localInput(state.since)});
+ const to = h("input", {type: "datetime-local", "aria-label": "To", value: localInput(state.until)});
return h("span", {class: "custom"}, from, h("span", {class: "muted"}, "to"), to,
h("button", {type: "button", class: "btn small soft", on: {click: () => {
const begin = Date.parse(from.value) / 1000;
@@ -23,47 +48,45 @@
toast("Time range invalid", ["“From” must come before “To”."], false);
return;
}
- stopReplay();
- ui.preset = -1;
- ui.from = begin;
- ui.to = end;
- reload();
+ setWindow(begin, end);
}}}, "Apply"));
}
- function timeBar() {
- const bar = document.getElementById("time");
+ function spanSlider() {
+ const [from, to] = ["from", "to"].map((end, index) => h("input", {type: "range", class: `span-${end}`, min: 0, max: SPAN_STEPS, step: 1, value: index * SPAN_STEPS, "aria-label": `Time range ${end}`}));
+ const fill = h("span", {class: "span-fill", "aria-hidden": "true"});
+ const at = (value) => state.first + (Math.max(state.now - state.first, 1) * Number(value)) / SPAN_STEPS;
+ const moved = (event) => {
+ if (Number(from.value) >= Number(to.value)) {
+ event.target.value = String(event.target === from ? Number(to.value) - 1 : Number(from.value) + 1);
+ }
+ setWindow(at(from.value), Number(to.value) >= SPAN_STEPS ? nowSeconds() : at(to.value));
+ };
+ from.addEventListener("input", moved);
+ to.addEventListener("input", moved);
+ return {box: h("div", {class: "span-slider"}, fill, from, to), from, to, fill};
+ }
+
+ function timeSection() {
const presets = h("div", {class: "segmented", role: "group", "aria-label": "Time range"});
- const summary = h("summary", {title: "Time range and replay"});
- const slider = h("input", {type: "range", min: 0, max: 1000, step: 1, value: 1000, "aria-label": "Replay position"});
- const play = iconBtn("bi-play-fill", "Replay", "btn small soft", null);
- const liveButton = h("button", {type: "button", class: "btn small ghost"}, "Live");
- const label = h("span", {class: "window-label"});
- for (const [seconds, name] of PRESETS.filter(([seconds]) => seconds !== -1)) {
+ for (const [seconds, name] of PRESETS) {
presets.append(h("button", {type: "button", "data-preset": seconds, on: {click: () => choosePreset(seconds)}}, name));
}
- slider.addEventListener("input", () => {
- stopReplay();
- const span = state.now - state.first;
- ui.asOf = Number(slider.value) >= 1000 ? null : state.first + (span * Number(slider.value)) / 1000;
- reload();
- });
- play.addEventListener("click", () => (ui.playing ? stopReplay() : startReplay()));
- liveButton.addEventListener("click", () => {
+ const slider = spanSlider();
+ const play = iconBtn("bi-play-fill", "Replay", "btn small soft", () => (ui.playing ? stopReplay() : startReplay()));
+ const liveButton = h("button", {type: "button", class: "btn small ghost", on: {click: () => {
stopReplay();
ui.asOf = null;
- if (ui.preset === -1) {
- ui.preset = DAY;
- }
reload();
- });
- bar.append(summary, h("div", {class: "menu-box end"}, h("h3", {}, "Time range"), presets, h("h3", {}, "Replay"), h("div", {class: "replay"}, play, slider, liveButton), label,
- h("p", {class: "muted"}, "Filter ▾ in the stream sets a custom time range.")));
- timeDom = {summary, presets, slider, play, live: liveButton, label};
+ }}}, "Live");
+ const label = h("span", {class: "window-label"});
+ timeDom = {presets, from: slider.from, to: slider.to, fill: slider.fill, play, live: liveButton, label};
+ return [h("h3", {}, "Time range"), presets, slider.box, h("div", {class: "replay"}, play, liveButton, label), customRange()];
}
function reload() {
load();
+ fetchRates();
if (ui.screen === "stream") {
fetchEvents();
}
@@ -72,6 +95,7 @@
function choosePreset(seconds) {
stopReplay();
ui.preset = seconds;
+ ui.asOf = null;
reload();
}
@@ -80,27 +104,26 @@
return;
}
for (const button of timeDom.presets.children) {
- button.setAttribute("aria-pressed", String(Number(button.dataset.preset) === ui.preset));
+ button.setAttribute("aria-pressed", String(Number(button.dataset.preset) === ui.preset && ui.asOf === null));
}
- timeDom.summary.textContent = `${(PRESETS.find(([seconds]) => seconds === ui.preset) || [0, "custom"])[1]}${ui.asOf === null ? "" : " · replay"} ▾`;
- timeDom.slider.disabled = ui.preset === -1;
- timeDom.play.disabled = ui.preset === -1;
- timeDom.live.disabled = live();
const span = Math.max(state.now - state.first, 1);
- if (!ui.playing) {
- timeDom.slider.value = String(ui.asOf === null ? 1000 : Math.round((1000 * (ui.asOf - state.first)) / span));
- }
+ const end = ui.asOf === null ? state.now : ui.asOf;
+ const place = (t) => Math.min(Math.max(Math.round((SPAN_STEPS * (t - state.first)) / span), 0), SPAN_STEPS);
+ const [low, high] = [place(ui.preset === 0 ? state.first : end - ui.preset), ui.asOf === null ? SPAN_STEPS : place(end)];
+ timeDom.from.value = String(Math.min(low, high - 1));
+ timeDom.to.value = String(high);
+ timeDom.from.classList.toggle("on-top", low > SPAN_STEPS / 2);
+ timeDom.fill.style.left = `${(100 * low) / SPAN_STEPS}%`;
+ timeDom.fill.style.right = `${100 - (100 * high) / SPAN_STEPS}%`;
+ timeDom.live.disabled = live();
timeDom.label.textContent = `${absolute.format(stampOf(state.since))} – ${absolute.format(stampOf(state.until))}${live() ? " · live" : ""}`;
}
function startReplay() {
- if (ui.preset === -1) {
- return;
- }
- const began = state.first;
+ const began = state.first + (ui.preset === 0 ? 0 : ui.preset);
const span = Math.max(state.now - began, 1);
const resume = ui.asOf !== null && ui.asOf < state.now - 60;
- let progress = resume ? (ui.asOf - began) / span : 0;
+ let progress = resume ? Math.max((ui.asOf - began) / span, 0) : 0;
ui.playing = true;
relabel(timeDom.play, "bi-pause-fill", "Pause");
const advance = async () => {
@@ -109,8 +132,8 @@
}
progress = Math.min(progress + 1 / 48, 1);
ui.asOf = progress >= 1 ? null : began + span * progress;
- timeDom.slider.value = String(Math.round(progress * 1000));
await load();
+ fetchRates();
if (ui.screen === "stream") {
fetchEvents();
}
diff --git a/orchestrator/templates/style-rates.css b/orchestrator/templates/style-rates.css
index 9ce0937..921e0aa 100644
--- a/orchestrator/templates/style-rates.css
+++ b/orchestrator/templates/style-rates.css
@@ -1,12 +1,28 @@
.rates { display: inline-flex; align-items: center; gap: 4px; min-width: 0; max-width: 100%; }
-.rates-period { padding: 3px 6px; border-radius: 999px; font-size: var(--fs-xs); }
-.rates-menu > summary.rates-chip { gap: 10px; padding: 3px 10px; border-radius: 999px; font-size: var(--fs-sm); font-weight: 560; font-variant-numeric: tabular-nums; white-space: nowrap; }
+.rates-menu { min-width: 0; max-width: 100%; }
+.rates-menu > summary.rates-chip { gap: 10px; max-width: 100%; min-width: 0; padding: 3px 10px; overflow: hidden; border-radius: 999px; font-size: var(--fs-sm); font-weight: 560; font-variant-numeric: tabular-nums; white-space: nowrap; }
.rates-chip .rate { display: inline-flex; align-items: center; gap: 4px; }
+.rates-chip .rate[hidden], .rates-chip .rate-more[hidden] { display: none; }
+.rates-chip .rate-window, .rates-chip .rate-more { color: var(--muted); font-size: var(--fs-xs); }
.rates-chip .bi { color: var(--muted); }
+.span-slider { position: relative; height: 28px; }
+.span-slider::before, .span-fill { position: absolute; top: 50%; height: 4px; margin-top: -2px; border-radius: 2px; content: ""; }
+.span-slider::before { right: 0; left: 0; background: var(--line); }
+.span-fill { background: var(--accent); }
+.span-slider input[type="range"] { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; background: none; pointer-events: none; appearance: none; -webkit-appearance: none; }
+.span-slider input[type="range"].on-top { z-index: 1; }
+.span-slider input[type="range"]::-webkit-slider-runnable-track { background: none; }
+.span-slider input[type="range"]::-moz-range-track { background: none; }
+.span-slider input[type="range"]::-webkit-slider-thumb { width: 18px; height: 18px; border: 2px solid var(--panel); border-radius: 50%; background: var(--accent); cursor: grab; pointer-events: auto; appearance: none; -webkit-appearance: none; }
+.span-slider input[type="range"]::-moz-range-thumb { width: 14px; height: 14px; border: 2px solid var(--panel); border-radius: 50%; background: var(--accent); cursor: grab; pointer-events: auto; }
+.span-slider input[type="range"]:focus-visible::-webkit-slider-thumb { outline: 2px solid var(--accent); outline-offset: 2px; }
+.span-slider input[type="range"]:focus-visible::-moz-range-thumb { outline: 2px solid var(--accent); outline-offset: 2px; }
.rates-chip .rate[data-approx="true"] .rate-value, .rates-table td[data-approx="true"] { color: var(--muted); }
.rates-box { max-width: min(1100px, calc(100vw - 32px)); }
.rates-scroll { max-width: 100%; overflow-x: auto; }
.rates-box > p { width: 0; min-width: 100%; margin: 0; }
+.rates-box .replay { flex-wrap: wrap; min-width: 0; }
+.rates-box .window-label { min-width: 0; white-space: normal; }
.rates-table { border-collapse: collapse; font-size: var(--fs-sm); font-variant-numeric: tabular-nums; }
.rates-table th, .rates-table td { padding: 3px 8px; border-bottom: 1px solid var(--line); text-align: right; white-space: nowrap; }
.rates-table thead th { color: var(--muted); font-size: var(--fs-xs); font-weight: 650; }
diff --git a/tests/e2e/bidi.mjs b/tests/e2e/bidi.mjs
index adff41b..5b523a8 100644
--- a/tests/e2e/bidi.mjs
+++ b/tests/e2e/bidi.mjs
@@ -4,14 +4,15 @@ import {createServer} from "node:net";
import {tmpdir} from "node:os";
import {join} from "node:path";
-// Args: ; the plan lists steps {width, height, url, wait, probe, shot, gesture} and an optional
-// init like cdp.mjs.
+// Args: ; the plan lists steps {width, height, url, wait, keys, probe, shot, gesture} and an
+// optional init like cdp.mjs.
// Prints {results, errors}: one probe value per step and every JavaScript error the page logged.
const [binary, planFile] = process.argv.slice(2);
const plan = JSON.parse(readFileSync(planFile, "utf8"));
const profile = mkdtempSync(join(tmpdir(), "bidi-"));
writeFileSync(join(profile, "user.js"), ['user_pref("media.navigator.streams.fake", true);', 'user_pref("media.navigator.permission.disabled", true);', ""].join("\n"));
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
+const KEYS = {Enter: "", Tab: "", ArrowLeft: "", ArrowRight: ""};
const port = await new Promise((resolve) => {
const probe = createServer().listen(0, "127.0.0.1", () => {
const {port: free} = probe.address();
@@ -81,6 +82,10 @@ try {
await send("browsingContext.navigate", {context, url: step.url, wait: "complete"});
}
await sleep(step.wait);
+ for (const key of step.keys || []) {
+ await send("input.performActions", {context, actions: [{type: "key", id: "keyboard", actions: [{type: "keyDown", value: KEYS[key]}, {type: "keyUp", value: KEYS[key]}]}]});
+ await sleep(250);
+ }
let value = null;
if (step.probe) {
const answer = await send("script.evaluate", {expression: `(async () => JSON.stringify(await ${step.probe}))()`, target: {context}, awaitPromise: true, resultOwnership: "none", userActivation: Boolean(step.gesture)});
diff --git a/tests/e2e/cdp.mjs b/tests/e2e/cdp.mjs
index 381dc24..1eee960 100644
--- a/tests/e2e/cdp.mjs
+++ b/tests/e2e/cdp.mjs
@@ -4,7 +4,7 @@ import {tmpdir} from "node:os";
import {join} from "node:path";
// Args: ; the plan lists steps {width, height, url, wait, keys, probe, shot, media, gesture},
-// keys being real key presses (Enter, Tab) on the focused element before the probe and gesture running the probe with a
+// keys being real key presses (Enter, Tab, ArrowLeft, ArrowRight) on the focused element before the probe and gesture running the probe with a
// user activation like a click, an optional init, the text of a function every new document runs before its own scripts,
// and an optional authenticator {credentialId, privateKey, rpId}: base64 id and PKCS#8 key of a passkey the page may sign with.
// Prints {results, errors}: one probe value per step and every uncaught page exception.
@@ -17,7 +17,7 @@ const profile = mkdtempSync(join(tmpdir(), "cdp-"));
const flags = ["--headless", "--no-sandbox", "--disable-gpu", "--use-angle=swiftshader", "--enable-unsafe-swiftshader", "--ignore-certificate-errors", "--use-fake-device-for-media-stream", "--use-fake-ui-for-media-stream", "--no-first-run", "--no-default-browser-check", "--hide-scrollbars", `--user-data-dir=${profile}`, "--remote-debugging-port=0", "about:blank"];
const browser = spawn(binary, flags, {stdio: ["ignore", "ignore", "pipe"]});
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
-const KEYS = {Enter: [13, "\r"], Tab: [9, ""]};
+const KEYS = {Enter: [13, "\r"], Tab: [9, ""], ArrowLeft: [37, ""], ArrowRight: [39, ""]};
const address = await new Promise((resolve, reject) => {
let text = "";
diff --git a/tests/e2e/hub_e2e_views.py b/tests/e2e/hub_e2e_views.py
index 61d4277..62ddad1 100644
--- a/tests/e2e/hub_e2e_views.py
+++ b/tests/e2e/hub_e2e_views.py
@@ -25,8 +25,8 @@ PROBE = """(async () => {@HELPERS@
layouts: all('#main [aria-label="Layout"] button').map(name),
chips: all('#main [aria-label="Categories"] button').map(name),
menus: all("#main details.menu > summary").map(name),
- time: text(document.querySelector("#time > summary")),
- replay: Boolean(document.querySelector("#time .replay input[type=range]")),
+ time: text(document.querySelector("#rates .rate-window")),
+ replay: Boolean(document.querySelector("#rates .span-slider input[type=range]")),
custom: Boolean(document.querySelector('#main details.menu input[type="datetime-local"]')),
toolbar: all('#main [role="toolbar"] > *').map((node) => name(node.tagName === "DETAILS" ? node.querySelector("summary") : node.querySelector("button") || node)),
spot: (() => {
diff --git a/tests/e2e/test_orchestrator_e2e.py b/tests/e2e/test_orchestrator_e2e.py
index 0ffbd70..14d93de 100644
--- a/tests/e2e/test_orchestrator_e2e.py
+++ b/tests/e2e/test_orchestrator_e2e.py
@@ -144,7 +144,7 @@ class TestOrchestratorE2E(base.OrchestratorE2ECase):
self.assertEqual(probe["badges"], {"tab-home": None, "tab-stream": None, "tab-rules": None, "tab-agents": "1", "tab-artifacts": None, "tab-tasks": "2", "tab-resources": None})
self.assertEqual(probe["banner"], "MAYDAY · Docs: disk full")
self.assertFalse(probe["rail"])
- self.assertEqual((probe["time"], probe["replay"]), ("24 h ▾", True))
+ self.assertEqual((probe["time"], probe["replay"]), ("24 h · /h", True))
self.assertGreater(probe["icons"], 8)
self.assertEqual(labelled(probe), {"unlabelled": [], "broken": []})
self.assertFalse(probe["wide"], "the page scrolls sideways")
diff --git a/tests/e2e/test_rates_chip_e2e.py b/tests/e2e/test_rates_chip_e2e.py
index 8b31cda..8de89db 100644
--- a/tests/e2e/test_rates_chip_e2e.py
+++ b/tests/e2e/test_rates_chip_e2e.py
@@ -18,22 +18,24 @@ from hub_support import iso
OUT = Path(os.environ.get("E2E_OUT", "/tmp/hub-header-rates-shots"))
HOUR = 3600
+DAY = 86400
GIB = 1 << 30
-PERIODS = {1920: "day", 1366: "week", 390: "minute"}
-BEFORE = {1920: "hour", 1366: "day", 390: "week"}
-CHOSEN = {1920: ["tasks", "tokens", "commits"], 1366: ["tasks", "tokens", "cpu"], 390: ["tasks", "tokens", "commits"]}
-TASKS = {"minute": ("≈ 0.02/min", "true"), "hour": ("1/h", "false"), "day": ("2/d", "false"), "week": ("2/wk", "false"), "month": ("2/mo", "false"), "year": ("2/yr", "false"), "total": ("2", "false")}
METRICS = ["tasks", "commits", "krs", "tokens", "cpu", "ram", "vram", "swap", "disk"]
HEAD = ["Metric", "Chip", "min", "h", "d", "wk", "mo", "yr", "all"]
+PARTS = {"presets": ["1 h", "6 h", "24 h", "7 d", "all"], "thumbs": 2, "replay": True, "live": True, "range": ["From", "To"], "units": ["min", "h", "d", "wk", "mo", "yr", "all"], "head": HEAD}
READ = """
const chip = () => document.querySelector("#rates summary.rates-chip");
- const period = () => document.querySelector('#rates select[aria-label="Rate period"]');
const menu = () => document.querySelector("#rates details.rates-menu");
const box = () => document.querySelector("#rates .rates-box");
+ const thumb = (end) => document.querySelector(`#rates .span-slider .span-${end}`);
+ const unit = (short) => all("#rates .rates-units button").find((button) => text(button) === short);
+ const preset = (name) => all('#rates [aria-label="Time range"] button').find((button) => text(button) === name);
const pick = (id) => document.querySelector(`#rates tr[data-metric="${id}"] input[type="checkbox"]`);
- const metrics = () => all("#rates summary .rate").map((node) => node.dataset.metric);
- const values = () => Object.fromEntries(all("#rates summary .rate").map((node) => [node.dataset.metric, text(node)]));
- const loaded = () => Boolean(chip() && metrics().length && !text(chip()).includes("…"));
+ const shown = () => all("#rates summary .rate").filter((node) => !node.hidden).map((node) => node.dataset.metric);
+ const value = (id) => text(document.querySelector(`#rates summary .rate[data-metric="${id}"] .rate-value`));
+ const cell = (id, period) => text(document.querySelector(`#rates tr[data-metric="${id}"] td[data-period="${period}"]`));
+ const windowText = () => text(document.querySelector("#rates .rate-window"));
+ const loaded = () => Boolean(chip() && shown().length && !text(chip()).includes("…"));
const stored = async () => (await (await fetch("/api/state", {cache: "no-store"})).json()).settings.rates;
const storedUntil = async (test, ms) => {
const end = Date.now() + ms;
@@ -46,67 +48,125 @@ READ = """
}
return null;
};
+ if (!window.e2eAsked) {
+ window.e2eAsked = [];
+ const send = window.fetch.bind(window);
+ window.fetch = (path, init) => {
+ window.e2eAsked.push(String(path));
+ return send(path, init);
+ };
+ }
+ const asked = (path) => {
+ const hits = window.e2eAsked.map((entry) => new URL(entry, location.href)).filter((url) => url.pathname === path);
+ const last = hits[hits.length - 1];
+ return last ? {from: Number(last.searchParams.get("from")), to: last.searchParams.has("to") ? Number(last.searchParams.get("to")) : null} : null;
+ };
const inside = (node) => {
const rect = node.getBoundingClientRect();
return rect.width > 0 && rect.left >= -1 && rect.right <= innerWidth + 1;
};
const fits = () => {
const bar = document.querySelector(".navbar");
- return {wide: document.documentElement.scrollWidth > innerWidth + 1, bar: bar.scrollWidth > bar.clientWidth + 1, chip: inside(chip()), period: inside(period())};
+ const spill = menu().open && box().getClientRects().length ? box().scrollWidth > box().clientWidth + 1 : false;
+ return {wide: document.documentElement.scrollWidth > innerWidth + 1, bar: bar.scrollWidth > bar.clientWidth + 1, chip: inside(chip()), spill};
+ };
+ const opened = async () => {
+ if (!menu().open) {
+ chip().click();
+ }
+ return Boolean(await until(() => menu().open && box().getClientRects().length, 4000));
};
"""
-CHOOSE = """(async () => {@HELPERS@@READ@
+PANEL = """(async () => {@HELPERS@@READ@
if (!(await until(loaded, 15000))) {
return {error: text(document.getElementById("rates"))};
}
- const before = {period: period().value, metrics: metrics()};
- period().value = "@PERIOD@";
- period().dispatchEvent(new Event("change", {bubbles: true}));
- const saved = await storedUntil((found) => found.period === "@PERIOD@", 8000);
- await until(() => period().value === "@PERIOD@" && loaded(), 4000);
- return {before, saved, after: {period: period().value, metrics: metrics(), values: values(), label: chip().getAttribute("aria-label")}, fits: fits(), open: menu().open, violations: window.e2eViolations || null};
+ const header = {time: Boolean(document.getElementById("time")), select: all(".navbar select").length, window: windowText()};
+ const open = await opened();
+ const parts = {
+ presets: all('#rates [aria-label="Time range"] button').map(text), thumbs: all("#rates .span-slider input[type=range]").length,
+ replay: Boolean(document.querySelector('#rates .replay button[aria-label="Replay"]')), live: Boolean(all("#rates .replay button").find((button) => text(button) === "Live")),
+ range: all('#rates .custom input[type="datetime-local"]').map((input) => input.getAttribute("aria-label")), units: all("#rates .rates-units button").map(text),
+ head: all("#rates .rates-table thead th").map(text),
+ };
+ unit("d").click();
+ const saved = await storedUntil((found) => found.period === "day", 8000);
+ preset("7 d").click();
+ const week = Boolean(await until(() => windowText() === "7 d · /d" && loaded() && value("tokens").endsWith("/d"), 8000));
+ await sleep(600);
+ const tokens = {chip: value("tokens"), cell: cell("tokens", "day"), current: all('#rates td[aria-current="true"]').map((node) => node.dataset.period)};
+ const rates = asked("/api/rates");
+ thumb("from").focus();
+ return {header, open, parts, saved, week, tokens, rates, now: Date.now() / 1000, from: Number(thumb("from").value), focused: document.activeElement === thumb("from"), fits: fits(), violations: window.e2eViolations || null};
})()"""
-OPEN = """(async () => {@HELPERS@@READ@
+STEPPED = """(async () => {@HELPERS@@READ@
+ await sleep(800);
+ const stepped = {from: Number(thumb("from").value), to: Number(thumb("to").value)};
+ thumb("from").value = String(Number(thumb("to").value) - 1);
+ thumb("from").dispatchEvent(new Event("input", {bubbles: true}));
+ await sleep(800);
+ thumb("from").focus();
+ return {stepped, near: {from: Number(thumb("from").value), to: Number(thumb("to").value)}, focused: document.activeElement === thumb("from")};
+})()"""
+BLOCKED = """(async () => {@HELPERS@@READ@
+ await sleep(800);
+ const blocked = {from: Number(thumb("from").value), to: Number(thumb("to").value)};
+ thumb("from").value = "100";
+ thumb("from").dispatchEvent(new Event("input", {bubbles: true}));
+ thumb("to").value = "800";
+ thumb("to").dispatchEvent(new Event("input", {bubbles: true}));
+ const past = Boolean(await until(() => windowText() === "7 d · past · /d" && loaded(), 8000));
+ await sleep(800);
+ const label = text(document.querySelector("#rates .window-label"));
+ return {blocked, past, label, rates: asked("/api/rates"), state: asked("/api/state"), now: Date.now() / 1000, fits: fits(), violations: window.e2eViolations || null};
+})()"""
+REPLAY = """(async () => {@HELPERS@@READ@
+ const play = () => document.querySelector("#rates .replay button.icon-only");
+ const liveButton = () => all("#rates .replay button").find((button) => text(button) === "Live");
+ play().click();
+ await sleep(1500);
+ play().click();
+ await sleep(800);
+ const paused = text(document.querySelector("#rates .window-label"));
+ await sleep(1500);
+ const held = text(document.querySelector("#rates .window-label")) === paused && !liveButton().disabled;
+ play().click();
+ const ended = Boolean(await until(() => liveButton().disabled && text(document.querySelector("#rates .window-label")).endsWith("· live"), 45000));
+ return {paused: !paused.endsWith("· live"), held, ended, window: windowText(), playing: play().getAttribute("aria-label"), violations: window.e2eViolations || null};
+})()"""
+NINE = """(async () => {@HELPERS@@READ@
+ for (const id of @METRICS@) {
+ if (!pick(id).checked) {
+ pick(id).click();
+ await storedUntil((found) => found.metrics.includes(id), 8000);
+ await until(() => pick(id) && pick(id).checked, 4000);
+ }
+ }
+ menu().open = false;
+ await until(() => !menu().open, 2000);
+ window.dispatchEvent(new Event("resize"));
+ await sleep(400);
+ const more = document.querySelector("#rates summary .rate-more");
+ return {stored: (await stored()).metrics.length, shown: shown().length, more: more.hidden ? null : text(more), fits: fits(), violations: window.e2eViolations || null};
+})()"""
+KEPT = """(async () => {@HELPERS@@READ@
if (!(await until(loaded, 15000))) {
return {error: text(document.getElementById("rates"))};
}
- const kept = {period: period().value, metrics: metrics(), values: values()};
- chip().click();
- const opened = Boolean(await until(() => menu().open && box().getClientRects().length, 4000));
- const head = all("#rates .rates-table thead th").map(text);
- const rows = all("#rates .rates-table tbody tr").map((row) => [row.dataset.metric, all("td[data-period]", row).length]);
- const current = all('#rates .rates-table td[aria-current="true"]').map((cell) => cell.dataset.period);
- const tasks = Object.fromEntries(all('#rates tr[data-metric="tasks"] td[data-period]').map((cell) => [cell.dataset.period, [text(cell), cell.dataset.approx]]));
- const hint = document.querySelector('#rates tr[data-metric="tasks"] td[data-period="minute"]').title;
- const [drop, add] = metrics().includes("cpu") ? ["cpu", "commits"] : ["commits", "cpu"];
- const locked = pick(add).disabled;
- pick(drop).click();
- await storedUntil((found) => !found.metrics.includes(drop), 8000);
- await until(() => !metrics().includes(drop) && pick(add) && !pick(add).disabled, 4000);
- const fewest = all("#rates tbody input:checked").map((input) => input.disabled);
- pick(add).click();
- const swapped = await storedUntil((found) => found.metrics.includes(add), 8000);
- await until(() => metrics().includes(add), 4000);
- return {kept, opened, head, rows, current, tasks, hint, locked, fewest, swapped, chip: metrics(), box: inside(box()), fits: fits(), open: menu().open, violations: window.e2eViolations || null};
-})()"""
-CLOSE = """(async () => {@HELPERS@@READ@
- const was = menu().open;
+ const kept = {window: windowText(), metrics: (await stored()).metrics.length, unit: all('#rates .rates-units button[aria-pressed="true"]').map(text)};
+ await opened();
document.body.dispatchEvent(new KeyboardEvent("keydown", {key: "Escape", bubbles: true}));
const escaped = Boolean(await until(() => !menu().open, 3000));
chip().click();
const reopened = Boolean(await until(() => menu().open, 3000));
- chip().click();
- const clicked = Boolean(await until(() => !menu().open, 3000));
- chip().click();
- await until(() => menu().open, 3000);
document.getElementById("main").click();
const outside = Boolean(await until(() => !menu().open, 3000));
- return {was, escaped, reopened, clicked, outside, fits: fits(), violations: window.e2eViolations || null};
+ return {kept, escaped, reopened, outside, fits: fits(), violations: window.e2eViolations || null};
})()"""
-def probe(template: str, width: int) -> str:
- names = {"@HELPERS@": base.HELPERS, "@READ@": READ, "@PERIOD@": PERIODS[width]}
+def probe(template: str) -> str:
+ names = {"@HELPERS@": base.HELPERS, "@READ@": READ, "@METRICS@": json.dumps(METRICS)}
for key, value in names.items():
template = template.replace(key, value)
return template
@@ -117,6 +177,10 @@ def sample(t: int) -> dict:
return {"t": t, "disks": [{"mount": "/", "used": 400 << 30, "total": 1000 << 30}], "cpu": 25.0, "ram": 40.0, "swap": 5.0, "load": 1.0, "gpus": gpus, "queue": {"waiting": 0, "active": 0}}
+def token(t: int, amount: int) -> dict:
+ return {"t": t, "agent": "Skills", "model": "claude-opus-5-5", "input": amount - 400_000, "output": 0, "cache_read": 400_000, "cache_write": 0}
+
+
@unittest.skipUnless(shutil.which(os.environ.get("CHROMIUM", "chromium")), "no Chromium")
class TestRatesChip(HubCase):
drive = base.OrchestratorE2ECase.drive
@@ -129,13 +193,15 @@ class TestRatesChip(HubCase):
folder = self.kept / "resources"
folder.mkdir()
(folder / "samples.jsonl").write_text(self.jsonl([sample(now - 2 * HOUR + step * 60) for step in range(140)]), encoding="utf-8")
- tokens = [{"t": t, "agent": "Skills", "model": "claude-opus-5-5", "input": 4000, "output": 1000, "cache_read": 20000, "cache_write": 500} for t in (hour - HOUR, hour)]
+ tokens = [token(hour - 9 * DAY, 500_000), *(token(hour - day * DAY, 1_000_000) for day in range(7))]
(folder / "tokens.jsonl").write_text(self.jsonl(tokens), encoding="utf-8")
- self.write_council({"rasci.json": {
- "work/first": {"title": "First", "status": "done", "worker": "Skills", "start": iso(now - 5 * HOUR), "end": iso(now - 3 * HOUR), "roles": {"Skills": "RA"}},
- "work/second": {"title": "Second", "status": "done", "worker": "Skills", "start": iso(now - 2 * HOUR), "end": iso(now - 1200), "roles": {"Skills": "RA"}, "key_results": [{"text": "shipped", "met": True, "met_at": iso(now - 1300)}]},
- "work/third": {"title": "Third", "status": "running", "worker": "Stage", "start": iso(now - HOUR), "roles": {"Stage": "RA"}},
- }})
+ self.write_council({
+ "rasci.json": {
+ "work/first": {"title": "First", "status": "done", "worker": "Skills", "start": iso(now - 5 * HOUR), "end": iso(now - 3 * HOUR), "roles": {"Skills": "RA"}},
+ "work/second": {"title": "Second", "status": "done", "worker": "Skills", "start": iso(now - 2 * HOUR), "end": iso(now - 1200), "roles": {"Skills": "RA"}, "key_results": [{"text": "shipped", "met": True, "met_at": iso(now - 1300)}]},
+ },
+ "news.jsonl": self.jsonl([{"ts": now - 10 * DAY, "from": "Skills", "to": [], "text": "the oldest record", "class": "post", "v": 2}]),
+ })
(self.council / "scores.jsonl").write_text(json.dumps({"time": now - 2 * HOUR, "remote": "example.org/skills", "path": "/repo", "branch": "main", "sha": "d" * 40, "values": {"score": 90}, "digest": "0" * 64}) + "\n", encoding="utf-8")
self.base = f"http://{self.host}/"
@@ -144,50 +210,65 @@ class TestRatesChip(HubCase):
for width, height in base.WIDTHS:
shot = OUT / f"{browser}-{width}"
steps += [
- {"width": width, "height": height, "url": f"{self.base}?{width}#home", "wait": 1200, "probe": probe(CHOOSE, width), "shot": f"{shot}-chip.png"},
- {"width": width, "height": height, "url": f"{self.base}?{width}-reloaded#home", "wait": 1200, "probe": probe(OPEN, width), "shot": f"{shot}-popover.png"},
- {"width": width, "height": height, "url": None, "wait": 0, "probe": probe(CLOSE, width), "shot": None},
+ {"width": width, "height": height, "url": f"{self.base}?{width}#home", "wait": 1200, "probe": probe(PANEL), "shot": f"{shot}-panel.png"},
+ {"width": width, "height": height, "url": None, "wait": 0, "keys": ["ArrowRight"], "probe": probe(STEPPED), "shot": None},
+ {"width": width, "height": height, "url": None, "wait": 0, "keys": ["ArrowRight"], "probe": probe(BLOCKED), "shot": f"{shot}-past.png"},
+ {"width": width, "height": height, "url": None, "wait": 0, "probe": probe(REPLAY), "shot": None},
+ {"width": width, "height": height, "url": None, "wait": 0, "probe": probe(NINE), "shot": f"{shot}-nine.png"},
+ {"width": width, "height": height, "url": f"{self.base}?{width}-reloaded#home", "wait": 1200, "probe": probe(KEPT), "shot": None},
]
return steps
def check(self, data: dict, browser: str) -> None:
results = iter(data["results"])
- fitting = {"wide": False, "bar": False, "chip": True, "period": True}
- previous = ["tasks", "tokens", "cpu"]
+ fitting = {"wide": False, "bar": False, "chip": True, "spill": False}
for width, _ in base.WIDTHS:
- chosen, opened, closed = next(results), next(results), next(results)
- chip = {"day": "2/d", "week": "2/wk", "minute": "≈ 0.02/min"}[PERIODS[width]]
+ panel, stepped, blocked, replay, nine, kept = (next(results) for _ in range(6))
with self.subTest(browser=browser, width=width):
- self.assertNotIn("error", chosen, chosen)
- self.assertEqual(chosen["before"], {"period": BEFORE[width], "metrics": previous})
- self.assertEqual(chosen["saved"], {"period": PERIODS[width], "metrics": previous})
- self.assertEqual((chosen["after"]["period"], chosen["after"]["metrics"], chosen["after"]["values"]["tasks"]), (PERIODS[width], previous, chip))
- self.assertTrue(chosen["after"]["label"].startswith("Rates per "), chosen["after"])
- self.assertEqual((chosen["fits"], chosen["open"]), (fitting, False))
- self.assertEqual(w4.quiet(chosen["violations"]), [])
- self.assertNotIn("error", opened, opened)
- self.assertEqual((opened["kept"]["period"], opened["kept"]["metrics"], opened["kept"]["values"]["tasks"]), (PERIODS[width], previous, chip))
- self.assertTrue(opened["opened"], opened)
- self.assertEqual((opened["head"], opened["rows"], opened["current"]), (HEAD, [[metric, 7] for metric in METRICS], [PERIODS[width]] * len(METRICS)))
- self.assertEqual({period: tuple(cell) for period, cell in opened["tasks"].items()}, TASKS)
- self.assertIn("scaled from the last hour", opened["hint"])
- self.assertEqual((opened["locked"], opened["fewest"]), (True, [True, True]))
- self.assertEqual((opened["swapped"], opened["chip"]), ({"period": PERIODS[width], "metrics": CHOSEN[width]}, CHOSEN[width]))
- self.assertEqual((opened["box"], opened["fits"], opened["open"]), (True, fitting, True))
- self.assertEqual(w4.quiet(opened["violations"]), [])
- self.assertEqual({key: closed[key] for key in ("was", "escaped", "reopened", "clicked", "outside")}, dict.fromkeys(("was", "escaped", "reopened", "clicked", "outside"), True))
- self.assertEqual((closed["fits"], w4.quiet(closed["violations"])), (fitting, []))
- previous = CHOSEN[width]
- self.assertEqual(self.httpd.hub.settings.get()["rates"], {"period": PERIODS[390], "metrics": CHOSEN[390]})
+ self.assertNotIn("error", panel, panel)
+ self.assertEqual(panel["header"]["time"], False)
+ self.assertEqual(panel["header"]["select"], 0)
+ self.assertTrue(panel["open"], panel)
+ self.assertEqual(panel["parts"], PARTS)
+ self.assertEqual(panel["saved"]["period"], "day", panel)
+ self.assertTrue(panel["week"], panel)
+ self.assertEqual(panel["tokens"], {"chip": "1.0M/d", "cell": "1.0M/d", "current": ["day"] * len(METRICS)}, panel)
+ self.assertLess(abs(panel["rates"]["from"] - (panel["now"] - 7 * DAY)), 120, panel["rates"])
+ self.assertEqual((panel["focused"], panel["fits"], w4.quiet(panel["violations"])), (True, fitting, []))
+ self.assertEqual(stepped["stepped"], {"from": panel["from"] + 1, "to": 1000}, stepped)
+ self.assertEqual((stepped["near"], stepped["focused"]), ({"from": 999, "to": 1000}, True), stepped)
+ self.assertEqual(blocked["blocked"], {"from": 999, "to": 1000}, blocked)
+ self.assertTrue(blocked["past"], blocked)
+ self.assertIn(" – ", blocked["label"])
+ self.assertFalse(blocked["label"].endswith("· live"), blocked)
+ self.assertEqual(panel["from"], 300, panel)
+ for asked in (blocked["rates"], blocked["state"]):
+ self.assertLess(abs(asked["from"] - (blocked["now"] - 9 * DAY)), 0.01 * 10 * DAY, asked)
+ self.assertLess(abs(asked["to"] - (blocked["now"] - 2 * DAY)), 0.01 * 10 * DAY, asked)
+ self.assertEqual((blocked["fits"], w4.quiet(blocked["violations"])), (fitting, []))
+ self.assertEqual({key: replay[key] for key in ("paused", "held", "ended", "playing")}, {"paused": True, "held": True, "ended": True, "playing": "Replay"}, replay)
+ self.assertEqual(w4.quiet(replay["violations"]), [])
+ self.assertEqual(nine["stored"], 9)
+ if width == 1920:
+ self.assertEqual((nine["shown"], nine["more"]), (9, None), nine)
+ if width == 390:
+ self.assertLess(nine["shown"], 9, nine)
+ self.assertEqual(nine["more"], f"+{9 - nine['shown']}", nine)
+ self.assertEqual((nine["fits"], w4.quiet(nine["violations"])), (fitting, []))
+ self.assertNotIn("error", kept, kept)
+ self.assertEqual(kept["kept"], {"window": "24 h · /d", "metrics": 9, "unit": ["d"]})
+ self.assertEqual({key: kept[key] for key in ("escaped", "reopened", "outside")}, dict.fromkeys(("escaped", "reopened", "outside"), True))
+ self.assertEqual((kept["fits"], w4.quiet(kept["violations"])), (fitting, []))
+ self.assertEqual(self.httpd.hub.settings.get()["rates"], {"period": "day", "metrics": ["tasks", "tokens", "cpu", "commits", "krs", "ram", "vram", "swap", "disk"]})
- def test_chromium_shows_the_rates_chip_keeps_its_period_over_a_reload_and_opens_and_closes_the_table_at_three_widths(self):
+ def test_chromium_moves_the_time_range_and_the_rate_unit_into_the_rates_panel_at_three_widths(self):
data = self.drive(self.steps("chromium"), None, init=w4.GUARD)
(OUT / "chromium-rates.json").write_text(json.dumps(data, indent=1), encoding="utf-8")
self.assertEqual(data["errors"], [])
self.check(data, "chromium")
@unittest.skipUnless(shutil.which(os.environ.get("FIREFOX", "firefox")), "no Firefox")
- def test_firefox_shows_the_rates_chip_keeps_its_period_over_a_reload_and_opens_and_closes_the_table_at_three_widths(self):
+ def test_firefox_moves_the_time_range_and_the_rate_unit_into_the_rates_panel_at_three_widths(self):
data = self.drive(self.steps("firefox"), None, "bidi.mjs", os.environ.get("FIREFOX", "firefox"), init=w4.GUARD)
(OUT / "firefox-rates.json").write_text(json.dumps(data, indent=1), encoding="utf-8")
self.assertEqual([error for error in data["errors"] if "/favicon.ico" not in error], [])
diff --git a/tests/test_hub_rates.py b/tests/test_hub_rates.py
index f9434ba..fe2d6c6 100644
--- a/tests/test_hub_rates.py
+++ b/tests/test_hub_rates.py
@@ -18,7 +18,8 @@ HOUR = 3600
DAY = 86400
GIB = 1 << 30
NOW = 1_800_000_000 + 1800
-EXACT_ZERO = {"value": 0.0, "approx": False, "source": None}
+LAST_HOUR = NOW - NOW % HOUR
+NOTHING = {"value": None, "approx": False, "covered": None}
def token(t: int, given: int, cached: int) -> dict:
@@ -31,6 +32,10 @@ def sample(t: float, cpu: float, span: int | None = None, vram: tuple[int, int]
return row if span is None else {**row, "span": span}
+def per_unit(amount: float, covered: float, approx: bool) -> dict[str, dict]:
+ return {name: {"value": rates.precise(amount if seconds is None else amount * seconds / covered), "approx": approx and seconds is not None, "covered": covered} for name, seconds in rates.PERIODS}
+
+
class TestRates(unittest.TestCase):
def setUp(self):
tmp = tempfile.TemporaryDirectory()
@@ -46,86 +51,88 @@ class TestRates(unittest.TestCase):
def lines(self, path: Path, rows: list[dict]) -> None:
path.write_text("".join(json.dumps(row) + "\n" for row in rows), encoding="utf-8")
- def rated(self) -> dict:
- return rates.rated(self.council, self.folder.parent, NOW)
+ def rated(self, begin: float, end: float) -> dict:
+ return rates.rated(self.council, self.folder.parent, NOW, (begin, end))
- def column(self, metric: str) -> dict:
- found = self.rated()
+ def column(self, metric: str, begin: float, end: float) -> dict:
+ found = self.rated(begin, end)
return {period: found["rates"][period][metric] for period in rates.PERIOD_NAMES}
- def test_done_tasks_count_in_every_window_their_end_lies_in_and_total_counts_each_done_task(self):
- ends = [NOW - 30, NOW - 1800, NOW - 2 * DAY, NOW - 20 * DAY, NOW - 200 * DAY, NOW - 800 * DAY]
+ def test_a_week_of_seven_million_tokens_gives_a_million_a_day_seven_a_week_and_seven_in_all(self):
+ self.lines(self.folder / "tokens.jsonl", [token(LAST_HOUR - 10 * DAY, 5, 0), token(LAST_HOUR - 2 * DAY, 6_000_000, 1_000_000)])
+ tokens = self.column("tokens", NOW - 7 * DAY, NOW)
+ self.assertEqual(tokens, per_unit(7_000_000, 7 * DAY, False))
+ self.assertEqual((tokens["day"]["value"], tokens["week"]["value"], tokens["total"]["value"], tokens["month"]["value"]), (1_000_000.0, 7_000_000.0, 7_000_000.0, 30_000_000.0))
+
+ def test_done_tasks_key_results_and_commits_count_inside_the_window_and_scale_per_unit(self):
+ ends = [NOW - 30, NOW - 1800, NOW - 2 * DAY, NOW - 20 * DAY]
tasks = {f"work/t{index}": {"status": "done", "end": iso(end)} for index, end in enumerate(ends)}
others = {"status": "done", "end": iso(NOW - 10)}
- self.register({**tasks, "work/legacy": {"status": "done"}, "work/open": {**others, "status": "running"}, "work/halted": {**others, "status": "stopped"}, "operator/run": others})
- counts = dict(zip(rates.PERIOD_NAMES, (1.0, 2.0, 2.0, 3.0, 4.0, 5.0, 7.0), strict=True))
- self.assertEqual(self.column("tasks"), {period: {"value": value, "approx": False, "source": None} for period, value in counts.items()})
-
- def test_an_empty_window_takes_the_shortest_longer_window_with_events_scaled_down_and_marked_approx(self):
- results = [{"text": "met", "met": True, "met_at": iso(NOW - 1800)}, {"text": "open", "met": False}]
- self.register({"work/a": {"status": "running", "key_results": results}})
+ results = [{"text": "met", "met": True, "met_at": iso(NOW - 600)}, {"text": "old", "met": True}, {"text": "open", "met": False}]
+ self.register({**tasks, "work/legacy": {"status": "done"}, "work/open": {**others, "status": "running", "key_results": results}, "work/halted": {**others, "status": "stopped"}, "operator/run": others})
commit = {"time": NOW - 5 * HOUR, "remote": "example.org/repo", "sha": "a" * 40, "values": {}}
- self.lines(self.council / "scores.jsonl", [commit, commit, {**commit, "sha": None, "time": NOW - 60}, {**commit, "sha": "b" * 40, "values": None}])
- krs = self.column("krs")
- self.assertEqual(krs["minute"], {"value": 0.01667, "approx": True, "source": "hour"})
- self.assertEqual([krs[period] for period in rates.PERIOD_NAMES[1:]], [{"value": 1.0, "approx": False, "source": None}] * 6)
- commits = self.column("commits")
- self.assertEqual(commits["minute"], {"value": 0.0006944, "approx": True, "source": "day"})
- self.assertEqual(commits["hour"], {"value": 0.04167, "approx": True, "source": "day"})
- self.assertEqual([commits[period] for period in rates.PERIOD_NAMES[2:]], [{"value": 1.0, "approx": False, "source": None}] * 5)
+ self.lines(self.council / "scores.jsonl", [commit, commit, {**commit, "sha": None, "time": NOW - 60}, {**commit, "sha": "b" * 40, "time": NOW - 3 * DAY}])
+ day = self.rated(NOW - DAY, NOW)["rates"]
+ self.assertEqual({period: day[period]["tasks"] for period in rates.PERIOD_NAMES}, per_unit(2, DAY, False))
+ self.assertEqual({period: day[period]["krs"] for period in rates.PERIOD_NAMES}, per_unit(1, DAY, False))
+ self.assertEqual({period: day[period]["commits"] for period in rates.PERIOD_NAMES}, per_unit(1, DAY, False))
+ past = self.rated(NOW - 3 * DAY, NOW - DAY)["rates"]
+ self.assertEqual((past["total"]["tasks"]["value"], past["total"]["commits"]["value"], past["day"]["tasks"]["value"]), (1.0, 1.0, 0.5))
- def test_key_results_met_before_met_at_existed_count_only_in_total_and_scale_from_there(self):
- self.register({"work/a": {"status": "running", "key_results": [{"text": "a", "met": True}]}})
- self.lines(self.folder / "samples.jsonl", [sample(NOW - 10 * DAY, 5.0)])
- krs = self.column("krs")
- self.assertEqual(krs["total"], {"value": 1.0, "approx": False, "source": None})
- self.assertEqual(krs["year"], {"value": 1.0, "approx": True, "source": "total"})
- self.assertEqual(krs["week"], {"value": 0.7, "approx": True, "source": "total"})
- self.assertEqual(krs["minute"], {"value": 6.944e-05, "approx": True, "source": "total"})
+ def test_a_window_reaching_before_the_first_token_row_divides_by_the_covered_part_and_marks_the_rates_approx(self):
+ self.register({"work/old": {"status": "done", "end": iso(NOW - 30 * DAY)}})
+ self.lines(self.folder / "tokens.jsonl", [token(LAST_HOUR - 2 * DAY, 4000, 800)])
+ tokens = self.column("tokens", NOW - 7 * DAY, NOW)
+ covered = NOW - (LAST_HOUR - 2 * DAY)
+ self.assertEqual(tokens, per_unit(4800, covered, True))
+ self.assertEqual(tokens["total"], {"value": 4800.0, "approx": False, "covered": covered})
+ self.assertEqual(self.column("tasks", NOW - 7 * DAY, NOW)["day"], {"value": 0.0, "approx": False, "covered": 7 * DAY})
- def test_tokens_share_their_hour_by_overlap_and_the_minute_always_takes_a_longer_window(self):
- hour = NOW - NOW % HOUR
- self.lines(self.folder / "tokens.jsonl", [token(hour, 1000, 800), token(hour - HOUR, 3000, 600), token(hour - 20 * DAY, 7, 0)])
- tokens = self.column("tokens")
- self.assertEqual(tokens["minute"], {"value": 60.0, "approx": True, "source": "hour"})
- self.assertEqual(tokens["hour"], {"value": 3600.0, "approx": False, "source": None})
- self.assertEqual((tokens["day"]["value"], tokens["month"]["value"], tokens["total"]["value"]), (5400.0, 5407.0, 5407.0))
+ def test_a_window_before_every_record_holds_no_value(self):
+ self.register({"work/a": {"status": "done", "end": iso(NOW - DAY)}})
+ self.lines(self.folder / "tokens.jsonl", [token(LAST_HOUR - DAY, 100, 0)])
+ self.lines(self.folder / "samples.jsonl", [sample(NOW - DAY, 40.0)])
+ found = self.rated(NOW - 9 * DAY, NOW - 8 * DAY)
+ self.assertEqual({found["rates"][period][kind] == NOTHING for period in rates.PERIOD_NAMES for kind in rates.COUNTS}, {True})
+ self.assertIsNone(found["rates"]["day"]["cpu"]["value"])
+ self.assertEqual((found["from"], found["to"], found["since"]), (NOW - 9 * DAY, NOW - 8 * DAY, LAST_HOUR - DAY))
- def test_an_empty_hour_takes_the_day_for_tokens_and_so_does_the_minute(self):
- hour = NOW - NOW % HOUR
- self.lines(self.folder / "tokens.jsonl", [token(hour - 5 * HOUR, 8000, 640)])
- tokens = self.column("tokens")
- self.assertEqual(tokens["minute"], {"value": 6.0, "approx": True, "source": "day"})
- self.assertEqual(tokens["hour"], {"value": 360.0, "approx": True, "source": "day"})
- self.assertEqual(tokens["day"], {"value": 8640.0, "approx": False, "source": None})
+ def test_a_window_shorter_than_an_hour_shares_the_token_hour_and_marks_every_token_rate_approx(self):
+ self.lines(self.folder / "tokens.jsonl", [token(LAST_HOUR, 1000, 800)])
+ tokens = self.column("tokens", NOW - 600, NOW)
+ self.assertEqual(tokens["minute"], {"value": 60.0, "approx": True, "covered": 600})
+ self.assertEqual(tokens["total"], {"value": 600.0, "approx": True, "covered": 600})
- def test_gauges_average_each_window_weighting_raw_rows_fifteen_minute_means_and_daily_means_by_their_span(self):
+ def test_gauges_average_the_window_weighting_each_row_by_its_sampled_seconds_inside_on_both_ends(self):
+ start = NOW - 10 * DAY - (NOW - 10 * DAY) % DAY
self.lines(self.folder / "samples.jsonl", [
- sample(NOW - 500 * DAY, 0.0, span=DAY),
- sample(NOW - 200 * DAY, 90.0, span=DAY),
- sample(NOW - 10 * DAY, 50.0, span=900),
- sample(NOW - 90, 30.0, vram=(4, 8), boot=70),
- sample(NOW - 30, 10.0, vram=(2, 8), boot=90),
+ {**sample(start, 40.0), "span": 3600, "width": DAY},
+ {**sample(NOW - 2 * DAY - 300, 70.0, boot=80), "span": 600, "width": 900},
+ sample(NOW - 30, 10.0, boot=20),
])
- found = self.rated()["rates"]
- self.assertEqual([found[period]["cpu"]["value"] for period in rates.PERIOD_NAMES], [10.0, 20.0, 20.0, 20.0, 46.5, 89.5, 45.0])
- self.assertEqual([found[period]["ram"]["value"] for period in ("minute", "hour")], [11.0, 21.0])
- self.assertEqual(found["minute"]["vram"], {"value": 25.0, "used": 2 * GIB, "approx": False, "source": None})
- self.assertEqual((found["hour"]["vram"]["value"], found["hour"]["vram"]["used"], found["month"]["vram"]["value"]), (37.5, 3 * GIB, 37.5))
- self.assertEqual(found["minute"]["disk"], {"value": 90.0, "mount": "/boot", "used": 90, "total": 100, "approx": False, "source": None})
- self.assertEqual((found["hour"]["disk"]["value"], found["month"]["disk"]["value"], found["month"]["disk"]["used"]), (80.0, 53.5, 54))
- self.assertEqual(self.rated()["since"], NOW - 500 * DAY)
+ found = self.rated(start + DAY / 2, NOW - 2 * DAY)["rates"]
+ self.assertEqual({found[period]["cpu"]["value"] for period in rates.PERIOD_NAMES}, {round((1800 * 40 + 200 * 70) / 2000, 1)})
+ self.assertEqual(found["hour"]["disk"], {"value": round((1800 * 50 + 200 * 80) / 2000, 1), "mount": "/boot", "used": 53, "total": 100, "approx": False})
+ self.assertEqual(found["week"]["vram"], {"value": None, "used": None, "approx": False})
- def test_a_window_without_a_sample_shows_no_gauge_and_kinds_without_any_event_count_an_exact_zero(self):
+ def test_gauges_of_the_last_minutes_read_vram_and_the_fullest_disk(self):
+ self.lines(self.folder / "samples.jsonl", [sample(NOW - 90, 30.0, vram=(4, 8), boot=70), sample(NOW - 30, 10.0, vram=(2, 8), boot=90)])
+ last = self.rated(NOW - 60, NOW)["rates"]["minute"]
+ self.assertEqual((last["cpu"]["value"], last["ram"]["value"]), (10.0, 11.0))
+ self.assertEqual(last["vram"], {"value": 25.0, "used": 2 * GIB, "approx": False})
+ self.assertEqual(last["disk"], {"value": 90.0, "mount": "/boot", "used": 90, "total": 100, "approx": False})
+ both = self.rated(NOW - 120, NOW)["rates"]["hour"]
+ self.assertEqual((both["cpu"]["value"], both["vram"]["value"], both["vram"]["used"], both["disk"]["value"]), (20.0, 37.5, 3 * GIB, 80.0))
+
+ def test_a_window_without_a_sample_shows_no_gauge_and_kinds_with_records_but_none_inside_count_an_exact_zero(self):
self.lines(self.folder / "samples.jsonl", [sample(NOW - 2 * HOUR, 40.0)])
- found = self.rated()
- self.assertEqual([found["rates"][period]["cpu"]["value"] for period in rates.PERIOD_NAMES], [None, None, 40.0, 40.0, 40.0, 40.0, 40.0])
- self.assertEqual(found["rates"]["hour"]["vram"], {"value": None, "used": None, "approx": False, "source": None})
- self.assertEqual(found["rates"]["minute"]["disk"], {"value": None, "mount": None, "used": None, "total": None, "approx": False, "source": None})
- self.assertEqual({found["rates"][period][kind] == EXACT_ZERO for period in rates.PERIOD_NAMES for kind in rates.COUNTS}, {True})
- self.assertEqual((found["now"], found["since"], [entry["name"] for entry in found["periods"]]), (NOW, NOW - 2 * HOUR, list(rates.PERIOD_NAMES)))
+ found = self.rated(NOW - HOUR, NOW)["rates"]
+ self.assertIsNone(found["day"]["cpu"]["value"])
+ self.assertEqual(found["minute"]["disk"], {"value": None, "mount": None, "used": None, "total": None, "approx": False})
+ self.assertEqual({found[period][kind]["value"] for period in rates.PERIOD_NAMES for kind in ("tasks", "commits", "krs")}, {0.0})
+ self.assertEqual(found["day"]["tokens"], NOTHING)
- def test_folded_sampler_rows_feed_the_windows_with_the_seconds_they_sampled(self):
+ def test_folded_sampler_rows_feed_the_window_with_the_seconds_they_sampled(self):
old = NOW - NOW % DAY - 100 * DAY
raw = [sample(old + 3600 + index * 60, 20.0) for index in range(30)] + [sample(NOW - 9 * DAY + index * 60, 60.0) for index in range(30)] + [sample(NOW - 59, 10.0)]
kept = sampler.retain(sorted(raw, key=lambda row: row["t"]), NOW)
@@ -134,10 +141,10 @@ class TestRates(unittest.TestCase):
self.assertEqual({row["width"] for row in kept[1:-1]}, {900})
self.assertNotIn("span", kept[-1])
self.lines(self.folder / "samples.jsonl", kept)
- found = self.rated()["rates"]
month = round((10 * 60 + 60 * 1800) / (60 + 1800), 1)
year = round((10 * 60 + 60 * 1800 + 20 * 1800) / (60 + 1800 * 2), 1)
- self.assertEqual([found[period]["cpu"]["value"] for period in ("minute", "week", "month", "year")], [10.0, 10.0, month, year])
+ cpu = [self.rated(NOW - seconds, NOW)["rates"]["day"]["cpu"]["value"] for seconds in (60, 7 * DAY, 30 * DAY, 365 * DAY)]
+ self.assertEqual(cpu, [10.0, 10.0, month, year])
def test_a_lone_fifteen_minute_bucket_weighs_its_sampled_seconds_wherever_it_lies_in_its_day(self):
day = NOW - NOW % DAY - 200 * DAY
@@ -156,22 +163,7 @@ class TestRates(unittest.TestCase):
old, recent = NOW - NOW % DAY - 100 * DAY, NOW - NOW % DAY - 8 * DAY
raw = [sample(old + 60 * index, 90.0) for index in range(60)] + [sample(recent + 60 * index, 10.0) for index in range(60)]
self.lines(self.folder / "samples.jsonl", sampler.retain(raw, NOW))
- self.assertEqual(self.rated()["rates"]["year"]["cpu"]["value"], 50.0)
-
- def test_a_folded_row_straddling_a_window_start_counts_by_its_share_inside(self):
- week, year = NOW - 7 * DAY, NOW - 365 * DAY
- self.lines(self.folder / "samples.jsonl", [
- {**sample(year - year % DAY, 40.0), "span": 3600, "width": DAY},
- {**sample(week - 300, 70.0, boot=80), "span": 600, "width": 900},
- sample(NOW - 30, 10.0, boot=20),
- ])
- found = self.rated()["rates"]
- inside = (year - year % DAY + DAY - year) / DAY * 3600
- self.assertEqual(found["week"]["cpu"]["value"], round((400 * 70 + 60 * 10) / 460, 1))
- self.assertEqual(found["month"]["cpu"]["value"], round((600 * 70 + 60 * 10) / 660, 1))
- self.assertEqual(found["year"]["cpu"]["value"], round((inside * 40 + 600 * 70 + 60 * 10) / (inside + 660), 1))
- self.assertEqual(found["total"]["cpu"]["value"], round((3600 * 40 + 600 * 70 + 60 * 10) / 4260, 1))
- self.assertEqual(found["week"]["disk"]["value"], round((400 * 80 + 60 * 20) / 460, 1))
+ self.assertEqual(self.rated(NOW - 365 * DAY, NOW)["rates"]["year"]["cpu"]["value"], 50.0)
def test_token_rows_older_than_ninety_days_fold_into_one_row_per_day_agent_and_model_kept_forever(self):
day, hour = NOW - NOW % DAY - 100 * DAY, NOW - NOW % HOUR
@@ -181,25 +173,11 @@ class TestRates(unittest.TestCase):
self.assertEqual(kept, [*daily, token(hour - HOUR, 7, 0)])
self.assertEqual(sampler.compact(kept, NOW + 400 * DAY)[:2], daily)
- def test_a_daily_token_row_counts_by_the_share_of_its_day_inside_a_window(self):
+ def test_a_daily_token_row_counts_by_the_share_of_its_day_inside_the_window(self):
year = NOW - 365 * DAY
self.lines(self.folder / "tokens.jsonl", [{**token(year - year % DAY, 8640, 0), "width": DAY}])
- tokens = self.column("tokens")
- self.assertEqual(tokens["year"], {"value": rates.precise((year - year % DAY + DAY - year) / DAY * 8640), "approx": False, "source": None})
- self.assertEqual(tokens["total"], {"value": 8640.0, "approx": False, "source": None})
-
- def test_token_windows_reaching_before_the_first_token_row_are_scaled_estimates_from_the_rows_there_are(self):
- hour = NOW - NOW % HOUR
- begins = hour - 10 * DAY
- self.register({"work/old": {"status": "done", "end": iso(NOW - 400 * DAY)}})
- self.lines(self.folder / "tokens.jsonl", [token(begins, 1000, 0), token(hour - HOUR, 500, 0)])
- tokens = self.column("tokens")
- covered = NOW - begins
- self.assertEqual(tokens["minute"], {"value": rates.precise(250 / 60), "approx": True, "source": "hour"})
- self.assertEqual([tokens[period] for period in ("hour", "day", "week")], [{"value": value, "approx": False, "source": None} for value in (250.0, 500.0, 500.0)])
- for period, seconds in (("month", 30 * DAY), ("year", 365 * DAY), ("total", 400 * DAY)):
- with self.subTest(period=period):
- self.assertEqual(tokens[period], {"value": rates.precise(1500 * seconds / covered), "approx": True, "source": "total"})
+ total = self.column("tokens", year, NOW)["total"]
+ self.assertEqual(total, {"value": rates.precise((year - year % DAY + DAY - year) / DAY * 8640), "approx": False, "covered": NOW - year})
def test_counts_shorten_to_thousands_millions_billions_and_trillions(self):
probe = "const R = require(process.argv[1]); console.log(JSON.stringify(JSON.parse(process.argv[2]).map(R.compact)));"
@@ -209,16 +187,19 @@ class TestRates(unittest.TestCase):
class TestHubRates(HubCase):
- def test_the_hub_serves_the_rates_of_the_register_the_journal_and_the_sampler(self):
- self.write_council({"rasci.json": {"work/a": {"status": "done", "end": iso(1_700_000_000), "key_results": [{"text": "a", "met": True, "met_at": iso(1_700_000_000)}]}}})
- (self.council / "scores.jsonl").write_text(json.dumps({"time": 1_700_000_000, "remote": "example.org/repo", "sha": "c" * 40, "values": {}}) + "\n", encoding="utf-8")
- status, headers, body = self.get("/api/rates")
+ def test_the_hub_serves_the_rates_of_the_window_from_the_register_the_journal_and_the_sampler(self):
+ done = 1_700_000_000
+ self.write_council({"rasci.json": {"work/a": {"status": "done", "end": iso(done), "key_results": [{"text": "a", "met": True, "met_at": iso(done)}]}}})
+ (self.council / "scores.jsonl").write_text(json.dumps({"time": done, "remote": "example.org/repo", "sha": "c" * 40, "values": {}}) + "\n", encoding="utf-8")
+ status, headers, body = self.get(f"/api/rates?from={done - DAY}&to={done + DAY}")
self.assertEqual((status, headers["content-type"]), (200, "application/json"), body)
found = json.loads(body)
self.assertEqual([entry["name"] for entry in found["periods"]], list(rates.PERIOD_NAMES))
- self.assertEqual({kind: found["rates"]["total"][kind]["value"] for kind in rates.COUNTS}, {"tasks": 1.0, "commits": 1.0, "krs": 1.0, "tokens": 0.0})
+ self.assertEqual({kind: found["rates"]["total"][kind]["value"] for kind in rates.COUNTS}, {"tasks": 1.0, "commits": 1.0, "krs": 1.0, "tokens": None})
+ self.assertEqual({kind: found["rates"]["day"][kind]["approx"] for kind in ("tasks", "commits", "krs")}, {"tasks": True, "commits": True, "krs": True})
self.assertEqual(set(found["rates"]["hour"]), set(rates.METRICS))
- self.assertEqual(found["since"], 1_700_000_000)
+ self.assertEqual((found["since"], found["from"], found["to"]), (done, done - DAY, done + DAY))
+ self.assertEqual(self.get(f"/api/rates?from={done}&to={done - 1}")[0], 400)
if __name__ == "__main__":
diff --git a/tests/test_hub_settings.py b/tests/test_hub_settings.py
index 77428fa..112afbd 100644
--- a/tests/test_hub_settings.py
+++ b/tests/test_hub_settings.py
@@ -5,7 +5,7 @@ import unittest
from hub_case import HubCase
from hub_support import STREAMS, Key, key
-from kvb_orchestrator import settings
+from kvb_orchestrator import rates, settings
class TestHubSettings(HubCase):
@@ -199,13 +199,13 @@ class TestHubSettings(HubCase):
self.assertEqual((settings.SETTINGS["rates"], self.snapshot()["settings"]["rates"]), (default, default))
good = self.guard(self.host)
wrongs = [{"period": period, "metrics": ["tasks", "cpu"]} for period in ("second", "Hour", "", None, 1)]
- wrongs += [{"period": "day", "metrics": metrics} for metrics in (["tasks"], [], ["tasks", "cpu", "ram", "swap"], ["tasks", "tasks"], ["tasks", "gpu"], [1, 2], "tasks,cpu", None)]
+ wrongs += [{"period": "day", "metrics": metrics} for metrics in (["tasks"], [], [*rates.METRICS, "tasks"], ["tasks", "tasks"], ["tasks", "gpu"], [1, 2], "tasks,cpu", None)]
wrongs += [{"period": "day"}, {"metrics": ["tasks", "cpu"]}, {**default, "extra": 1}, "day", ["day"]]
for wrong in wrongs:
with self.subTest(wrong=wrong):
self.assertEqual(self.post_json("/api/settings", {**settings.SETTINGS, "rates": wrong}, good)[0], 400)
self.assertEqual(self.snapshot()["settings"]["rates"], default)
- for chosen in ({"period": "minute", "metrics": ["commits", "krs"]}, {"period": "total", "metrics": ["disk", "vram", "swap"]}, {"period": "week", "metrics": ["ram", "tokens"]}):
+ for chosen in ({"period": "minute", "metrics": ["commits", "krs"]}, {"period": "total", "metrics": list(reversed(rates.METRICS))}, {"period": "week", "metrics": ["ram", "tokens"]}):
with self.subTest(chosen=chosen):
self.assertIsNone(self.prepared({**self.snapshot()["settings"], "rates": chosen})["payload"])
status, _, body = self.post_json("/api/settings", {**self.snapshot()["settings"], "rates": chosen, "proof": None}, good)