- Speedtest Tracker integration: charts, alerts for a slow line (median of the last three tests under a share of what is paid for) and for failing tests. - Costs page (Records): things paid for by hand plus Hetzner's estimate, totals per currency, bills rolled forward into Coming up as Renewals. - Status pages: a "Having trouble" state for things that answer but have an open alert; Speedtest reads as the internet. - Integrations page and kind picker grouped like the sidebar (one shared types file); Notifications "What to send" shown one group at a time; Dashboard integration sections follow the menu groups, with tiles for integrations that have no widget. - Docs, tests, and ROLES.md menu table brought up to date. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
239 lines
12 KiB
TypeScript
239 lines
12 KiB
TypeScript
/**
|
|
* A status page: the one thing someone at home wants to know — "is Plex working?" — without a sign-in and without anything that isn't
|
|
* theirs to see. An administrator makes a share link of the status kind and picks what is on it, each with a name people will recognise
|
|
* ("Movies", "Photos") instead of what the machine is called. Each is a server (reporting, or not), an integration (its latest calls work,
|
|
* or don't) or an Uptime Kuma monitor (up, down, or in maintenance).
|
|
*
|
|
* What the public gets back is only: the page's title and message, and for each item its name, whether it is up, having trouble, down, being
|
|
* worked on or can't be told, and — where known — how it has done over 30 days. No addresses, no ids, no alert text.
|
|
*
|
|
* "Having trouble" is a thing that answers but has an alert open that nobody has silenced, snoozed or closed — a disk filling up, a queue that
|
|
* is stuck, an internet line slower than it is sold at. The alert itself is never shown; the family is only told that it is not quite right.
|
|
*/
|
|
import { and, asc, eq } from "drizzle-orm";
|
|
import { db } from "../db/client.js";
|
|
import { integrations, servers, statusItems, type StatusItemKind } from "../db/schema.js";
|
|
import { createUptimeKumaAdapter, type UptimeKumaMonitor } from "../integrations/uptimekuma/adapter.js";
|
|
import { loadIntegrationConfig } from "../integrations/loadIntegration.js";
|
|
import { getAlerts } from "./alerts.js";
|
|
import type { Alert } from "./alertTypes.js";
|
|
import { getSourceStatus } from "./integrationHealthMonitor.js";
|
|
import { activeSubjects } from "./maintenance.js";
|
|
import { effectiveThresholds, parseThresholds } from "./serverThresholds.js";
|
|
import { getSettings } from "./settingsStore.js";
|
|
import { getUptime } from "./uptimeHistory.js";
|
|
|
|
export type ItemState = "up" | "trouble" | "down" | "maintenance" | "unknown";
|
|
export type PageLevel = "ok" | "trouble" | "issue" | "maintenance" | "unknown";
|
|
|
|
export const MAX_ITEMS = 30;
|
|
export const MAX_LABEL = 60;
|
|
export const MAX_TITLE = 60;
|
|
export const MAX_MESSAGE = 300;
|
|
|
|
export interface StatusItemInput {
|
|
label: string;
|
|
kind: StatusItemKind;
|
|
/** The server, the integration, or the Uptime Kuma integration a monitor belongs to. */
|
|
targetId: number;
|
|
/** For a monitor: Uptime Kuma's own id for it. */
|
|
monitorId?: string | null;
|
|
}
|
|
|
|
export class StatusPageError extends Error {
|
|
constructor(
|
|
readonly status: number,
|
|
readonly code: string,
|
|
message: string,
|
|
) {
|
|
super(message);
|
|
}
|
|
}
|
|
|
|
export interface StoredItem {
|
|
id: number;
|
|
label: string;
|
|
kind: StatusItemKind;
|
|
targetId: number;
|
|
monitorId: string | null;
|
|
/** What it points at, for the editor: the server's or integration's name, or the monitor's. */
|
|
targetName: string | null;
|
|
}
|
|
|
|
export async function getStatusItems(linkId: number): Promise<StoredItem[]> {
|
|
const rows = await db.select().from(statusItems).where(eq(statusItems.linkId, linkId)).orderBy(asc(statusItems.position), asc(statusItems.id));
|
|
if (rows.length === 0) return [];
|
|
const serverNames = new Map((await db.select({ id: servers.id, name: servers.name }).from(servers)).map((s) => [s.id, s.name]));
|
|
const integrationNames = new Map((await db.select({ id: integrations.id, name: integrations.name }).from(integrations)).map((i) => [i.id, i.name]));
|
|
return rows.map((r) => ({
|
|
id: r.id,
|
|
label: r.label,
|
|
kind: r.kind,
|
|
targetId: r.targetId,
|
|
monitorId: r.monitorId,
|
|
targetName: r.kind === "server" ? (serverNames.get(r.targetId) ?? null) : (integrationNames.get(r.targetId) ?? null),
|
|
}));
|
|
}
|
|
|
|
/** Replaces the whole list, in the order given. Everything it points at has to exist; nothing is changed if anything is wrong. */
|
|
export async function setStatusItems(linkId: number, items: StatusItemInput[]): Promise<void> {
|
|
if (items.length > MAX_ITEMS) throw new StatusPageError(400, "too_many", `A status page can show up to ${MAX_ITEMS} things.`);
|
|
const serverIds = new Set((await db.select({ id: servers.id }).from(servers)).map((s) => s.id));
|
|
const integrationRows = new Map((await db.select({ id: integrations.id, type: integrations.type }).from(integrations)).map((i) => [i.id, i.type]));
|
|
for (const [i, item] of items.entries()) {
|
|
const where = `"${item.label}"`;
|
|
if (!item.label.trim() || item.label.length > MAX_LABEL) throw new StatusPageError(400, "invalid_label", `Give each thing a name of up to ${MAX_LABEL} characters (item ${i + 1}).`);
|
|
if (item.kind === "server" && !serverIds.has(item.targetId)) throw new StatusPageError(400, "unknown_target", `${where} points at a server that doesn't exist.`);
|
|
if (item.kind === "integration" && !integrationRows.has(item.targetId)) throw new StatusPageError(400, "unknown_target", `${where} points at an integration that doesn't exist.`);
|
|
if (item.kind === "monitor") {
|
|
if (integrationRows.get(item.targetId) !== "uptimekuma") throw new StatusPageError(400, "unknown_target", `${where} points at an Uptime Kuma that doesn't exist.`);
|
|
if (!item.monitorId || item.monitorId.length > 100) throw new StatusPageError(400, "invalid_monitor", `${where} needs one of Uptime Kuma's monitors.`);
|
|
}
|
|
}
|
|
await db.delete(statusItems).where(eq(statusItems.linkId, linkId));
|
|
if (items.length > 0) {
|
|
await db.insert(statusItems).values(items.map((item, position) => ({ linkId, position, label: item.label.trim(), kind: item.kind, targetId: item.targetId, monitorId: item.kind === "monitor" ? (item.monitorId ?? null) : null })));
|
|
}
|
|
}
|
|
|
|
// ─── The picture the public gets ────────────────────────────────────────────
|
|
|
|
export interface PublicStatusItem {
|
|
id: number;
|
|
label: string;
|
|
state: ItemState;
|
|
/** Percent over the last 30 days, where it is known. */
|
|
uptime30d: number | null;
|
|
}
|
|
|
|
export interface PublicStatus {
|
|
title: string;
|
|
message: string | null;
|
|
level: PageLevel;
|
|
counts: Record<ItemState, number>;
|
|
items: PublicStatusItem[];
|
|
generatedAt: string;
|
|
display: { dateFormat: string; timeFormat: string };
|
|
}
|
|
|
|
/** How the whole page reads: anything down is an issue; then anything having trouble; then anything being worked on; otherwise fine — unless nothing could be told at all. */
|
|
export function pageLevel(counts: Record<ItemState, number>): PageLevel {
|
|
if (counts.down > 0) return "issue";
|
|
if (counts.trouble > 0) return "trouble";
|
|
if (counts.maintenance > 0) return "maintenance";
|
|
if (counts.up === 0 && counts.unknown > 0) return "unknown";
|
|
return "ok";
|
|
}
|
|
|
|
let alertsLoader: (() => Promise<Pick<Alert, "id" | "severity" | "silenced" | "target">[]>) | null = null;
|
|
|
|
/** For tests: use these alerts instead of collecting the real ones (which asks every integration). */
|
|
export function setAlertsLoaderForTests(loader: typeof alertsLoader): void {
|
|
alertsLoader = loader;
|
|
}
|
|
|
|
// Uptime Kuma is asked at most this often per instance, however many people have the page open.
|
|
const KUMA_CACHE_MS = 30_000;
|
|
const kumaCache = new Map<number, { at: number; monitors: Map<string, UptimeKumaMonitor> | null }>();
|
|
let kumaLoader: ((integrationId: number) => Promise<UptimeKumaMonitor[] | null>) | null = null;
|
|
|
|
/** For tests: read monitors from this instead of a real Uptime Kuma. */
|
|
export function setKumaLoaderForTests(loader: typeof kumaLoader): void {
|
|
kumaLoader = loader;
|
|
kumaCache.clear();
|
|
}
|
|
|
|
async function monitorsOf(integrationId: number, now: number): Promise<Map<string, UptimeKumaMonitor> | null> {
|
|
const cached = kumaCache.get(integrationId);
|
|
if (cached && now - cached.at < KUMA_CACHE_MS) return cached.monitors;
|
|
let monitors: Map<string, UptimeKumaMonitor> | null = null;
|
|
try {
|
|
let list: UptimeKumaMonitor[] | null;
|
|
if (kumaLoader) list = await kumaLoader(integrationId);
|
|
else {
|
|
const loaded = await loadIntegrationConfig(integrationId);
|
|
list = loaded && loaded.integration.type === "uptimekuma" && loaded.integration.enabled ? await createUptimeKumaAdapter(loaded.config as never).listMonitors() : null;
|
|
}
|
|
monitors = list ? new Map(list.map((m) => [m.id, m])) : null;
|
|
} catch {
|
|
monitors = null; // can't be told, so it says so — never "up"
|
|
}
|
|
kumaCache.set(integrationId, { at: now, monitors });
|
|
return monitors;
|
|
}
|
|
|
|
export async function buildPublicStatus(link: { id: number; label: string; title: string | null; message: string | null }, now: Date = new Date()): Promise<PublicStatus> {
|
|
const items = await db.select().from(statusItems).where(eq(statusItems.linkId, link.id)).orderBy(asc(statusItems.position), asc(statusItems.id));
|
|
const settings = await getSettings();
|
|
const subjects = await activeSubjects(now);
|
|
const serverRows = new Map((await db.select().from(servers)).map((s) => [s.id, s]));
|
|
const integrationRows = new Map((await db.select({ id: integrations.id, type: integrations.type, enabled: integrations.enabled }).from(integrations)).map((i) => [i.id, i]));
|
|
const uptime = items.some((i) => i.kind !== "monitor") ? await getUptime(now, 30).catch(() => null) : null;
|
|
|
|
// What has an alert open that nobody has put aside: a server or an integration that answers but is not quite right. If the alerts cannot be read nothing is said to be in trouble.
|
|
const trouble = { server: new Set<number>(), integration: new Set<number>() };
|
|
const failingTests = new Set<number>();
|
|
try {
|
|
for (const a of alertsLoader ? await alertsLoader() : (await getAlerts(false)).alerts) {
|
|
if (a.silenced || a.severity === "info" || !a.target) continue;
|
|
if (a.target.type === "server") trouble.server.add(a.target.id);
|
|
else if (a.target.type === "integration") {
|
|
trouble.integration.add(a.target.id);
|
|
// Speed tests that keep failing mean the internet is down, not slow.
|
|
if (a.id.includes(`speedtest-failing:${a.target.id}`)) failingTests.add(a.target.id);
|
|
}
|
|
}
|
|
} catch {
|
|
// no alerts, no "trouble"
|
|
}
|
|
|
|
const out: PublicStatusItem[] = [];
|
|
for (const item of items) {
|
|
let state: ItemState = "unknown";
|
|
let uptime30d: number | null = null;
|
|
|
|
if (item.kind === "server") {
|
|
const s = serverRows.get(item.targetId);
|
|
if (s?.lastSeenAt) {
|
|
const limit = effectiveThresholds({ ...settings.healthChecks }, parseThresholds(s.thresholds)).serverOfflineMinutes * 60_000;
|
|
const quiet = now.getTime() - Date.parse(s.lastSeenAt) > limit;
|
|
state = subjects.has(`server:${s.id}`) ? "maintenance" : quiet ? "down" : trouble.server.has(s.id) ? "trouble" : "up";
|
|
}
|
|
uptime30d = uptime?.servers[item.targetId]?.percent ?? null;
|
|
} else if (item.kind === "integration") {
|
|
const i = integrationRows.get(item.targetId);
|
|
if (i?.enabled) {
|
|
const status = getSourceStatus(i.type).status;
|
|
state = subjects.has(`integration:${i.id}`) ? "maintenance" : status === "failing" || failingTests.has(i.id) ? "down" : status === "ok" ? (trouble.integration.has(i.id) ? "trouble" : "up") : "unknown";
|
|
}
|
|
uptime30d = uptime?.integrations[item.targetId]?.percent ?? null;
|
|
} else {
|
|
const monitor = item.monitorId ? (await monitorsOf(item.targetId, now.getTime()))?.get(item.monitorId) : undefined;
|
|
if (monitor) {
|
|
state = monitor.status === "up" ? "up" : monitor.status === "down" ? "down" : monitor.status === "maintenance" ? "maintenance" : "unknown";
|
|
uptime30d = monitor.uptime30d;
|
|
}
|
|
}
|
|
out.push({ id: item.id, label: item.label, state, uptime30d: uptime30d === null ? null : Math.round(uptime30d * 10) / 10 });
|
|
}
|
|
|
|
const counts: Record<ItemState, number> = { up: 0, trouble: 0, down: 0, maintenance: 0, unknown: 0 };
|
|
for (const o of out) counts[o.state]++;
|
|
return {
|
|
title: link.title?.trim() || link.label,
|
|
message: link.message?.trim() || null,
|
|
level: pageLevel(counts),
|
|
counts,
|
|
items: out,
|
|
generatedAt: now.toISOString(),
|
|
display: { dateFormat: settings.display.dateFormat, timeFormat: settings.display.timeFormat },
|
|
};
|
|
}
|
|
|
|
/** What an administrator can choose from, for the editor: servers and integrations (monitors are read from each Uptime Kuma on request). */
|
|
export async function statusChoices(): Promise<{ servers: { id: number; name: string }[]; integrations: { id: number; name: string; type: string }[] }> {
|
|
const s = await db.select({ id: servers.id, name: servers.name }).from(servers).orderBy(asc(servers.name));
|
|
const i = await db.select({ id: integrations.id, name: integrations.name, type: integrations.type }).from(integrations).where(and(eq(integrations.enabled, true))).orderBy(asc(integrations.name));
|
|
return { servers: s, integrations: i };
|
|
}
|