diff --git a/orchestrator/kvb_orchestrator/handler.py b/orchestrator/kvb_orchestrator/handler.py index 4cfdc6b..7b7f582 100644 --- a/orchestrator/kvb_orchestrator/handler.py +++ b/orchestrator/kvb_orchestrator/handler.py @@ -11,6 +11,7 @@ from .diffs import DIFF_BODY_LIMIT from .engines import STT_LIMIT from .exchange import PLAIN_CSP, Exchange from .resources import RESOURCES, sampled +from .scores import scored BODY_LIMIT = 1 << 20 UPLOAD_LIMIT = 50 << 20 @@ -119,6 +120,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 == "/api/scores": + self.answer(lambda: scored(hub.council, hub.scripts.skills, hub.span(self.query(), time.time())), HTTPStatus.OK) elif path == "/api/subagents": self.answer(lambda: hub.subagents(self.query()), HTTPStatus.OK) elif path == "/api/runner/jobs": diff --git a/orchestrator/kvb_orchestrator/scores.py b/orchestrator/kvb_orchestrator/scores.py new file mode 100644 index 0000000..135405c --- /dev/null +++ b/orchestrator/kvb_orchestrator/scores.py @@ -0,0 +1,44 @@ +from __future__ import annotations + +from pathlib import Path + +from .records import epoch, parse_line, read_json, read_regular + +SCORES = "scores.jsonl" +LABELS = "kvb-check-score/labels.json" +FILE_LIMIT = 64 << 20 + + +def checked(labels: object) -> dict | None: + """Args: + labels: the parsed label file, or None. + + Returns: + labels when its "metrics" is a non-empty object of entries with a string + emoji and label, else None. + """ + metrics = labels.get("metrics") if isinstance(labels, dict) else None + valid = isinstance(metrics, dict) and metrics and all(isinstance(metric, dict) and isinstance(metric.get("emoji"), str) and isinstance(metric.get("label"), str) for metric in metrics.values()) + return labels if valid else None + + +def scored(council: Path, skills: Path, span: tuple[float, float]) -> dict: + """Args: + council: the council directory holding scores.jsonl, which score.py + and backfill.py of kvb-check-score append to. + skills: the skills directory holding kvb-check-score/labels.json. + span: (begin, end) of the burndown's time range in epoch seconds. + + Returns: + {"since", "until", "labels", "rows"}: the label file, None when + unreadable or not as checked() wants it, and the journal rows whose time lies within the span, in + time order. + """ + begin, end = span + try: + lines = read_regular(council / SCORES, FILE_LIMIT).splitlines() + except (OSError, ValueError): + lines = [] + rows = [row for row in map(parse_line, lines) if row is not None and isinstance(row.get("values"), dict)] + inside = [row for row in rows if (moment := epoch(row.get("time"))) is not None and begin <= moment <= end] + return {"since": begin, "until": end, "labels": checked(read_json(skills / LABELS)), "rows": sorted(inside, key=lambda row: row["time"])} diff --git a/orchestrator/templates/page-tasks-gantt.js b/orchestrator/templates/page-tasks-gantt.js index 5c5fdca..470f4e1 100644 --- a/orchestrator/templates/page-tasks-gantt.js +++ b/orchestrator/templates/page-tasks-gantt.js @@ -1,5 +1,7 @@ const GANTT = {label: 240, right: 28, top: 34, lane: 30, width: 1180, bar: 16, ticks: 4}; - const BURN = {width: 900, height: 340, left: 52, right: 24, top: 20, bottom: 40}; + const BURN = {width: 900, height: 340, left: 52, right: 24, axis: 56, top: 20, bottom: 40}; + const OVERLAY_PATH = "burn.overlays"; + const scoreView = {key: "", rows: [], labels: null, busy: false, bound: false}; const EDGE_WORDS = {after: "Follows (rasci.py set --after)", base: "Branches from (base branch)", queue: "Waits behind (queue order)"}; const SERIES_WORDS = {scope: "Scope (burnup)", done: "Done (burnup)", remaining: "Remaining (burndown)", unestimated: "Unestimated tasks"}; const STEP_WORDS = [["started", "Started"], ["ended", "Ended"], ["joined", "Joined the scope"]]; @@ -80,11 +82,11 @@ return h("div", {class: "gantt-box"}, h("div", {class: "gantt-scroll"}, svg), legend); } - function burnAxis(svg, from, to, top) { - const x = (t) => BURN.left + ((t - from) / Math.max(to - from, 1)) * (BURN.width - BURN.left - BURN.right); + function burnAxis(svg, from, to, top, right) { + const x = (t) => BURN.left + ((t - from) / Math.max(to - from, 1)) * (BURN.width - BURN.left - right); const y = (value) => BURN.top + (1 - value / top) * (BURN.height - BURN.top - BURN.bottom); for (const value of [0, top / 2, top]) { - svg.append(s("line", {class: "grid", x1: BURN.left, x2: BURN.width - BURN.right, y1: fixed(y(value)), y2: fixed(y(value))})); + svg.append(s("line", {class: "grid", x1: BURN.left, x2: BURN.width - right, y1: fixed(y(value)), y2: fixed(y(value))})); svg.append(s("text", {class: "axis", x: BURN.left - 8, y: fixed(y(value)), dy: "0.35em", "text-anchor": "end"}, String(Math.round(value * 10) / 10))); } for (const [index, t] of [from, (from + to) / 2, to].entries()) { @@ -165,6 +167,68 @@ return {reach, end: forecast === null ? picked.to : Math.min(forecast, reach)}; } + const overlaysReady = () => typeof ViewStore === "object" && typeof LegendChips === "object"; + + async function fetchScores(key, from, to) { + scoreView.busy = true; + scoreView.key = key; + try { + const response = await fetch(`/api/scores?from=${Math.floor(from)}&to=${Math.ceil(to)}`, {cache: "no-store"}); + if (response.ok) { + const found = await response.json(); + scoreView.rows = Array.isArray(found.rows) ? found.rows.filter((row) => row && row.values && typeof row.values === "object") : []; + scoreView.labels = Plan.overlayLabels(found.labels); + } + } catch { + ui.offline = true; + } + scoreView.busy = false; + taskView.sig = ""; + drawTasks(); + } + + function overlayKeys() { + const kept = ViewStore.get(OVERLAY_PATH); + return Array.isArray(kept) && scoreView.labels ? Object.keys(scoreView.labels).filter((key) => kept.includes(key)) : []; + } + + const burnRight = () => (overlaysReady() && overlayKeys().length ? BURN.axis : BURN.right); + + function burnOverlays(svg, picked, x, right) { + const key = `${Math.floor(picked.from)}:${Math.floor(picked.to / 60)}`; + if (key !== scoreView.key && !scoreView.busy) { + fetchScores(key, picked.from, picked.to); + } + if (!scoreView.bound) { + scoreView.bound = true; + ViewStore.on(OVERLAY_PATH, () => { + taskView.sig = ""; + drawTasks(); + }); + } + if (!scoreView.labels) { + return null; + } + const series = Plan.overlaySeries(picked.rows, scoreView.rows, overlayKeys(), picked); + const top = Plan.overlayTop(series); + const y = (value) => BURN.top + (1 - value / top) * (BURN.height - BURN.top - BURN.bottom); + if (series.length) { + for (const value of [0, top / 2, top]) { + svg.append(s("text", {class: "axis overlay-axis", x: BURN.width - right + 6, y: fixed(y(value)), dy: "0.35em", "text-anchor": "start"}, `${Math.round(value)} %`)); + } + } + for (const {key: name, points} of series) { + const d = points.map((point, index) => `${index ? "L" : "M"}${fixed(x(point.t))},${fixed(y(point.value))}`).join(" "); + const group = s("g", {class: `burn-overlay k-${name}`, "data-overlay": name, "data-points": String(points.length)}, d ? s("path", {class: "burn-overlay-line", d}) : null); + for (const point of points) { + group.append(s("circle", {class: "burn-overlay-point", cx: fixed(x(point.t)), cy: fixed(y(point.value)), r: 3, "data-t": point.t, "data-value": point.value}, + s("title", {}, `${absolute.format(stampOf(point.t))} ยท ${scoreView.labels[name]}: ${point.value} % ยท ${point.branch}`))); + } + svg.append(group); + } + return LegendChips.render(Object.entries(scoreView.labels).map(([key, label]) => ({id: key, label, tone: `k-${key}`})), OVERLAY_PATH, "shown"); + } + function burnChart(rows, shown) { const picked = Plan.scoped(rows, taskView.scope, {filtered: shown, since: state.since, until: state.until, now: state.now, holders: lineHolders()}); const series = Plan.burn(picked.rows, {estimation: estimation(), from: picked.from, to: picked.to}).points; @@ -177,7 +241,8 @@ const top = Math.max(1, ...series.flatMap((point) => names.map((name) => point[name])), ...ends); const unit = estimation() === "points" ? "story points; unestimated tasks as a line of their own" : "tasks"; const svg = s("svg", {class: "chart burn", viewBox: `0 0 ${BURN.width} ${BURN.height}`, role: "img", "aria-label": `Burndown and burnup in ${unit}; hover a point for the work packages of its step`}); - const {x, y} = burnAxis(svg, picked.from, reach, top); + const right = burnRight(); + const {x, y} = burnAxis(svg, picked.from, reach, top, right); const last = series[series.length - 1]; if (reach > picked.to) { svg.append(s("line", {class: "now", x1: fixed(x(picked.to)), x2: fixed(x(picked.to)), y1: BURN.top, y2: BURN.height - BURN.bottom})); @@ -191,10 +256,11 @@ burnFinish(svg, segments, x, y); } svg.append(burnPoints(series, Plan.steps(picked.rows, series, picked.from), names, x, y)); + const overlays = overlaysReady() ? burnOverlays(svg, picked, x, right) : null; const stale = h("span", {class: "pill burn-stale", "data-tone": "warn", "data-stale": String(picked.stale.length), role: "img", ...named(`Stale: ${picked.stale.length}`, `Stale: ${picked.stale.length} tasks without a register change for ${Plan.STALE_DAYS} days, counted apart`)}, icon("bi-hourglass-bottom"), String(picked.stale.length)); const legend = h("div", {class: "legend"}, names.map((name) => h("span", {class: `burn-key k-${name}`, "data-series": name}, `${SERIES_WORDS[name]}: ${last[name]}`)), settings.trend.mode === "off" ? null : finishNotes(trend, finish), stale); - return h("div", {class: "burn-box"}, svg, legend, h("p", {class: "muted"}, `${picked.rows.length} tasks in scope, counted in ${unit}`)); + return h("div", {class: "burn-box", "data-panel": "burndown"}, svg, legend, overlays, h("p", {class: "muted"}, `${picked.rows.length} tasks in scope, counted in ${unit}`)); } async function chooseEstimation(id) { diff --git a/orchestrator/templates/plan.js b/orchestrator/templates/plan.js index ccb0f7f..5e3c5d7 100644 --- a/orchestrator/templates/plan.js +++ b/orchestrator/templates/plan.js @@ -262,6 +262,44 @@ const Plan = (() => { return {fixed: reachable(fixed), growing: found.forecast}; } + const branchOf = (row) => (row.branch !== "-" ? row.branch : row.worktree_branch); + + /** + * @param rows - the burndown scope's task rows with branch ("-" for none) and worktree_branch ("" for none). + * @param journal - /api/scores rows {time, branch, values: {metric key: number or null}}. + * @param keys - the metric keys switched on, in the label file's order. + * @param span - {from, to} in epoch seconds. + * @returns one {key, points: [{t, value, branch}]} per key, in keys order: the journal rows of a branch of the + * scope's rows within span whose value for the key is a number, in time order. + */ + function overlaySeries(rows, journal, keys, span) { + const branches = new Set(rows.map(branchOf).filter(Boolean)); + const inside = journal.filter((entry) => branches.has(entry.branch) && entry.time >= span.from && entry.time <= span.to).sort((a, b) => a.time - b.time); + return keys.map((key) => ({ + key, points: inside.filter((entry) => Number.isFinite(entry.values[key])).map((entry) => ({t: entry.time, value: entry.values[key], branch: entry.branch})), + })); + } + + /** + * @param labels - the labels of /api/scores: {metrics: {key: {emoji, label}}}, or anything else. + * @returns {key: "