Files
tendrils/static/index.html

192 lines
5.9 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tendrils Network</title>
<style>
* { box-sizing: border-box; }
body {
font-family: system-ui, sans-serif;
margin: 0;
padding: 10px;
background: #111;
color: #eee;
height: 100vh;
}
#controls {
margin-bottom: 10px;
}
#controls button {
background: #333;
color: #fff;
border: 1px solid #555;
padding: 6px 12px;
margin-right: 5px;
border-radius: 4px;
cursor: pointer;
}
#controls button:hover { background: #444; }
#cy {
background: #1a1a1a;
border: 1px solid #333;
height: calc(100vh - 50px);
width: 100%;
}
#error { color: #f66; padding: 20px; }
</style>
</head>
<body>
<div id="controls">
<strong>Tendrils</strong>
<button onclick="doLayout()">Layout</button>
<button onclick="cy.fit(50)">Fit</button>
<span id="stats"></span>
</div>
<div id="error"></div>
<div id="cy"></div>
<script src="cytoscape.min.js"></script>
<script>
let cy;
function getLabel(node) {
if (node.names && node.names.length > 0) return node.names[0];
return '??';
}
function isSwitch(node) {
return !!(node.poe_budget);
}
function doLayout() {
cy.layout({
name: 'cose',
animate: false,
padding: 50,
nodeDimensionsIncludeLabels: true,
avoidOverlap: true,
avoidOverlapPadding: 20,
nodeRepulsion: 100000,
idealEdgeLength: 200,
edgeElasticity: 100,
gravity: 0.1,
numIter: 2000,
fit: true,
randomize: true
}).run();
}
async function init() {
const resp = await fetch('/api/status');
const data = await resp.json();
const nodes = data.nodes || [];
const links = data.links || [];
document.getElementById('stats').textContent =
`${nodes.length} nodes, ${links.length} links`;
const elements = [];
const idMap = new Map();
const switchIds = new Set();
nodes.forEach((n, i) => {
const id = 'n' + i;
idMap.set(n.typeid, id);
if (isSwitch(n)) switchIds.add(id);
});
nodes.forEach((n, i) => {
const id = 'n' + i;
const sw = switchIds.has(id);
elements.push({
data: {
id: id,
label: getLabel(n),
isSwitch: sw
}
});
});
links.forEach((link, i) => {
const idA = idMap.get(link.node_a?.typeid);
const idB = idMap.get(link.node_b?.typeid);
if (!idA || !idB) return;
let label = '';
if (link.interface_a) label = link.interface_a;
if (link.interface_b) label += (label ? ' ↔ ' : '') + link.interface_b;
elements.push({
data: {
id: 'e' + i,
source: idA,
target: idB,
label: label
}
});
});
cy = cytoscape({
container: document.getElementById('cy'),
elements: elements,
style: [
{
selector: 'node',
style: {
'label': 'data(label)',
'text-valign': 'center',
'text-halign': 'center',
'background-color': '#a6d',
'color': '#fff',
'font-size': 12,
'width': 120,
'height': 40,
'padding': 8,
'shape': 'round-rectangle',
'text-wrap': 'wrap',
'text-max-width': 110
}
},
{
selector: 'node[?isSwitch]',
style: {
'background-color': '#2a2',
'border-width': 3,
'border-color': '#4f4',
'font-size': 14,
'font-weight': 'bold',
'width': 100,
'height': 50
}
},
{
selector: 'edge',
style: {
'width': 2,
'line-color': '#666',
'curve-style': 'bezier',
'label': 'data(label)',
'font-size': 9,
'color': '#aaa',
'text-background-color': '#1a1a1a',
'text-background-opacity': 1,
'text-background-padding': 2,
'text-rotation': 'autorotate'
}
}
],
layout: { name: 'preset' }
});
doLayout();
}
init().catch(e => {
document.getElementById('error').textContent = e.message;
});
</script>
</body>
</html>