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
@@ -0,0 +1,184 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
import time
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
HERE = Path(__file__).resolve().parent
|
||||
sys.path[:0] = [str(HERE.parent), str(HERE)]
|
||||
|
||||
import test_orchestrator_e2e as base
|
||||
import test_workbench_w4_e2e as w4
|
||||
from hub_case import HubCase
|
||||
from hub_support import iso
|
||||
from kvb_orchestrator.viewstore import VIEW
|
||||
|
||||
OUT = Path(os.environ.get("E2E_OUT", "/tmp/hub-007-shots"))
|
||||
CPU = "host:CPU"
|
||||
STAGE = "agent:Stage"
|
||||
UTIL = "util:0000_3a_01_3a_00.0"
|
||||
SCOPE = "burn:scope"
|
||||
READ = """
|
||||
const charts = () => all("#main svg.res-chart").map((chart) => chart.dataset.chart);
|
||||
const chip = (id) => document.querySelector(`#main .res-panel:not([hidden]) .legend-chip[data-chip="${id}"]`);
|
||||
const pressed = () => Object.fromEntries(all("#main .res-panel:not([hidden]) .legend-chip").map((node) => [node.dataset.chip, node.getAttribute("aria-pressed")]));
|
||||
const lines = () => [...new Set(all("#main path.res-line").map((path) => path.dataset.id))];
|
||||
const agents = () => [...new Set(all("#main rect.res-bar").map((rect) => rect.dataset.agent))].sort();
|
||||
const sums = () => all("#main path.res-sum").map((path) => path.dataset.agent).sort();
|
||||
const rising = () => all("#main path.res-sum").every((path) => JSON.parse(path.dataset.points).every((value, index, list) => !index || value >= list[index - 1]));
|
||||
const burn = () => all("#main path.res-burn").map((path) => path.dataset.series);
|
||||
const switched = (key) => document.querySelector(`#main [data-switch="${key}"]`);
|
||||
const stored = async () => (await (await fetch("/api/view", {cache: "no-store"})).json()).resources;
|
||||
const storedUntil = async (test, ms) => {
|
||||
const end = Date.now() + ms;
|
||||
while (Date.now() < end) {
|
||||
const view = await stored();
|
||||
if (test(view)) {
|
||||
return view;
|
||||
}
|
||||
await sleep(200);
|
||||
}
|
||||
return null;
|
||||
};
|
||||
const legendFits = () => all("#main .res-panel:not([hidden])").every((panel) => panel.querySelector(".res-plot").getBoundingClientRect().height > 40);
|
||||
const page = () => ({tall: document.documentElement.scrollHeight > innerHeight, wide: document.documentElement.scrollWidth > innerWidth + 1, fits: legendFits()});
|
||||
"""
|
||||
TOGGLE = """(async () => {@HELPERS@@READ@
|
||||
if (!(await until(() => charts().length === 2 && chip("@CPU@") && chip("@STAGE@") && agents().length === 2, 15000))) {
|
||||
return {error: text(document.getElementById("main")), charts: charts()};
|
||||
}
|
||||
const before = {charts: charts(), lines: lines(), agents: agents(), pressed: pressed(), panels: all("#main .res-panel").map((panel) => [panel.dataset.panel, panel.hidden])};
|
||||
chip("@CPU@").click();
|
||||
await until(() => chip("@STAGE@") && !lines().includes("@CPU@"), 4000);
|
||||
chip("@STAGE@").click();
|
||||
const hidden = Boolean(await until(() => !lines().includes("@CPU@") && agents().join() === "Skills", 4000));
|
||||
const saved = await storedUntil((view) => view.hidden.includes("@CPU@") && view.hidden.includes("@STAGE@"), 6000);
|
||||
return {before, hidden, after: {charts: charts(), lines: lines(), agents: agents(), pressed: pressed()}, saved, page: page(), violations: window.e2eViolations || null};
|
||||
})()"""
|
||||
COMBINE = """(async () => {@HELPERS@@READ@
|
||||
if (!(await until(() => charts().length === 2 && chip("@CPU@"), 15000))) {
|
||||
return {error: text(document.getElementById("main")), charts: charts()};
|
||||
}
|
||||
const kept = {charts: charts(), lines: lines(), agents: agents(), cpu: pressed()["@CPU@"], stage: pressed()["@STAGE@"]};
|
||||
switched("tokens").click();
|
||||
const accumulated = Boolean(await until(() => sums().length === 1 && !agents().length, 4000));
|
||||
const sumsAt = {sums: sums(), rising: rising(), pressed: switched("tokens").getAttribute("aria-pressed")};
|
||||
switched("combined").click();
|
||||
const combinedOn = Boolean(await until(() => charts().join() === "combined" && burn().length === 2, 4000));
|
||||
const combined = {charts: charts(), lines: lines(), sums: sums(), burn: burn(), right: all('#main text[data-axis="right"]').length, pressed: pressed(), panels: all("#main .res-panel").map((panel) => [panel.dataset.panel, panel.hidden]), switch: switched("combined").getAttribute("aria-pressed")};
|
||||
return {kept, accumulated, sumsAt, combinedOn, combined, page: page(), violations: window.e2eViolations || null};
|
||||
})()"""
|
||||
DESELECT = """(async () => {@HELPERS@@READ@
|
||||
chip("@SCOPE@").click();
|
||||
await until(() => burn().join() === "done" && chip("@UTIL@"), 4000);
|
||||
chip("@UTIL@").click();
|
||||
const deselected = Boolean(await until(() => burn().join() === "done" && !lines().includes("@UTIL@"), 4000));
|
||||
const saved = await storedUntil((view) => view.combinedHidden.length === 2, 6000);
|
||||
const current = await (await fetch("/api/view", {cache: "no-store"})).json();
|
||||
const reset = await fetch("/api/view", {method: "PATCH", headers: {"X-Kvb-Orchestrator": "1", "Content-Type": "application/json"}, body: JSON.stringify({set: @DEFAULTS@, expect: current}), cache: "no-store"});
|
||||
return {deselected, burn: burn(), lines: lines(), saved, reset: reset.status, violations: window.e2eViolations || null};
|
||||
})()"""
|
||||
|
||||
|
||||
def probe(template: str) -> str:
|
||||
names = {"@HELPERS@": base.HELPERS, "@READ@": READ, "@CPU@": CPU, "@STAGE@": STAGE, "@UTIL@": UTIL, "@SCOPE@": SCOPE, "@DEFAULTS@": json.dumps(VIEW)}
|
||||
for key, value in names.items():
|
||||
template = template.replace(key, value)
|
||||
return template
|
||||
|
||||
|
||||
@unittest.skipUnless(shutil.which(os.environ.get("CHROMIUM", "chromium")), "no Chromium")
|
||||
class TestResourcesView(HubCase):
|
||||
drive = base.TestOrchestratorE2E.drive
|
||||
sample = w4.TestWorkbenchResources.sample
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
OUT.mkdir(parents=True, exist_ok=True)
|
||||
now = int(time.time())
|
||||
self.start = now - now % 60 - w4.ROWS * 60
|
||||
hour = self.start - self.start % 3600
|
||||
tokens = [{"t": hour + step * 3600, "agent": "Skills", "model": "claude-opus-5-5", "input": 1000 * (step + 1), "output": 500, "cache_read": 20000, "cache_write": 1000} for step in range(7)]
|
||||
tokens += [{"t": hour + step * 3600, "agent": "Stage", "model": "claude-haiku-4-5", "input": 200, "output": 100, "cache_read": 3000, "cache_write": 0} for step in range(0, 7, 2)]
|
||||
folder = self.kept / "resources"
|
||||
folder.mkdir()
|
||||
(folder / "samples.jsonl").write_text(self.jsonl([self.sample(index) for index in range(w4.ROWS)]), encoding="utf-8")
|
||||
(folder / "tokens.jsonl").write_text(self.jsonl([row for row in tokens if row["t"] <= now]), encoding="utf-8")
|
||||
self.write_council({"rasci.json": {
|
||||
"work/first": {"title": "First", "status": "done", "worker": "Skills", "start": iso(now - 20000), "end": iso(now - 9000), "updated": iso(now - 9000), "roles": {"Skills": "RA"}},
|
||||
"work/second": {"title": "Second", "status": "running", "worker": "Stage", "start": iso(now - 15000), "updated": iso(now - 600), "roles": {"Stage": "RA"}},
|
||||
"work/third": {"title": "Third", "status": "done", "worker": "Skills", "start": iso(now - 7000), "end": iso(now - 3000), "updated": iso(now - 3000), "roles": {"Skills": "RA"}},
|
||||
}})
|
||||
self.base = f"http://{self.host}/"
|
||||
|
||||
def steps(self, browser: str) -> list[dict]:
|
||||
steps = []
|
||||
for width, height in base.WIDTHS:
|
||||
shot = OUT / f"{browser}-{width}"
|
||||
steps += [
|
||||
{"width": width, "height": height, "url": f"{self.base}?{width}#resources", "wait": 1200, "probe": probe(TOGGLE), "shot": f"{shot}-chips.png"},
|
||||
{"width": width, "height": height, "url": f"{self.base}?{width}-reloaded#resources", "wait": 1200, "probe": probe(COMBINE), "shot": f"{shot}-combined.png"},
|
||||
{"width": width, "height": height, "url": None, "wait": 0, "probe": probe(DESELECT), "shot": f"{shot}-deselected.png"},
|
||||
]
|
||||
return steps
|
||||
|
||||
def check(self, data: dict, browser: str) -> None:
|
||||
results = iter(data["results"])
|
||||
hardware = ["disk:/", "disk:/boot/efi", CPU, "host:RAM", "host:Swap", UTIL, "vram:0000_3a_01_3a_00.0"]
|
||||
for width, _ in base.WIDTHS:
|
||||
toggle, combine, deselect = next(results), next(results), next(results)
|
||||
with self.subTest(browser=browser, width=width):
|
||||
self.assertNotIn("error", toggle, toggle)
|
||||
before = toggle["before"]
|
||||
self.assertEqual(before["charts"], ["hardware", "tokens"])
|
||||
self.assertEqual(before["panels"], [["resources-hardware", False], ["resources-tokens", False], ["resources-combined", True]])
|
||||
self.assertEqual((before["lines"], before["agents"]), (hardware, ["Skills", "Stage"]))
|
||||
self.assertEqual(set(before["pressed"].values()), {"true"})
|
||||
self.assertEqual(set(before["pressed"]), {*hardware, "agent:Skills", STAGE})
|
||||
self.assertTrue(toggle["hidden"], toggle)
|
||||
after = toggle["after"]
|
||||
self.assertEqual((after["charts"], after["lines"], after["agents"]), (["hardware", "tokens"], [line for line in hardware if line != CPU], ["Skills"]))
|
||||
self.assertEqual((after["pressed"][CPU], after["pressed"][STAGE], after["pressed"]["host:RAM"]), ("false", "false", "true"))
|
||||
self.assertEqual(toggle["saved"], {**VIEW["resources"], "hidden": [CPU, STAGE]})
|
||||
self.assertEqual(toggle["page"], {"tall": False, "wide": False, "fits": True})
|
||||
self.assertEqual(w4.quiet(toggle["violations"]), [])
|
||||
self.assertNotIn("error", combine, combine)
|
||||
self.assertEqual(combine["kept"], {"charts": ["hardware", "tokens"], "lines": [line for line in hardware if line != CPU], "agents": ["Skills"], "cpu": "false", "stage": "false"})
|
||||
self.assertTrue(combine["accumulated"], combine)
|
||||
self.assertEqual(combine["sumsAt"], {"sums": ["Skills"], "rising": True, "pressed": "true"})
|
||||
self.assertTrue(combine["combinedOn"], combine)
|
||||
combined = combine["combined"]
|
||||
self.assertEqual((combined["charts"], combined["lines"], combined["sums"], combined["burn"]), (["combined"], hardware, ["Skills", "Stage"], ["done", "scope"]))
|
||||
self.assertEqual((combined["right"], combined["switch"]), (2, "true"))
|
||||
self.assertEqual(combined["panels"], [["resources-hardware", True], ["resources-tokens", True], ["resources-combined", False]])
|
||||
self.assertEqual(set(combined["pressed"].values()), {"true"})
|
||||
self.assertEqual(set(combined["pressed"]), {*hardware, "agent:Skills", STAGE, "burn:done", SCOPE})
|
||||
self.assertEqual(combine["page"], {"tall": False, "wide": False, "fits": True})
|
||||
self.assertEqual(w4.quiet(combine["violations"]), [])
|
||||
self.assertTrue(deselect["deselected"], deselect)
|
||||
self.assertEqual((deselect["burn"], deselect["lines"]), (["done"], [line for line in hardware if line != UTIL]))
|
||||
self.assertEqual(deselect["saved"], {"hidden": [CPU, STAGE], "tokens": "accumulated", "combined": True, "combinedHidden": [SCOPE, UTIL]})
|
||||
self.assertEqual(deselect["reset"], 200)
|
||||
self.assertEqual(w4.quiet(deselect["violations"]), [])
|
||||
self.assertEqual(self.httpd.hub.view_store.get(), VIEW)
|
||||
|
||||
def test_chromium_draws_two_charts_whose_chips_toggle_and_persist_and_a_combined_chart_at_three_widths(self):
|
||||
data = self.drive(self.steps("chromium"), None, init=w4.GUARD)
|
||||
(OUT / "chromium-resources-view.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_draws_two_charts_whose_chips_toggle_and_persist_and_a_combined_chart_at_three_widths(self):
|
||||
data = self.drive(self.steps("firefox"), None, "bidi.mjs", os.environ.get("FIREFOX", "firefox"), init=w4.GUARD)
|
||||
(OUT / "firefox-resources-view.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], [])
|
||||
self.check(data, "firefox")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -20,14 +20,14 @@ ROWS = 360
|
||||
WAITED = (120, 180)
|
||||
ASLEEP = 180
|
||||
UNREAD = (300, 320)
|
||||
PANELS = {"disks": "Disks", "host": "CPU, RAM and swap", "gpu": "GPU and VRAM", "tokens": "Tokens per agent"}
|
||||
PANELS = {"resources-hardware": "Hardware: disks, CPU, RAM, swap, GPU and VRAM", "resources-tokens": "Tokens per agent"}
|
||||
GUARD = """() => {
|
||||
window.e2eViolations = [];
|
||||
document.addEventListener("securitypolicyviolation", (event) => window.e2eViolations.push(`${event.violatedDirective} ${event.blockedURI}`));
|
||||
}"""
|
||||
CHARTS = """(async () => {@HELPERS@
|
||||
const counted = () => text(document.querySelector('#main [aria-label="Samples"]'));
|
||||
if (!(await until(() => all("#main svg.res-chart").length === 4 && counted() === "@ROWS@", 15000))) {
|
||||
if (!(await until(() => all("#main svg.res-chart").length === 2 && counted() === "@ROWS@", 15000))) {
|
||||
return {error: text(document.getElementById("main")), count: counted()};
|
||||
}
|
||||
await sleep(300);
|
||||
@@ -41,24 +41,24 @@ CHARTS = """(async () => {@HELPERS@
|
||||
const piles = Object.values(stacks).map((parts) => parts.sort((a, b) => b.y - a.y));
|
||||
const labelled = (node) => (node ? [node.getAttribute("title"), node.getAttribute("aria-label")] : null);
|
||||
return {
|
||||
panels: all("#main .res-panel").map((panel) => panel.dataset.panel),
|
||||
panels: all("#main .res-panel:not([hidden])").map((panel) => panel.dataset.panel),
|
||||
axes: [...new Set(charts.map((chart) => `${chart.dataset.from}|${chart.dataset.to}`))],
|
||||
since: number(charts[0], "from"),
|
||||
bands: charts.filter((chart) => chart.dataset.panel !== "tokens").map((chart) => {
|
||||
bands: charts.filter((chart) => chart.dataset.chart === "hardware").map((chart) => {
|
||||
const [idle, saturated] = [chart.querySelector("rect.band.idle"), chart.querySelector("rect.band.saturated")];
|
||||
return [chart.dataset.panel, attr(idle, "height") / attr(saturated, "height"), attr(idle, "y") > attr(saturated, "y")];
|
||||
return [chart.dataset.chart, attr(idle, "height") / attr(saturated, "height"), attr(idle, "y") > attr(saturated, "y")];
|
||||
}),
|
||||
series: charts.map((chart) => [chart.dataset.panel, [...new Set(all("path.res-line", chart).map((path) => `${path.dataset.series}|${path.dataset.kind}`))]]),
|
||||
series: charts.map((chart) => [chart.dataset.chart, [...new Set(all("path.res-line", chart).map((path) => `${path.dataset.series}|${path.dataset.kind}`))]]),
|
||||
waits: all("#main rect.res-mark.wait").map((rect) => [number(rect, "from"), number(rect, "to"), number(rect, "peak")]),
|
||||
asleep: all("#main rect.res-mark.asleep").map((rect) => [number(rect, "from"), number(rect, "to")]),
|
||||
unknown: all("#main rect.res-mark.unknown").map((rect) => [number(rect, "from"), number(rect, "to")]),
|
||||
agents: [...new Set(all("#main rect.res-bar").map((rect) => rect.dataset.agent))].sort(),
|
||||
bottoms: [...new Set(piles.map((parts) => parts[0].agent))],
|
||||
stacked: piles.every((parts) => parts.every((part, index) => !index || Math.abs(part.y + part.height - parts[index - 1].y) < 0.2)),
|
||||
legend: all("#main .res-host .res-key").map(text),
|
||||
labels: {tab: labelled(document.getElementById("tab-resources")), reload: labelled(document.querySelector('#main [aria-label="Reload"]')), titles: all("#main .res-title").map(labelled)},
|
||||
legend: all("#main .res-hardware .legend-chip.k-host").map(text),
|
||||
labels: {tab: labelled(document.getElementById("tab-resources")), reload: labelled(document.querySelector('#main [aria-label="Reload"]')), titles: all("#main .res-panel:not([hidden]) .res-title").map(labelled)},
|
||||
unlabelled: all("#main svg.bi").map((svg) => svg.parentElement).filter((owner) => !text(owner) && !(owner.getAttribute("title") && owner.getAttribute("aria-label"))).length,
|
||||
plots: all("#main .res-plot").map((plot) => plot.getBoundingClientRect().height > 20),
|
||||
plots: all("#main .res-panel:not([hidden]) .res-plot").map((plot) => plot.getBoundingClientRect().height > 20),
|
||||
tall: document.documentElement.scrollHeight > innerHeight,
|
||||
wide: document.documentElement.scrollWidth > innerWidth + 1,
|
||||
violations: window.e2eViolations || null,
|
||||
@@ -119,17 +119,17 @@ class TestWorkbenchResources(HubCase):
|
||||
self.assertEqual(found["panels"], list(PANELS))
|
||||
self.assertEqual(len(found["axes"]), 1, found["axes"])
|
||||
self.assertLess(abs(found["since"] - (time.time() - 86400)), 600)
|
||||
self.assertEqual([(panel, lower) for panel, _, lower in found["bands"]], [("disks", True), ("host", True), ("gpu", True)])
|
||||
self.assertEqual([(panel, lower) for panel, _, lower in found["bands"]], [("hardware", True)])
|
||||
for panel, ratio, _ in found["bands"]:
|
||||
self.assertAlmostEqual(ratio, 3, delta=0.2, msg=panel)
|
||||
self.assertEqual(found["series"], [["disks", ["/|disk", "/boot/efi|disk"]], ["host", ["CPU|host", "RAM|host", "Swap|host"]], ["gpu", [f"{GPU}|util", f"{GPU}|vram"]], ["tokens", []]])
|
||||
self.assertEqual(found["series"], [["hardware", ["/|disk", "/boot/efi|disk", "CPU|host", "RAM|host", "Swap|host", f"{GPU}|util", f"{GPU}|vram"]], ["tokens", []]])
|
||||
self.assertEqual(found["waits"], [[self.start + WAITED[0] * 60, self.start + WAITED[1] * 60, 2]])
|
||||
self.assertEqual(found["asleep"], [[self.start, self.start + ASLEEP * 60]])
|
||||
self.assertEqual(found["unknown"], [[self.start + UNREAD[0] * 60, self.start + UNREAD[1] * 60]])
|
||||
self.assertEqual((found["agents"], found["bottoms"], found["stacked"]), (["Skills", "Stage"], ["Skills"], True))
|
||||
self.assertEqual(found["legend"], ["CPU 60 %", "RAM 55 %", "Swap 5 %"])
|
||||
self.assertEqual(found["labels"], {"tab": ["Resources (key 5)", "Resources"], "reload": ["Reload: read the sampler files again", "Reload"], "titles": [[name, name] for name in PANELS.values()]})
|
||||
self.assertEqual((found["unlabelled"], found["plots"]), (0, [True] * 4))
|
||||
self.assertEqual((found["unlabelled"], found["plots"]), (0, [True] * 2))
|
||||
self.assertEqual((found["tall"], found["wide"]), (False, False), "the page scrolls")
|
||||
self.assertEqual(quiet(found["violations"]), [])
|
||||
self.assertEqual(results[len(base.WIDTHS)], {"opened": True, "current": "page"})
|
||||
|
||||
Reference in new issue
Block a user