A "What to do" note (text and/or link) can be written once for a whole kind of alert and shows beside every alert of that kind; alerts now carry their kind and source type. New optional setting: warn when a Docker host has N or more high vulnerabilities (off by default). The wall display gains tiles (Hetzner and game servers running, critical vulnerabilities, stuck downloads) and a summary line for each group of integrations, also in share links. Migration 0074; docs and tests included. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
95 lines
4.8 KiB
TypeScript
95 lines
4.8 KiB
TypeScript
/**
|
|
* What a share link may show: the wall display's picture of how things are, built here on the server so that nothing beyond it ever
|
|
* reaches someone who isn't signed in. "summary" is only numbers and an overall state; "names" adds the names of what is wrong, of what
|
|
* is in maintenance, and of the integrations — but never addresses, tokens or other settings.
|
|
*/
|
|
import { db } from "../db/client.js";
|
|
import { integrations, servers } from "../db/schema.js";
|
|
import { env } from "../env.js";
|
|
import { getAlerts } from "./alerts.js";
|
|
import { getSourceStatus } from "./integrationHealthMonitor.js";
|
|
import { describeTarget, listActiveWindows, activeSubjects } from "./maintenance.js";
|
|
import { buildServerSummary } from "./serverSummary.js";
|
|
import { buildWallExtras, type WallGroup, type WallTile } from "./wallExtras.js";
|
|
import { getSettings } from "./settingsStore.js";
|
|
import type { ShareLinkDetail } from "../db/schema.js";
|
|
|
|
export interface PublicWall {
|
|
detail: ShareLinkDetail;
|
|
/** Overall state, decided here exactly as the signed-in wall decides it. */
|
|
level: "ok" | "warning" | "critical" | "unsure";
|
|
environment: string | null;
|
|
servers: { online: number; offline: number };
|
|
alerts: { critical: number; warning: number };
|
|
/** Windows running now. */
|
|
maintenance: number;
|
|
integrationsFailing: number;
|
|
/** What else is worth a glance (Hetzner servers running, critical vulnerabilities …) and a summary for each group of integrations — numbers and group names only, so every detail level has them. */
|
|
tiles: WallTile[];
|
|
groups: WallGroup[];
|
|
/** Only with detail "names". */
|
|
problems?: { id: string; severity: "critical" | "warning"; message: string }[];
|
|
maintenanceWindows?: { id: number; name: string; endsAt: string }[];
|
|
integrations?: { id: number; name: string; type: string; status: "ok" | "failing" | "unknown" }[];
|
|
generatedAt: string;
|
|
/** How dates and times are written here — so the board reads the way the rest of the app does. */
|
|
display: { dateFormat: string; timeFormat: string };
|
|
}
|
|
|
|
const MAX_PROBLEMS = 7;
|
|
const MAX_WINDOWS = 8;
|
|
|
|
export async function buildPublicWall(detail: ShareLinkDetail, now: Date = new Date()): Promise<PublicWall> {
|
|
const [report, settings, windows, serverRows, integrationRows, subjects] = await Promise.all([
|
|
getAlerts(false),
|
|
getSettings(),
|
|
listActiveWindows(now),
|
|
db.select({ id: servers.id, name: servers.name, osType: servers.osType, lastSeenAt: servers.lastSeenAt, disks: servers.disks, thresholds: servers.thresholds }).from(servers),
|
|
db.select({ id: integrations.id, name: integrations.name, type: integrations.type, enabled: integrations.enabled }).from(integrations),
|
|
activeSubjects(now),
|
|
]);
|
|
|
|
const summary = buildServerSummary(serverRows, settings.healthChecks, now.getTime(), subjects);
|
|
const enabled = integrationRows.filter((i) => i.enabled);
|
|
const statusOf = (type: string) => getSourceStatus(type).status;
|
|
const failing = enabled.filter((i) => statusOf(i.type) === "failing");
|
|
|
|
const critical = report.counts.critical;
|
|
const warning = report.counts.warning;
|
|
const level: PublicWall["level"] = critical > 0 ? "critical" : warning > 0 ? "warning" : report.couldntCheck.length > 0 ? "unsure" : "ok";
|
|
|
|
const extras = await buildWallExtras(enabled.map((i) => ({ id: i.id, type: i.type })), new Set(failing.map((i) => i.type)), report.alerts, now.getTime());
|
|
|
|
const out: PublicWall = {
|
|
detail,
|
|
level,
|
|
environment: env.environment?.name || null,
|
|
servers: { online: summary.online, offline: summary.offline.length },
|
|
alerts: { critical, warning },
|
|
maintenance: windows.length,
|
|
integrationsFailing: failing.length,
|
|
tiles: extras.tiles,
|
|
groups: extras.groups,
|
|
generatedAt: now.toISOString(),
|
|
display: { dateFormat: settings.display.dateFormat, timeFormat: settings.display.timeFormat },
|
|
};
|
|
|
|
if (detail === "names") {
|
|
out.problems = report.alerts
|
|
.filter((a): a is typeof a & { severity: "critical" | "warning" } => !a.silenced && a.severity !== "info")
|
|
.sort((a, b) => (a.severity === b.severity ? 0 : a.severity === "critical" ? -1 : 1))
|
|
.slice(0, MAX_PROBLEMS)
|
|
.map((a) => ({ id: a.id, severity: a.severity, message: a.message }));
|
|
const named: { id: number; name: string; endsAt: string }[] = [];
|
|
for (const w of [...windows].sort((a, b) => a.endsAt.localeCompare(b.endsAt))) {
|
|
const target = await describeTarget(w.targetType, w.targetId);
|
|
if (target) named.push({ id: w.id, name: target.name, endsAt: w.endsAt });
|
|
}
|
|
out.maintenanceWindows = named.slice(0, MAX_WINDOWS);
|
|
out.integrations = [...enabled]
|
|
.sort((a, b) => Number(statusOf(b.type) === "failing") - Number(statusOf(a.type) === "failing") || a.name.localeCompare(b.name))
|
|
.map((i) => ({ id: i.id, name: i.name, type: i.type, status: statusOf(i.type) }));
|
|
}
|
|
return out;
|
|
}
|