~james/core

core/web/src/lib/status.ts -rw-r--r-- 949 bytes
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
export type Status = "up" | "degraded" | "down" | "maintenance" | "unknown";

export interface Beat {
	status: number;
}

// kuma's beat statuses
const DOWN = 0;
const UP = 1;
const PENDING = 2;
const MAINTENANCE = 3;

export const summarize = (heartbeats: Record<string, Beat[]>): Status => {
	const latest = Object.values(heartbeats)
		.map((beats) => beats.at(-1)?.status)
		.filter((s) => s !== undefined);
	if (latest.length === 0) return "unknown";
	const down = latest.filter((s) => s === DOWN).length;
	if (down === latest.length) return "down";
	if (down > 0 || latest.includes(PENDING)) return "degraded";
	if (latest.includes(MAINTENANCE)) return "maintenance";
	return latest.every((s) => s === UP) ? "up" : "unknown";
};

export const STATUS_LABEL: Record<Status, string> = {
	up: "All systems operational",
	degraded: "Some systems degraded",
	down: "Major outage",
	maintenance: "Under maintenance",
	unknown: "Status unavailable"
};