Files
pipelines/web/src/miniGraph.js
T

94 lines
4.5 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) {
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 evidence = run && Array.isArray(run.steps) ? runGraphState(run) : null;
const visits = new Map((run?.steps ?? []).map((step) => [step.stage, step]));
return { ...layout, evidence: evidence != null,
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 = current ? run.status : step?.status;
const color = status === "completed" ? edgeStrokeColor(step.outcome) : status ? miniatureColor(status) : null;
return { ...node, color,
untaken: evidence != null && !step && !current };
}),
edges: layout.edges.map((edge) => ({ ...edge,
untaken: evidence != null && !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("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" : ""}`);
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 ? "visits" : "summary" }, svg);
if (run) wrap.style.color = miniatureColor(run.status);
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);
};
}