Add a since-you-last-looked summary, 30-day uptime ribbons, a maintenance timeline and hover timestamps

- Dashboard card on what changed since the user pressed Got it (users.last_looked_at,
  migration 0021), folding each problem and its recovery into one line.
- Uptime ribbons on servers and integrations, built from the audit log; integrations'
  failing/working-again moments are now recorded.
- Timeline of running and scheduled maintenance windows on the Maintenance page.
- TimeAgo component: relative times with the exact time in the chosen format on hover.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-16 21:55:04 +02:00
1 parent 3945c60681
commit 906220040e
32 files changed
+3380 -15

No files matched your search

+164
View File
@@ -0,0 +1,164 @@
/**
* How each server and integration has done over the last weeks, as one figure per day: how long it was up, how long down, and for
* how much of the day anything is known. It is worked out from the moments the health checks wrote down in the audit log (a
* server stopped reporting / reported again; an integration's calls started failing / worked again), so it needs no table of its
* own — and it can only be as old as those records.
*/
import { inArray } from "drizzle-orm";
import { db } from "../db/client.js";
import { auditLog, integrations, servers } from "../db/schema.js";
import { auditTimeToIso } from "./serverActivity.js";
import { getInternalFlag, setInternalFlag } from "./settingsStore.js";
const DAY_MS = 24 * 3600_000;
const HISTORY_FLAG = "integration_history_since";
export const DEFAULT_DAYS = 30;
export interface Transition {
/** Epoch ms. */
at: number;
down: boolean;
}
/** Minutes of one 24-hour stretch that are known, and of those, minutes spent down. */
export interface UptimeBucket {
k: number;
d: number;
}
export interface UptimeSeries {
/** Oldest first; the last bucket ends now. */
buckets: UptimeBucket[];
/** Share of the known time spent up over the whole period. */
percent: number;
}
/**
* Splits [knownFrom, now] into up and down stretches from the recorded transitions and lays them over `days` consecutive 24-hour
* buckets ending at `now`. Before the first transition the thing is taken to have been in the opposite state (a "went offline" means it
* was up until then); with no transitions at all it has been up the whole time.
*/
export function computeBuckets(transitions: Transition[], knownFrom: number, now: number, days: number): UptimeBucket[] {
const start = now - days * DAY_MS;
const sorted = [...transitions].sort((a, b) => a.at - b.at);
// State at knownFrom: after any transitions that happened before it, otherwise the opposite of the first one that follows.
let state = false;
let i = 0;
while (i < sorted.length && sorted[i].at <= knownFrom) state = sorted[i++].down;
if (i === 0 && sorted.length > 0) state = !sorted[0].down;
const segments: { from: number; to: number; down: boolean }[] = [];
let cursor = Math.min(knownFrom, now);
for (; i < sorted.length; i++) {
const t = Math.min(sorted[i].at, now);
if (t > cursor) segments.push({ from: cursor, to: t, down: state });
cursor = Math.max(cursor, t);
state = sorted[i].down;
}
if (now > cursor) segments.push({ from: cursor, to: now, down: state });
const known = new Array<number>(days).fill(0);
const down = new Array<number>(days).fill(0);
for (const seg of segments) {
if (seg.to <= start) continue;
const first = Math.max(0, Math.floor((Math.max(seg.from, start) - start) / DAY_MS));
for (let b = first; b < days; b++) {
const bStart = start + b * DAY_MS;
if (bStart >= seg.to) break;
const overlap = Math.min(seg.to, bStart + DAY_MS) - Math.max(seg.from, bStart);
if (overlap <= 0) continue;
known[b] += overlap;
if (seg.down) down[b] += overlap;
}
}
return known.map((k, b) => ({ k: Math.round(k / 60_000), d: Math.round(down[b] / 60_000) }));
}
/** Share of the known time spent up, to one decimal; null when nothing is known. */
export function uptimePercent(buckets: UptimeBucket[]): number | null {
const known = buckets.reduce((n, b) => n + b.k, 0);
if (known === 0) return null;
const down = buckets.reduce((n, b) => n + b.d, 0);
return Math.round((1 - down / known) * 1000) / 10;
}
function series(transitions: Transition[], knownFrom: number, now: number, days: number): UptimeSeries | null {
const buckets = computeBuckets(transitions, knownFrom, now, days);
const percent = uptimePercent(buckets);
return percent === null ? null : { buckets, percent };
}
/**
* Integrations' failing / working-again moments only exist from the version that began writing them, so nothing earlier can be claimed.
* The first time anything asks, that moment is written down and kept.
*/
let historyStart: number | null = null;
export async function ensureHistoryStart(now: number = Date.now()): Promise<number> {
if (historyStart !== null) return historyStart;
const stored = await getInternalFlag(HISTORY_FLAG);
const parsed = stored ? Date.parse(stored) : NaN;
if (Number.isFinite(parsed)) {
historyStart = parsed;
return parsed;
}
await setInternalFlag(HISTORY_FLAG, new Date(now).toISOString());
historyStart = now;
return now;
}
/** For tests: forget the remembered start. */
export function resetHistoryStartForTests(): void {
historyStart = null;
}
export interface UptimeReport {
days: number;
/** ISO — the end of the last bucket. */
endsAt: string;
servers: Record<number, UptimeSeries>;
integrations: Record<number, UptimeSeries>;
}
export async function getUptime(now: Date = new Date(), days: number = DEFAULT_DAYS): Promise<UptimeReport> {
const nowMs = now.getTime();
const rows = await db.select().from(auditLog).where(inArray(auditLog.action, ["went_offline", "back_online", "went_failing", "recovered"]));
const serverEvents = new Map<number, Transition[]>();
const sourceEvents = new Map<string, Transition[]>();
for (const r of rows) {
const at = Date.parse(auditTimeToIso(r.createdAt));
if (!Number.isFinite(at) || at > nowMs) continue;
if (r.category === "server" && r.targetType === "server" && (r.action === "went_offline" || r.action === "back_online")) {
const id = Number(r.targetId);
const list = serverEvents.get(id) ?? [];
list.push({ at, down: r.action === "went_offline" });
serverEvents.set(id, list);
} else if (r.category === "integration" && r.targetType === "integration_type" && r.targetId && (r.action === "went_failing" || r.action === "recovered")) {
const list = sourceEvents.get(r.targetId) ?? [];
list.push({ at, down: r.action === "went_failing" });
sourceEvents.set(r.targetId, list);
}
}
const out: UptimeReport = { days, endsAt: now.toISOString(), servers: {}, integrations: {} };
for (const s of await db.select({ id: servers.id, createdAt: servers.createdAt, lastSeenAt: servers.lastSeenAt }).from(servers)) {
const events = serverEvents.get(s.id) ?? [];
// A server that has never reported and never been seen to stop has no history to speak of.
if (!s.lastSeenAt && events.length === 0) continue;
const created = Date.parse(auditTimeToIso(s.createdAt));
const result = series(events, Number.isFinite(created) ? created : nowMs, nowMs, days);
if (result) out.servers[s.id] = result;
}
const since = await ensureHistoryStart(nowMs);
for (const i of await db.select({ id: integrations.id, type: integrations.type, createdAt: integrations.createdAt }).from(integrations)) {
const created = Date.parse(auditTimeToIso(i.createdAt));
const knownFrom = Math.max(Number.isFinite(created) ? created : 0, since);
const result = series(sourceEvents.get(i.type) ?? [], knownFrom, nowMs, days);
if (result) out.integrations[i.id] = result;
}
return out;
}