Compare commits

..

No commits in common. "f82a95ab8d8581b1956c3fad01384cc106f558aa" and "964e9b38064a99090074998ae4a789ce91070781" have entirely different histories.

2 changed files with 158 additions and 213 deletions

View File

@ -4,17 +4,11 @@
<title>Kalzu</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdnjs.cloudflare.com/ajax/libs/sigma.js/2.4.0/sigma.min.js" integrity="sha512-iiPEYww3QXZU5C795JnnINBRNgHqDnRHs9mA7aJoqx4pNE4u3CknCDGmePHFoHtKR/6C9aIcRFa+HJ6obtlteQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/graphology/0.25.4/graphology.umd.min.js" integrity="sha512-tjMBhL9fLMcqoccPOwpRiIQIOAyUh18lWUlUvE10zvG1UNMfxUC4qSERmUq+VF30iavIyqs/q6fSP2o475FAUw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="/static/d3.v7.min.js"></script>
<style>
#container {
height: 95vh;
}
</style>
</head>
<body style="background: #eee">
<body style="background: #EEE">
<div id="container"></div>
<script src="/static/index.js"></script>
</body>
</html>
</html>

View File

@ -1,5 +1,5 @@
const linkArc = (d) => {
const r = Math.hypot(d.target.x - d.source.x, d.target.y - d.source.y) * 3;
const r = Math.hypot(d.target.x - d.source.x, d.target.y - d.source.y);
return `
M${d.source.x},${d.source.y}
A${r},${r} 0 0,1 ${d.target.x},${d.target.y}
@ -31,119 +31,116 @@ const drag = (simulation) => {
.on("end", dragended);
};
// const drawChart = (data) => {
// // Specify the dimensions of the chart.
// const width = 1600;
// const height = 1200;
//
// // Specify the color scale.
// const color = d3.scaleOrdinal(d3.schemeCategory10);
//
// // The force simulation mutates links and nodes, so create a copy
// // so that re-evaluating this cell produces the same result.
// const links = data.links.map((d) => ({ ...d }));
// const nodes = data.nodes.map((d) => ({ ...d }));
//
// // Create a simulation with several forces.
// const simulation = d3
// .forceSimulation(nodes)
// .force(
// "link",
// d3.forceLink(links).id((d) => d.id),
// )
// .force("charge", d3.forceManyBody())
// .force("x", d3.forceX())
// .force("y", d3.forceY());
//
// // Create the SVG container.
// const svg = d3
// .create("svg")
// .attr("width", width)
// .attr("height", height)
// .attr("viewBox", [-width / 2, -height / 2, width, height])
// .attr("style", "max-width: 100%; height: auto;");
//
// // Add a line for each link, and a circle for each node.
// const link = svg
// .append("g")
// .attr("stroke", "#999")
// .attr("stroke-opacity", 0.6)
// .selectAll("line")
// .data(links)
// .join("line")
// .attr("stroke-width", 1); // (d) => Math.sqrt(d.value));
//
// const node = svg
// .append("g")
// .attr("stroke", "#fff")
// .attr("stroke-width", 1.5)
// .selectAll("circle")
// .data(nodes)
// .join("circle")
// .attr("r", 5)
// .attr("fill", (d) => color(d.group));
//
// node.append("title").text((d) => d.id);
//
// // Add a drag behavior.
// node.call(
// d3.drag().on("start", dragstarted).on("drag", dragged).on("end", dragended),
// );
//
// // Set the position attributes of links and nodes each time the simulation ticks.
// simulation.on("tick", () => {
// link
// .attr("x1", (d) => d.source.x)
// .attr("y1", (d) => d.source.y)
// .attr("x2", (d) => d.target.x)
// .attr("y2", (d) => d.target.y);
//
// node.attr("cx", (d) => d.x).attr("cy", (d) => d.y);
// });
//
// // Reheat the simulation when drag starts, and fix the subject position.
// function dragstarted(event) {
// if (!event.active) simulation.alphaTarget(0.3).restart();
// event.subject.fx = event.subject.x;
// event.subject.fy = event.subject.y;
// }
//
// // Update the subject (dragged node) position during drag.
// function dragged(event) {
// event.subject.fx = event.x;
// event.subject.fy = event.y;
// }
//
// // Restore the target alpha so the simulation cools after dragging ends.
// // Unfix the subject position now that its no longer being dragged.
// function dragended(event) {
// if (!event.active) simulation.alphaTarget(0);
// event.subject.fx = null;
// event.subject.fy = null;
// }
//
// // When this cell is re-run, stop the previous simulation. (This doesnt
// // really matter since the target alpha is zero and the simulation will
// // stop naturally, but its a good practice.)
// // invalidation.then(() => simulation.stop());
//
// return svg.node();
// };
const drawChart = (data) => {
// Specify the dimensions of the chart.
const width = 1600;
const height = 1200;
// Specify the color scale.
const color = d3.scaleOrdinal(d3.schemeCategory10);
// The force simulation mutates links and nodes, so create a copy
// so that re-evaluating this cell produces the same result.
const links = data.links.map((d) => ({ ...d }));
const nodes = data.nodes.map((d) => ({ ...d }));
// Create a simulation with several forces.
const simulation = d3
.forceSimulation(nodes)
.force(
"link",
d3.forceLink(links).id((d) => d.id),
)
.force("charge", d3.forceManyBody())
.force("x", d3.forceX())
.force("y", d3.forceY());
// Create the SVG container.
const svg = d3
.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [-width / 2, -height / 2, width, height])
.attr("style", "max-width: 100%; height: auto;");
// Add a line for each link, and a circle for each node.
const link = svg
.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width", 1); // (d) => Math.sqrt(d.value));
const node = svg
.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", 5)
.attr("fill", (d) => color(d.group));
node.append("title").text((d) => d.id);
// Add a drag behavior.
node.call(
d3.drag().on("start", dragstarted).on("drag", dragged).on("end", dragended),
);
// Set the position attributes of links and nodes each time the simulation ticks.
simulation.on("tick", () => {
link
.attr("x1", (d) => d.source.x)
.attr("y1", (d) => d.source.y)
.attr("x2", (d) => d.target.x)
.attr("y2", (d) => d.target.y);
node.attr("cx", (d) => d.x).attr("cy", (d) => d.y);
});
// Reheat the simulation when drag starts, and fix the subject position.
function dragstarted(event) {
if (!event.active) simulation.alphaTarget(0.3).restart();
event.subject.fx = event.subject.x;
event.subject.fy = event.subject.y;
}
// Update the subject (dragged node) position during drag.
function dragged(event) {
event.subject.fx = event.x;
event.subject.fy = event.y;
}
// Restore the target alpha so the simulation cools after dragging ends.
// Unfix the subject position now that its no longer being dragged.
function dragended(event) {
if (!event.active) simulation.alphaTarget(0);
event.subject.fx = null;
event.subject.fy = null;
}
// When this cell is re-run, stop the previous simulation. (This doesnt
// really matter since the target alpha is zero and the simulation will
// stop naturally, but its a good practice.)
// invalidation.then(() => simulation.stop());
return svg.node();
};
const drawChart2 = (data) => {
// Data parsing
const width = 1600;
const height = 1200;
const types = Array.from(new Set(data.map((d) => d.type)));
const nodes = Array.from(
new Set(data.flatMap((l) => [l.source, l.target])),
(id) => ({ id }),
);
const links = data.map((d) => Object.create(d));
// Styles
const width = window.visualViewport.width;
const height = window.visualViewport.height - 10;
const colors = Array.from(new Set(data.map((d) => d.traceId)));
const color = d3.scaleOrdinal(colors, d3.schemeCategory10);
const color = d3.scaleOrdinal(types, d3.schemeCategory10);
const simulation = d3
.forceSimulation(nodes)
@ -155,26 +152,25 @@ const drawChart2 = (data) => {
.force("x", d3.forceX())
.force("y", d3.forceY());
// Canvas settings
const svg = d3
.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, height])
.attr("width", width)
.attr("height", height)
.attr("viewBox", [-width / 2, -height / 2, width, height])
.attr("style", "max-width: 100%; height: auto; font: 14px monospace;");
.attr("style", "max-width: 100%; height: auto; font: 12px sans-serif;");
// Pre-type arrowheads, as they don't inherit styles.
// Per-type markers, as they don't inherit styles.
svg
.append("defs")
.selectAll("marker")
.data(colors)
.data(types)
.join("marker")
.attr("id", (d) => `arrow-${d}`)
.attr("viewBox", "0 -5 10 10")
.attr("refX", 15)
.attr("refY", -0.5)
.attr("markerWidth", 5)
.attr("markerHeight", 5)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
.append("path")
.attr("fill", color)
@ -187,11 +183,8 @@ const drawChart2 = (data) => {
.selectAll("path")
.data(links)
.join("path")
.attr("stroke", (d) => color(d.traceId))
.attr(
"marker-end",
(d) => `url(${new URL(`#arrow-${d.traceId}`, location)})`,
);
.attr("stroke", (d) => color(d.type))
.attr("marker-end", (d) => `url(${new URL(`#arrow-${d.type}`, location)})`);
const node = svg
.append("g")
@ -203,32 +196,25 @@ const drawChart2 = (data) => {
.join("g")
.call(drag(simulation));
// Node "icon"
node
.append("circle")
.attr("stroke", "white")
.attr("stroke-width", 1.5)
.attr("r", 5);
.attr("r", 4);
// Node text
node
.append("text")
.attr("x", 8)
.attr("y", 4)
.attr("y", "0.31em")
.text((d) => (d.id.endsWith("*") ? "*" : d.id))
.clone(true)
.lower()
.attr("fill", "black")
.attr("fill", "none")
.attr("stroke", "white")
.attr("stroke-width", 2);
.attr("stroke-width", 3);
simulation.on("tick", () => {
link.attr("d", linkArc);
// link
// .attr("x1", (d) => d.source.x)
// .attr("y1", (d) => d.source.y)
// .attr("x2", (d) => d.target.x)
// .attr("y2", (d) => d.target.y);
node.attr("transform", (d) => `translate(${d.x},${d.y})`);
});
@ -237,30 +223,16 @@ const drawChart2 = (data) => {
return Object.assign(svg.node(), { scales: { color } });
};
const drawSigma = (data) => {
// Create a graphology graph
const graph = new graphology.MultiDirectedGraph();
const parseNodesAndLinks = (traces) => {
const result = {
nodes: [],
links: [],
};
data.nodes.forEach((n, i) => {
console.log("Node:", n);
try {
graph.addNode(n.id, n);
} catch (e) {
console.log("Node add:", e);
}
});
traces.forEach((trace) => {
let prevId = null;
data.links.forEach((l, i) => {
graph.addEdge(l.source, l.target, l);
});
// Instantiate sigma.js and render the graph
const renderer = new Sigma(graph, document.getElementById("container"), {
labelThreshold: -10000,
});
};
const getNodeID = (hop, prevId, trace) => {
const getId = (hop) => {
if (prevId === null) {
return trace.origin;
}
@ -269,74 +241,55 @@ const getNodeID = (hop, prevId, trace) => {
}
return hop.ip;
};
const parseNodesAndLinks = (traces) => {
const nodes = [];
const links = [];
const colors = Array.from(new Set(traces.map((t) => t.id)));
const color = d3.scaleOrdinal(colors, d3.schemeCategory10);
traces.forEach((trace) => {
let prevId = null;
let latestNumber = null;
};
trace.hops.forEach((hop) => {
const id = getNodeID(hop, prevId, trace);
const id = getId(hop);
// New node
nodes.push({
result.nodes.push({
id: id,
label: id.endsWith("*") ? "*" : id,
x: trace.id,
y: hop.number / 2,
size: 9,
labelSize: 30,
color: color(trace.id),
group: trace.origin,
radius: 8,
value: hop.name || "name?",
origin: trace.origin,
});
if (prevId) {
// New link
links.push({
label: "asd",
result.links.push({
source: prevId,
target: id,
traceId: trace.id,
origin: trace.origin,
size: 1,
color: color(trace.id),
type: trace.origin,
group: trace.origin,
});
}
prevId = id;
latestNumber = hop.number;
});
/**
* Last "destination" node, just for candy
*/
nodes.push({
// Last "destination" node
result.nodes.push({
id: trace.id,
label: trace.target,
x: trace.id,
y: (latestNumber + 1) / 2,
size: 8,
color: "black",
group: trace.origin,
radius: 8,
value: trace.target,
});
links.push({
if (prevId) {
// New link
result.links.push({
source: prevId,
target: trace.id,
target: trace.target,
type: trace.origin,
group: trace.origin,
});
}
});
// { id: ip, group: origin, radius: 2 }
// { source: prev.ip, target: ip, value: latency }
return {
nodes,
links,
};
return result;
};
async function main() {
@ -347,11 +300,9 @@ async function main() {
const data = parseNodesAndLinks(traces);
console.log("Data:", data);
// const chart = drawChart2(data.links);
const chart = drawChart2(data.links);
const chart = drawSigma(data);
// container.append(chart);
container.append(chart);
}
main();