111 lines
3.2 KiB
TypeScript
111 lines
3.2 KiB
TypeScript
import type { VMState } from "../index.js";
|
|
|
|
export type SystemHealthStatus = "ok" | "warning" | "critical" | "unknown";
|
|
export type SystemHealthTone = "success" | "warning" | "danger" | "neutral";
|
|
export type SystemHealthCategory = "runtime" | "database" | "queue" | "integration" | "observability" | "storage";
|
|
|
|
export type SystemHealthServiceInput = {
|
|
readonly id: string;
|
|
readonly labelKey: string;
|
|
readonly category: SystemHealthCategory;
|
|
readonly configured: boolean | null;
|
|
readonly reachable?: boolean | null;
|
|
readonly detailKey?: string;
|
|
readonly detailValue?: string;
|
|
readonly href?: string;
|
|
};
|
|
|
|
export type SystemHealthService = SystemHealthServiceInput & {
|
|
readonly status: SystemHealthStatus;
|
|
readonly statusLabelKey: string;
|
|
readonly tone: SystemHealthTone;
|
|
readonly categoryLabelKey: string;
|
|
};
|
|
|
|
export type SystemHealthDeploy = {
|
|
readonly environment: string;
|
|
readonly release: string;
|
|
readonly nodeEnv: string;
|
|
};
|
|
|
|
export type SystemHealthData = {
|
|
readonly services: readonly SystemHealthService[];
|
|
readonly summary: {
|
|
readonly total: number;
|
|
readonly ok: number;
|
|
readonly warning: number;
|
|
readonly critical: number;
|
|
readonly unknown: number;
|
|
};
|
|
readonly overallStatus: SystemHealthStatus;
|
|
readonly overallLabelKey: string;
|
|
readonly deploy: SystemHealthDeploy;
|
|
};
|
|
|
|
export type SystemHealthVM = VMState<SystemHealthData>;
|
|
|
|
export function deriveSystemHealthStatus(input: Pick<SystemHealthServiceInput, "configured" | "reachable">): SystemHealthStatus {
|
|
if (input.reachable === false) return "critical";
|
|
if (input.configured === false) return "warning";
|
|
if (input.reachable === true || input.configured === true) return "ok";
|
|
return "unknown";
|
|
}
|
|
|
|
export function systemHealthTone(status: SystemHealthStatus): SystemHealthTone {
|
|
if (status === "ok") return "success";
|
|
if (status === "warning") return "warning";
|
|
if (status === "critical") return "danger";
|
|
return "neutral";
|
|
}
|
|
|
|
const STATUS_SEVERITY: Record<SystemHealthStatus, number> = {
|
|
ok: 0,
|
|
unknown: 1,
|
|
warning: 2,
|
|
critical: 3,
|
|
};
|
|
|
|
export function buildSystemHealthVM(input: {
|
|
readonly services: readonly SystemHealthServiceInput[];
|
|
readonly deploy: SystemHealthDeploy;
|
|
}): SystemHealthVM {
|
|
if (input.services.length === 0) {
|
|
return { status: "empty" };
|
|
}
|
|
|
|
const services = input.services.map<SystemHealthService>((service) => {
|
|
const status = deriveSystemHealthStatus(service);
|
|
return {
|
|
...service,
|
|
status,
|
|
tone: systemHealthTone(status),
|
|
statusLabelKey: `admin.system.status.${status}`,
|
|
categoryLabelKey: `admin.system.categories.${service.category}`,
|
|
};
|
|
});
|
|
|
|
const summary = services.reduce(
|
|
(counts, service) => ({
|
|
...counts,
|
|
[service.status]: counts[service.status] + 1,
|
|
}),
|
|
{ total: services.length, ok: 0, warning: 0, critical: 0, unknown: 0 }
|
|
);
|
|
|
|
const overallStatus = services.reduce<SystemHealthStatus>(
|
|
(worst, service) => (STATUS_SEVERITY[service.status] > STATUS_SEVERITY[worst] ? service.status : worst),
|
|
"ok"
|
|
);
|
|
|
|
return {
|
|
status: "ready",
|
|
data: {
|
|
services,
|
|
summary,
|
|
overallStatus,
|
|
overallLabelKey: `admin.system.status.${overallStatus}`,
|
|
deploy: input.deploy,
|
|
},
|
|
};
|
|
}
|