1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>continuum</title>
<style>
:root {
--bg: #fafaf9; --fg: #1c1917; --muted: #78716c; --line: #e7e5e4; --panel: #ffffff;
--ok: #15803d; --warn: #c2410c; --bad: #b91c1c; --intended: #a8a29e; --self: #1d4ed8;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #1c1917; --fg: #f5f5f4; --muted: #a8a29e; --line: #44403c; --panel: #292524;
--ok: #4ade80; --warn: #fb923c; --bad: #f87171; --intended: #57534e; --self: #60a5fa;
}
}
* { box-sizing: border-box; }
html { background: var(--bg); }
/* The window's height, unless it is way too small: then the page scrolls. */
body { margin: 0; background: var(--bg); color: var(--fg);
font: 14px/1.4 system-ui, sans-serif;
height: 100vh; min-height: 480px; display: flex; flex-direction: column; }
header { display: flex; gap: 1em; align-items: baseline; padding: 12px 16px;
border-bottom: 1px solid var(--line); flex-wrap: wrap; }
header h1 { font-size: 16px; margin: 0; }
#connection { margin-left: auto; color: var(--muted); }
#connection.live { color: var(--ok); }
#connection.down { color: var(--bad); }
body.stale main { opacity: .55; }
/* Swarm on the left, the full height; peers (as tall as their rows, up to 8) above
the figures (the rest) on the right. */
main { flex: 1; min-height: 0; display: grid; gap: 16px; padding: 16px;
grid-template-columns: minmax(0, 3fr) minmax(0, 2fr);
grid-template-rows: auto minmax(0, 1fr);
grid-template-areas: "map peers" "map figures"; }
@media (max-width: 900px) {
body { height: auto; }
main { grid-template-columns: minmax(0, 1fr); grid-template-rows: 70vh auto 70vh;
grid-template-areas: "map" "peers" "figures"; }
}
section { background: var(--panel); border: 1px solid var(--line); border-radius: 6px;
padding: 12px; min-width: 0; min-height: 0; display: flex; flex-direction: column; }
section h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em;
color: var(--muted); margin: 0 0 8px; flex: none; }
section h2 .what { text-transform: none; letter-spacing: 0; color: var(--fg); }
#map { grid-area: map; }
#peers-box { grid-area: peers; }
#figures-box { grid-area: figures; }
svg { flex: 1; min-height: 0; width: 100%; display: block; cursor: default; }
svg circle { cursor: pointer; }
.legend { color: var(--muted); font-size: 12px; display: flex; gap: 1em; flex-wrap: wrap;
flex: none; }
.legend span::before { content: ""; display: inline-block; width: 18px; height: 0;
vertical-align: middle; margin-right: 4px; border-top: 2px solid var(--colour, currentColor); }
.legend .intended::before { border-top-style: dashed; --colour: var(--intended); }
.legend .used::before { border-top-width: 3px; --colour: var(--muted); }
.legend .other::before { border-top-width: 1px; --colour: var(--muted); }
.legend .direct::before { --colour: var(--muted); }
.legend .relayed::before { border-top-style: dashed; --colour: var(--muted); }
.legend .dormant::before { border-top-style: dotted; --colour: var(--bad); }
.scroll { overflow: auto; min-height: 0; }
#figures-box .scroll { flex: 1; }
table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
th, td { text-align: left; padding: 3px 8px 3px 0; border-bottom: 1px solid var(--line);
white-space: nowrap; }
th { color: var(--muted); font-weight: 500; position: sticky; top: 0;
background: var(--panel); }
#peers tr[data-peer] { cursor: pointer; }
#peers tr.focused td { background: var(--line); }
.id { font-family: ui-monospace, monospace; font-size: 12px; }
.active { color: var(--ok); } .dormant { color: var(--bad); } .unknown { color: var(--muted); }
details { margin-left: 1em; }
summary { cursor: pointer; }
.tree { font-family: ui-monospace, monospace; font-size: 12px; }
.tree > details { margin-left: 0; }
.key { color: var(--muted); }
</style>
</head>
<body>
<header>
<h1>continuum</h1>
<span id="node" class="id"></span>
<span id="connection">connecting…</span>
</header>
<main>
<section id="map">
<h2>Swarm</h2>
<div class="legend" id="legend"></div>
<svg id="graph"></svg>
</section>
<section id="peers-box">
<h2>Peers</h2>
<div class="scroll" id="peers-scroll"><table id="peers"></table></div>
</section>
<section id="figures-box">
<h2 id="figures-title">Figures</h2>
<div class="scroll"><div id="stats" class="tree"></div></div>
</section>
</main>
<script>
"use strict";
// The state as the node published it, and when it did (the last event).
let state = null;
let stateAt = 0;
// Fields that grow by themselves (`GROWING` and `is_age` in the node's dashboard).
const GROWING = new Set(["uptimeSecs", "dormantForSecs", "observedSecs",
"lastFromNeighbourSecs", "oldestQuarantineSecs"]);
const isAge = key => key === "secsAgo" || key.endsWith("SecsAgo") || GROWING.has(key);
const elapsed = () => Math.floor((Date.now() - stateAt) / 1000);
const aged = (key, value) =>
isAge(key) && typeof value === "number" ? value + elapsed() : value;
// RFC 7386.
function mergePatch(target, patch) {
if (patch === null || typeof patch !== "object" || Array.isArray(patch)) return patch;
const result = target !== null && typeof target === "object" && !Array.isArray(target)
? target : {};
for (const [key, value] of Object.entries(patch)) {
if (value === null) delete result[key];
else result[key] = mergePatch(result[key], value);
}
return result;
}
function el(tag, attrs, ...children) {
const node = document.createElement(tag);
for (const [key, value] of Object.entries(attrs || {})) node.setAttribute(key, value);
for (const child of children) {
node.append(child instanceof Node ? child : document.createTextNode(String(child)));
}
return node;
}
const SVG = "http://www.w3.org/2000/svg";
function svg(tag, attrs) {
const node = document.createElementNS(SVG, tag);
for (const [key, value] of Object.entries(attrs || {})) node.setAttribute(key, value);
return node;
}
const short = id => id.length > 14 ? id.slice(0, 6) + "…" + id.slice(-6) : id;
function name(id) {
const member = state.graph.members[id];
return (member && member.alias) || (state.peers.peers[id] || {}).alias || short(id);
}
function age(secs) {
if (secs === null || secs === undefined) return "";
if (secs < 120) return secs + " s";
if (secs < 7200) return Math.floor(secs / 60) + " min";
if (secs < 172800) return Math.floor(secs / 3600) + " h";
return Math.floor(secs / 86400) + " d";
}
// The node in focus: its figures are shown. null: this node's (`/admin/stats`).
let focused = null;
function focus(id) {
focused = id === null || id === state.node.id ? null : id;
render();
}
// --- Peers ---
function renderPeers() {
const table = document.getElementById("peers");
const rows = [el("tr", {}, ...["peer", "status", "seen", "via", "rtt", "neighbour", "same epoch"]
.map(h => el("th", {}, h)))];
for (const [id, peer] of Object.entries(state.peers.peers)) {
const destination = peer.destination;
const path = destination && destination.paths && destination.paths[0];
const rtt = path && typeof path.rttMs === "number" ? path.rttMs.toFixed(1) + " ms" : "";
const via = destination
? destination.address + (destination.connection ? " (" + destination.connection + ")" : "")
: "";
const row = el("tr", { "data-peer": id, class: id === focused ? "focused" : "" },
el("td", { title: id }, peer.alias || el("span", { class: "id" }, short(id))),
el("td", { class: peer.status }, peer.status +
(peer.status === "dormant" ? " " + age(aged("dormantForSecs", peer.dormantForSecs)) : "")),
el("td", {}, age(aged("lastSeenSecsAgo", peer.lastSeenSecsAgo))),
el("td", { class: "id" }, via),
el("td", {}, rtt),
el("td", {}, peer.neighbour ? "yes" : ""),
el("td", {}, peer.sameEpoch === null || peer.sameEpoch === undefined
? "" : peer.sameEpoch ? "yes" : "no"));
row.addEventListener("click", () => focus(id === focused ? null : id));
rows.push(row);
}
table.replaceChildren(...rows);
// As tall as the rows, up to 8 of them (and the header); then it scrolls.
const scroll = document.getElementById("peers-scroll");
scroll.style.maxHeight = rows.length > 9
? (rows[8].offsetTop + rows[8].offsetHeight) + "px" : "";
}
// --- Figures: a tree, its open branches kept across renders ---
const open = new Set(["stats", "peer", "links"]);
function tree(key, value, path) {
if (value !== null && typeof value === "object") {
const details = el("details", {});
if (open.has(path)) details.open = true;
details.addEventListener("toggle", () => {
if (details.open) open.add(path); else open.delete(path);
});
const entries = Array.isArray(value) ? value.map((v, i) => [String(i), v]) : Object.entries(value);
details.append(el("summary", {}, el("span", { class: "key" }, key),
Array.isArray(value) ? " [" + value.length + "]" : ""));
for (const [k, v] of entries) details.append(tree(k, v, path + "." + k));
return details;
}
return el("div", {}, el("span", { class: "key" }, key + ": "),
JSON.stringify(aged(key, value)));
}
function renderStats() {
const title = document.getElementById("figures-title");
const what = el("span", { class: "what" });
let shown;
if (focused === null) {
what.textContent = name(state.node.id) + " (this node)";
shown = [tree("stats", state.stats, "stats")];
} else {
what.textContent = name(focused);
what.title = focused;
const peer = state.peers.peers[focused];
const report = state.graph.measured[focused];
shown = [];
if (peer) shown.push(tree("peer", peer, "peer"));
else shown.push(el("div", { class: "key" }, "Not a peer of this node."));
if (report) shown.push(tree("links", report, "links"));
}
title.replaceChildren(focused === null ? "Figures: " : "Peer: ", what);
document.getElementById("stats").replaceChildren(...shown);
}
// --- Swarm map: a small force layout, positions kept across renders ---
const positions = new Map();
const transportColour = new Map([["mycelium", "#2563eb"], ["libp2p", "#9333ea"],
["tor", "#0891b2"]]);
const SPARE = ["#ca8a04", "#db2777", "#65a30d"];
function colourOf(transport) {
if (!transport) return "var(--muted)";
if (!transportColour.has(transport)) {
transportColour.set(transport, SPARE[(transportColour.size - 3) % SPARE.length]);
}
return transportColour.get(transport);
}
function renderLegend(measured) {
const transports = new Set();
for (const { link } of measured.values()) {
transports.add(link.transport || "");
for (const other of link.others || []) transports.add(other.transport);
}
const items = [el("span", { class: "intended" }, "intended (member list)")];
for (const transport of [...transports].sort()) {
const item = el("span", {}, transport || "unknown transport");
item.style.setProperty("--colour", colourOf(transport || null));
items.push(item);
}
items.push(el("span", { class: "used" }, "in use"),
el("span", { class: "other" }, "other transport"),
el("span", { class: "direct" }, "direct"),
el("span", { class: "relayed" }, "relayed"),
el("span", { class: "dormant" }, "dormant"));
document.getElementById("legend").replaceChildren(...items);
}
function edges() {
const intended = new Map();
for (const [a, neighbours] of Object.entries(state.graph.intended)) {
for (const b of neighbours) {
const key = a < b ? a + " " + b : b + " " + a;
intended.set(key, [a, b]);
}
}
// One measured edge per pair: a report with an RTT, then the fresher.
const measured = new Map();
for (const [a, report] of Object.entries(state.graph.measured)) {
for (const link of report.links) {
const key = a < link.peer ? a + " " + link.peer : link.peer + " " + a;
const known = measured.get(key);
const timed = typeof link.rttMs === "number";
const knownTimed = known && typeof known.link.rttMs === "number";
if (!known || (timed && !knownTimed) ||
(timed === knownTimed && report.secsAgo < known.secsAgo)) {
measured.set(key, { a, b: link.peer, link, secsAgo: report.secsAgo });
}
}
}
return { intended, measured };
}
function layout(nodes, pairs, width, height) {
for (const id of nodes) {
if (!positions.has(id)) {
positions.set(id, { x: width / 2 + (Math.random() - .5) * width / 2,
y: height / 2 + (Math.random() - .5) * height / 2 });
}
}
const ideal = Math.min(width, height) / Math.max(2, Math.sqrt(nodes.length) * 1.5);
for (let round = 0; round < 120; round++) {
const force = new Map(nodes.map(id => [id, { x: 0, y: 0 }]));
for (let i = 0; i < nodes.length; i++) {
for (let j = i + 1; j < nodes.length; j++) {
const p = positions.get(nodes[i]), q = positions.get(nodes[j]);
const dx = p.x - q.x, dy = p.y - q.y, d2 = Math.max(dx * dx + dy * dy, 1);
const push = ideal * ideal / d2;
force.get(nodes[i]).x += dx * push / 10; force.get(nodes[i]).y += dy * push / 10;
force.get(nodes[j]).x -= dx * push / 10; force.get(nodes[j]).y -= dy * push / 10;
}
}
for (const [a, b] of pairs) {
const p = positions.get(a), q = positions.get(b);
if (!p || !q) continue;
const dx = q.x - p.x, dy = q.y - p.y, d = Math.max(Math.hypot(dx, dy), 1);
const pull = (d - ideal) / d / 4;
force.get(a).x += dx * pull; force.get(a).y += dy * pull;
force.get(b).x -= dx * pull; force.get(b).y -= dy * pull;
}
for (const id of nodes) {
const p = positions.get(id), f = force.get(id);
p.x = Math.min(width - 40, Math.max(40, p.x + Math.max(-10, Math.min(10, f.x))));
p.y = Math.min(height - 20, Math.max(20, p.y + Math.max(-10, Math.min(10, f.y))));
}
}
}
let lastShape = "";
function renderGraph() {
const root = document.getElementById("graph");
const width = root.clientWidth || 800, height = root.clientHeight || 560;
root.setAttribute("viewBox", "0 0 " + width + " " + height);
const nodes = Object.keys(state.graph.members).sort();
const { intended, measured } = edges();
renderLegend(measured);
const pairs = [...intended.values(), ...[...measured.values()].map(m => [m.a, m.b])];
// Laid out anew only when who's there or who links to whom changed.
const shape = nodes.join(",") + "|" + [...intended.keys(), ...measured.keys()].sort().join(",");
if (shape !== lastShape) {
for (const id of [...positions.keys()]) if (!nodes.includes(id)) positions.delete(id);
layout(nodes, pairs, width, height);
lastShape = shape;
}
const children = [];
for (const [key, [a, b]] of intended) {
if (measured.has(key)) continue;
const p = positions.get(a), q = positions.get(b);
children.push(svg("line", { x1: p.x, y1: p.y, x2: q.x, y2: q.y,
stroke: "var(--intended)", "stroke-dasharray": "4 4", "stroke-width": 1 }));
}
const ms = rtt => rtt < 10 ? rtt.toFixed(1) : String(Math.round(rtt));
for (const { a, b, link, secsAgo } of measured.values()) {
const p = positions.get(a), q = positions.get(b);
if (!p || !q) continue;
// The transport in use thick, in the middle; the others thin, beside it.
const lines = [{ transport: link.transport, rttMs: link.rttMs, used: true }];
for (const other of link.others || []) lines.push({ ...other, used: false });
const length = Math.max(Math.hypot(q.x - p.x, q.y - p.y), 1);
const nx = -(q.y - p.y) / length, ny = (q.x - p.x) / length;
lines.forEach((line, i) => {
// 0, then +1, -1, +2, -2 … steps of 6 px to the side.
const side = i === 0 ? 0 : (i % 2 ? 1 : -1) * Math.ceil(i / 2) * 6;
const ox = nx * side, oy = ny * side;
const drawn = svg("line", { x1: p.x + ox, y1: p.y + oy, x2: q.x + ox, y2: q.y + oy,
stroke: link.dormant ? "var(--bad)" : colourOf(line.transport),
"stroke-width": line.used ? 3 : 1.2 });
// Dotted where dormant, dashed where relayed, solid where direct.
if (link.dormant) {
drawn.setAttribute("stroke-dasharray", line.used ? "1 5" : "1 3");
drawn.setAttribute("stroke-linecap", "round");
} else if (line.used && link.relayed) {
drawn.setAttribute("stroke-dasharray", "7 4");
}
const title = svg("title");
title.textContent = name(a) + " – " + name(b) +
(line.transport ? " via " + line.transport : "") + (line.used ? " (in use)" : "") +
(line.used && link.relayed ? ", relayed" : "") + (link.dormant ? ", dormant" : "") +
(typeof line.rttMs === "number" ? ", " + line.rttMs.toFixed(1) + " ms" : "") +
", measured " + age(secsAgo + elapsed()) + " ago";
drawn.append(title);
children.push(drawn);
if (typeof line.rttMs === "number") {
// The one in use at the middle, larger; the others further along, beside theirs.
const t = line.used ? .5 : .3;
const out = line.used ? -4 : Math.sign(side) * 9;
const label = svg("text", {
x: p.x + (q.x - p.x) * t + ox + nx * out,
y: p.y + (q.y - p.y) * t + oy + ny * out + (line.used ? 0 : 3),
"text-anchor": "middle", "font-size": line.used ? 11 : 9,
fill: line.used ? "var(--fg)" : "var(--muted)" });
label.textContent = ms(line.rttMs);
children.push(label);
}
});
}
for (const id of nodes) {
const p = positions.get(id), member = state.graph.members[id];
const reported = id in state.graph.measured;
const shown = focused === null ? member.self : id === focused;
if (shown) {
children.push(svg("circle", { cx: p.x, cy: p.y, r: 13, fill: "none",
stroke: "var(--self)", "stroke-width": 2, "stroke-opacity": .5 }));
}
const circle = svg("circle", { cx: p.x, cy: p.y, r: member.self ? 8 : 6,
fill: member.self ? "var(--self)" : reported ? "var(--fg)" : "var(--panel)",
stroke: member.self ? "var(--self)" : "var(--fg)", "stroke-width": 1.5 });
const title = svg("title");
title.textContent = id + (reported ? "" : " (no links reported)");
circle.append(title);
circle.addEventListener("click", event => { event.stopPropagation(); focus(id); });
const label = svg("text", { x: p.x + 10, y: p.y + 4, "font-size": 12, fill: "var(--fg)" });
label.textContent = name(id);
label.style.cursor = "pointer";
label.addEventListener("click", event => { event.stopPropagation(); focus(id); });
children.push(circle, label);
}
root.replaceChildren(...children);
}
function render() {
if (!state) return;
document.getElementById("node").textContent =
(state.node.alias ? state.node.alias + " · " : "") + state.node.id;
renderPeers();
renderStats();
renderGraph();
}
// A click beside the nodes: back to this node.
document.getElementById("graph").addEventListener("click", () => focus(null));
// --- Events ---
const connection = document.getElementById("connection");
function status(text, live) {
connection.textContent = text;
connection.className = live ? "live" : "down";
document.body.classList.toggle("stale", !live && state !== null);
}
// While not live, what is shown stays as it was last (ages don't count on).
let live = false;
let events = null;
function connect() {
events = new EventSource("dashboard/events");
events.addEventListener("snapshot", event => {
state = JSON.parse(event.data); stateAt = Date.now(); live = true;
status("live", true); render();
});
events.addEventListener("patch", event => {
if (!state) return;
state = mergePatch(state, JSON.parse(event.data)); stateAt = Date.now(); render();
});
// The node stops: it says so, and ends the stream. Back once it is.
events.addEventListener("offline", () => {
events.close(); live = false;
status("node stopped; reconnecting…", false);
setTimeout(connect, 3000);
});
events.addEventListener("error", () => {
live = false;
if (events.readyState === EventSource.CLOSED) {
// Given up (an HTTP error, e.g. the node is up but not serving the page yet).
status("disconnected; retrying…", false);
setTimeout(connect, 5000);
} else {
// EventSource reconnects by itself; the node sends a snapshot first again.
status("reconnecting…", false);
}
});
}
connect();
// Ages count on between events; nothing else changes without one.
setInterval(() => { if (live) render(); }, 1000);
window.addEventListener("resize", () => { lastShape = ""; render(); });
</script>
</body>
</html>
|