Phone card layout, status symbols, breadcrumbs, integration cards and server trends

Tables stack into labelled cards on a phone. Green, red and amber badges carry
a check, cross or triangle as well as their colour. Pages inside a menu group
show a breadcrumb, with the thing's name on a server's page. Integrations are
cards with an Open button and a live status (working, failing, not checked yet,
disabled). Agent-reported servers keep a short history of load, memory and disk
use (migration 0019, one sample per ten minutes for 35 days) and show it as a
Trends card with a days-until-full forecast for a filling disk.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-14 23:56:05 +02:00
1 parent 617dc1ea2f
commit 9965d94036
22 files changed
+3000 -84

No files matched your search

@@ -24,6 +24,18 @@ export function getFailingSources(): { source: string; consecutiveFailures: numb
.map(([source, state]) => ({ source, consecutiveFailures: state.consecutiveFailures, alerted: state.alerted }));
}
/**
* How a service has been doing since the app started: "failing" while its latest calls keep failing, "ok" once a call has gone
* through, and "unknown" when nothing has called it yet (this is a live streak, kept in memory, not a record).
*/
export function getSourceStatus(source: string): { status: "ok" | "failing" | "unknown"; consecutiveFailures: number } {
const state = health.get(source);
if (!state) return { status: "unknown", consecutiveFailures: 0 };
return state.consecutiveFailures > 0
? { status: "failing", consecutiveFailures: state.consecutiveFailures }
: { status: "ok", consecutiveFailures: 0 };
}
/** Called after every diagnostic-log entry is recorded, to track consecutive failures per source and alert on threshold-cross / recovery. */
export async function trackIntegrationHealth(source: string, ok: boolean): Promise<void> {
const state = health.get(source) ?? { consecutiveFailures: 0, alerted: false };
+169
View File
@@ -0,0 +1,169 @@
/**
* A short history of what each agent-reported server's load, memory and disks looked like, so a page can show a trend
* (and say how soon a filling disk will run out) instead of only the latest figure.
*
* One sample is kept per server every ten minutes at most, however often its agent reports, for 35 days. Everything here is a
* percentage: CPU load (as the agent reports it), memory in use, and each disk's used share.
*/
import { and, desc, eq, gte, lt } from "drizzle-orm";
import { db } from "../db/client.js";
import { serverMetrics } from "../db/schema.js";
import type { IncomingSystemInfo } from "./taskSync.js";
/** Reports closer together than this add no sample. */
export const MIN_GAP_MS = 10 * 60_000;
export const RETENTION_DAYS = 35;
const DAY_MS = 24 * 3600_000;
export interface Sample {
metric: string;
value: number;
}
const round1 = (n: number) => Math.round(n * 10) / 10;
const clampPercent = (n: number) => Math.min(100, Math.max(0, n));
/** The percentages in one agent report. Anything missing or nonsensical (no size, not a number) is left out. */
export function samplesFromSystem(system: IncomingSystemInfo | null | undefined): Sample[] {
if (!system) return [];
const out: Sample[] = [];
const load = system.cpu?.load_percent;
if (typeof load === "number" && Number.isFinite(load)) out.push({ metric: "cpu", value: round1(clampPercent(load)) });
const total = system.memory?.total_bytes;
const used = system.memory?.used_bytes;
if (typeof total === "number" && typeof used === "number" && total > 0) out.push({ metric: "mem", value: round1(clampPercent((used / total) * 100)) });
for (const d of system.disks ?? []) {
if (typeof d.mount !== "string" || !d.mount || !(d.size_bytes > 0) || typeof d.used_bytes !== "number") continue;
out.push({ metric: `disk:${d.mount}`, value: round1(clampPercent((d.used_bytes / d.size_bytes) * 100)) });
}
return out;
}
/** Records a report's figures, unless this server was sampled a moment ago. Returns whether anything was stored. */
export async function recordServerMetrics(serverId: number, system: IncomingSystemInfo | null | undefined, now: Date = new Date()): Promise<boolean> {
const samples = samplesFromSystem(system);
if (samples.length === 0) return false;
const [latest] = await db
.select({ at: serverMetrics.recordedAt })
.from(serverMetrics)
.where(eq(serverMetrics.serverId, serverId))
.orderBy(desc(serverMetrics.recordedAt))
.limit(1);
if (latest && now.getTime() - Date.parse(latest.at) < MIN_GAP_MS) return false;
const at = now.toISOString();
await db.insert(serverMetrics).values(samples.map((s) => ({ serverId, metric: s.metric, value: s.value, recordedAt: at })));
await db.delete(serverMetrics).where(and(eq(serverMetrics.serverId, serverId), lt(serverMetrics.recordedAt, new Date(now.getTime() - RETENTION_DAYS * DAY_MS).toISOString())));
return true;
}
export interface Point {
/** ISO time. */
t: string;
v: number;
}
/** Averages points into at most `maxPoints` equal slices of [from, to], so a month of samples draws as a short line. */
export function bucketPoints(points: { t: number; v: number }[], from: number, to: number, maxPoints: number): Point[] {
if (points.length === 0 || to <= from) return [];
const size = Math.max((to - from) / maxPoints, 1);
const buckets = new Map<number, { sum: number; n: number; tSum: number }>();
for (const p of points) {
if (p.t < from || p.t > to) continue;
const key = Math.min(maxPoints - 1, Math.floor((p.t - from) / size));
const b = buckets.get(key) ?? { sum: 0, n: 0, tSum: 0 };
b.sum += p.v;
b.n += 1;
b.tSum += p.t;
buckets.set(key, b);
}
return [...buckets.entries()]
.sort((a, b) => a[0] - b[0])
.map(([, b]) => ({ t: new Date(b.tSum / b.n).toISOString(), v: round1(b.sum / b.n) }));
}
export interface Trend {
/** Change in the percentage per day, from a straight line through the points. */
perDay: number;
/** For something that fills up: days until it reaches 100% at that rate. Null when it isn't rising or the rate is negligible. */
daysToFull: number | null;
}
/**
* A straight-line fit over the points. Needs at least six points covering at least a day; with less there is no honest trend to
* give. A slope under 0.05 points a day counts as flat, so a disk that wobbles by a fraction isn't forecast to fill in a century.
*/
export function trendOf(points: { t: number; v: number }[]): Trend | null {
if (points.length < 6) return null;
const span = points[points.length - 1].t - points[0].t;
if (span < DAY_MS) return null;
const t0 = points[0].t;
const xs = points.map((p) => (p.t - t0) / DAY_MS);
const ys = points.map((p) => p.v);
const n = points.length;
const meanX = xs.reduce((a, b) => a + b, 0) / n;
const meanY = ys.reduce((a, b) => a + b, 0) / n;
let num = 0;
let den = 0;
for (let i = 0; i < n; i++) {
num += (xs[i] - meanX) * (ys[i] - meanY);
den += (xs[i] - meanX) ** 2;
}
if (den === 0) return null;
const slope = num / den;
const perDay = Math.round(slope * 100) / 100;
const last = ys[n - 1];
const daysToFull = perDay >= 0.05 && last < 100 ? Math.ceil((100 - last) / perDay) : null;
return { perDay, daysToFull };
}
export interface MetricSeries {
metric: string;
kind: "cpu" | "mem" | "disk";
label: string;
points: Point[];
latest: number | null;
trend: Trend | null;
}
function describe(metric: string): { kind: MetricSeries["kind"]; label: string } {
if (metric === "cpu") return { kind: "cpu", label: "CPU load" };
if (metric === "mem") return { kind: "mem", label: "Memory" };
return { kind: "disk", label: `Disk ${metric.slice("disk:".length)}` };
}
const ORDER = { cpu: 0, mem: 1, disk: 2 } as const;
/** Every series recorded for a server over the last `days`, ready to draw. */
export async function getServerMetrics(serverId: number, days: number, now: Date = new Date()): Promise<MetricSeries[]> {
const to = now.getTime();
const from = to - days * DAY_MS;
const rows = await db
.select({ metric: serverMetrics.metric, value: serverMetrics.value, at: serverMetrics.recordedAt })
.from(serverMetrics)
.where(and(eq(serverMetrics.serverId, serverId), gte(serverMetrics.recordedAt, new Date(from).toISOString())))
.orderBy(serverMetrics.recordedAt);
const byMetric = new Map<string, { t: number; v: number }[]>();
for (const r of rows) {
const list = byMetric.get(r.metric) ?? [];
list.push({ t: Date.parse(r.at), v: r.value });
byMetric.set(r.metric, list);
}
return [...byMetric.entries()]
.map(([metric, raw]) => {
const { kind, label } = describe(metric);
return {
metric,
kind,
label,
points: bucketPoints(raw, from, to, 96),
latest: raw[raw.length - 1]?.v ?? null,
// A disk filling is the thing worth forecasting; load and memory go up and down by the hour.
trend: kind === "disk" ? trendOf(raw) : null,
};
})
.sort((a, b) => ORDER[a.kind] - ORDER[b.kind] || a.label.localeCompare(b.label));
}
+8
View File
@@ -1,6 +1,7 @@
import { and, eq, notInArray } from "drizzle-orm";
import { db } from "../db/client.js";
import { scheduledTasks, servers } from "../db/schema.js";
import { recordServerMetrics } from "./serverMetrics.js";
export interface IncomingTask {
scheduleType: "cron" | "systemd_timer" | "windows_task";
@@ -121,4 +122,11 @@ export async function syncServerTasks(serverId: number, report: AgentReport) {
...(system?.listening_ports ? { listeningPorts: JSON.stringify(system.listening_ports) } : {}),
})
.where(eq(servers.id, serverId));
// Keep a little history of the figures too. A failure here must not lose the report itself, which is already saved.
try {
await recordServerMetrics(serverId, system, new Date(now));
} catch (err) {
console.error("[serverMetrics] couldn't record a sample:", err);
}
}