network-monitor/app/static/js/common.js
neil 54a765510a Network monitor: hub, probes, Docker and native installers
Hub (FastAPI + no-build web UI):
- Network health: live probe/target matrix, status strips, incidents
- Probes: add (with install command), rename, remove, latency and throughput charts
- Clients merged from AdGuard, DHCP, query log and probe ARP tables; names,
  categories, suspicious flag, notes, per-client DNS requests
- Query log, domain detail (clients, related subdomains), blocked domains via
  the AdGuard Home /control API

Probe: tests peers (ping, HTTP, throughput), gateway, LAN and public DNS,
internet URLs and an optional VPS; buffers results while the hub is down.
Gateway and ARP from /proc/net on Linux, route/arp on macOS.

Install: install.sh (hub) and install-probe.sh (generated from it), each in
docker mode (compose; default on Linux hosts) or native mode (venv + systemd
or launchd; default on macOS and in LXC). Both modes deploy with an update
command that checks the new commit and rolls back on failure.

CLAUDE.md carries shared context for Claude Code sessions on each machine.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 18:10:29 -08:00

75 lines
3.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Shared bits for the views.
import { el } from './ui.js';
/** The screen frame: toolbar, scrolling pane, status bar. Returns the parts. */
export function screen(root, toolbarItems) {
const toolbar = el('div.toolbar', {}, ...toolbarItems);
const body = el('div.content');
const pane = el('div.pane.fill', {}, body);
const status = el('div.statusbar', {}, 'Loading…');
root.append(el('div.screen', {}, toolbar, pane, status));
return {
toolbar, body, pane, status,
setStatus(text, kind = '') { status.textContent = text; status.className = `statusbar${kind ? ` ${kind}` : ''}`; },
};
}
/** replaceChildren without the "null" text nodes the DOM makes from empty slots. */
export function fill(node, ...children) {
node.replaceChildren(...children.flat().filter((c) => c != null && c !== false));
return node;
}
export const section = (title, hint, ...extra) =>
el('div.section-head', {}, el('h2.section-title', {}, title), hint ? el('span.hint', {}, hint) : null, ...extra);
export function table(headers, rows, { empty = 'Nothing to show.' } = {}) {
if (!rows.length) return el('div.callout', {}, empty);
return el('div.table-wrap', {},
el('table.table', {},
el('thead', {}, el('tr', {}, ...headers.map((h) => (typeof h === 'string' ? el('th', {}, h) : el('th', h.props || {}, h.text))))),
el('tbody', {}, ...rows)));
}
export function errorBox(err, hint) {
return el('div.callout.error', { role: 'alert' }, err.message || String(err), hint ? el('div.small.muted', {}, hint) : null);
}
/** A notice for AdGuard-backed views when it isn't set up or reachable. */
export function adguardProblem(err) {
if (err.status === 503) {
return el('div.empty', {},
el('p', {}, 'AdGuard Home isn’t connected yet.'),
el('p.small', {}, 'Set ADGUARD_URL, ADGUARD_USERNAME and ADGUARD_PASSWORD in the hub’s .env, then restart the hub. Settings shows where that file is.'),
el('a.btn', { href: '/settings' }, 'Open settings'));
}
return errorBox(err, err.status === 502 ? 'Check that AdGuard Home is running and the hub’s .env points at its web address.' : '');
}
export const domainLink = (d) => el('a.domain', { href: `/domains/${encodeURIComponent(d)}` }, d);
export function clientLink(ip, device, fallbackName) {
const name = device?.name || fallbackName || ip;
const href = device ? `/clients/${encodeURIComponent(device.key)}` : `/queries?ip=${encodeURIComponent(ip)}`;
return el('span.chips', {},
el('a', { href, title: ip }, name),
name !== ip ? el('span.muted.small', {}, ip) : null,
device?.suspicious ? el('span.chip.danger', {}, 'Suspicious') : null);
}
export function resultChip(r) {
if (r.blocked) return el('span.chip.danger', { title: r.rule || r.reason }, 'Blocked');
if (r.reason?.startsWith('Rewrite') || r.reason === 'FilteredSafeSearch') return el('span.chip.accent', { title: r.reason }, 'Rewritten');
if (r.reason === 'NotFilteredWhiteList') return el('span.chip.ok', { title: r.rule }, 'Allowed by rule');
if (r.status && r.status !== 'NOERROR') return el('span.chip.warn', {}, r.status);
return el('span.chip', {}, 'OK');
}
export const RANGES = [['1h', '1 h'], ['6h', '6 h'], ['24h', '24 h'], ['7d', '7 d'], ['30d', '30 d']];
export const SPANS = { '1h': 3600, '6h': 21600, '24h': 86400, '7d': 604800, '30d': 2592000 };
/** Re-run fn every `ms` while the tab is visible; returns a stop function. */
export function every(ms, fn) {
const t = setInterval(() => { if (!document.hidden) fn(); }, ms);
return () => clearInterval(t);
}