import type { VMState } from "../index.js"; export type DeploySignalStatus = "ok" | "warning" | "critical" | "unknown"; export type DeploySignalTone = "success" | "warning" | "danger" | "neutral"; export type MigrationState = "applied" | "pending" | "failed" | "rolledBack" | "unknown"; export type DeployMetadata = { readonly environment: string; readonly nodeEnv: string; readonly release: string; readonly revision: string; readonly branch: string; readonly image: string; readonly buildTime: string; }; export type DeployMigrationInput = { readonly name: string; readonly state: MigrationState; readonly appliedAt?: string | null; }; export type DeploySnapshotInput = { readonly metadata: DeployMetadata; readonly migrations: readonly DeployMigrationInput[]; readonly migrationDbReachable: boolean; }; export type DeploySignal = { readonly id: string; readonly labelKey: string; readonly detailKey: string; readonly value: string; readonly status: DeploySignalStatus; readonly tone: DeploySignalTone; }; export type DeployAdminData = { readonly metadata: DeployMetadata; readonly signals: readonly DeploySignal[]; readonly migrations: readonly DeployMigrationInput[]; readonly migrationSummary: { readonly total: number; readonly applied: number; readonly pending: number; readonly failed: number; readonly rolledBack: number; readonly unknown: number; }; readonly overallStatus: DeploySignalStatus; readonly overallLabelKey: string; }; export type DeployAdminVM = VMState; export function deploySignalTone(status: DeploySignalStatus): DeploySignalTone { if (status === "ok") return "success"; if (status === "warning") return "warning"; if (status === "critical") return "danger"; return "neutral"; } const STATUS_SEVERITY: Record = { ok: 0, unknown: 1, warning: 2, critical: 3, }; export function buildDeployAdminVM(input: DeploySnapshotInput): DeployAdminVM { const summary = input.migrations.reduce( (counts, migration) => ({ ...counts, [migration.state]: counts[migration.state] + 1, }), { total: input.migrations.length, applied: 0, pending: 0, failed: 0, rolledBack: 0, unknown: 0 } ); const signals: DeploySignal[] = [ buildSignal({ id: "release", labelKey: "admin.deploys.signals.release.label", detailKey: input.metadata.release === "unknown" || input.metadata.release === "local" ? "admin.deploys.signals.release.detailMissing" : "admin.deploys.signals.release.detailOk", value: input.metadata.release, status: input.metadata.release === "unknown" ? "warning" : "ok", }), buildSignal({ id: "image", labelKey: "admin.deploys.signals.image.label", detailKey: input.metadata.image === "unknown" ? "admin.deploys.signals.image.detailMissing" : "admin.deploys.signals.image.detailOk", value: input.metadata.image, status: input.metadata.image === "unknown" ? "warning" : "ok", }), buildSignal({ id: "migrations", labelKey: "admin.deploys.signals.migrations.label", detailKey: !input.migrationDbReachable ? "admin.deploys.signals.migrations.detailUnknown" : summary.failed > 0 ? "admin.deploys.signals.migrations.detailFailed" : summary.pending > 0 ? "admin.deploys.signals.migrations.detailPending" : "admin.deploys.signals.migrations.detailOk", value: `${summary.applied}/${summary.total}`, status: !input.migrationDbReachable ? "unknown" : summary.failed > 0 ? "critical" : summary.pending > 0 ? "warning" : "ok", }), ]; const overallStatus = signals.reduce( (worst, signal) => (STATUS_SEVERITY[signal.status] > STATUS_SEVERITY[worst] ? signal.status : worst), "ok" ); return { status: "ready", data: { metadata: input.metadata, signals, migrations: input.migrations, migrationSummary: summary, overallStatus, overallLabelKey: `admin.deploys.status.${overallStatus}`, }, }; } function buildSignal(input: Omit): DeploySignal { return { ...input, tone: deploySignalTone(input.status), }; }