Files
pruefwerk/compliance-dashboard/assets/graph-viz.js
T
sharang c8deabd4d9
CI / Check (push) Skipped
CI / Agent integration (push) Skipped
CI / Detect Changes (push) Successful in 5s
CI / Deploy Agent (push) Skipped
CI / Deploy Docs (push) Skipped
CI / Deploy MCP (push) Skipped
CI / Deploy Dashboard (push) Successful in 3m27s
feat(dashboard): show the code graph, from its card and from a finding (t82) (#939)
2026-11-07 10:43:02 +00:00

278 lines
9.3 KiB
JavaScript

// Code graph drawing: a thin vis-network wrapper.
//
// The dashboard's Rust code decides what is drawn (the whole graph, the
// community overview or one community, see pages/graph_view.rs) and passes
// the nodes, edges and the UI texts in the reader's language. This file only
// draws them, in the colours of the current theme.
(function () {
"use strict";
// CSS custom properties per node kind, defined in main.css from the theme
// tokens, so the graph follows the light and dark theme.
var KIND_TOKEN = {
function: "--graph-function",
method: "--graph-function",
class: "--graph-type",
struct: "--graph-type",
enum: "--graph-enum",
interface: "--graph-interface",
trait: "--graph-interface",
module: "--graph-module",
file: "--graph-file",
community: "--graph-community",
folder: "--graph-community",
};
var EDGE_TOKEN = {
calls: "--graph-function",
imports: "--graph-enum",
inherits: "--graph-type",
implements: "--graph-interface",
bundle: "--graph-community",
};
var SHAPE = { file: "diamond", module: "square", folder: "hexagon" };
// Above this many nodes the layout runs a short while and then stops,
// so a large drawing does not keep the CPU busy.
var LARGE = 400;
var network = null;
var nodesDataset = null;
var edgesDataset = null;
var rawNodes = [];
var rawEdges = [];
var texts = { kinds: {}, edges: {}, program: "", plc: [], layout: "", entry: "", file: "" };
function token(name) {
return getComputedStyle(document.documentElement).getPropertyValue(name).trim();
}
function palette() {
var kinds = {};
Object.keys(KIND_TOKEN).forEach(function (k) {
kinds[k] = token(KIND_TOKEN[k]);
});
var edges = {};
Object.keys(EDGE_TOKEN).forEach(function (k) {
edges[k] = token(EDGE_TOKEN[k]);
});
return {
kinds: kinds,
edges: edges,
quiet: token("--border-strong"),
text: token("--text-primary"),
halo: token("--bg-primary"),
ring: token("--text-primary"),
fallback: token("--text-tertiary"),
font: token("--font-body"),
};
}
function edgeCounts(edges) {
var counts = {};
edges.forEach(function (e) {
counts[e.source] = (counts[e.source] || 0) + 1;
counts[e.target] = (counts[e.target] || 0) + 1;
});
return counts;
}
function toVisNode(node, counts, colors) {
var color = colors.kinds[node.kind] || colors.fallback;
var isGroup = node.kind === "community" || node.kind === "folder";
// A PLC PROGRAM is kept as a module; it is named a program.
var kindName = node.kind === "module" && texts.plc.indexOf(node.language) >= 0
? texts.program
: texts.kinds[node.kind] || node.kind;
// A group is sized by its members through `value`, which also scales its
// label, so the large groups stay legible when the overview is zoomed out.
var size = Math.max(8, Math.min(30, 8 + (counts[node.qualified_name] || 0) * 1.5));
var title = isGroup
? kindName + " " + node.name + ": " + node.member_count
: kindName + ": " + node.qualified_name +
"\n" + texts.file + ": " + node.file_path + ":" + node.start_line +
(node.is_entry_point ? "\n" + texts.entry : "");
return {
id: node.qualified_name,
label: isGroup ? node.name + "\n" + node.member_count : node.name,
title: title,
size: isGroup ? undefined : size,
value: isGroup ? node.member_count : undefined,
color: {
background: color,
border: node.is_entry_point ? colors.ring : color,
highlight: { background: color, border: colors.ring },
hover: { background: color, border: colors.ring },
},
font: {
color: colors.text,
size: isGroup ? 13 : 11,
face: colors.font,
strokeWidth: 3,
strokeColor: colors.halo,
},
borderWidth: node.is_entry_point ? 3 : 1,
borderWidthSelected: 3,
shape: SHAPE[node.kind] || "dot",
_raw: node,
};
}
function toVisEdge(edge, colors) {
var color = colors.edges[edge.kind] || colors.quiet;
var count = edge.count || 1;
var label = edge.kind === "bundle" ? String(count) : texts.edges[edge.kind] || edge.kind;
return {
id: edge.source + "|" + edge.target + "|" + edge.kind,
from: edge.source,
to: edge.target,
color: {
color: color,
highlight: colors.ring,
hover: color,
opacity: edge.kind === "bundle" ? 0.35 : 0.7,
},
width: edge.kind === "bundle" ? Math.min(8, 1 + Math.log2(count)) : 1,
arrows: { to: { enabled: edge.kind !== "contains", scaleFactor: 0.4 } },
smooth: { enabled: true, type: "continuous", roundness: 0.3 },
title: label,
};
}
// Recolour everything after a theme change, without a new layout.
function recolor() {
if (!network) return;
var colors = palette();
var counts = edgeCounts(rawEdges);
nodesDataset.update(rawNodes.map(function (n) {
return toVisNode(n, counts, colors);
}));
edgesDataset.update(rawEdges.map(function (e) {
return toVisEdge(e, colors);
}));
}
new MutationObserver(recolor).observe(document.documentElement, {
attributes: true,
attributeFilter: ["data-theme"],
});
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", recolor);
function uniqueBy(items, key) {
var seen = {};
return items.filter(function (item) {
var k = key(item);
if (seen[k]) return false;
seen[k] = true;
return true;
});
}
/**
* Draw `nodes` and `edges`, then select and show the nodes named in
* `focus` (may be empty). `labels` holds the UI texts. Called from Rust.
*/
window.__loadGraph = function (nodes, edges, labels, focus) {
var container = document.getElementById("graph-canvas");
if (!container) return;
texts = labels;
rawNodes = uniqueBy(nodes, function (n) { return n.qualified_name; });
rawEdges = uniqueBy(edges, function (e) { return e.source + "|" + e.target + "|" + e.kind; });
var colors = palette();
var counts = edgeCounts(rawEdges);
nodesDataset = new vis.DataSet(rawNodes.map(function (n) {
return toVisNode(n, counts, colors);
}));
edgesDataset = new vis.DataSet(rawEdges.map(function (e) {
return toVisEdge(e, colors);
}));
var large = rawNodes.length > LARGE;
var options = {
nodes: {
scaling: {
min: 12,
max: 56,
label: { enabled: true, min: 22, max: 48, drawThreshold: 5, maxVisible: 30 },
},
},
physics: {
enabled: true,
solver: "forceAtlas2Based",
forceAtlas2Based: {
gravitationalConstant: -60,
centralGravity: 0.008,
springLength: 110,
springConstant: 0.04,
damping: 0.6,
avoidOverlap: 0.5,
},
stabilization: { enabled: true, iterations: large ? 300 : 1000, updateInterval: 25 },
maxVelocity: 40,
minVelocity: 0.75,
},
interaction: {
hover: true,
tooltipDelay: 200,
hideEdgesOnDrag: large,
hideEdgesOnZoom: large,
keyboard: { enabled: true, bindToWindow: false },
},
layout: { improvedLayout: rawNodes.length < 300 },
};
if (network) network.destroy();
network = new vis.Network(container, { nodes: nodesDataset, edges: edgesDataset }, options);
network.on("click", function (params) {
if (params.nodes.length === 0) return;
var raw = rawNodes.find(function (n) { return n.qualified_name === params.nodes[0]; });
if (raw && window.__onNodeClick) window.__onNodeClick(JSON.stringify(raw));
});
var overlay = document.getElementById("graph-stabilization-overlay");
var progressFill = document.getElementById("graph-stab-progress-fill");
var pctLabel = document.getElementById("graph-stab-pct");
if (overlay) {
overlay.classList.remove("graph-stab-fade-out");
overlay.style.display = "flex";
}
network.on("stabilizationProgress", function (params) {
var pct = Math.round((params.iterations / params.total) * 100);
if (progressFill) progressFill.style.width = pct + "%";
if (pctLabel) pctLabel.textContent = texts.layout + " · " + pct + " %";
});
network.once("stabilizationIterationsDone", function () {
if (progressFill) progressFill.style.width = "100%";
if (overlay) {
overlay.classList.add("graph-stab-fade-out");
setTimeout(function () { overlay.style.display = "none"; }, 600);
}
// A large drawing stops moving; a small one keeps settling gently
// and answers to dragging.
network.setOptions({ physics: large ? { enabled: false } : { maxVelocity: 15 } });
window.__highlightNodes(focus || []);
});
};
/** Select the named nodes that are drawn and show them. */
window.__highlightNodes = function (qualifiedNames) {
if (!network || !nodesDataset) return;
var drawn = qualifiedNames.filter(function (id) { return nodesDataset.get(id); });
if (drawn.length === 0) return;
network.selectNodes(drawn);
network.fit({ nodes: drawn, animation: { duration: 500, easingFunction: "easeInOutQuad" } });
};
/** Fit the whole drawing in view. */
window.__fitGraph = function () {
if (!network) return;
network.fit({ animation: { duration: 400, easingFunction: "easeInOutQuad" } });
};
})();