Files
pipelines/web/src/miniGraph.js
T

104 lines
5.2 KiB
JavaScript

// Label-free list cue, sharing the detail graph's layout and route evidence.
import { h } from "./dom.js";
import { layoutGraph, edgeIsExecuted, edgeStrokeColor } from "./graph.js";
import { runGraphState } from "./runDetail.js";
const METRICS = Object.freeze({ nodeWidth: 26, nodeHeight: 12, hGap: 12, pad: 2,
laneSpacing: 8, terminalGap: 8, trackStep: 6, portSpacing: 4,
labelCharWidth: 0, labelPadding: 0, labelHeight: 0, labelGap: 0 });
const NS = "http://www.w3.org/2000/svg";
const STATUSES = new Set(["succeeded", "failed", "running", "waiting", "queued",
"interrupted", "canceled", "blocked", "skipped"]);
export function miniatureColor(status) {
if (status === "waiting") return "var(--warn)";
if (status === "running") return "var(--accent)";
return `var(--status-${STATUSES.has(status) ? status : "unknown"})`;
}
export function miniatureModel(graph, run = null) {
const stages = graph?.stages ?? [];
const keys = new Set(stages.map((stage) => stage.key));
const shape = { stages: stages.map((stage) => ({ ...stage,
edges: Object.fromEntries(Object.entries(stage.edges ?? {}).filter(([, target]) => keys.has(target))),
})) };
const layout = layoutGraph(shape, METRICS);
const steps = Array.isArray(run?.steps) ? run.steps : run?.graph_evidence?.visits;
const evidence = Array.isArray(steps) ? runGraphState({ ...run, steps }) : null;
const visits = new Map((steps ?? []).map((step) => [step.stage, step]));
return { ...layout, evidence: evidence != null,
complete: Array.isArray(run?.steps) || run?.graph_evidence?.complete === true,
nodes: layout.nodes.map((node) => {
const step = visits.get(node.key);
// A recorded current stage is evidence even in a compact list summary.
const current = run?.executing && run.current_stage === node.key;
const status = step?.status ?? (current ? run.status : null);
// Custom outcome names do not override a recorded terminal verdict.
const color = step?.target === "#failure" ? "var(--bad)"
: step?.target === "#success" ? "var(--ok)"
: status === "completed" ? edgeStrokeColor(step.outcome) : status ? miniatureColor(status) : null;
return { ...node, color,
untaken: Boolean(run) && !step && !current };
}),
edges: layout.edges.map((edge) => ({ ...edge,
untaken: Boolean(run) && (!evidence || !edgeIsExecuted(edge, evidence)),
color: evidence && edgeIsExecuted(edge, evidence) ? edgeStrokeColor(edge.outcome) : null,
})),
};
}
export function renderMiniGraph(graph, run = null) {
if (!graph?.stages?.length) return null;
const model = miniatureModel(graph, run);
if (!model.nodes.length || !model.width || !model.height) return null;
const svg = document.createElementNS(NS, "svg");
svg.setAttribute("class", "pipeline-mini-graph");
svg.setAttribute("viewBox", `0 0 ${Math.ceil(model.width)} ${Math.ceil(model.height)}`);
svg.setAttribute("preserveAspectRatio", "xMaxYMid meet");
svg.setAttribute("aria-hidden", "true");
svg.setAttribute("focusable", "false");
for (const edge of model.edges) {
if (edge.points.length < 2) continue;
const line = document.createElementNS(NS, "polyline");
line.setAttribute("class", `pipeline-mini-edge${edge.untaken ? " mini-untaken" : ""}`);
line.setAttribute("data-route", `${edge.source}:${edge.outcome}:${edge.target}`);
line.setAttribute("points", edge.points.map((p) => `${p.x},${p.y}`).join(" "));
if (edge.color) line.style.color = edge.color;
svg.append(line);
}
for (const node of model.nodes) {
const rect = document.createElementNS(NS, "rect");
rect.setAttribute("class", `pipeline-mini-node${node.untaken ? " mini-untaken" : ""}`);
rect.setAttribute("data-stage", node.key);
for (const key of ["x", "y", "width", "height"]) rect.setAttribute(key, String(node[key]));
rect.setAttribute("rx", "3");
if (node.color) rect.style.color = node.color;
svg.append(rect);
}
const wrap = h("span", { class: `pipeline-mini${run ? " run-mini" : ""}`,
"aria-hidden": "true", "data-evidence": model.evidence ? model.complete ? "visits" : "partial" : "unknown" }, svg);
// Unknown/unvisited graph elements must never inherit the aggregate result.
if (run) wrap.style.color = "var(--color-muted)";
return wrap;
}
/** One read per pinned pipeline version, never get_run per row or latest fallback. */
export function createMiniGraphCache(readPipeline, changed) {
const cache = new Map();
return (run, pipelines = [], scope = "") => {
if (run.graph?.stages?.length) return run.graph;
if (!(run.pipeline_id > 0 && run.version > 0)) return null;
const summary = pipelines.find((p) => p.id === run.pipeline_id && p.version === run.version);
if (summary?.shape?.stages?.length) return summary.shape;
const key = JSON.stringify([scope, run.pipeline_id, run.version]);
if (!cache.has(key)) {
cache.set(key, null);
Promise.resolve().then(() => readPipeline(run.pipeline_id, run.version)).then((detail) => {
if (detail?.version !== run.version) return;
cache.set(key, detail.graph ?? null);
changed();
}).catch(() => { /* Missing topology stays absent, never invented. */ });
}
return cache.get(key);
};
}