Files
2026-05-31 16:07:30 +02:00

263 lines
7.3 KiB
Plaintext

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Node Balancer</title>
<style>
html, body {
margin: 0;
padding: 0;
min-height: 100%;
}
body { font-family: sans-serif; margin: 20px; }
svg { border:1px solid #ccc; width:100%; background:#f9f9f9; }
.node { fill:#4CAF50; stroke:#333; stroke-width:2; }
.pod { fill:#2196F3; stroke:#000; stroke-width:1; }
.label { font-size:14px; text-anchor:middle; dominant-baseline:middle; }
.pod-label { font-size:14px; text-anchor:middle; fill:#fff; }
header {
display: flex;
align-items: center;
margin-bottom: 20px;
background-color: #f0f0f0;
padding: 10px 15px;
border-radius: 8px;
}
header .logo {
font-weight: bold;
font-size: 18px;
color: #2196F3;
margin-right: 15px;
padding: 5px 10px;
background-color: #333;
color: #fff;
border-radius: 4px;
font-family: monospace;
}
header .title {
font-size: 18px;
}
header .title span {
margin-left: 10px;
color: #555;
font-weight: normal;
}
header .cluster-label {
margin-left: 20px;
color: #333;
font-weight: normal;
}
</style>
</head>
<body>
<header>
<div class="logo">allarddcs</div>
<div class="title">
Node Balancer <span id="version">v?</span>
<span class="cluster-label">Cluster: <span id="cluster-name">?</span></span>
</div>
</header>
<svg id="canvas"></svg>
<script>
const svg = document.getElementById("canvas");
const nodePositions = {};
const nodeY = 50;
const nodeWidth = 120; // node box width
const nodeHeight = 55; // node box height
const podHeight = 36; // pod box height
const podSpacing = 6; // vertical spacing between pods
let animationDuration = 2000;
const podsPerNode = {};
// Fetch config
fetch("/api/config")
.then(res => res.json())
.then(cfg => {
animationDuration = cfg.animationDurationMs;
document.getElementById("version").textContent = "v" + cfg.version;
document.getElementById("cluster-name").textContent = cfg.clusterName;
});
// Draw nodes
fetch("/api/nodes")
.then(res => res.json())
.then(nodeNames => {
nodeNames.forEach((name,i)=>{
const x = 50 + i*220;
nodePositions[name] = {x: x + nodeWidth/2, y: nodeY + nodeHeight/2};
// Node box
const rect = document.createElementNS("http://www.w3.org/2000/svg","rect");
rect.setAttribute("x", x);
rect.setAttribute("y", nodeY);
rect.setAttribute("width", nodeWidth);
rect.setAttribute("height", nodeHeight);
rect.setAttribute("class","node");
svg.appendChild(rect);
// Node label
const label = document.createElementNS("http://www.w3.org/2000/svg","text");
label.setAttribute("x", x + nodeWidth/2);
label.setAttribute("y", nodeY + nodeHeight/2);
label.setAttribute("class","label");
label.setAttribute("text-anchor","middle");
label.setAttribute("dominant-baseline","middle");
label.textContent = name;
svg.appendChild(label);
podsPerNode[name] = [];
});
// Fetch pods after nodes
fetch("/api/pods")
.then(res => res.json())
.then(podsByNode => {
for(const nodeName in podsByNode){
podsByNode[nodeName].forEach(podName => addPod(podName, nodeName));
}
});
});
// Add pod
function addPod(podName, nodeName){
if(!nodePositions[nodeName]) return;
const stack = podsPerNode[nodeName] || [];
const index = stack.length;
const x = nodePositions[nodeName].x - nodeWidth/2;
const y = nodeY + nodeHeight + index*(podHeight + podSpacing);
const g = document.createElementNS("http://www.w3.org/2000/svg","g");
g.dataset.podName = podName;
// Pod rectangle
const rect = document.createElementNS("http://www.w3.org/2000/svg","rect");
rect.setAttribute("x", x);
rect.setAttribute("y", y);
rect.setAttribute("width", nodeWidth);
rect.setAttribute("height", podHeight);
rect.setAttribute("class","pod");
g.appendChild(rect);
const text = document.createElementNS("http://www.w3.org/2000/svg","text");
text.setAttribute("class","pod-label");
text.setAttribute("text-anchor","middle");
text.setAttribute("x", x + nodeWidth/2);
text.setAttribute("y", y); // <-- top of pod rectangle
g.appendChild(text);
// first tspan for top padding
const tspan = document.createElementNS("http://www.w3.org/2000/svg","tspan");
tspan.setAttribute("x", x + nodeWidth/2);
tspan.setAttribute("dy", 6); // top padding inside pod
tspan.textContent = podName;
text.appendChild(tspan);
svg.appendChild(g);
stack.push(g);
podsPerNode[nodeName] = stack;
updateSVGHeight();
}
// Update SVG height
function updateSVGHeight() {
let maxY = nodeY + nodeHeight;
for(const nodeName in podsPerNode){
const stack = podsPerNode[nodeName];
if(!stack || stack.length===0) continue;
const bottomY = nodeY + nodeHeight + stack.length*(podHeight + podSpacing);
if(bottomY > maxY) maxY = bottomY;
}
svg.setAttribute("height", maxY + 20);
}
// Animate pod moving
function flyPod(podName, fromNode, toNode){
if(!fromNode || !toNode) return;
const fromStack = podsPerNode[fromNode];
const idx = fromStack.findIndex(g => g.dataset.podName===podName);
if(idx===-1) return;
const g = fromStack[idx];
fromStack.splice(idx,1);
redrawPods(fromNode);
svg.appendChild(g);
const rect = g.querySelector("rect");
const text = g.querySelector("text");
const startX = parseFloat(rect.getAttribute("x"));
const startY = parseFloat(rect.getAttribute("y"));
const endX = nodePositions[toNode].x - nodeWidth/2;
const endY = nodeY + nodeHeight + podsPerNode[toNode].length*(podHeight+podSpacing);
let progress = 0;
const steps = 60;
const dx = (endX - startX)/steps;
const dy = (endY - startY)/steps;
const anim = setInterval(()=>{
progress++;
const nx = startX + dx*progress;
const ny = startY + dy*progress;
rect.setAttribute("x", nx);
rect.setAttribute("y", ny);
text.setAttribute("x", nx + nodeWidth/2);
// text y remains managed by first tspan dy
if(progress>=steps){
clearInterval(anim);
podsPerNode[toNode].push(g);
redrawPods(toNode);
}
}, animationDuration/steps);
}
// Redraw pods on a node
function redrawPods(nodeName){
if(!podsPerNode[nodeName]) return;
podsPerNode[nodeName].forEach((g,idx)=>{
const rect = g.querySelector("rect");
const text = g.querySelector("text");
const x = nodePositions[nodeName].x - nodeWidth/2;
const y = nodeY + nodeHeight + idx*(podHeight+podSpacing);
rect.setAttribute("x", x);
rect.setAttribute("y", y);
text.setAttribute("x", x + nodeWidth/2);
// leave first tspan dy as 6 for top padding
});
updateSVGHeight();
}
// SSE stream
const evtSource = new EventSource("/api/stream");
evtSource.onmessage = e=>{
const evt = JSON.parse(e.data);
if(evt.fromNode===evt.toNode){
redrawPods(evt.toNode);
return;
}
if(!evt.fromNode){
addPod(evt.pod, evt.toNode);
redrawPods(evt.toNode);
} else {
flyPod(evt.pod, evt.fromNode, evt.toNode);
}
};
</script>
</body>
</html>