network-monitor/app/static/js/common.js
neil be727949c1 Probes log locally only while the hub is unreachable; the hub confirms silences with that log
Probe: no access log, nothing written while reports get through. When the hub
can't be reached, failed reports go to data/outage.jsonl and unsent results to
data/backlog.jsonl (survives restarts). The first report that gets through
uploads both and they are deleted.

Hub: a probe that misses reports gets a "Not reporting" incident. When it
reports again, the runs from its outage log become confirmed "Hub unreachable"
incidents; the rest of the silence (probe off or restarting) is dropped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 20:50:30 -08:00

103 lines
4.7 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';
import { ms } from './fmt.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);
}
// Incident cells, shared by Network health and a probe's page. kind "report" rows are stretches
// when a probe didn't report; the probe's local outage log confirms them when it reports again.
const METHOD_NAMES = { icmp: 'Ping', http: 'HTTP', dns: 'DNS' };
export function incidentTarget(i) {
if (i.kind === 'report') return el('span', {}, i.label);
return el('span', {}, i.label, el('span.muted.small', {}, ` ${i.methods.map((m) => METHOD_NAMES[m] || m).join(', ')}`));
}
export function incidentTypeText(i) {
if (i.kind !== 'report') return undefined;
return i.confirmed ? 'Hub unreachable' : 'Not reporting';
}
export function incidentChecks(i) {
if (i.kind === 'report') return i.confirmed ? `${i.failed} failed reports` : 'No reports';
return i.failed ? `${i.failed}/${i.samples} failed` : `${i.samples} slow`;
}
export function incidentDetail(i) {
if (i.kind === 'report') {
if (i.confirmed) return `The probe logged that it couldn’t reach the hub${i.error ? `: ${i.error}` : ''}`;
return 'Kept if the probe’s log shows it couldn’t reach the hub, removed if it was off';
}
return i.error || (i.max_latency ? `peak ${ms(i.max_latency)}` : '');
}