Files
nettopo-go/web/index.html
T
Andrey Lutsenko 975a45ffd6 Enhance UI with latest-scan and inline diff tools.
Add automatic latest scan selection and two-scan diff viewer in web UI, then reflect completion in technical specification checklist.

Made-with: Cursor
2026-04-09 22:08:55 +10:00

191 lines
7.4 KiB
HTML

<!doctype html>
<html lang="ru">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>NetTopo UI</title>
<style>
body { font-family: Arial, sans-serif; margin: 16px; color: #1f2937; }
.row { display: flex; gap: 8px; align-items: center; margin-bottom: 12px; }
input, button { padding: 8px 10px; font-size: 14px; }
#status { margin: 8px 0; color: #374151; }
#canvas { border: 1px solid #d1d5db; border-radius: 8px; width: 100%; height: 620px; }
.hint { color: #6b7280; font-size: 13px; margin-bottom: 8px; }
</style>
</head>
<body>
<h2>NetTopo: просмотр топологии</h2>
<div class="hint">Можно выбрать последний scan автоматически или сравнить два скана через встроенный diff.</div>
<div class="row">
<input id="scanId" placeholder="scan_id" style="min-width: 380px;" />
<button id="loadBtn">Загрузить</button>
<button id="latestBtn">Последний scan</button>
<button id="fitBtn">По центру</button>
</div>
<div class="row">
<input id="fromScanId" placeholder="from scan_id" style="min-width: 280px;" />
<input id="toScanId" placeholder="to scan_id" style="min-width: 280px;" />
<button id="diffBtn">Сравнить</button>
</div>
<div id="status">Ожидание данных...</div>
<pre id="diffBox" style="background:#f8fafc;border:1px solid #e5e7eb;border-radius:8px;padding:10px;white-space:pre-wrap;"></pre>
<svg id="canvas" viewBox="0 0 1200 620"></svg>
<script>
const scanIdInput = document.getElementById("scanId");
const fromScanIdInput = document.getElementById("fromScanId");
const toScanIdInput = document.getElementById("toScanId");
const loadBtn = document.getElementById("loadBtn");
const latestBtn = document.getElementById("latestBtn");
const diffBtn = document.getElementById("diffBtn");
const fitBtn = document.getElementById("fitBtn");
const statusEl = document.getElementById("status");
const diffBox = document.getElementById("diffBox");
const svg = document.getElementById("canvas");
const NS = "http://www.w3.org/2000/svg";
function setStatus(text) { statusEl.textContent = text; }
function setDiff(text) { diffBox.textContent = text; }
function clearSVG() {
while (svg.firstChild) svg.removeChild(svg.firstChild);
}
function drawTopology(data) {
clearSVG();
const nodes = data.nodes || [];
const edges = data.edges || [];
const byIP = new Map();
nodes.forEach(n => byIP.set(n.ip, n));
const centerX = 600;
const centerY = 310;
const radius = Math.max(160, Math.min(260, 60 + nodes.length * 8));
const pos = new Map();
nodes.forEach((n, i) => {
const a = (Math.PI * 2 * i) / Math.max(nodes.length, 1);
pos.set(n.ip, {
x: centerX + radius * Math.cos(a),
y: centerY + radius * Math.sin(a),
});
});
edges.forEach(e => {
const s = pos.get(e.source_ip);
if (!s) return;
const t = pos.get(e.target_ip) || { x: s.x + 30, y: s.y + 30 };
const line = document.createElementNS(NS, "line");
line.setAttribute("x1", String(s.x));
line.setAttribute("y1", String(s.y));
line.setAttribute("x2", String(t.x));
line.setAttribute("y2", String(t.y));
line.setAttribute("stroke", "#94a3b8");
line.setAttribute("stroke-width", "1.5");
svg.appendChild(line);
});
nodes.forEach(n => {
const p = pos.get(n.ip);
if (!p) return;
const circle = document.createElementNS(NS, "circle");
circle.setAttribute("cx", String(p.x));
circle.setAttribute("cy", String(p.y));
circle.setAttribute("r", "16");
circle.setAttribute("fill", "#2563eb");
circle.setAttribute("opacity", "0.9");
svg.appendChild(circle);
const txt = document.createElementNS(NS, "text");
txt.setAttribute("x", String(p.x + 22));
txt.setAttribute("y", String(p.y + 5));
txt.setAttribute("font-size", "12");
txt.setAttribute("fill", "#111827");
txt.textContent = n.name ? `${n.name} (${n.ip})` : n.ip;
svg.appendChild(txt);
});
}
async function loadTopology() {
const id = scanIdInput.value.trim();
if (!id) {
setStatus("Введите scan_id");
return;
}
setStatus("Загружаю topology...");
try {
const res = await fetch(`/api/scans/${encodeURIComponent(id)}/topology?limit=5000`);
if (!res.ok) {
const t = await res.text();
setStatus(`Ошибка ${res.status}: ${t}`);
return;
}
const data = await res.json();
drawTopology(data);
setStatus(`Готово. Узлов: ${data.nodes?.length || 0}, связей: ${data.edges?.length || 0}`);
} catch (e) {
setStatus(`Ошибка загрузки: ${e.message}`);
}
}
async function loadLatestScan() {
setStatus("Ищу последний scan...");
try {
const res = await fetch("/api/scans?limit=1");
if (!res.ok) {
setStatus(`Ошибка ${res.status} при получении scan list`);
return;
}
const data = await res.json();
const latest = data.items && data.items[0];
if (!latest || !latest.id) {
setStatus("Сканы не найдены");
return;
}
scanIdInput.value = latest.id;
toScanIdInput.value = latest.id;
setStatus(`Найден последний scan: ${latest.id}`);
await loadTopology();
} catch (e) {
setStatus(`Ошибка загрузки последнего scan: ${e.message}`);
}
}
async function loadDiff() {
const fromId = fromScanIdInput.value.trim();
const toId = toScanIdInput.value.trim();
if (!fromId || !toId) {
setStatus("Введите from и to scan_id");
return;
}
setStatus("Сравниваю сканы...");
try {
const url = `/api/scans/diff?from=${encodeURIComponent(fromId)}&to=${encodeURIComponent(toId)}`;
const res = await fetch(url);
if (!res.ok) {
const t = await res.text();
setStatus(`Ошибка ${res.status}: ${t}`);
return;
}
const d = await res.json();
setDiff(JSON.stringify(d, null, 2));
setStatus(
`Diff готов: +hosts ${d.new_hosts?.length || 0}, -hosts ${d.missing_hosts?.length || 0}, +links ${d.new_links?.length || 0}, -links ${d.missing_links?.length || 0}`
);
} catch (e) {
setStatus(`Ошибка diff: ${e.message}`);
}
}
loadBtn.addEventListener("click", loadTopology);
latestBtn.addEventListener("click", loadLatestScan);
diffBtn.addEventListener("click", loadDiff);
fitBtn.addEventListener("click", () => {
svg.setAttribute("viewBox", "0 0 1200 620");
});
setDiff("Diff будет показан здесь...");
</script>
</body>
</html>