feat(orchestrator): resources view with legend chips and a server-side view store (007)
The Resources tab draws one hardware chart and one tokens chart with an accumulated switch, plus a combined overlay of hardware, tokens and the burnup, every series switched by a legend chip and all shown by default. The hub keeps every toggle in a validated view store: PATCH /api/view takes a set and the expected old values and answers 409 on a stale expectation, update() re-runs a change on the stored value, list paths change only through update(), and a page shown again from the back-forward cache fetches the stored view anew. 🎯 Fix: 93 % 💥 Break: 4 % 💸 Cost: 100 % ⏱️ Time: 100 % 🛡️ Security: 100 % 🏆 Score: 89 🚦 Gate: ✅ 90/5, cost and time <= 100, security 100 % Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3418738993
commit
c128fa4000
15 files changed
+1794
-102
No files matched your search
+176
-29
@@ -6,7 +6,7 @@ import time
|
||||
import unittest
|
||||
|
||||
from hub_case import HubCase
|
||||
from hub_support import HUB, NAV
|
||||
from hub_support import HUB, NAV, PLAN
|
||||
|
||||
MODULE = HUB / "templates" / "resources.js"
|
||||
PAGE = HUB / "templates" / "page-resources.js"
|
||||
@@ -52,43 +52,92 @@ console.log(JSON.stringify({
|
||||
asleep: R.asleep(samples),
|
||||
unknown: R.unknown(samples),
|
||||
stack: R.stack(tokens),
|
||||
hardware: R.hardware(samples).map((entry) => [entry.name, entry.kind, entry.points.length]),
|
||||
accumulated: R.accumulate(tokens),
|
||||
many: (() => {
|
||||
let seed = 7;
|
||||
const next = () => {
|
||||
seed = (seed * 1103515245 + 12345) % 2147483648;
|
||||
return seed;
|
||||
};
|
||||
const rows = Array.from({length: 80}, () => ({t: T + 3600 * (next() % 12), agent: ["A", "B", "C"][next() % 3], model: "m", input: (next() % 900) - 100, output: next() % 50, cache_read: next() % 2 ? NaN : 7, cache_write: 0}));
|
||||
const sums = R.accumulate(rows);
|
||||
const totals = R.stack(rows).totals;
|
||||
return {
|
||||
rising: sums.lines.every((line) => line.points.every(([, value], index) => !index || value >= line.points[index - 1][1])),
|
||||
finals: Object.fromEntries(sums.lines.map((line) => [line.agent, line.points[line.points.length - 1][1]])),
|
||||
totals, top: sums.top, lengths: [...new Set(sums.lines.map((line) => line.points.length))],
|
||||
};
|
||||
})(),
|
||||
burnup: R.burnup([{t: 1, scope: 2, done: 0}, {t: 2, scope: 4, done: 1}, {t: 3, scope: 5, done: 5}, {t: 4, scope: 4, done: 4}]),
|
||||
empty: R.burnup([{t: 1, scope: 0, done: 0}]),
|
||||
}));
|
||||
""".replace("@FIXTURE@", FIXTURE)
|
||||
PAGE_PROBE = """
|
||||
const fs = require("fs");
|
||||
const vm = require("vm");
|
||||
const Resources = require(process.argv[1]);
|
||||
const code = fs.readFileSync(process.argv[2], "utf8");
|
||||
const T = Number(process.argv[3]);
|
||||
const Plan = require(process.argv[2]);
|
||||
const code = [process.argv[3], process.argv[4], process.argv[5]].map((path) => fs.readFileSync(path, "utf8")).join("\\n");
|
||||
const T = Number(process.argv[6]);
|
||||
@FIXTURE@
|
||||
const make = (tag, attrs, children) => {
|
||||
const node = {tag, attrs: {}, children: [], textContent: "", isConnected: true,
|
||||
const node = {tag, attrs: {}, children: [], textContent: "", isConnected: true, listeners: {},
|
||||
set className(value) { this.attrs.class = value; }, set type(value) { this.attrs.type = value; },
|
||||
setAttribute(key, value) { this.attrs[key] = String(value); }, getAttribute(key) { return this.attrs[key]; },
|
||||
addEventListener(name, run) { this.listeners[name] = run; },
|
||||
append(...items) { this.children.push(...items); },
|
||||
replaceChildren(...items) { this.children = items; },
|
||||
getBoundingClientRect() { return {width: 800, height: 120}; }};
|
||||
for (const [key, value] of Object.entries(attrs)) {
|
||||
if (key !== "on" && value !== null && value !== undefined && value !== false) {
|
||||
if (key === "on") {
|
||||
Object.assign(node.listeners, value);
|
||||
} else if (value !== null && value !== undefined && value !== false) {
|
||||
node.attrs[key] = String(value);
|
||||
}
|
||||
}
|
||||
node.hidden = Object.hasOwn(node.attrs, "hidden");
|
||||
node.children = children.flat(Infinity).filter((child) => child !== null && child !== undefined && child !== false);
|
||||
return node;
|
||||
};
|
||||
const h = (tag, attrs, ...children) => make(tag, attrs, children);
|
||||
const icon = (name) => make("svg", {class: "bi", "data-icon": name}, []);
|
||||
const view = {resources: {hidden: [], tokens: "interval", combined: false, combinedHidden: []}, burn: {overlays: []}, home: {pins: []}};
|
||||
const served = JSON.parse(JSON.stringify(view));
|
||||
const fetched = [];
|
||||
const tasks = [
|
||||
{task: "a", kind: "task", status: "done", start: T - 300, end: T + 600, points: 0},
|
||||
{task: "b", kind: "task", status: "running", start: T + 1000, end: null, points: 0},
|
||||
{task: "c", kind: "task", status: "done", start: T + 2000, end: T + 3000, points: 0},
|
||||
{task: "q", kind: "queue", status: "done", start: T - 500, end: T - 400, points: 0},
|
||||
];
|
||||
const context = {
|
||||
Resources, h, s: h, icon, DAY: 86400, ui: {preset: 86400, from: null, to: null, asOf: null, offline: false},
|
||||
Resources, Plan, h, s: h, icon, DAY: 86400, ui: {preset: 86400, from: null, to: null, asOf: null, offline: false},
|
||||
state: {tasks, now: T + 6000}, settings: {estimation: "none"}, setTimeout, clearTimeout,
|
||||
named: (label, hint) => ({title: hint || label, "aria-label": label}),
|
||||
iconBtn: (name, label, cls, run, hint) => make("button", {title: hint, "aria-label": label}, [icon(name)]),
|
||||
fixed: (value) => value.toFixed(1), dash: (value) => (value === null ? "–" : value), absolute: {format: () => "d"}, clock: {format: () => "c"}, stampOf: (t) => new Date(t * 1000),
|
||||
window: {setTimeout: (run) => run(), setInterval: () => 1, addEventListener: () => null}, document: {hidden: false},
|
||||
window: {setTimeout: (run) => run(), setInterval: () => 1, addEventListener: () => null},
|
||||
document: {
|
||||
hidden: false, activeElement: null, createElement: (tag) => make(tag, {}, []), getElementById: (name) => (name === "view" ? {textContent: JSON.stringify(view)} : null),
|
||||
querySelectorAll: (selector) => (selector === ".legend-chip" ? walk(context.screen, []).filter((node) => has(node, "legend-chip")) : []),
|
||||
},
|
||||
windowQuery: () => "from=1", nowSeconds: () => 0,
|
||||
fetch: async (url) => {
|
||||
fetched.push(url);
|
||||
return {ok: true, json: async () => ({since: T - 600, until: T + 6000, samples, tokens})};
|
||||
fetch: async (url, options) => {
|
||||
if (url !== "/api/view") {
|
||||
fetched.push(url);
|
||||
return {ok: true, json: async () => ({since: T - 600, until: T + 6000, samples, tokens})};
|
||||
}
|
||||
const changes = JSON.parse(options.body).set;
|
||||
fetched.push([options.method, url, changes]);
|
||||
for (const [path, value] of Object.entries(changes)) {
|
||||
const [section, key] = path.split(".");
|
||||
served[section][key] = value;
|
||||
}
|
||||
return {ok: true, json: async () => JSON.parse(JSON.stringify(served))};
|
||||
},
|
||||
};
|
||||
vm.runInNewContext(`${code}\\nscreen = resourcesScreen();`, context);
|
||||
vm.runInNewContext(`${code}\\nscreen = resourcesScreen();\\nstore = ViewStore;`, context);
|
||||
const walk = (node, found) => {
|
||||
if (node && typeof node === "object") {
|
||||
found.push(node);
|
||||
@@ -97,27 +146,52 @@ const walk = (node, found) => {
|
||||
return found;
|
||||
};
|
||||
const has = (node, name) => (node.attrs.class || "").split(" ").includes(name);
|
||||
(async () => {
|
||||
const settle = async () => {
|
||||
for (let round = 0; round < 5; round += 1) {
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
}
|
||||
};
|
||||
const read = () => {
|
||||
const all = walk(context.screen, []);
|
||||
const charts = all.filter((node) => has(node, "res-chart"));
|
||||
const marks = (kind) => all.filter((node) => has(node, "res-mark") && has(node, kind)).map((node) => [Number(node.attrs["data-from"]), Number(node.attrs["data-to"])]);
|
||||
console.log(JSON.stringify({
|
||||
fetched,
|
||||
return {
|
||||
count: all.find((node) => node.attrs["aria-label"] === "Samples").textContent,
|
||||
charts: charts.map((chart) => [chart.attrs["data-panel"], chart.attrs["data-from"], chart.attrs["data-to"]]),
|
||||
panels: all.filter((node) => has(node, "res-panel")).map((node) => [node.attrs["data-panel"], node.hidden]),
|
||||
charts: charts.map((chart) => [chart.attrs["data-chart"], chart.attrs["data-from"], chart.attrs["data-to"]]),
|
||||
bands: charts.map((chart) => walk(chart, []).filter((node) => has(node, "band")).map((node) => node.attrs["data-zone"])),
|
||||
lines: [...new Set(all.filter((node) => has(node, "res-line")).map((node) => `${node.attrs["data-series"]}|${node.attrs["data-kind"]}`))],
|
||||
ids: [...new Set(all.filter((node) => has(node, "res-line")).map((node) => node.attrs["data-id"]))],
|
||||
waits: marks("wait"),
|
||||
asleep: marks("asleep"),
|
||||
unknown: marks("unknown"),
|
||||
words: all.filter((node) => has(node, "res-mark") && has(node, "unknown")).map((node) => node.children[0].children.join("")),
|
||||
bars: all.filter((node) => has(node, "res-bar")).map((node) => [node.attrs["data-agent"], Number(node.attrs["data-t"])]),
|
||||
legends: all.filter((node) => has(node, "res-key")).map((node) => node.children.join("")),
|
||||
bars: all.filter((node) => has(node, "res-bar")).map((node) => [node.attrs["data-agent"], Number(node.attrs["data-t"]), has(node, "faint")]),
|
||||
sums: all.filter((node) => has(node, "res-sum")).map((node) => [node.attrs["data-agent"], JSON.parse(node.attrs["data-points"])]),
|
||||
burn: all.filter((node) => node.tag === "path" && has(node, "res-burn")).map((node) => [node.attrs["data-series"], Number(node.attrs["data-last"]), Number(node.attrs["data-top"])]),
|
||||
right: all.filter((node) => node.attrs["data-axis"] === "right").map((node) => node.children.join("")),
|
||||
chips: all.filter((node) => has(node, "legend-chip")).map((node) => [node.attrs["data-chip"], node.attrs["data-path"], node.attrs["aria-pressed"], node.textContent]),
|
||||
switches: all.filter((node) => has(node, "res-switch")).map((node) => [node.attrs["data-switch"], node.attrs["aria-pressed"]]),
|
||||
titles: all.filter((node) => has(node, "res-title")).map((node) => [node.attrs.title, node.attrs["aria-label"], node.children[0].attrs["data-icon"]]),
|
||||
}));
|
||||
};
|
||||
};
|
||||
const press = (selector) => walk(context.screen, []).find(selector).listeners.click();
|
||||
(async () => {
|
||||
await settle();
|
||||
const normal = read();
|
||||
press((node) => node.attrs["data-chip"] === "host:CPU");
|
||||
press((node) => node.attrs["data-chip"] === "agent:Stage");
|
||||
const hidden = read();
|
||||
press((node) => node.attrs["data-switch"] === "tokens");
|
||||
const accumulated = read();
|
||||
press((node) => node.attrs["data-switch"] === "combined");
|
||||
const combined = read();
|
||||
press((node) => node.attrs["data-chip"] === "burn:scope" && node.attrs["data-path"] === "resources.combinedHidden");
|
||||
press((node) => node.attrs["data-chip"] === "util:0000_3a_01_3a_00.0" && node.attrs["data-path"] === "resources.combinedHidden");
|
||||
const deselected = read();
|
||||
await new Promise((resolve) => setTimeout(resolve, 400));
|
||||
await settle();
|
||||
console.log(JSON.stringify({fetched, normal, hidden, accumulated, combined, deselected, stored: context.store.get("resources")}));
|
||||
})();
|
||||
""".replace("@FIXTURE@", FIXTURE)
|
||||
|
||||
@@ -162,24 +236,97 @@ class TestResourcesModule(unittest.TestCase):
|
||||
self.assertEqual({key: first["parts"][1][key] for key in ("input", "output", "cache_read", "cache_write")}, {"input": 15, "output": 20, "cache_read": 300, "cache_write": 40})
|
||||
self.assertEqual((second["t"], [(part["agent"], part["to"]) for part in second["parts"]]), (T + 3600, [("Stage", 500)]))
|
||||
|
||||
def test_the_hardware_series_merge_disks_host_and_gpu_lines_onto_one_axis(self):
|
||||
self.assertEqual(self.data["hardware"], [["/", "disk", 8], ["/mnt", "disk", 1], ["CPU", "host", 8], ["RAM", "host", 8], ["Swap", "host", 8], ["0000:01:00.0", "util", 8], ["0000:01:00.0", "vram", 8]])
|
||||
|
||||
def test_accumulated_tokens_are_per_agent_running_sums_from_zero_at_each_hour_end(self):
|
||||
found = self.data["accumulated"]
|
||||
self.assertEqual((found["agents"], found["top"]), (["Stage", "Skills"], 510))
|
||||
self.assertEqual(found["lines"], [{"agent": "Stage", "points": [[T, 0], [T + 3600, 10], [T + 7200, 510]]}, {"agent": "Skills", "points": [[T, 0], [T + 3600, 375], [T + 7200, 375]]}])
|
||||
|
||||
def test_accumulated_sums_never_decrease_and_end_at_each_agents_total(self):
|
||||
many = self.data["many"]
|
||||
self.assertTrue(many["rising"])
|
||||
self.assertEqual(many["finals"], many["totals"])
|
||||
self.assertEqual(many["top"], max(many["totals"].values()))
|
||||
self.assertEqual(len(many["lengths"]), 1)
|
||||
|
||||
def test_burnup_scales_done_and_scope_to_percent_of_the_largest_scope_in_the_window(self):
|
||||
found = self.data["burnup"]
|
||||
self.assertEqual(found["top"], 5)
|
||||
self.assertEqual(found["done"], [[1, 0], [2, 20], [3, 100], [4, 80]])
|
||||
self.assertEqual(found["scope"], [[1, 40], [2, 80], [3, 100], [4, 80]])
|
||||
self.assertEqual(self.data["empty"], {"top": 0, "done": [[1, 0]], "scope": [[1, 0]]})
|
||||
|
||||
|
||||
LINES = ["/|disk", "/mnt|disk", "CPU|host", "RAM|host", "Swap|host", "0000:01:00.0|util", "0000:01:00.0|vram"]
|
||||
GPU_ID = "0000_3a_01_3a_00.0"
|
||||
HARDWARE_CHIPS = [["disk:/", "/ 25 %"], ["disk:/mnt", "/mnt 25 %"], ["host:CPU", "CPU 10 %"], ["host:RAM", "RAM 10 %"], ["host:Swap", "Swap 0 %"], [f"util:{GPU_ID}", "0000:01:00.0 10 %"], [f"vram:{GPU_ID}", "0000:01:00.0 VRAM 10 %"]]
|
||||
AGENT_CHIPS = [["agent:Stage", "Stage 510"], ["agent:Skills", "Skills 375"]]
|
||||
|
||||
|
||||
def chips(entries: list[list[str]], path: str, hidden: set[str]) -> list[list[str]]:
|
||||
return [[chip, path, "false" if chip in hidden else "true", label] for chip, label in entries]
|
||||
|
||||
|
||||
class TestResourcesPage(unittest.TestCase):
|
||||
def test_the_view_draws_four_panels_on_one_time_axis_from_the_api_rows_alone(self):
|
||||
done = subprocess.run(["node", "-e", PAGE_PROBE, str(MODULE), str(PAGE), str(T)], capture_output=True, text=True, timeout=60, check=True)
|
||||
found = json.loads(done.stdout)
|
||||
self.assertEqual((found["fetched"], found["count"]), (["/api/resources?from=1"], "8"))
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
sources = [str(HUB / "templates" / name) for name in ("view-store.js", "legend-chips.js", "page-resources.js")]
|
||||
done = subprocess.run(["node", "-e", PAGE_PROBE, str(MODULE), str(PLAN), *sources, str(T)], capture_output=True, text=True, timeout=60, check=True)
|
||||
cls.found = json.loads(done.stdout)
|
||||
|
||||
def test_normal_mode_draws_exactly_a_hardware_and_a_tokens_chart_on_one_time_axis(self):
|
||||
found = self.found["normal"]
|
||||
self.assertEqual(found["count"], "8")
|
||||
self.assertEqual(found["panels"], [["resources-hardware", False], ["resources-tokens", False], ["resources-combined", True]])
|
||||
axis = [str(T - 600), str(T + 6000)]
|
||||
self.assertEqual(found["charts"], [[panel, *axis] for panel in ("disks", "host", "gpu", "tokens")])
|
||||
self.assertEqual(found["bands"], [["idle", "saturated"]] * 3 + [[]])
|
||||
self.assertEqual(found["lines"], ["/|disk", "/mnt|disk", "CPU|host", "RAM|host", "Swap|host", "0000:01:00.0|util", "0000:01:00.0|vram"])
|
||||
self.assertEqual(found["charts"], [["hardware", *axis], ["tokens", *axis]])
|
||||
self.assertEqual(found["bands"], [["idle", "saturated"], []])
|
||||
self.assertEqual(found["lines"], LINES)
|
||||
self.assertEqual(found["waits"], [[T + 60, T + 180], [T + 240, T + 300], [T + 5000, T + 5060]])
|
||||
self.assertEqual(found["asleep"], [[T, T + 120], [T + 5000, T + 5120]])
|
||||
self.assertEqual((found["unknown"], found["words"]), ([[T + 300, T + 360]], ["0000:01:00.0 unknown: no reading"]))
|
||||
self.assertEqual(found["bars"], [["Stage", T], ["Skills", T], ["Stage", T + 3600]])
|
||||
self.assertEqual(found["legends"], ["/ 25 %", "/mnt 25 %", "CPU 10 %", "RAM 10 %", "Swap 0 %", "0000:01:00.0 10 %", "0000:01:00.0 VRAM 10 %", "Stage 510", "Skills 375"])
|
||||
names = [("Disks", "bi-hdd-stack"), ("CPU, RAM and swap", "bi-cpu"), ("GPU and VRAM", "bi-gpu-card"), ("Tokens per agent", "bi-coin")]
|
||||
self.assertEqual(found["bars"], [["Stage", T, False], ["Skills", T, False], ["Stage", T + 3600, False]])
|
||||
self.assertEqual(found["chips"], chips(HARDWARE_CHIPS + AGENT_CHIPS, "resources.hidden", set()))
|
||||
self.assertEqual(found["switches"], [["tokens", "false"], ["combined", "false"]])
|
||||
names = [("Hardware: disks, CPU, RAM, swap, GPU and VRAM", "bi-cpu"), ("Tokens per agent", "bi-coin"), ("Hardware, tokens and burnup", "bi-layers")]
|
||||
self.assertEqual(found["titles"], [[name, name, icon] for name, icon in names])
|
||||
|
||||
def test_hiding_a_chip_removes_its_line_or_its_agent_and_stores_the_list(self):
|
||||
found = self.found["hidden"]
|
||||
self.assertEqual(found["lines"], [line for line in LINES if line != "CPU|host"])
|
||||
self.assertEqual(found["bars"], [["Skills", T, False]])
|
||||
self.assertEqual(found["chips"], chips(HARDWARE_CHIPS + AGENT_CHIPS, "resources.hidden", {"host:CPU", "agent:Stage"}))
|
||||
|
||||
def test_the_accumulated_switch_draws_running_sums_of_the_shown_agents_instead_of_bars(self):
|
||||
found = self.found["accumulated"]
|
||||
self.assertEqual((found["bars"], found["sums"]), ([], [["Skills", [0, 375]]]))
|
||||
self.assertEqual(found["switches"], [["tokens", "true"], ["combined", "false"]])
|
||||
|
||||
def test_the_combined_switch_replaces_both_charts_with_one_holding_every_series_switched_on(self):
|
||||
found = self.found["combined"]
|
||||
self.assertEqual(found["panels"], [["resources-hardware", True], ["resources-tokens", True], ["resources-combined", False]])
|
||||
self.assertEqual(found["charts"], [["combined", str(T - 600), str(T + 6000)]])
|
||||
self.assertEqual(found["lines"], LINES)
|
||||
self.assertEqual(found["sums"], [["Stage", [0, 10, 510]], ["Skills", [0, 375, 375]]])
|
||||
self.assertEqual(found["right"], ["0", "510"])
|
||||
(done, done_last, done_top), (scope, scope_last, scope_top) = found["burn"]
|
||||
self.assertEqual((done, scope, done_top, scope_top, scope_last), ("done", "scope", 3, 3, 100))
|
||||
self.assertAlmostEqual(done_last, 200 / 3)
|
||||
burn = [["burn:done", "Done 67 %"], ["burn:scope", "Scope 100 %"]]
|
||||
self.assertEqual(found["chips"], chips(HARDWARE_CHIPS + AGENT_CHIPS + burn, "resources.combinedHidden", set()))
|
||||
self.assertEqual(found["switches"], [["tokens", "true"], ["combined", "true"]])
|
||||
|
||||
def test_each_combined_series_is_deselectable_and_every_switch_is_stored_in_one_patch(self):
|
||||
found = self.found["deselected"]
|
||||
self.assertEqual([series for series, _, _ in found["burn"]], ["done"])
|
||||
self.assertEqual(found["lines"], [line for line in LINES if line != "0000:01:00.0|util"])
|
||||
stored = {"hidden": ["host:CPU", "agent:Stage"], "tokens": "accumulated", "combined": True, "combinedHidden": ["burn:scope", f"util:{GPU_ID}"]}
|
||||
self.assertEqual(self.found["stored"], stored)
|
||||
changes = {f"resources.{key}": value for key, value in stored.items()}
|
||||
self.assertEqual(self.found["fetched"], ["/api/resources?from=1", ["PATCH", "/api/view", changes]])
|
||||
|
||||
|
||||
class TestHubResources(HubCase):
|
||||
def write(self, name: str, rows: list[dict], tail: str = "") -> None:
|
||||
@@ -218,7 +365,7 @@ class TestHubResources(HubCase):
|
||||
self.assertIn(MODULE.read_text(encoding="utf-8").strip(), html)
|
||||
self.assertIn(PAGE.read_text(encoding="utf-8").strip(), html)
|
||||
self.assertIn(STYLE.read_text(encoding="utf-8").strip(), html)
|
||||
for name in ("speedometer2", "hdd-stack", "cpu", "gpu-card", "coin", "hourglass-split", "moon", "question-circle"):
|
||||
for name in ("speedometer2", "cpu", "coin", "layers", "graph-up-arrow", "hourglass-split", "moon", "question-circle"):
|
||||
with self.subTest(icon=name):
|
||||
self.assertIn(f'<symbol id="bi-{name}"', html)
|
||||
done = subprocess.run(["node", "-e", "const N = require(process.argv[1]); console.log(JSON.stringify([N.TABS.at(-1), N.ICONS.resources, N.route('#resources')]));", str(NAV)], capture_output=True, text=True, timeout=60, check=True)
|
||||
|
||||
Reference in new issue
Block a user