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
@@ -45,6 +45,7 @@ OFFICE_STATE = re.compile(r"/api/offices/([0-9a-f]{16})")
|
||||
TASKS_ROUTE = "/api/tasks"
|
||||
GATES_ROUTE = "/api/gates"
|
||||
TOKEN_ROUTE = "/api/cleanup/token"
|
||||
VIEW_ROUTE = "/api/view"
|
||||
VENDOR_ROUTE = re.compile(r"/vendor/([a-z0-9.-]{1,64})")
|
||||
SCRIPT_TYPE = "text/javascript; charset=utf-8"
|
||||
FONT_TYPE = "font/woff2"
|
||||
@@ -118,6 +119,8 @@ class Handler(Exchange):
|
||||
self.answer(hub.repositories, HTTPStatus.OK)
|
||||
elif path == "/api/resources":
|
||||
self.answer(lambda: sampled(hub.kept / RESOURCES, hub.span(self.query(), time.time())), HTTPStatus.OK)
|
||||
elif path == VIEW_ROUTE:
|
||||
self.answer(hub.view_store.get, HTTPStatus.OK)
|
||||
elif path == "/api/subagents":
|
||||
self.answer(lambda: hub.subagents(self.query()), HTTPStatus.OK)
|
||||
elif path == "/api/runner/jobs":
|
||||
@@ -263,6 +266,21 @@ class Handler(Exchange):
|
||||
if receive is not None:
|
||||
receive(self.rfile.read(length))
|
||||
|
||||
def do_PATCH(self) -> None:
|
||||
path = urlsplit(self.path).path
|
||||
host = self.host()
|
||||
if host is None:
|
||||
self.send_text(HTTPStatus.FORBIDDEN, "forbidden host")
|
||||
elif path != VIEW_ROUTE:
|
||||
self.send_text(HTTPStatus.NOT_FOUND, "not found")
|
||||
elif not self.guarded(host):
|
||||
self.send_text(HTTPStatus.FORBIDDEN, "forbidden: same-origin PATCH with the X-Kvb-Orchestrator header only")
|
||||
else:
|
||||
length = self.body_length(BODY_LIMIT)
|
||||
if length is not None:
|
||||
body = self.rfile.read(length)
|
||||
self.answer(lambda: self.server.hub.view_store.patch(body), HTTPStatus.OK)
|
||||
|
||||
def index(self) -> None:
|
||||
nonce = secrets.token_urlsafe(18)
|
||||
page, csp = self.server.hub.render(nonce)
|
||||
|
||||
@@ -40,6 +40,7 @@ from .timeline import Timeline
|
||||
from .transcripts import Transcripts
|
||||
from .tree import TREE_HEARTBEAT, Tree
|
||||
from .view import View
|
||||
from .viewstore import ViewStore
|
||||
from .voicenotes import VoiceNotes
|
||||
|
||||
|
||||
@@ -103,6 +104,7 @@ class Hub(Mcp, Reviewing, Asking, Messaging, Choosing, Approving, Denying, Reque
|
||||
touched: Touched
|
||||
voice_notes: VoiceNotes
|
||||
replay: Path
|
||||
view_store: ViewStore
|
||||
|
||||
def hosts(self) -> tuple[str, str]:
|
||||
return f"127.0.0.1:{self.public_port}", f"localhost:{self.public_port}"
|
||||
@@ -127,6 +129,7 @@ def open_hub(council: Path, pages: Path, public_port: int, state: Path, host_sta
|
||||
speech=speech,
|
||||
voice_notes=VoiceNotes(state / "voice", host_state / "voice", speech, lambda: {message["voice"] for message in messages.listed() if message.get("voice")}),
|
||||
replay=state / "replay" / "messages.json",
|
||||
view_store=ViewStore(state / "view.json"),
|
||||
rules=Ledger(state / "rules.json"),
|
||||
jobs=Ledger(state / "jobs.json"),
|
||||
marks=Marks(state / "marks.json"),
|
||||
|
||||
@@ -210,5 +210,5 @@ class View:
|
||||
def render(self, nonce: str) -> tuple[str, str]:
|
||||
now = time.time()
|
||||
state = self.state(now - RADIO_WINDOW, now, now, 0)
|
||||
page = self.templates.get_template("index.html.j2").render(nonce=nonce, state=state, icons=self.icons)
|
||||
page = self.templates.get_template("index.html.j2").render(nonce=nonce, state=state, view=self.view_store.get(), icons=self.icons)
|
||||
return page, INDEX_CSP.format(nonce, " ".join(["'self'", *state["media"]]))
|
||||
@@ -0,0 +1,168 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
import json
|
||||
import re
|
||||
import threading
|
||||
from pathlib import Path
|
||||
|
||||
from .errors import Conflict, ToolError
|
||||
from .fields import decode
|
||||
from .records import read_json, write_atomic
|
||||
from .settings import flag, section
|
||||
|
||||
VIEW_LIMIT = 64 << 10
|
||||
CHANGE_LIMIT = 2 * VIEW_LIMIT + (1 << 10)
|
||||
VIEW = {
|
||||
"resources": {"hidden": [], "tokens": "interval", "combined": False, "combinedHidden": []},
|
||||
"burn": {"overlays": []},
|
||||
"home": {"pins": []},
|
||||
}
|
||||
VIEW_PATH = re.compile(r"[A-Za-z]{1,32}(?:\.[A-Za-z]{1,32})?")
|
||||
SERIES_ID = re.compile(r"[A-Za-z0-9:_./-]{1,200}")
|
||||
PIN_KIND = re.compile(r"[A-Za-z0-9:_./-]{1,32}")
|
||||
IDS_LIMIT = 500
|
||||
TOKEN_MODES = ("interval", "accumulated")
|
||||
OVERLAYS = ("fix", "break", "tokens", "velocity", "security", "score")
|
||||
PINS_LIMIT = 64
|
||||
SPAN = (1, 12)
|
||||
|
||||
|
||||
def series_ids(value: object, name: str) -> list[str]:
|
||||
if not isinstance(value, list) or len(value) > IDS_LIMIT or not all(isinstance(item, str) and SERIES_ID.fullmatch(item) for item in value):
|
||||
raise ToolError(f"{name} must list at most {IDS_LIMIT} ids of 1 to 200 characters from A-Z, a-z, 0-9 and :_./-")
|
||||
if len(set(value)) != len(value):
|
||||
raise ToolError(f"{name} must not repeat an id")
|
||||
return list(value)
|
||||
|
||||
|
||||
def span(value: object, name: str) -> int:
|
||||
low, high = SPAN
|
||||
if isinstance(value, bool) or not isinstance(value, int) or not low <= value <= high:
|
||||
raise ToolError(f"{name} must be a whole number from {low} to {high}")
|
||||
return value
|
||||
|
||||
|
||||
def pin_entry(raw: object, index: int) -> dict:
|
||||
name = f"home.pins[{index}]"
|
||||
entry = section(raw, name, ("kind", "id", "w", "h"))
|
||||
if not isinstance(entry["kind"], str) or not PIN_KIND.fullmatch(entry["kind"]):
|
||||
raise ToolError(f"{name}.kind must be 1 to 32 characters from A-Z, a-z, 0-9 and :_./-")
|
||||
if not isinstance(entry["id"], str) or not SERIES_ID.fullmatch(entry["id"]):
|
||||
raise ToolError(f"{name}.id must be 1 to 200 characters from A-Z, a-z, 0-9 and :_./-")
|
||||
return {"kind": entry["kind"], "id": entry["id"], "w": span(entry["w"], f"{name}.w"), "h": span(entry["h"], f"{name}.h")}
|
||||
|
||||
|
||||
def pins(value: object) -> list[dict]:
|
||||
if not isinstance(value, list) or len(value) > PINS_LIMIT:
|
||||
raise ToolError(f"home.pins must be a list of at most {PINS_LIMIT} pins")
|
||||
found = [pin_entry(raw, index) for index, raw in enumerate(value)]
|
||||
if len({(pin["kind"], pin["id"]) for pin in found}) != len(found):
|
||||
raise ToolError("home.pins must not pin the same kind and id twice")
|
||||
return found
|
||||
|
||||
|
||||
def valid_view(raw: object) -> dict:
|
||||
"""Args:
|
||||
raw: the whole view document as the page sent it.
|
||||
|
||||
Returns:
|
||||
The document with every key of VIEW and nothing else; any other
|
||||
key, a missing one or a malformed value raises ToolError.
|
||||
"""
|
||||
data = section(raw, "view", tuple(VIEW))
|
||||
resources = section(data["resources"], "resources", tuple(VIEW["resources"]))
|
||||
burn = section(data["burn"], "burn", tuple(VIEW["burn"]))
|
||||
home = section(data["home"], "home", tuple(VIEW["home"]))
|
||||
if not isinstance(resources["tokens"], str) or resources["tokens"] not in TOKEN_MODES:
|
||||
raise ToolError(f"resources.tokens must be one of {', '.join(TOKEN_MODES)}")
|
||||
overlays = burn["overlays"]
|
||||
if not isinstance(overlays, list) or not all(isinstance(item, str) and item in OVERLAYS for item in overlays) or len(set(overlays)) != len(overlays):
|
||||
raise ToolError(f"burn.overlays must list distinct metrics of {', '.join(OVERLAYS)}")
|
||||
return {
|
||||
"resources": {
|
||||
"hidden": series_ids(resources["hidden"], "resources.hidden"),
|
||||
"tokens": resources["tokens"],
|
||||
"combined": flag(resources["combined"], "resources.combined"),
|
||||
"combinedHidden": series_ids(resources["combinedHidden"], "resources.combinedHidden"),
|
||||
},
|
||||
"burn": {"overlays": list(overlays)},
|
||||
"home": {"pins": pins(home["pins"])},
|
||||
}
|
||||
|
||||
|
||||
def slot(view: dict, path: object) -> tuple[dict, str]:
|
||||
"""Args:
|
||||
view: a document of the shape of VIEW.
|
||||
path: a section or section.key of VIEW, such as resources.hidden.
|
||||
|
||||
Returns:
|
||||
The object holding the path's value and its key there.
|
||||
"""
|
||||
if not isinstance(path, str) or not VIEW_PATH.fullmatch(path):
|
||||
raise ToolError("a view path must be a section or section.key, such as resources.hidden")
|
||||
keys = path.split(".")
|
||||
shape, node = VIEW, view
|
||||
for key in keys[:-1]:
|
||||
if key not in shape:
|
||||
raise ToolError(f"unknown view path {path}")
|
||||
shape, node = shape[key], node[key]
|
||||
if keys[-1] not in shape:
|
||||
raise ToolError(f"unknown view path {path}")
|
||||
return node, keys[-1]
|
||||
|
||||
|
||||
def value_at(view: dict, path: object) -> object:
|
||||
node, key = slot(view, path)
|
||||
return node[key]
|
||||
|
||||
|
||||
class ViewStore:
|
||||
def __init__(self, file: Path) -> None:
|
||||
self.file = file
|
||||
self.lock = threading.Lock()
|
||||
try:
|
||||
self.current = valid_view(read_json(file))
|
||||
except ToolError:
|
||||
self.current = copy.deepcopy(VIEW)
|
||||
|
||||
def get(self) -> dict:
|
||||
with self.lock:
|
||||
return copy.deepcopy(self.current)
|
||||
|
||||
def patch(self, body: bytes) -> dict:
|
||||
"""Args:
|
||||
body: {"set": {path: value}, "expect": {path: value}} as JSON, at
|
||||
most CHANGE_LIMIT bytes, so that a document at VIEW_LIMIT can
|
||||
still send both values of a path; expect names the same paths
|
||||
with the values the page last saw stored there, and every
|
||||
other path keeps its stored value.
|
||||
|
||||
Returns:
|
||||
The whole stored document. A path whose stored value differs
|
||||
from its expected one raises Conflict with {"view", "paths"}: the
|
||||
stored document and those paths. A refused change, or one that
|
||||
would grow the document past VIEW_LIMIT bytes, leaves the file
|
||||
and the served document as they were.
|
||||
"""
|
||||
if len(body) > CHANGE_LIMIT:
|
||||
raise ToolError(f"a view change must not exceed {CHANGE_LIMIT} bytes")
|
||||
change = section(decode(body), "change", ("set", "expect"))
|
||||
values, expected = change["set"], change["expect"]
|
||||
if not isinstance(values, dict) or not values or not isinstance(expected, dict) or set(expected) != set(values):
|
||||
raise ToolError("set must map at least one view path to its new value and expect the same paths to their stored values")
|
||||
with self.lock:
|
||||
moved = [path for path in values if value_at(self.current, path) != expected[path]]
|
||||
if moved:
|
||||
raise Conflict({"view": copy.deepcopy(self.current), "paths": moved})
|
||||
draft = copy.deepcopy(self.current)
|
||||
for path, value in values.items():
|
||||
node, key = slot(draft, path)
|
||||
node[key] = value
|
||||
view = valid_view(draft)
|
||||
data = json.dumps(view, separators=(",", ":")).encode("utf-8")
|
||||
if len(data) > VIEW_LIMIT:
|
||||
raise ToolError(f"the view document must not exceed {VIEW_LIMIT} bytes")
|
||||
write_atomic(self.file, data)
|
||||
self.current = view
|
||||
return copy.deepcopy(view)
|
||||
@@ -408,6 +408,8 @@ pre.notes { margin: 0; padding: 8px 10px; background: var(--sunken); border-radi
|
||||
|
||||
{% include "style-resources.css" %}
|
||||
|
||||
{% include "style-legend-chips.css" %}
|
||||
|
||||
{% include "style-inbox.css" %}
|
||||
|
||||
{% include "style-ui.css" %}
|
||||
@@ -455,6 +457,7 @@ pre.notes { margin: 0; padding: 8px 10px; background: var(--sunken); border-radi
|
||||
<div class="sky-panel" id="sky-panel" role="group" aria-label="View of the sky" hidden></div>
|
||||
<div class="bubbles" id="bubbles" aria-hidden="true" hidden><div class="bubble-bar"></div></div>
|
||||
<script nonce="{{ nonce }}" id="state" type="application/json">{{ state | tojson }}</script>
|
||||
<script nonce="{{ nonce }}" id="view" type="application/json">{{ view | tojson }}</script>
|
||||
<script nonce="{{ nonce }}">
|
||||
{% include "ambience.js" %}
|
||||
</script>
|
||||
@@ -465,6 +468,10 @@ pre.notes { margin: 0; padding: 8px 10px; background: var(--sunken); border-radi
|
||||
|
||||
{% include "diffs.js" %}
|
||||
|
||||
{% include "view-store.js" %}
|
||||
|
||||
{% include "legend-chips.js" %}
|
||||
|
||||
{% include "resources.js" %}
|
||||
|
||||
{% include "nav.js" %}
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
const LegendChips = (() => {
|
||||
"use strict";
|
||||
const KEEP = 100;
|
||||
const MODES = {
|
||||
hidden: {pressed: (list, id) => !list.includes(id), words: ["shown, press to hide", "hidden, press to show"], group: "Series: press a chip to hide or show it"},
|
||||
shown: {pressed: (list, id) => list.includes(id), words: ["on, press to switch it off", "off, press to switch it on"], group: "Overlays: press a chip to switch it on or off"},
|
||||
};
|
||||
|
||||
/**
|
||||
* @param list - the series ids the view-store key holds.
|
||||
* @param id - the series to place.
|
||||
* @param wanted - true when the list must hold id, false when it must not.
|
||||
* @returns the list holding id once, appended when missing, or the list without id.
|
||||
*/
|
||||
function placed(list, id, wanted) {
|
||||
if (!wanted) {
|
||||
return list.filter((item) => item !== id);
|
||||
}
|
||||
return list.includes(id) ? list : [...list, id];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list - the hidden series ids, oldest first.
|
||||
* @param present - the ids of the series whose chips the page shows now for the same view-store key.
|
||||
* @returns the list with its oldest ids of series no longer shown dropped until at most KEEP ids remain, or
|
||||
* until only shown ones are left.
|
||||
*/
|
||||
function pruned(list, present) {
|
||||
let surplus = list.length - KEEP;
|
||||
return list.filter((item) => present.has(item) || surplus-- <= 0);
|
||||
}
|
||||
|
||||
function shownChips(path) {
|
||||
return new Set([...document.querySelectorAll(".legend-chip")].filter((node) => node.getAttribute("data-path") === path).map((node) => node.getAttribute("data-chip")));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param series - [{id, label, tone}]: one chip per series; label is its text, tone the classes that colour its swatch.
|
||||
* @param path - the section.key of the view store that holds a list of series ids.
|
||||
* @param mode - "hidden" when the list names the series switched off (all on by default; old ids of series gone
|
||||
* from the page are pruned beyond KEEP), "shown" when it names the series switched on (all off by default).
|
||||
* @returns a group with one toggle button per series, pressed while the series is on; a press fixes at once
|
||||
* whether the list must hold its id, the opposite of what this page shows, and applies that through
|
||||
* ViewStore.update, so a retry on a list another browser changed keeps that choice; the caller redraws from
|
||||
* ViewStore.on.
|
||||
*/
|
||||
function render(series, path, mode) {
|
||||
if (!Object.hasOwn(MODES, mode)) {
|
||||
throw new Error(`a legend chip mode must be hidden or shown, not ${mode}`);
|
||||
}
|
||||
const {pressed, words, group: label} = MODES[mode];
|
||||
const hint = (text, on) => `${text}: ${words[on ? 0 : 1]}`;
|
||||
const listed = ViewStore.get(path);
|
||||
const group = document.createElement("div");
|
||||
group.className = "legend-chips";
|
||||
group.setAttribute("role", "group");
|
||||
group.setAttribute("aria-label", label);
|
||||
for (const entry of series) {
|
||||
const on = pressed(listed, entry.id);
|
||||
const chip = document.createElement("button");
|
||||
chip.type = "button";
|
||||
chip.className = `legend-chip ${entry.tone}`.trim();
|
||||
chip.setAttribute("data-chip", entry.id);
|
||||
chip.setAttribute("data-path", path);
|
||||
chip.setAttribute("aria-pressed", String(on));
|
||||
chip.setAttribute("title", hint(entry.label, on));
|
||||
chip.textContent = entry.label;
|
||||
chip.addEventListener("click", () => {
|
||||
const present = shownChips(path);
|
||||
const wanted = !ViewStore.get(path).includes(entry.id);
|
||||
ViewStore.update(path, (list) => (mode === "hidden" ? pruned(placed(list, entry.id, wanted), present) : placed(list, entry.id, wanted)));
|
||||
const now = pressed(ViewStore.get(path), entry.id);
|
||||
chip.setAttribute("aria-pressed", String(now));
|
||||
chip.setAttribute("title", hint(entry.label, now));
|
||||
});
|
||||
group.append(chip);
|
||||
}
|
||||
return group;
|
||||
}
|
||||
|
||||
return {KEEP, render, placed, pruned};
|
||||
})();
|
||||
if (typeof module === "object" && module.exports) {
|
||||
module.exports = LegendChips;
|
||||
}
|
||||
@@ -1,29 +1,41 @@
|
||||
const RESOURCE = {left: 46, right: 10, top: 6, bottom: 6, axis: 18, poll: 5000, stale: 60, tones: 6, hour: 3600, narrow: 600};
|
||||
const RESOURCE_PANELS = [["disks", "bi-hdd-stack", "Disks"], ["host", "bi-cpu", "CPU, RAM and swap"], ["gpu", "bi-gpu-card", "GPU and VRAM"], ["tokens", "bi-coin", "Tokens per agent"]];
|
||||
const RESOURCE = {left: 46, right: 10, wide: 50, top: 6, bottom: 6, axis: 18, poll: 5000, stale: 60, tones: 6, narrow: 600};
|
||||
const RESOURCE_PANELS = [["hardware", "bi-cpu", "Hardware: disks, CPU, RAM, swap, GPU and VRAM"], ["tokens", "bi-coin", "Tokens per agent"], ["combined", "bi-layers", "Hardware, tokens and burnup"]];
|
||||
const RESOURCE_ZONES = {idle: "Idle: below 30 %", saturated: "Saturated: above 90 %"};
|
||||
const RESOURCE_MARKS = {
|
||||
wait: ["#bi-hourglass-split", (mark) => `${mark.peak} jobs waited while CPU and RAM stayed under 50 %`],
|
||||
asleep: ["#bi-moon", (mark) => `${mark.name} asleep`],
|
||||
unknown: ["#bi-question-circle", (mark) => `${mark.name} unknown: no reading`],
|
||||
};
|
||||
const RESOURCE_BURN = {done: "Done", scope: "Scope"};
|
||||
const resourceView = {data: null, dom: null, busy: false, key: "", at: 0, timer: 0};
|
||||
const resourceKey = () => JSON.stringify([ui.preset, ui.from, ui.to, ui.asOf]);
|
||||
const compactCount = (value) => (value >= 1e6 ? `${fixed(value / 1e6)}M` : value >= 1e3 ? `${fixed(value / 1e3)}k` : String(Math.round(value)));
|
||||
const resourceLabel = (line) => (line.kind === "vram" ? `${line.name} VRAM` : line.name);
|
||||
const percentOf = (value) => `${value} %`;
|
||||
|
||||
function resourceSwitch(key, next, name, label, hint) {
|
||||
const path = `resources.${key}`;
|
||||
return h("button", {type: "button", class: "btn small soft res-switch", "data-switch": key, "aria-pressed": "false", ...named(label, hint), on: {click: () => ViewStore.set(path, next(ViewStore.get(path)))}}, icon(name), h("span", {}, label));
|
||||
}
|
||||
|
||||
function resourcesScreen() {
|
||||
const count = h("span", {class: "count", title: "Samples", "aria-label": "Samples"}, "0");
|
||||
const switches = {
|
||||
tokens: resourceSwitch("tokens", (mode) => (mode === "accumulated" ? "interval" : "accumulated"), "bi-graph-up-arrow", "Accumulated", "Accumulated: per-agent running sums of the tokens instead of the tokens per hour"),
|
||||
combined: resourceSwitch("combined", (on) => !on, "bi-layers", "Combined", "Combined: hardware in %, tokens and the burnup in one chart"),
|
||||
};
|
||||
const panels = RESOURCE_PANELS.map(([kind, name, label]) => {
|
||||
const legend = h("div", {class: "res-legend"});
|
||||
const plot = h("div", {class: "res-plot"});
|
||||
const panel = h("section", {class: `card res-panel res-${kind}`, "data-panel": kind, "aria-label": label},
|
||||
const panel = h("section", {class: `card res-panel res-${kind}`, "data-panel": `resources-${kind}`, "aria-label": label, hidden: kind === "combined"},
|
||||
h("div", {class: "res-head"}, h("span", {class: "res-title", title: label, "aria-label": label}, icon(name)), legend), plot);
|
||||
return {kind, label, legend, plot, panel};
|
||||
});
|
||||
resourceView.dom = {count, panels};
|
||||
resourceView.dom = {count, switches, panels};
|
||||
if (!resourceView.timer) {
|
||||
resourceView.timer = window.setInterval(pollResources, RESOURCE.poll);
|
||||
window.addEventListener("resize", drawResources);
|
||||
ViewStore.on("resources", drawResources);
|
||||
}
|
||||
window.setTimeout(() => {
|
||||
drawResources();
|
||||
@@ -31,7 +43,7 @@
|
||||
}, 0);
|
||||
const reload = iconBtn("bi-arrow-clockwise", "Reload", "btn small soft", fetchResources, "Reload: read the sampler files again");
|
||||
return h("div", {class: "graph resources"},
|
||||
h("div", {class: "card controls"}, h("div", {class: "toolbar", role: "toolbar", "aria-label": "Resources"}, reload, count)),
|
||||
h("div", {class: "card controls"}, h("div", {class: "toolbar", role: "toolbar", "aria-label": "Resources"}, reload, switches.tokens, switches.combined, count)),
|
||||
h("div", {class: "res-panels"}, panels.map((entry) => entry.panel)));
|
||||
}
|
||||
|
||||
@@ -64,11 +76,12 @@
|
||||
drawResources();
|
||||
}
|
||||
|
||||
function resourceFrame(entry, span, size) {
|
||||
const axis = entry.kind === "tokens";
|
||||
function resourceFrame(entry, span, size, dual) {
|
||||
const axis = entry.kind !== "hardware";
|
||||
const right = size.width - (dual ? RESOURCE.wide : RESOURCE.right);
|
||||
const bottom = size.height - (axis ? RESOURCE.axis : RESOURCE.bottom);
|
||||
const x = (t) => RESOURCE.left + ((t - span.from) / (span.to - span.from)) * (size.width - RESOURCE.left - RESOURCE.right);
|
||||
const svg = s("svg", {class: `chart res-chart res-${entry.kind}`, "data-panel": entry.kind, "data-from": span.from, "data-to": span.to, viewBox: `0 0 ${size.width} ${size.height}`, role: "img", "aria-label": entry.label});
|
||||
const x = (t) => RESOURCE.left + ((Math.min(Math.max(t, span.from), span.to) - span.from) / (span.to - span.from)) * (right - RESOURCE.left);
|
||||
const svg = s("svg", {class: `chart res-chart res-${entry.kind}`, "data-chart": entry.kind, "data-from": span.from, "data-to": span.to, viewBox: `0 0 ${size.width} ${size.height}`, role: "img", "aria-label": entry.label});
|
||||
const ticks = size.width < RESOURCE.narrow ? 2 : 4;
|
||||
for (let index = 0; index <= ticks; index += 1) {
|
||||
const t = span.from + ((span.to - span.from) * index) / ticks;
|
||||
@@ -77,19 +90,25 @@
|
||||
svg.append(s("text", {class: "axis", x: fixed(x(t)), y: size.height - 4, "text-anchor": index === 0 ? "start" : index === ticks ? "end" : "middle"}, (span.to - span.from > DAY ? absolute : clock).format(stampOf(t))));
|
||||
}
|
||||
}
|
||||
return {svg, x, top: RESOURCE.top, bottom, right: size.width - RESOURCE.right};
|
||||
return {svg, x, top: RESOURCE.top, bottom, right};
|
||||
}
|
||||
|
||||
function resourceScale(frame, values, label) {
|
||||
function resourceScale(frame, values, label, side) {
|
||||
const y = (value) => frame.top + (1 - value / values[values.length - 1]) * (frame.bottom - frame.top);
|
||||
const left = side === "left";
|
||||
for (const value of values) {
|
||||
frame.svg.append(s("line", {class: "grid", x1: RESOURCE.left, x2: fixed(frame.right), y1: fixed(y(value)), y2: fixed(y(value))}));
|
||||
frame.svg.append(s("text", {class: "axis", x: RESOURCE.left - 6, y: fixed(y(value)), dy: "0.35em", "text-anchor": "end"}, label(value)));
|
||||
if (left) {
|
||||
frame.svg.append(s("line", {class: "grid", x1: RESOURCE.left, x2: fixed(frame.right), y1: fixed(y(value)), y2: fixed(y(value))}));
|
||||
}
|
||||
frame.svg.append(s("text", {class: `axis ${side}`, "data-axis": side, x: left ? RESOURCE.left - 6 : fixed(frame.right + 6), y: fixed(y(value)), dy: "0.35em", "text-anchor": left ? "end" : "start"}, label(value)));
|
||||
}
|
||||
return y;
|
||||
}
|
||||
|
||||
function resourceMarks(frame, marks, span) {
|
||||
function resourceBackdrop(frame, y, marks, span) {
|
||||
for (const zone of Resources.ZONES) {
|
||||
frame.svg.append(s("rect", {class: `band ${zone.kind}`, "data-zone": zone.kind, x: RESOURCE.left, width: fixed(frame.right - RESOURCE.left), y: fixed(y(zone.to)), height: fixed(y(zone.from) - y(zone.to))}, s("title", {}, RESOURCE_ZONES[zone.kind])));
|
||||
}
|
||||
for (const mark of marks) {
|
||||
const left = frame.x(Math.max(mark.from, span.from));
|
||||
const right = frame.x(Math.min(mark.to, span.to));
|
||||
@@ -102,73 +121,165 @@
|
||||
}
|
||||
}
|
||||
|
||||
function resourcePercent(entry, list, marks, span, size) {
|
||||
const frame = resourceFrame(entry, span, size);
|
||||
const y = resourceScale(frame, [0, 50, 100], (value) => `${value} %`);
|
||||
for (const zone of Resources.ZONES) {
|
||||
frame.svg.append(s("rect", {class: `band ${zone.kind}`, "data-zone": zone.kind, x: RESOURCE.left, width: fixed(frame.right - RESOURCE.left), y: fixed(y(zone.to)), height: fixed(y(zone.from) - y(zone.to))}, s("title", {}, RESOURCE_ZONES[zone.kind])));
|
||||
}
|
||||
resourceMarks(frame, marks, span);
|
||||
const tones = new Map();
|
||||
const legend = [];
|
||||
function resourceLines(frame, y, list) {
|
||||
for (const line of list) {
|
||||
if (!tones.has(line.name)) {
|
||||
tones.set(line.name, tones.size % RESOURCE.tones);
|
||||
}
|
||||
const tone = `res-c${tones.get(line.name)} k-${line.kind}`;
|
||||
const last = [...line.points].reverse().find(([, value]) => value !== null);
|
||||
const words = `${resourceLabel(line)} ${dash(last === undefined ? null : last[1])} %`;
|
||||
for (const part of Resources.segments(line.points)) {
|
||||
const d = part.map(([t, value], index) => `${index ? "L" : "M"}${fixed(frame.x(t))},${fixed(y(Math.min(Math.max(value, 0), 100)))}`).join(" ");
|
||||
frame.svg.append(s("path", {class: `res-line ${tone}`, "data-series": line.name, "data-kind": line.kind, "data-points": part.length, d: part.length === 1 ? `${d} h1` : d}, s("title", {}, words)));
|
||||
frame.svg.append(s("path", {class: `res-line ${line.tone}`, "data-id": line.id, "data-series": line.name, "data-kind": line.kind, "data-points": part.length, d: part.length === 1 ? `${d} h1` : d}, s("title", {}, line.label)));
|
||||
}
|
||||
legend.push(h("span", {class: `res-key ${tone}`, "data-series": line.name, "data-kind": line.kind}, words));
|
||||
}
|
||||
return {svg: frame.svg, legend};
|
||||
}
|
||||
|
||||
function resourceTokens(entry, stack, span, size) {
|
||||
const frame = resourceFrame(entry, span, size);
|
||||
const y = resourceScale(frame, [0, Math.max(stack.top, 1)], compactCount);
|
||||
const tones = new Map(stack.agents.map((agent, index) => [agent, index % RESOURCE.tones]));
|
||||
for (const bar of stack.bars) {
|
||||
function resourceSteps(frame, y, points) {
|
||||
return points.map(([t, value], index) => (index ? `H${fixed(frame.x(t))} V${fixed(y(value))}` : `M${fixed(frame.x(t))},${fixed(y(value))}`)).join(" ");
|
||||
}
|
||||
|
||||
function tokenPlot(rows, mode) {
|
||||
if (mode === "accumulated") {
|
||||
const sums = Resources.accumulate(rows);
|
||||
return {mode, top: Math.max(sums.top, 1), sums};
|
||||
}
|
||||
const stack = Resources.stack(rows);
|
||||
return {mode, top: Math.max(stack.top, 1), stack};
|
||||
}
|
||||
|
||||
function tokenMarks(frame, y, plot, tones, span, faint) {
|
||||
if (plot.mode === "accumulated") {
|
||||
for (const line of plot.sums.lines) {
|
||||
const last = line.points[line.points.length - 1][1];
|
||||
frame.svg.append(s("path", {class: `res-sum res-c${tones.get(line.agent)}`, "data-agent": line.agent, "data-last": last, "data-points": JSON.stringify(line.points.map(([, value]) => value)), d: resourceSteps(frame, y, line.points)},
|
||||
s("title", {}, `${line.agent}: ${last} tokens summed up in the window`)));
|
||||
}
|
||||
return;
|
||||
}
|
||||
for (const bar of plot.stack.bars) {
|
||||
const left = frame.x(Math.max(bar.t, span.from));
|
||||
const right = frame.x(Math.min(bar.t + RESOURCE.hour, span.to));
|
||||
const right = frame.x(Math.min(bar.t + Resources.HOUR, span.to));
|
||||
if (right <= left) {
|
||||
continue;
|
||||
}
|
||||
for (const part of bar.parts) {
|
||||
const words = `${part.agent} · ${absolute.format(stampOf(bar.t))}: ${part.total} tokens (input ${part.input}, output ${part.output}, cache read ${part.cache_read}, cache write ${part.cache_write}) · ${part.models.join(", ")}`;
|
||||
frame.svg.append(s("rect", {class: `res-bar res-c${tones.get(part.agent)}`, "data-agent": part.agent, "data-t": bar.t, "data-total": part.total, x: fixed(left), width: fixed(Math.max(right - left - 1, 1)), y: fixed(y(part.to)), height: fixed(Math.max(y(part.from) - y(part.to), 0.5))}, s("title", {}, words)));
|
||||
frame.svg.append(s("rect", {class: `res-bar res-c${tones.get(part.agent)}${faint ? " faint" : ""}`, "data-agent": part.agent, "data-t": bar.t, "data-total": part.total, x: fixed(left), width: fixed(Math.max(right - left - 1, 1)), y: fixed(y(part.to)), height: fixed(Math.max(y(part.from) - y(part.to), 0.5))}, s("title", {}, words)));
|
||||
}
|
||||
}
|
||||
const legend = stack.agents.map((agent) => h("span", {class: `res-key res-c${tones.get(agent)}`, "data-series": agent}, `${agent} ${compactCount(stack.totals[agent])}`));
|
||||
return {svg: frame.svg, legend};
|
||||
}
|
||||
|
||||
function hardwareSeries(samples) {
|
||||
const tones = new Map();
|
||||
return Resources.hardware(samples).map((line) => {
|
||||
if (!tones.has(line.name)) {
|
||||
tones.set(line.name, tones.size % RESOURCE.tones);
|
||||
}
|
||||
const last = [...line.points].reverse().find(([, value]) => value !== null);
|
||||
return {...line, id: ViewStore.id(line.kind, line.name), label: `${resourceLabel(line)} ${dash(last === undefined ? null : last[1])} %`, tone: `res-c${tones.get(line.name)} k-${line.kind}`};
|
||||
});
|
||||
}
|
||||
|
||||
function agentSeries(tokens) {
|
||||
const full = Resources.stack(tokens);
|
||||
const tones = new Map(full.agents.map((agent, index) => [agent, index % RESOURCE.tones]));
|
||||
const list = full.agents.map((agent) => ({id: ViewStore.id("agent", agent), agent, label: `${agent} ${compactCount(full.totals[agent])}`, tone: `res-c${tones.get(agent)}`}));
|
||||
return {list, tones};
|
||||
}
|
||||
|
||||
function burnSeries(span) {
|
||||
const picked = Plan.scoped(state.tasks, "filter", {filtered: state.tasks, since: span.from, until: span.to, now: state.now, holders: {}});
|
||||
const scaled = Resources.burnup(Plan.burn(picked.rows, {estimation: settings.estimation, from: span.from, to: span.to}).points);
|
||||
return Object.entries(RESOURCE_BURN).map(([name, word]) => {
|
||||
const points = scaled[name];
|
||||
const last = points.length ? points[points.length - 1][1] : 0;
|
||||
return {id: ViewStore.id("burn", name), name, points, label: `${word} ${Math.round(last)} %`, tone: `res-burn k-${name}`, top: scaled.top};
|
||||
});
|
||||
}
|
||||
|
||||
function shownRows(tokens, agents, hidden) {
|
||||
const gone = new Set(agents.list.filter((entry) => hidden.has(entry.id)).map((entry) => entry.agent));
|
||||
return tokens.filter((row) => !gone.has(row.agent));
|
||||
}
|
||||
|
||||
function hardwareChart(entry, parts, span, size) {
|
||||
const frame = resourceFrame(entry, span, size, false);
|
||||
const y = resourceScale(frame, [0, 50, 100], percentOf, "left");
|
||||
resourceBackdrop(frame, y, parts.marks, span);
|
||||
resourceLines(frame, y, parts.hardware.filter((line) => !parts.hidden.has(line.id)));
|
||||
return {svg: frame.svg, series: parts.hardware, path: "resources.hidden"};
|
||||
}
|
||||
|
||||
function tokensChart(entry, parts, span, size) {
|
||||
const frame = resourceFrame(entry, span, size, false);
|
||||
const plot = tokenPlot(shownRows(parts.tokens, parts.agents, parts.hidden), parts.mode);
|
||||
const y = resourceScale(frame, [0, plot.top], compactCount, "left");
|
||||
tokenMarks(frame, y, plot, parts.agents.tones, span, false);
|
||||
return {svg: frame.svg, series: parts.agents.list, path: "resources.hidden"};
|
||||
}
|
||||
|
||||
function combinedChart(entry, parts, span, size) {
|
||||
const frame = resourceFrame(entry, span, size, true);
|
||||
const y = resourceScale(frame, [0, 50, 100], percentOf, "left");
|
||||
resourceBackdrop(frame, y, parts.marks, span);
|
||||
const plot = tokenPlot(shownRows(parts.tokens, parts.agents, parts.combinedHidden), parts.mode);
|
||||
tokenMarks(frame, resourceScale(frame, [0, plot.top], compactCount, "right"), plot, parts.agents.tones, span, true);
|
||||
resourceLines(frame, y, parts.hardware.filter((line) => !parts.combinedHidden.has(line.id)));
|
||||
for (const line of parts.burn.filter((item) => !parts.combinedHidden.has(item.id) && item.points.length)) {
|
||||
frame.svg.append(s("path", {class: line.tone, "data-id": line.id, "data-series": line.name, "data-last": line.points[line.points.length - 1][1], "data-top": line.top, d: resourceSteps(frame, y, line.points)}, s("title", {}, `${line.label} of the largest scope in the window (${line.top})`)));
|
||||
}
|
||||
return {svg: frame.svg, series: [...parts.hardware, ...parts.agents.list, ...parts.burn], path: "resources.combinedHidden"};
|
||||
}
|
||||
|
||||
const RESOURCE_CHARTS = {hardware: hardwareChart, tokens: tokensChart, combined: combinedChart};
|
||||
|
||||
function resourceParts(data, view, span) {
|
||||
return {
|
||||
hardware: hardwareSeries(data.samples),
|
||||
agents: agentSeries(data.tokens),
|
||||
tokens: data.tokens,
|
||||
mode: view.tokens,
|
||||
hidden: new Set(view.hidden),
|
||||
combinedHidden: new Set(view.combinedHidden),
|
||||
burn: view.combined ? burnSeries(span) : [],
|
||||
marks: [
|
||||
...Resources.waits(data.samples).map((mark) => ({...mark, kind: "wait"})),
|
||||
...["asleep", "unknown"].flatMap((kind) => Resources[kind](data.samples).flatMap((gpu) => gpu.spans.map((mark) => ({...mark, kind, name: gpu.name})))),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
function drawResources() {
|
||||
const dom = resourceView.dom;
|
||||
const data = resourceView.data;
|
||||
if (!dom || !dom.count.isConnected || data === null) {
|
||||
if (!dom || !dom.count.isConnected) {
|
||||
return;
|
||||
}
|
||||
const view = ViewStore.get("resources");
|
||||
dom.switches.tokens.setAttribute("aria-pressed", String(view.tokens === "accumulated"));
|
||||
dom.switches.combined.setAttribute("aria-pressed", String(view.combined));
|
||||
for (const entry of dom.panels) {
|
||||
entry.panel.hidden = (entry.kind === "combined") !== view.combined;
|
||||
}
|
||||
if (data === null) {
|
||||
return;
|
||||
}
|
||||
const active = document.activeElement;
|
||||
const focused = active && typeof active.getAttribute === "function" ? active.getAttribute("data-chip") : null;
|
||||
const span = {from: data.since, to: Math.max(data.until, data.since + 60)};
|
||||
const lines = Resources.lines(data.samples);
|
||||
const marks = {
|
||||
disks: [],
|
||||
host: Resources.waits(data.samples).map((mark) => ({...mark, kind: "wait"})),
|
||||
gpu: ["asleep", "unknown"].flatMap((kind) => Resources[kind](data.samples).flatMap((gpu) => gpu.spans.map((mark) => ({...mark, kind, name: gpu.name})))),
|
||||
};
|
||||
const parts = resourceParts(data, view, span);
|
||||
dom.count.textContent = String(data.samples.length);
|
||||
for (const entry of dom.panels) {
|
||||
const box = entry.plot.getBoundingClientRect();
|
||||
const size = {width: Math.round(box.width), height: Math.round(box.height)};
|
||||
if (size.width < RESOURCE.left + RESOURCE.right + 10 || size.height < RESOURCE.axis + 10) {
|
||||
if (entry.panel.hidden) {
|
||||
entry.plot.replaceChildren();
|
||||
entry.legend.replaceChildren();
|
||||
continue;
|
||||
}
|
||||
const drawn = entry.kind === "tokens" ? resourceTokens(entry, Resources.stack(data.tokens), span, size) : resourcePercent(entry, lines[entry.kind], marks[entry.kind], span, size);
|
||||
entry.plot.replaceChildren(drawn.svg);
|
||||
entry.legend.replaceChildren(...drawn.legend);
|
||||
const box = entry.plot.getBoundingClientRect();
|
||||
const size = {width: Math.round(box.width), height: Math.round(box.height)};
|
||||
const drawn = RESOURCE_CHARTS[entry.kind](entry, parts, span, size);
|
||||
const small = size.width < RESOURCE.left + RESOURCE.wide + 10 || size.height < RESOURCE.axis + 10;
|
||||
entry.plot.replaceChildren(...(small ? [] : [drawn.svg]));
|
||||
entry.legend.replaceChildren(LegendChips.render(drawn.series, drawn.path, "hidden"));
|
||||
const chip = focused === null ? null : [...entry.legend.querySelectorAll("[data-chip]")].find((node) => node.getAttribute("data-chip") === focused);
|
||||
if (chip) {
|
||||
chip.focus();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ const Resources = (() => {
|
||||
const CALM = 50;
|
||||
const GAP = 1800;
|
||||
const STEP = 60;
|
||||
const HOUR = 3600;
|
||||
const COUNTS = ["input", "output", "cache_read", "cache_write"];
|
||||
const ZONES = [{kind: "idle", from: 0, to: IDLE}, {kind: "saturated", from: SATURATED, to: 100}];
|
||||
const finite = (value) => typeof value === "number" && Number.isFinite(value);
|
||||
@@ -34,6 +35,15 @@ const Resources = (() => {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param samples - sampler rows.
|
||||
* @returns every percent line of lines() on one axis: the disks, then CPU, RAM and swap, then each GPU and its VRAM.
|
||||
*/
|
||||
function hardware(samples) {
|
||||
const found = lines(samples);
|
||||
return [...found.disks, ...found.host, ...found.gpu];
|
||||
}
|
||||
|
||||
function segments(points) {
|
||||
const found = [];
|
||||
let current = [];
|
||||
@@ -137,7 +147,38 @@ const Resources = (() => {
|
||||
return {agents, totals: Object.fromEntries(totals), bars, top: Math.max(0, ...bars.map((bar) => bar.total))};
|
||||
}
|
||||
|
||||
return {IDLE, SATURATED, CALM, GAP, COUNTS, ZONES, lines, segments, spans, waits, asleep, unknown, stack};
|
||||
/**
|
||||
* @param tokens - token rows as stack() takes them.
|
||||
* @returns {agents, lines: [{agent, points: [[t, sum]]}], top}: per agent its running sum, 0 at the first hour's
|
||||
* start and then at the end of every hour any agent spent in, so a line never decreases; top is the largest sum.
|
||||
*/
|
||||
function accumulate(tokens) {
|
||||
const {agents, bars} = stack(tokens);
|
||||
const sums = new Map(agents.map((agent) => [agent, 0]));
|
||||
const found = new Map(agents.map((agent) => [agent, bars.length ? [[bars[0].t, 0]] : []]));
|
||||
for (const bar of bars) {
|
||||
for (const part of bar.parts) {
|
||||
sums.set(part.agent, sums.get(part.agent) + part.total);
|
||||
}
|
||||
for (const agent of agents) {
|
||||
found.get(agent).push([bar.t + HOUR, sums.get(agent)]);
|
||||
}
|
||||
}
|
||||
return {agents, lines: agents.map((agent) => ({agent, points: found.get(agent)})), top: Math.max(0, ...sums.values())};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param points - Plan.burn() points of the window.
|
||||
* @returns {top, done, scope}: the largest scope in the window and the done and scope series as [t, percent of
|
||||
* that scope]; both stay 0 while the scope is empty.
|
||||
*/
|
||||
function burnup(points) {
|
||||
const top = Math.max(0, ...points.map((point) => point.scope));
|
||||
const percent = (value) => (top > 0 ? (100 * value) / top : 0);
|
||||
return {top, done: points.map((point) => [point.t, percent(point.done)]), scope: points.map((point) => [point.t, percent(point.scope)])};
|
||||
}
|
||||
|
||||
return {IDLE, SATURATED, CALM, GAP, HOUR, COUNTS, ZONES, lines, hardware, segments, spans, waits, asleep, unknown, stack, accumulate, burnup};
|
||||
})();
|
||||
if (typeof module === "object" && module.exports) {
|
||||
module.exports = Resources;
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
.legend-chips { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 6px; min-width: 0; }
|
||||
.legend-chip { display: inline-flex; align-items: center; gap: 5px; max-width: 260px; padding: 1px 8px; overflow: hidden; border: 1px solid var(--line); border-radius: 999px; background: transparent; color: var(--text); font: inherit; font-size: var(--fs-xs); font-variant-numeric: tabular-nums; line-height: 18px; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
|
||||
.legend-chip::before { content: ""; flex: none; width: 12px; height: 3px; border-radius: 2px; background: var(--c, var(--muted)); }
|
||||
.legend-chip.k-vram::before, .legend-chip.k-scope::before { background: repeating-linear-gradient(90deg, var(--c) 0 4px, transparent 4px 6px); }
|
||||
.legend-chip:hover { border-color: var(--line-strong); }
|
||||
.legend-chip:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
|
||||
.legend-chip[aria-pressed="false"] { color: var(--muted); text-decoration: line-through; opacity: 0.5; }
|
||||
@@ -1,27 +1,32 @@
|
||||
body.fit .graph.resources > .res-panels { flex: 1; }
|
||||
.res-panels { display: flex; flex-direction: column; gap: 8px; min-height: 0; }
|
||||
.res-panel { display: flex; flex: 1 1 0; flex-direction: column; gap: 2px; min-height: 72px; padding: 6px 10px; overflow: hidden; }
|
||||
.res-head { display: flex; align-items: center; gap: 4px 12px; min-width: 0; overflow: hidden; color: var(--muted); font-size: var(--fs-xs); white-space: nowrap; }
|
||||
.res-panel[hidden] { display: none; }
|
||||
.res-head { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px; min-width: 0; color: var(--muted); font-size: var(--fs-xs); }
|
||||
.res-title { display: inline-flex; color: var(--text); font-size: var(--fs-md); }
|
||||
.res-legend { display: flex; gap: 4px 12px; min-width: 0; overflow: hidden; font-variant-numeric: tabular-nums; }
|
||||
.res-key { display: inline-flex; align-items: center; gap: 5px; }
|
||||
.res-key::before { content: ""; width: 12px; height: 3px; border-radius: 2px; background: var(--c); }
|
||||
.res-key.k-vram::before { background: repeating-linear-gradient(90deg, var(--c) 0 4px, transparent 4px 6px); }
|
||||
.res-legend { display: flex; flex: 1; min-width: 0; font-variant-numeric: tabular-nums; }
|
||||
.res-switch[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); }
|
||||
.res-plot { position: relative; flex: 1; min-height: 0; }
|
||||
.res-plot > svg { position: absolute; inset: 0; width: 100%; height: 100%; }
|
||||
svg .band.idle { fill: var(--ok-soft); }
|
||||
svg .band.saturated { fill: var(--bad-soft); }
|
||||
svg .res-line { fill: none; stroke: var(--c); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
|
||||
svg .res-line.k-vram { stroke-dasharray: 5 3; }
|
||||
svg .res-sum { fill: none; stroke: var(--c); stroke-width: 2; stroke-linejoin: round; }
|
||||
svg .res-burn { fill: none; stroke: var(--c); stroke-width: 2.4; stroke-linejoin: round; }
|
||||
svg .res-burn.k-scope { stroke-dasharray: 7 4; }
|
||||
svg .res-mark.wait { fill: var(--warn-soft); stroke: var(--warn); stroke-dasharray: 3 3; }
|
||||
svg .res-mark.asleep { fill: var(--neutral-soft); }
|
||||
svg .res-mark.unknown { fill: var(--neutral-soft); stroke: var(--muted); stroke-dasharray: 2 3; }
|
||||
svg .res-icon.wait { fill: var(--warn); }
|
||||
svg .res-icon.asleep, svg .res-icon.unknown { fill: var(--muted); }
|
||||
svg .res-bar { fill: var(--c); }
|
||||
svg .res-bar.faint { opacity: 0.35; }
|
||||
.res-c0 { --c: var(--info); }
|
||||
.res-c1 { --c: var(--ok); }
|
||||
.res-c2 { --c: var(--accent); }
|
||||
.res-c3 { --c: var(--warn); }
|
||||
.res-c4 { --c: var(--violet); }
|
||||
.res-c5 { --c: var(--bad); }
|
||||
.res-burn.k-done { --c: var(--ok); }
|
||||
.res-burn.k-scope { --c: var(--text); }
|
||||
@@ -0,0 +1,259 @@
|
||||
const ViewStore = (() => {
|
||||
"use strict";
|
||||
const ROUTE = "/api/view";
|
||||
const DELAY = 250;
|
||||
const TRIES = 3;
|
||||
const ID_LIMIT = 200;
|
||||
const HASHED = 8;
|
||||
const ESCAPE = /[^A-Za-z0-9./-]/gu;
|
||||
const LEAF = /^[A-Za-z]{1,32}\.[A-Za-z]{1,32}$/u;
|
||||
const HEADERS = {"X-Kvb-Orchestrator": "1", "Content-Type": "application/json"};
|
||||
const store = {doc: null, saved: null, flight: new Map(), pending: new Map(), listeners: [], timer: 0, chain: Promise.resolve(), waiting: [], error: "", leaving: false};
|
||||
const clone = (value) => JSON.parse(JSON.stringify(value));
|
||||
const related = (one, other) => one === "" || other === "" || one === other || one.startsWith(`${other}.`) || other.startsWith(`${one}.`);
|
||||
|
||||
function slot(path, doc) {
|
||||
const keys = String(path).split(".");
|
||||
let parent = null;
|
||||
let node = doc;
|
||||
for (const key of keys) {
|
||||
if (node === null || typeof node !== "object" || Array.isArray(node) || !Object.hasOwn(node, key)) {
|
||||
throw new Error(`unknown view path ${path}`);
|
||||
}
|
||||
parent = node;
|
||||
node = node[key];
|
||||
}
|
||||
return {parent, key: keys[keys.length - 1], value: node};
|
||||
}
|
||||
|
||||
const fold = (changes, base) => changes.reduce((value, change) => clone(change(clone(value))), base);
|
||||
|
||||
function rebuild() {
|
||||
const next = clone(store.saved);
|
||||
for (const [path, entry] of store.flight) {
|
||||
const found = slot(path, next);
|
||||
found.parent[found.key] = clone(entry.value);
|
||||
}
|
||||
for (const [path, changes] of store.pending) {
|
||||
const found = slot(path, next);
|
||||
found.parent[found.key] = fold(changes, found.value);
|
||||
}
|
||||
store.doc = next;
|
||||
}
|
||||
|
||||
function notify(path) {
|
||||
for (const entry of [...store.listeners]) {
|
||||
if (related(entry.path, path)) {
|
||||
entry.callback(get(entry.path));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param doc - the whole view document the hub served.
|
||||
*/
|
||||
function start(doc) {
|
||||
store.saved = clone(doc);
|
||||
store.flight = new Map();
|
||||
store.pending = new Map();
|
||||
rebuild();
|
||||
notify("");
|
||||
}
|
||||
|
||||
function current() {
|
||||
if (store.doc === null) {
|
||||
start(JSON.parse(document.getElementById("view").textContent));
|
||||
}
|
||||
return store.doc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param path - a dotted key of the view document, such as resources.hidden.
|
||||
* @returns a copy of its value as this page shows it, its own unsent changes included.
|
||||
*/
|
||||
const get = (path) => clone(slot(path, current()).value);
|
||||
|
||||
function adopt(doc) {
|
||||
store.saved = clone(doc);
|
||||
rebuild();
|
||||
notify("");
|
||||
}
|
||||
|
||||
async function send(body, keepalive) {
|
||||
try {
|
||||
const response = await fetch(ROUTE, {method: "PATCH", headers: HEADERS, body: JSON.stringify(body), cache: "no-store", keepalive});
|
||||
if (response.ok || response.status === 409) {
|
||||
return {status: response.ok ? 200 : 409, data: await response.json()};
|
||||
}
|
||||
return {status: response.status, error: (await response.text()).trim() || String(response.status)};
|
||||
} catch (error) {
|
||||
return {status: 0, error: String(error && error.message ? error.message : error)};
|
||||
}
|
||||
}
|
||||
|
||||
function takeOff() {
|
||||
const body = {set: {}, expect: {}};
|
||||
for (const [path, changes] of store.pending) {
|
||||
const base = slot(path, store.saved).value;
|
||||
const value = fold(changes, base);
|
||||
store.flight.set(path, {changes, value});
|
||||
body.set[path] = value;
|
||||
body.expect[path] = base;
|
||||
}
|
||||
store.pending = new Map();
|
||||
return body;
|
||||
}
|
||||
|
||||
function land() {
|
||||
const back = new Map([...store.flight].map(([path, entry]) => [path, [...entry.changes, ...(store.pending.get(path) || [])]]));
|
||||
store.flight = new Map();
|
||||
for (const [path, changes] of store.pending) {
|
||||
if (!back.has(path)) {
|
||||
back.set(path, changes);
|
||||
}
|
||||
}
|
||||
store.pending = back;
|
||||
}
|
||||
|
||||
async function flush() {
|
||||
const waiting = store.waiting.splice(0);
|
||||
let stored = true;
|
||||
for (let attempt = 1; store.pending.size; attempt += 1) {
|
||||
const answer = await send(takeOff(), false);
|
||||
if (answer.status === 200) {
|
||||
store.flight = new Map();
|
||||
store.error = "";
|
||||
adopt(answer.data);
|
||||
break;
|
||||
}
|
||||
if (answer.status === 409 && attempt < TRIES) {
|
||||
land();
|
||||
adopt(answer.data.view);
|
||||
continue;
|
||||
}
|
||||
store.error = answer.status === 409 ? `another browser kept changing ${answer.data.paths.join(", ")}; try again` : answer.error;
|
||||
store.flight = new Map();
|
||||
adopt(answer.status === 409 ? answer.data.view : store.saved);
|
||||
stored = false;
|
||||
break;
|
||||
}
|
||||
waiting.forEach((resolve) => resolve(stored));
|
||||
}
|
||||
|
||||
function leave() {
|
||||
clearTimeout(store.timer);
|
||||
store.timer = 0;
|
||||
if (!store.pending.size) {
|
||||
return;
|
||||
}
|
||||
const body = {set: {}, expect: {}};
|
||||
for (const [path, changes] of store.pending) {
|
||||
const base = store.flight.has(path) ? store.flight.get(path).value : slot(path, store.saved).value;
|
||||
body.set[path] = fold(changes, base);
|
||||
body.expect[path] = base;
|
||||
}
|
||||
store.pending = new Map();
|
||||
const waiting = store.waiting.splice(0);
|
||||
send(body, true).then((answer) => {
|
||||
if (answer.status === 200) {
|
||||
adopt(answer.data);
|
||||
}
|
||||
waiting.forEach((resolve) => resolve(answer.status === 200));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param path - a section.key of the view document, such as home.pins.
|
||||
* @param change - a function from the key's current value to its new one; when another browser changed the key
|
||||
* meanwhile, the hub refuses the write and the page runs change again on the stored value, up to three times.
|
||||
* @returns a promise of true once the hub stored the change, false when it refused it; the page then shows the
|
||||
* stored value again and error() says why.
|
||||
*/
|
||||
function update(path, change) {
|
||||
if (!LEAF.test(String(path))) {
|
||||
throw new Error(`a view path to change must be section.key, not ${path}`);
|
||||
}
|
||||
slot(path, current());
|
||||
store.pending.set(path, [...(store.pending.get(path) || []), change]);
|
||||
rebuild();
|
||||
notify(path);
|
||||
if (!store.leaving && typeof addEventListener === "function") {
|
||||
store.leaving = true;
|
||||
addEventListener("pagehide", leave);
|
||||
}
|
||||
clearTimeout(store.timer);
|
||||
store.timer = setTimeout(() => {
|
||||
store.timer = 0;
|
||||
store.chain = store.chain.then(flush);
|
||||
}, DELAY);
|
||||
return new Promise((resolve) => store.waiting.push(resolve));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param path - a section.key of the view document that holds a scalar; a list path throws, it changes through
|
||||
* update() only.
|
||||
* @param value - its new value, replacing whatever another browser stored there.
|
||||
* @returns the promise update() returns.
|
||||
*/
|
||||
function set(path, value) {
|
||||
if (Array.isArray(slot(path, current()).value)) {
|
||||
throw new Error(`a list view path changes through update(), not set(): ${path}`);
|
||||
}
|
||||
return update(path, () => clone(value));
|
||||
}
|
||||
|
||||
async function restore(event) {
|
||||
if (!event.persisted) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const response = await fetch(ROUTE, {method: "GET", cache: "no-store"});
|
||||
if (response.ok) {
|
||||
adopt(await response.json());
|
||||
}
|
||||
} catch (error) {
|
||||
store.error = String(error && error.message ? error.message : error);
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof addEventListener === "function") {
|
||||
addEventListener("pageshow", restore);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param path - a dotted key of the view document.
|
||||
* @param callback - called with the key's new value whenever it, a key inside it or one around it changes.
|
||||
* @returns a function that stops the calls.
|
||||
*/
|
||||
function on(path, callback) {
|
||||
slot(path, current());
|
||||
const entry = {path: String(path), callback};
|
||||
store.listeners.push(entry);
|
||||
return () => {
|
||||
store.listeners = store.listeners.filter((item) => item !== entry);
|
||||
};
|
||||
}
|
||||
|
||||
function hash(text) {
|
||||
let value = 0x811c9dc5;
|
||||
for (const char of text) {
|
||||
value = Math.imul(value ^ char.codePointAt(0), 0x01000193) >>> 0;
|
||||
}
|
||||
return value.toString(16).padStart(HASHED, "0");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param parts - the names that make one series, such as its kind and its name.
|
||||
* @returns an id of the view document's grammar: every part with each character outside A-Z, a-z, 0-9 and ./-
|
||||
* written as _<hex>_, joined by ":", and cut to 200 characters with a hash of the whole when longer.
|
||||
*/
|
||||
function id(...parts) {
|
||||
const text = parts.map((part) => String(part).replace(ESCAPE, (char) => `_${char.codePointAt(0).toString(16)}_`)).join(":");
|
||||
return text.length <= ID_LIMIT ? text : `${text.slice(0, ID_LIMIT - HASHED - 1)}:${hash(text)}`;
|
||||
}
|
||||
|
||||
return {ROUTE, DELAY, TRIES, start, get, set, update, on, id, error: () => store.error};
|
||||
})();
|
||||
if (typeof module === "object" && module.exports) {
|
||||
module.exports = ViewStore;
|
||||
}
|
||||
@@ -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"})
|
||||
|
||||
+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)
|
||||
|
||||
@@ -0,0 +1,657 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
import json
|
||||
import re
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
from hub_case import HubCase
|
||||
from hub_support import HOST_STATE, HUB, server
|
||||
from kvb_orchestrator import records
|
||||
from kvb_orchestrator.errors import Conflict, ToolError
|
||||
from kvb_orchestrator.handler import BODY_LIMIT
|
||||
from kvb_orchestrator.viewstore import (
|
||||
CHANGE_LIMIT,
|
||||
VIEW,
|
||||
VIEW_LIMIT,
|
||||
ViewStore,
|
||||
valid_view,
|
||||
value_at,
|
||||
)
|
||||
|
||||
STORE = HUB / "templates" / "view-store.js"
|
||||
CHIPS = HUB / "templates" / "legend-chips.js"
|
||||
CHIPS_STYLE = HUB / "templates" / "style-legend-chips.css"
|
||||
FULL = {
|
||||
"resources": {"hidden": ["host:CPU", "agent:Stage", "disk:/mnt_20_data"], "tokens": "accumulated", "combined": True, "combinedHidden": ["burn:scope"]},
|
||||
"burn": {"overlays": ["fix", "score"]},
|
||||
"home": {"pins": [{"kind": "panel", "id": "resources-hardware", "w": 12, "h": 4}, {"kind": "task", "id": "work/feature", "w": 1, "h": 12}]},
|
||||
}
|
||||
PIN = {"kind": "panel", "id": "resources-hardware", "w": 6, "h": 3}
|
||||
EMBEDDED = re.compile(r'<script nonce="[^"]+" id="view" type="application/json">(.*?)</script>')
|
||||
MAKE = """
|
||||
const make = (tag) => {
|
||||
const node = {tag, attrs: {}, children: [], listeners: {}, textContent: "",
|
||||
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); }};
|
||||
return node;
|
||||
};
|
||||
"""
|
||||
CLIENT_PROBE = """
|
||||
const fs = require("fs");
|
||||
const vm = require("vm");
|
||||
const doc = JSON.parse(process.argv[2]);
|
||||
const clone = (value) => JSON.parse(JSON.stringify(value));
|
||||
const same = (one, other) => JSON.stringify(one) === JSON.stringify(other);
|
||||
const server = clone(doc);
|
||||
const sent = [];
|
||||
const hooks = {};
|
||||
let refuse = false;
|
||||
let churn = 0;
|
||||
let hold = null;
|
||||
let gets = 0;
|
||||
const at = (path) => path.split(".").reduce((node, key) => node[key], server);
|
||||
const context = {
|
||||
setTimeout, clearTimeout,
|
||||
addEventListener: (name, run) => {
|
||||
hooks[name] = (hooks[name] || 0) + 1;
|
||||
hooks[`${name}:run`] = run;
|
||||
},
|
||||
document: {getElementById: (name) => (name === "view" ? {textContent: JSON.stringify(doc)} : null)},
|
||||
fetch: async (url, options) => {
|
||||
if (options.method === "GET") {
|
||||
gets += 1;
|
||||
return {ok: true, status: 200, json: async () => clone(server)};
|
||||
}
|
||||
const body = JSON.parse(options.body);
|
||||
sent.push({url, method: options.method, headers: options.headers, keepalive: options.keepalive, body});
|
||||
if (hold) {
|
||||
await hold;
|
||||
}
|
||||
if (refuse) {
|
||||
refuse = false;
|
||||
return {ok: false, status: 400, text: async () => "resources.tokens must be one of interval, accumulated\\n"};
|
||||
}
|
||||
if (churn && Object.hasOwn(body.set, "resources.hidden")) {
|
||||
server.resources.hidden = [...server.resources.hidden, `churn${churn}`];
|
||||
churn += 1;
|
||||
}
|
||||
const moved = Object.keys(body.set).filter((path) => !same(at(path), body.expect[path]));
|
||||
if (moved.length) {
|
||||
return {ok: false, status: 409, json: async () => ({view: clone(server), paths: moved})};
|
||||
}
|
||||
for (const [path, value] of Object.entries(body.set)) {
|
||||
const [section, key] = path.split(".");
|
||||
server[section][key] = value;
|
||||
}
|
||||
return {ok: true, status: 200, json: async () => clone(server)};
|
||||
},
|
||||
};
|
||||
vm.runInNewContext(`${fs.readFileSync(process.argv[1], "utf8")}\\nstore = ViewStore;`, context);
|
||||
const V = context.store;
|
||||
const heard = [];
|
||||
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
const caught = (run) => {
|
||||
try {
|
||||
run();
|
||||
} catch (error) {
|
||||
return error.message;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
(async () => {
|
||||
V.on("resources.hidden", (value) => heard.push(["hidden", value]));
|
||||
V.on("home.pins", (value) => heard.push(["pins", value]));
|
||||
const unknown = caught(() => V.get("resources.nope"));
|
||||
const wide = caught(() => V.update("resources", (value) => value));
|
||||
const listSet = caught(() => V.set("resources.hidden", ["host:RAM"]));
|
||||
const first = V.update("resources.hidden", (list) => [...list, "host:CPU"]);
|
||||
const second = V.set("resources.tokens", "accumulated");
|
||||
const local = [V.get("resources.hidden"), V.get("resources.tokens"), sent.length, clone(heard)];
|
||||
const stored = await Promise.all([first, second]);
|
||||
server.home.pins = [@PIN@];
|
||||
server.burn.overlays = ["fix"];
|
||||
const combined = await V.set("resources.combined", true);
|
||||
const adopted = [V.get("home.pins"), V.get("burn.overlays"), V.get("resources.hidden"), heard.filter(([name]) => name === "pins").at(-1)];
|
||||
server.resources.hidden = ["host:CPU", "host:RAM"];
|
||||
const rebased = await V.update("resources.hidden", (list) => [...list, "disk:/"]);
|
||||
const merged = V.get("resources.hidden");
|
||||
churn = 1;
|
||||
const churned = await V.update("resources.hidden", (list) => list.filter((item) => item !== "disk:/"));
|
||||
churn = 0;
|
||||
const gaveUp = [V.get("resources.hidden"), V.error()];
|
||||
refuse = true;
|
||||
let release = null;
|
||||
hold = new Promise((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
const refusing = V.set("resources.tokens", "interval");
|
||||
await wait(400);
|
||||
const newer = V.update("resources.combinedHidden", (list) => [...list, "burn:scope"]);
|
||||
hold = null;
|
||||
release();
|
||||
const refused = await refusing;
|
||||
const during = [V.get("resources.tokens"), V.get("resources.combinedHidden"), V.error()];
|
||||
const kept = await newer;
|
||||
hold = new Promise((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
const flying = V.update("burn.overlays", (list) => [...list.filter((item) => item !== "fix"), "score"]);
|
||||
await wait(400);
|
||||
const leaving = V.update("home.pins", (pins) => pins.filter((pin) => pin.id !== "@PIN_ID@"));
|
||||
const beforeLeave = sent.length;
|
||||
hooks["pagehide:run"]();
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
const atLeave = sent.length;
|
||||
hold = null;
|
||||
release();
|
||||
const landed = await Promise.all([flying, leaving]);
|
||||
await wait(400);
|
||||
const left = clone(server);
|
||||
server.resources.combined = false;
|
||||
await hooks["pageshow:run"]({persisted: false});
|
||||
const notRestored = [gets, V.get("resources.combined")];
|
||||
await hooks["pageshow:run"]({persisted: true});
|
||||
const restored = [gets, V.get("resources.combined"), hooks.pageshow];
|
||||
const long = "x".repeat(300);
|
||||
console.log(JSON.stringify({
|
||||
unknown, wide, listSet, local, stored, combined, adopted, rebased, merged, churned, gaveUp, refused, during, kept, beforeLeave, atLeave, landed, sent, server: left, hooks: hooks.pagehide, error: V.error(), notRestored, restored,
|
||||
ids: [V.id("host", "CPU"), V.id("disk", "/"), V.id("util", "0000:01:00.0"), V.id("agent", "Stage (this session) [5d88e9]"), V.id("a:b", "c"), V.id("a", "b:c"), V.id("a_3a_b"), V.id("agent", long), V.id("agent", `${long}y`), V.id("agent", "Ärger 🦫")],
|
||||
}));
|
||||
})();
|
||||
""".replace("@PIN@", json.dumps(PIN)).replace("@PIN_ID@", PIN["id"])
|
||||
TWO_TABS = (
|
||||
"""
|
||||
const fs = require("fs");
|
||||
const vm = require("vm");
|
||||
const code = `${fs.readFileSync(process.argv[1], "utf8")}\\n${fs.readFileSync(process.argv[2], "utf8")}`;
|
||||
const base = process.argv[3];
|
||||
@MAKE@
|
||||
const SERIES = {
|
||||
"resources.hidden": [{id: "host:CPU", label: "CPU", tone: "res-c0"}, {id: "host:RAM", label: "RAM", tone: "res-c1"}],
|
||||
"burn.overlays": [{id: "fix", label: "Fix", tone: "k-fix"}, {id: "score", label: "Score", tone: "k-score"}],
|
||||
};
|
||||
const MODE = {"resources.hidden": "hidden", "burn.overlays": "shown"};
|
||||
const tab = async () => {
|
||||
const doc = await (await fetch(`${base}/api/view`)).json();
|
||||
const groups = [];
|
||||
const context = {
|
||||
setTimeout, clearTimeout,
|
||||
document: {getElementById: () => ({textContent: JSON.stringify(doc)}), createElement: make, querySelectorAll: () => groups.flatMap((group) => group.children)},
|
||||
fetch: (url, options) => fetch(`${base}${url}`, {...options, headers: {...options.headers, Origin: base}}),
|
||||
};
|
||||
vm.runInNewContext(`${code}\\nstore = ViewStore;\\nchips = LegendChips;`, context);
|
||||
const draw = (path) => {
|
||||
const group = context.chips.render(SERIES[path], path, MODE[path]);
|
||||
groups.splice(0, groups.length, group);
|
||||
return group;
|
||||
};
|
||||
const press = (path, id) => {
|
||||
draw(path).children.find((chip) => chip.attrs["data-chip"] === id).listeners.click();
|
||||
return context.store.update(path, (list) => list);
|
||||
};
|
||||
const pressed = (path) => draw(path).children.map((chip) => [chip.attrs["data-chip"], chip.attrs["aria-pressed"]]);
|
||||
const pin = (id) => context.store.update("home.pins", (pins) => [...pins, {kind: "panel", id, w: 6, h: 3}]);
|
||||
const refused = (path, value) => {
|
||||
try {
|
||||
context.store.set(path, value);
|
||||
} catch (error) {
|
||||
return error.message;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
return {store: context.store, press, pressed, pin, refused};
|
||||
};
|
||||
const SCENARIOS = {
|
||||
merge: async (a, b) => {
|
||||
const refused = [a.refused("resources.hidden", ["host:RAM"]), b.refused("home.pins", [])];
|
||||
const results = [await a.press("resources.hidden", "host:CPU"), await b.press("resources.hidden", "host:RAM"), await a.pin("p1"), await b.pin("p2")];
|
||||
return {refused, results, seen: [a.store.get("resources.hidden"), b.store.get("home.pins").map((item) => item.id)]};
|
||||
},
|
||||
same: async (a, b) => {
|
||||
const results = [];
|
||||
for (const [path, id] of [["resources.hidden", "host:CPU"], ["burn.overlays", "fix"]]) {
|
||||
results.push(await a.press(path, id), await b.press(path, id));
|
||||
}
|
||||
return {results, chips: [b.pressed("resources.hidden"), b.pressed("burn.overlays")]};
|
||||
},
|
||||
};
|
||||
(async () => {
|
||||
const a = await tab();
|
||||
const b = await tab();
|
||||
const found = await SCENARIOS[process.argv[4]](a, b);
|
||||
const served = await (await fetch(`${base}/api/view`)).json();
|
||||
console.log(JSON.stringify({...found, errors: [a.store.error(), b.store.error()], hidden: served.resources.hidden, overlays: served.burn.overlays, pins: served.home.pins.map((item) => item.id)}));
|
||||
})();
|
||||
"""
|
||||
).replace("@MAKE@", MAKE)
|
||||
CHIPS_PROBE = (
|
||||
"""
|
||||
const fs = require("fs");
|
||||
const vm = require("vm");
|
||||
@MAKE@
|
||||
const updates = [];
|
||||
const groups = [];
|
||||
const lists = {"resources.hidden": ["b"], "burn.overlays": ["fix"]};
|
||||
const context = {
|
||||
document: {createElement: make, querySelectorAll: (selector) => (selector === ".legend-chip" ? groups.flatMap((group) => group.children) : [])},
|
||||
ViewStore: {get: (path) => [...lists[path]], update: (path, change) => { lists[path] = change([...lists[path]]); updates.push([path, lists[path]]); return Promise.resolve(true); }},
|
||||
};
|
||||
vm.runInNewContext(`${fs.readFileSync(process.argv[1], "utf8")}\\nchips = LegendChips;`, context);
|
||||
const L = context.chips;
|
||||
const group = L.render([{id: "a", label: "CPU 10 %", tone: "res-c0 k-host"}, {id: "b", label: "RAM 20 %", tone: "res-c1 k-host"}], "resources.hidden", "hidden");
|
||||
groups.push(group);
|
||||
const read = (node) => node.children.map((chip) => [chip.attrs["data-chip"], chip.attrs["aria-pressed"], chip.attrs.class, chip.textContent, chip.attrs.title]);
|
||||
const before = read(group);
|
||||
group.children[0].listeners.click();
|
||||
group.children[1].listeners.click();
|
||||
const after = read(group);
|
||||
lists["resources.hidden"] = [...Array.from({length: 100}, (_, index) => `old${index}`), "a"];
|
||||
group.children[1].listeners.click();
|
||||
const overlays = L.render([{id: "fix", label: "Fix", tone: "k-fix"}, {id: "score", label: "Score", tone: "k-score"}], "burn.overlays", "shown");
|
||||
const shownBefore = read(overlays);
|
||||
overlays.children[1].listeners.click();
|
||||
overlays.children[0].listeners.click();
|
||||
let bad = null;
|
||||
try {
|
||||
L.render([], "burn.overlays", "on");
|
||||
} catch (error) {
|
||||
bad = error.message;
|
||||
}
|
||||
const many = Array.from({length: 103}, (_, index) => `o${index}`);
|
||||
console.log(JSON.stringify({
|
||||
group: [group.tag, group.attrs.class, group.attrs.role], before, after, updates, keep: L.KEEP, shownBefore, shownAfter: read(overlays), overlaysGroup: overlays.attrs["aria-label"], bad,
|
||||
placed: [L.placed(["a"], "a", false), L.placed(["a"], "b", true), L.placed([], "a", true), L.placed(["a"], "a", true), L.placed([], "a", false)],
|
||||
pruned: [L.pruned(many, new Set(["o0", "o1"])), L.pruned(["x", "y"], new Set())],
|
||||
}));
|
||||
"""
|
||||
).replace("@MAKE@", MAKE)
|
||||
|
||||
|
||||
def change(values: dict, expected: dict) -> bytes:
|
||||
return json.dumps({"set": values, "expect": expected}).encode()
|
||||
|
||||
|
||||
class TestViewDocument(unittest.TestCase):
|
||||
def test_the_defaults_and_a_full_document_pass_unchanged(self):
|
||||
self.assertEqual(valid_view(copy.deepcopy(VIEW)), VIEW)
|
||||
self.assertEqual(valid_view(copy.deepcopy(FULL)), FULL)
|
||||
self.assertEqual(VIEW, {"resources": {"hidden": [], "tokens": "interval", "combined": False, "combinedHidden": []}, "burn": {"overlays": []}, "home": {"pins": []}})
|
||||
|
||||
def test_unknown_or_missing_keys_and_malformed_values_are_refused(self):
|
||||
def changed(section: str | None, key: str | None, value: object) -> dict:
|
||||
doc = copy.deepcopy(FULL)
|
||||
if section is None:
|
||||
doc[key] = value
|
||||
elif key is None:
|
||||
doc[section] = value
|
||||
elif value is KeyError:
|
||||
del doc[section][key]
|
||||
else:
|
||||
doc[section][key] = value
|
||||
return doc
|
||||
|
||||
pin = FULL["home"]["pins"][0]
|
||||
cases = {
|
||||
"not an object": [],
|
||||
"unknown top key": changed(None, "theme", "dark"),
|
||||
"unknown nested key": changed("resources", "extra", 1),
|
||||
"missing key": changed("resources", "combinedHidden", KeyError),
|
||||
"section not an object": changed("burn", None, ["fix"]),
|
||||
"tokens mode": changed("resources", "tokens", "sum"),
|
||||
"combined not a flag": changed("resources", "combined", 1),
|
||||
"hidden not a list": changed("resources", "hidden", "host:CPU"),
|
||||
"id with a space": changed("resources", "hidden", ["host CPU"]),
|
||||
"empty id": changed("resources", "combinedHidden", [""]),
|
||||
"id too long": changed("resources", "hidden", ["x" * 201]),
|
||||
"repeated id": changed("resources", "hidden", ["a", "a"]),
|
||||
"too many ids": changed("resources", "hidden", [f"id{index}" for index in range(501)]),
|
||||
"unknown overlay": changed("burn", "overlays", ["cost"]),
|
||||
"repeated overlay": changed("burn", "overlays", ["fix", "fix"]),
|
||||
"null overlay": changed("burn", "overlays", [None]),
|
||||
"65 pins": changed("home", "pins", [{**pin, "id": f"p{index}"} for index in range(65)]),
|
||||
"pin width 0": changed("home", "pins", [{**pin, "w": 0}]),
|
||||
"pin height 13": changed("home", "pins", [{**pin, "h": 13}]),
|
||||
"pin width true": changed("home", "pins", [{**pin, "w": True}]),
|
||||
"pin width 1.5": changed("home", "pins", [{**pin, "w": 1.5}]),
|
||||
"pin kind too long": changed("home", "pins", [{**pin, "kind": "k" * 33}]),
|
||||
"pin id too long": changed("home", "pins", [{**pin, "id": "i" * 201}]),
|
||||
"pin unknown key": changed("home", "pins", [{**pin, "x": 1}]),
|
||||
"pin twice": changed("home", "pins", [pin, dict(pin)]),
|
||||
}
|
||||
for name, doc in cases.items():
|
||||
with self.subTest(case=name), self.assertRaises(ToolError):
|
||||
valid_view(doc)
|
||||
|
||||
def test_sixty_four_pins_of_one_by_one_to_twelve_by_twelve_pass(self):
|
||||
doc = copy.deepcopy(VIEW)
|
||||
doc["home"]["pins"] = [{"kind": "panel", "id": f"p{index}", "w": 1 + index % 12, "h": 12 - index % 12} for index in range(64)]
|
||||
self.assertEqual(valid_view(doc), doc)
|
||||
|
||||
def store(self) -> ViewStore:
|
||||
folder = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(folder.cleanup)
|
||||
return ViewStore(Path(folder.name) / "view.json")
|
||||
|
||||
def test_two_tabs_changing_other_paths_both_land_in_the_store(self):
|
||||
store = self.store()
|
||||
store.patch(change({"home.pins": [PIN], "burn.overlays": ["fix"]}, {"home.pins": [], "burn.overlays": []}))
|
||||
found = store.patch(change({"resources.hidden": ["host:CPU"]}, {"resources.hidden": []}))
|
||||
expected = {**VIEW, "resources": {**VIEW["resources"], "hidden": ["host:CPU"]}, "burn": {"overlays": ["fix"]}, "home": {"pins": [PIN]}}
|
||||
self.assertEqual((found, ViewStore(store.file).get()), (expected, expected))
|
||||
|
||||
def test_a_stale_tab_on_the_same_path_gets_a_conflict_and_its_retry_keeps_both_changes(self):
|
||||
store = self.store()
|
||||
store.patch(change({"resources.hidden": ["host:CPU"], "home.pins": [PIN]}, {"resources.hidden": [], "home.pins": []}))
|
||||
before = store.file.read_bytes()
|
||||
second = {**PIN, "id": "p2"}
|
||||
with self.assertRaises(Conflict) as caught:
|
||||
store.patch(change({"resources.hidden": ["host:RAM"], "home.pins": [second]}, {"resources.hidden": [], "home.pins": []}))
|
||||
self.assertEqual(caught.exception.payload["paths"], ["resources.hidden", "home.pins"])
|
||||
self.assertEqual(value_at(caught.exception.payload["view"], "home.pins"), [PIN])
|
||||
self.assertEqual((store.file.read_bytes(), store.get()["resources"]["hidden"]), (before, ["host:CPU"]))
|
||||
found = store.patch(change({"resources.hidden": ["host:CPU", "host:RAM"], "home.pins": [PIN, second]}, {"resources.hidden": ["host:CPU"], "home.pins": [PIN]}))
|
||||
self.assertEqual((found["resources"]["hidden"], found["home"]["pins"]), (["host:CPU", "host:RAM"], [PIN, second]))
|
||||
|
||||
|
||||
class TestViewClient(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
done = subprocess.run(["node", "-e", CLIENT_PROBE, str(STORE), json.dumps(VIEW)], capture_output=True, text=True, timeout=60, check=True)
|
||||
cls.data = json.loads(done.stdout)
|
||||
chips = subprocess.run(["node", "-e", CHIPS_PROBE, str(CHIPS)], capture_output=True, text=True, timeout=60, check=True)
|
||||
cls.chips = json.loads(chips.stdout)
|
||||
|
||||
def bodies(self) -> list[dict]:
|
||||
return [entry["body"] for entry in self.data["sent"]]
|
||||
|
||||
def test_set_changes_the_value_at_once_and_sends_one_debounced_patch_against_the_stored_values(self):
|
||||
data = self.data
|
||||
self.assertEqual(data["unknown"], "unknown view path resources.nope")
|
||||
self.assertEqual(data["wide"], "a view path to change must be section.key, not resources")
|
||||
self.assertEqual(data["local"], [["host:CPU"], "accumulated", 0, [["hidden", ["host:CPU"]]]])
|
||||
self.assertEqual(data["stored"], [True, True])
|
||||
first = data["sent"][0]
|
||||
self.assertEqual((first["url"], first["method"], first["headers"], first["keepalive"]), ("/api/view", "PATCH", {"X-Kvb-Orchestrator": "1", "Content-Type": "application/json"}, False))
|
||||
self.assertEqual(self.bodies()[:2], [
|
||||
{"set": {"resources.hidden": ["host:CPU"], "resources.tokens": "accumulated"}, "expect": {"resources.hidden": [], "resources.tokens": "interval"}},
|
||||
{"set": {"resources.combined": True}, "expect": {"resources.combined": False}},
|
||||
])
|
||||
|
||||
def test_an_older_tab_adopts_what_another_browser_stored_on_other_paths(self):
|
||||
self.assertTrue(self.data["combined"])
|
||||
self.assertEqual(self.data["adopted"], [[PIN], ["fix"], ["host:CPU"], ["pins", [PIN]]])
|
||||
|
||||
def test_a_conflict_on_the_same_path_reruns_the_change_on_the_stored_value_and_keeps_both(self):
|
||||
self.assertTrue(self.data["rebased"])
|
||||
self.assertEqual(self.data["merged"], ["host:CPU", "host:RAM", "disk:/"])
|
||||
self.assertEqual(self.bodies()[2:4], [
|
||||
{"set": {"resources.hidden": ["host:CPU", "disk:/"]}, "expect": {"resources.hidden": ["host:CPU"]}},
|
||||
{"set": {"resources.hidden": ["host:CPU", "host:RAM", "disk:/"]}, "expect": {"resources.hidden": ["host:CPU", "host:RAM"]}},
|
||||
])
|
||||
|
||||
def test_three_conflicts_in_a_row_give_up_with_the_stored_value_and_a_reason(self):
|
||||
self.assertFalse(self.data["churned"])
|
||||
self.assertEqual(len([body for body in self.bodies()[4:7] if "resources.hidden" in body["set"]]), 3)
|
||||
self.assertEqual(self.data["gaveUp"], [["host:CPU", "host:RAM", "disk:/", "churn1", "churn2", "churn3"], "another browser kept changing resources.hidden; try again"])
|
||||
|
||||
def test_a_refused_change_falls_back_to_the_stored_value_and_keeps_a_newer_change(self):
|
||||
self.assertFalse(self.data["refused"])
|
||||
self.assertEqual(self.data["during"], ["accumulated", ["burn:scope"], "resources.tokens must be one of interval, accumulated"])
|
||||
self.assertTrue(self.data["kept"])
|
||||
|
||||
def test_leaving_the_page_while_a_patch_is_in_flight_sends_the_pending_change_at_once_with_keepalive(self):
|
||||
data = self.data
|
||||
self.assertEqual(data["hooks"], 1)
|
||||
self.assertEqual((data["beforeLeave"], data["atLeave"]), (10, 11))
|
||||
last = data["sent"][-1]
|
||||
self.assertEqual((last["keepalive"], last["body"]), (True, {"set": {"home.pins": []}, "expect": {"home.pins": [PIN]}}))
|
||||
self.assertEqual(data["landed"], [True, True])
|
||||
self.assertEqual((data["server"]["burn"]["overlays"], data["server"]["home"]["pins"], data["error"]), (["score"], [], ""))
|
||||
|
||||
def test_set_refuses_a_list_path_so_a_retry_cannot_overwrite_what_another_browser_stored(self):
|
||||
self.assertEqual(self.data["listSet"], "a list view path changes through update(), not set(): resources.hidden")
|
||||
self.assertEqual(self.data["local"][0], ["host:CPU"])
|
||||
|
||||
def test_a_page_restored_from_the_back_forward_cache_refetches_the_stored_view(self):
|
||||
self.assertEqual(self.data["notRestored"], [0, True])
|
||||
self.assertEqual(self.data["restored"], [1, False, 1])
|
||||
|
||||
def test_ids_follow_the_view_grammar_and_stay_distinct(self):
|
||||
ids = self.data["ids"]
|
||||
self.assertEqual(ids[:4], ["host:CPU", "disk:/", "util:0000_3a_01_3a_00.0", "agent:Stage_20__28_this_20_session_29__20__5b_5d88e9_5d_"])
|
||||
self.assertEqual(len(set(ids)), len(ids))
|
||||
for value in ids:
|
||||
with self.subTest(id=value):
|
||||
self.assertRegex(value, r"\A[A-Za-z0-9:_./-]{1,200}\Z")
|
||||
self.assertEqual(len(ids[7]), 200)
|
||||
|
||||
def test_hidden_mode_chips_are_pressed_while_shown_and_toggle_through_update(self):
|
||||
chips = self.chips
|
||||
self.assertEqual(chips["group"], ["div", "legend-chips", "group"])
|
||||
self.assertEqual(chips["before"], [
|
||||
["a", "true", "legend-chip res-c0 k-host", "CPU 10 %", "CPU 10 %: shown, press to hide"],
|
||||
["b", "false", "legend-chip res-c1 k-host", "RAM 20 %", "RAM 20 %: hidden, press to show"],
|
||||
])
|
||||
self.assertEqual([chip[1] for chip in chips["after"]], ["false", "true"])
|
||||
self.assertEqual(chips["updates"][:2], [["resources.hidden", ["b", "a"]], ["resources.hidden", ["a"]]])
|
||||
self.assertEqual(chips["placed"], [[], ["a", "b"], ["a"], ["a"], []])
|
||||
|
||||
def test_shown_mode_chips_are_pressed_while_switched_on_and_store_the_on_list(self):
|
||||
chips = self.chips
|
||||
self.assertEqual([chip[:2] for chip in chips["shownBefore"]], [["fix", "true"], ["score", "false"]])
|
||||
self.assertEqual(chips["shownBefore"][1][4], "Score: off, press to switch it on")
|
||||
self.assertEqual(chips["updates"][3:], [["burn.overlays", ["fix", "score"]], ["burn.overlays", ["score"]]])
|
||||
self.assertEqual([chip[1] for chip in chips["shownAfter"]], ["false", "true"])
|
||||
self.assertEqual(chips["overlaysGroup"], "Overlays: press a chip to switch it on or off")
|
||||
self.assertEqual(chips["bad"], "a legend chip mode must be hidden or shown, not on")
|
||||
|
||||
def test_hidden_ids_of_series_gone_from_the_page_are_dropped_oldest_first_beyond_the_cap(self):
|
||||
chips = self.chips
|
||||
self.assertEqual(chips["keep"], 100)
|
||||
many, short = chips["pruned"]
|
||||
self.assertEqual((len(many), many[:3]), (100, ["o0", "o1", "o5"]))
|
||||
self.assertEqual(short, ["x", "y"])
|
||||
path, last = chips["updates"][2]
|
||||
self.assertEqual((path, len(last), last[:2], last[-2:]), ("resources.hidden", 100, ["old2", "old3"], ["a", "b"]))
|
||||
|
||||
|
||||
class TestHubView(HubCase):
|
||||
def patch(self, body: bytes, headers: dict[str, str]) -> tuple[int, bytes]:
|
||||
status, _, answer = self.call("PATCH", "/api/view", body, {**headers, "Content-Type": "application/json"})
|
||||
return status, answer
|
||||
|
||||
def view(self) -> dict:
|
||||
status, _, body = self.get("/api/view")
|
||||
self.assertEqual(status, 200, body)
|
||||
return json.loads(body)
|
||||
|
||||
def write(self, values: dict) -> tuple[int, bytes]:
|
||||
stored = self.view()
|
||||
return self.patch(change(values, {path: value_at(stored, path) for path in values}), self.guard(self.host))
|
||||
|
||||
def reopen(self) -> None:
|
||||
self.httpd.hub = server.open_hub(
|
||||
council=self.council, pages=self.pages, public_port=self.port, state=self.state, host_state=HOST_STATE, transcripts=self.projects, scripts=self.scripts,
|
||||
templates=HUB / "templates",
|
||||
places=server.Places(tree_file=self.tree_file, timers=self.timers, vendor=HUB / "vendor", cleanup=self.cleanup, offices=self.offices, kept=self.kept),
|
||||
model=server.Model(url="", model="", key=""), engines=self.engines, approvers=self.approvers, redirects=self.redirects(),
|
||||
)
|
||||
|
||||
def test_a_get_and_a_patch_round_trip_and_the_file_is_private(self):
|
||||
self.assertEqual(self.view(), VIEW)
|
||||
status, body = self.write(FULL)
|
||||
self.assertEqual((status, json.loads(body)), (200, FULL))
|
||||
self.assertEqual(self.view(), FULL)
|
||||
stored = self.state / "view.json"
|
||||
self.assertEqual(json.loads(stored.read_text(encoding="utf-8")), FULL)
|
||||
self.assertEqual(stored.stat().st_mode & 0o777, 0o600)
|
||||
|
||||
def test_two_tabs_changing_other_paths_keep_both_changes(self):
|
||||
self.assertEqual(self.patch(change({"home.pins": [PIN], "burn.overlays": ["fix"]}, {"home.pins": [], "burn.overlays": []}), self.guard(self.host))[0], 200)
|
||||
status, body = self.patch(change({"resources.hidden": ["host:CPU"]}, {"resources.hidden": []}), self.guard(self.host))
|
||||
expected = {**VIEW, "resources": {**VIEW["resources"], "hidden": ["host:CPU"]}, "burn": {"overlays": ["fix"]}, "home": {"pins": [PIN]}}
|
||||
self.assertEqual((status, json.loads(body), self.view()), (200, expected, expected))
|
||||
|
||||
def test_a_stale_expect_answers_409_with_the_stored_view_and_stores_nothing(self):
|
||||
self.assertEqual(self.write({"resources.hidden": ["host:CPU"]})[0], 200)
|
||||
before = (self.state / "view.json").read_bytes()
|
||||
status, body = self.patch(change({"resources.hidden": ["host:RAM"], "burn.overlays": ["fix"]}, {"resources.hidden": [], "burn.overlays": []}), self.guard(self.host))
|
||||
found = json.loads(body)
|
||||
self.assertEqual((status, found["paths"], found["view"]["resources"]["hidden"]), (409, ["resources.hidden"], ["host:CPU"]))
|
||||
self.assertEqual(((self.state / "view.json").read_bytes(), self.view()["burn"]["overlays"]), (before, []))
|
||||
|
||||
def tabs(self, scenario: str) -> dict:
|
||||
done = subprocess.run(["node", "-e", TWO_TABS, str(STORE), str(CHIPS), self.origin, scenario], capture_output=True, text=True, timeout=60, check=True)
|
||||
return json.loads(done.stdout)
|
||||
|
||||
def test_two_stale_tabs_of_the_page_scripts_keep_both_hidden_series_and_both_pins(self):
|
||||
found = self.tabs("merge")
|
||||
refusal = "a list view path changes through update(), not set(): "
|
||||
self.assertEqual(found["refused"], [f"{refusal}resources.hidden", f"{refusal}home.pins"])
|
||||
self.assertEqual((found["results"], found["errors"]), ([True, True, True, True], ["", ""]))
|
||||
self.assertEqual((found["hidden"], found["pins"]), (["host:CPU", "host:RAM"], ["p1", "p2"]))
|
||||
self.assertEqual(found["seen"], [["host:CPU", "host:RAM"], ["p1", "p2"]])
|
||||
self.assertEqual((self.view()["resources"]["hidden"], [pin["id"] for pin in self.view()["home"]["pins"]]), (["host:CPU", "host:RAM"], ["p1", "p2"]))
|
||||
|
||||
def test_a_stale_tab_pressing_a_chip_another_tab_already_pressed_keeps_that_choice_in_both_modes(self):
|
||||
found = self.tabs("same")
|
||||
self.assertEqual((found["results"], found["errors"]), ([True, True, True, True], ["", ""]))
|
||||
self.assertEqual((found["hidden"], found["overlays"]), (["host:CPU"], ["fix"]))
|
||||
self.assertEqual(found["chips"], [[["host:CPU", "false"], ["host:RAM", "true"]], [["fix", "true"], ["score", "false"]]])
|
||||
self.assertEqual((self.view()["resources"]["hidden"], self.view()["burn"]["overlays"]), (["host:CPU"], ["fix"]))
|
||||
|
||||
def test_a_malformed_change_an_unknown_path_or_a_body_past_the_change_limit_answers_400_and_stores_nothing(self):
|
||||
self.assertEqual(self.write(FULL)[0], 200)
|
||||
stored = self.state / "view.json"
|
||||
before = stored.read_bytes()
|
||||
padded = change({"resources.tokens": "interval"}, {"resources.tokens": "accumulated"})
|
||||
bodies = {
|
||||
"not json": b"{resources",
|
||||
"not utf-8": b"\xff\xfe{}",
|
||||
"a list": b"[]",
|
||||
"no expect": json.dumps({"set": {"resources.tokens": "interval"}}).encode(),
|
||||
"expect other paths": change({"resources.tokens": "interval"}, {"resources.combined": True}),
|
||||
"set and more": json.dumps({"set": {"resources.tokens": "interval"}, "expect": {"resources.tokens": "accumulated"}, "theme": "dark"}).encode(),
|
||||
"empty set": change({}, {}),
|
||||
"set a list": json.dumps({"set": [["resources.tokens", "interval"]], "expect": {}}).encode(),
|
||||
"unknown section": change({"theme": "dark"}, {"theme": "sky"}),
|
||||
"unknown key": change({"resources.extra": 1}, {"resources.extra": 0}),
|
||||
"too deep": change({"home.pins.0": PIN}, {"home.pins.0": PIN}),
|
||||
"a section missing a key": change({"resources": {"hidden": []}}, {"resources": FULL["resources"]}),
|
||||
"malformed value": change({"resources.combined": "yes"}, {"resources.combined": True}),
|
||||
"null overlay": change({"burn.overlays": [None]}, {"burn.overlays": ["fix", "score"]}),
|
||||
"one bad of two": change({"resources.tokens": "interval", "home.pins": [{**PIN, "w": 13}]}, {"resources.tokens": "accumulated", "home.pins": FULL["home"]["pins"]}),
|
||||
"the change limit and one byte": padded + b" " * (CHANGE_LIMIT + 1 - len(padded)),
|
||||
"512 KiB": padded + b" " * (512 << 10),
|
||||
}
|
||||
self.assertEqual((CHANGE_LIMIT, len(bodies["the change limit and one byte"])), (129 << 10, CHANGE_LIMIT + 1))
|
||||
for name, body in bodies.items():
|
||||
with self.subTest(case=name):
|
||||
status, answer = self.patch(body, self.guard(self.host))
|
||||
self.assertEqual(status, 400, answer)
|
||||
self.assertEqual(stored.read_bytes(), before)
|
||||
self.assertEqual(self.view(), FULL)
|
||||
exact = padded + b" " * (CHANGE_LIMIT - len(padded))
|
||||
self.assertEqual(self.patch(exact, self.guard(self.host))[0], 200)
|
||||
|
||||
def test_a_list_near_the_document_cap_can_still_shrink_but_never_grow(self):
|
||||
ids = [f"agent:{'a' * 190}{index:04d}" for index in range(320)]
|
||||
status, answer = self.write({"resources.hidden": ids})
|
||||
self.assertEqual(status, 200, answer)
|
||||
stored = self.state / "view.json"
|
||||
self.assertGreater(len(stored.read_bytes()), VIEW_LIMIT - (1 << 10))
|
||||
before = stored.read_bytes()
|
||||
status, answer = self.write({"resources.hidden": [*ids, *(f"agent:{'b' * 190}{index:04d}" for index in range(3))]})
|
||||
self.assertEqual(status, 400, answer)
|
||||
self.assertIn(b"the view document must not exceed", answer)
|
||||
self.assertEqual((stored.read_bytes(), self.view()["resources"]["hidden"]), (before, ids))
|
||||
shrink = change({"resources.hidden": ids[:-1]}, {"resources.hidden": ids})
|
||||
self.assertGreater(len(shrink), VIEW_LIMIT)
|
||||
status, answer = self.patch(shrink, self.guard(self.host))
|
||||
self.assertEqual(status, 200, answer)
|
||||
self.assertEqual(self.view()["resources"]["hidden"], ids[:-1])
|
||||
|
||||
def test_changes_that_grow_the_document_past_64_kib_are_refused(self):
|
||||
ids = [f"agent:{'a' * 180}{index:04d}" for index in range(300)]
|
||||
self.assertEqual(self.write({"resources.hidden": ids})[0], 200)
|
||||
before = (self.state / "view.json").read_bytes()
|
||||
status, answer = self.write({"resources.combinedHidden": [f"burn:{'b' * 180}{index:04d}" for index in range(100)]})
|
||||
self.assertEqual(status, 400, answer)
|
||||
self.assertEqual((self.state / "view.json").read_bytes(), before)
|
||||
self.assertEqual(self.view()["resources"]["combinedHidden"], [])
|
||||
|
||||
def test_a_body_beyond_the_hub_wide_limit_answers_413_and_stores_nothing(self):
|
||||
status, _, answer = self.call("PATCH", "/api/view", b" " * (BODY_LIMIT + 1), {**self.guard(self.host), "Content-Type": "application/json"})
|
||||
self.assertEqual(status, 413, answer)
|
||||
self.assertFalse((self.state / "view.json").exists())
|
||||
|
||||
def test_a_whole_document_put_no_longer_exists(self):
|
||||
status, _, _ = self.call("PUT", "/api/view", json.dumps(FULL).encode(), {**self.guard(self.host), "Content-Type": "application/json"})
|
||||
self.assertEqual(status, 501)
|
||||
self.assertEqual(self.view(), VIEW)
|
||||
|
||||
def test_a_patch_needs_no_passkey_and_leaves_the_signed_settings_file_byte_identical(self):
|
||||
saved = {**self.snapshot()["settings"], "theme": "dark"}
|
||||
status, _, body = self.post_json("/api/settings", saved, self.guard(self.host))
|
||||
self.assertEqual(status, 200, body)
|
||||
settings = self.state / "settings.json"
|
||||
before, stamp = settings.read_bytes(), settings.stat().st_mtime_ns
|
||||
self.assertFalse(self.approvers.exists())
|
||||
status, body = self.write(FULL)
|
||||
self.assertEqual(status, 200, body)
|
||||
self.assertEqual((settings.read_bytes(), settings.stat().st_mtime_ns), (before, stamp))
|
||||
self.assertEqual(self.snapshot()["settings"], saved)
|
||||
|
||||
def test_a_patch_without_the_header_from_a_foreign_origin_or_host_is_refused(self):
|
||||
body = change(FULL, VIEW)
|
||||
cases = {
|
||||
"no header": {"Host": self.host, "Origin": f"http://{self.host}"},
|
||||
"foreign origin": {"Host": self.host, "Origin": "http://evil.example", "X-Kvb-Orchestrator": "1"},
|
||||
"no origin": {"Host": self.host, "X-Kvb-Orchestrator": "1"},
|
||||
}
|
||||
for name, headers in cases.items():
|
||||
with self.subTest(case=name):
|
||||
self.assertEqual(self.patch(body, headers)[0], 403)
|
||||
self.assertEqual(self.patch(body, {**self.guard(self.host), "Host": "evil.example"})[0], 403)
|
||||
self.assertEqual(self.call("PATCH", "/api/settings", body, self.guard(self.host))[0], 404)
|
||||
self.assertFalse((self.state / "view.json").exists())
|
||||
self.assertEqual(self.view(), VIEW)
|
||||
|
||||
def test_a_failed_write_keeps_the_stored_file_and_the_served_document(self):
|
||||
self.assertEqual(self.write(VIEW)[0], 200)
|
||||
stored = self.state / "view.json"
|
||||
before = stored.read_bytes()
|
||||
with mock.patch.object(records.os, "replace", side_effect=OSError(28, "No space left on device")):
|
||||
status, body = self.write(FULL)
|
||||
self.assertEqual(status, 500, body)
|
||||
self.assertEqual(stored.read_bytes(), before)
|
||||
self.assertEqual(self.view(), VIEW)
|
||||
|
||||
def test_the_store_survives_a_hub_restart_and_the_page_carries_it(self):
|
||||
self.assertEqual(self.write(FULL)[0], 200)
|
||||
self.reopen()
|
||||
self.assertEqual(self.view(), FULL)
|
||||
embedded = EMBEDDED.search(self.index())
|
||||
self.assertIsNotNone(embedded)
|
||||
self.assertEqual(json.loads(embedded.group(1)), FULL)
|
||||
|
||||
def test_a_broken_stored_file_falls_back_to_the_defaults(self):
|
||||
(self.state / "view.json").write_text('{"resources": {"hidden": ["bad id"]}}', encoding="utf-8")
|
||||
self.reopen()
|
||||
self.assertEqual(self.view(), VIEW)
|
||||
|
||||
def test_the_page_carries_the_store_the_chips_and_their_style(self):
|
||||
html = self.index()
|
||||
for path in (STORE, CHIPS, CHIPS_STYLE):
|
||||
with self.subTest(file=path.name):
|
||||
self.assertIn(path.read_text(encoding="utf-8").strip(), html)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user