Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/services/admin/system-health.server.ts
T

160 lines
5.8 KiB
TypeScript

import { getDbClient } from "@hivegrid/db/client.server";
import type { SystemHealthDeploy, SystemHealthServiceInput } from "@hivegrid/view-models";
import { adminPath } from "~/utils/app/CanonicalUrlUtils";
import { buildDeployMetadata } from "./deploy-admin.server";
export type SystemHealthSnapshot = {
readonly services: readonly SystemHealthServiceInput[];
readonly deploy: SystemHealthDeploy;
};
export type SystemHealthDeps = {
readonly env?: NodeJS.ProcessEnv;
readonly pingDatabase?: () => Promise<boolean>;
};
export async function getSystemHealthSnapshot(deps: SystemHealthDeps = {}): Promise<SystemHealthSnapshot> {
const env = deps.env ?? process.env;
const pingDatabase = deps.pingDatabase ?? defaultDatabasePing;
const databaseReachable = await pingDatabase().catch(() => false);
const deploy = buildDeployInfo(env);
return {
deploy,
services: [
{
id: "web",
labelKey: "admin.system.services.web.label",
category: "runtime",
configured: true,
reachable: true,
detailKey: "admin.system.services.web.detail",
detailValue: deploy.release,
},
{
id: "api",
labelKey: "admin.system.services.api.label",
category: "runtime",
configured: hasAny(env, ["API_HOST", "API_BASE_URL", "API_RESOURCE_URL"]),
reachable: null,
detailKey: "admin.system.services.api.detail",
detailValue: firstPresent(env, ["API_HOST", "API_BASE_URL", "API_RESOURCE_URL"]) ?? "api.hivegrid.de",
},
{
id: "mcp",
labelKey: "admin.system.services.mcp.label",
category: "runtime",
configured: hasAny(env, ["MCP_RESOURCE_URL", "MCP_HOST"]),
reachable: null,
detailKey: "admin.system.services.mcp.detail",
detailValue: firstPresent(env, ["MCP_RESOURCE_URL", "MCP_HOST"]) ?? "mcp.hivegrid.de",
},
{
id: "postgres",
labelKey: "admin.system.services.postgres.label",
category: "database",
configured: hasAny(env, ["DATABASE_URL", "DIRECT_URL"]),
reachable: databaseReachable,
detailKey: databaseReachable ? "admin.system.services.postgres.detailOk" : "admin.system.services.postgres.detailFailed",
},
{
id: "redis",
labelKey: "admin.system.services.redis.label",
category: "queue",
configured: hasAny(env, ["BULLMQ_REDIS_URL", "REDIS_URL"]),
reachable: null,
detailKey: "admin.system.services.redis.detail",
href: adminPath("system/jobs", { mode: "host-aware" }),
},
{
id: "meili",
labelKey: "admin.system.services.meili.label",
category: "integration",
configured: hasAll(env, ["MEILI_URL", "MEILI_API_KEY"]),
reachable: null,
detailKey: "admin.system.services.meili.detail",
},
{
id: "storage",
labelKey: "admin.system.services.storage.label",
category: "storage",
configured: storageConfigured(env),
reachable: null,
detailKey: "admin.system.services.storage.detail",
detailValue: firstPresent(env, ["STORAGE_BUCKET", "STORAGE_LOCAL_ROOT"]) ?? "",
},
{
id: "lago",
labelKey: "admin.system.services.lago.label",
category: "integration",
configured: hasAll(env, ["LAGO_API_URL", "LAGO_API_KEY"]),
reachable: null,
detailKey: "admin.system.services.lago.detail",
},
{
id: "mollie",
labelKey: "admin.system.services.mollie.label",
category: "integration",
configured: hasAll(env, ["MOLLIE_API_KEY", "MOLLIE_PROFILE_ID"]),
reachable: null,
detailKey: "admin.system.services.mollie.detail",
},
{
id: "mailjet",
labelKey: "admin.system.services.mailjet.label",
category: "integration",
configured: hasAll(env, ["MAILJET_API_KEY", "MAILJET_SECRET_KEY", "MAILJET_FROM_EMAIL"]),
reachable: null,
detailKey: "admin.system.services.mailjet.detail",
},
{
id: "sentry",
labelKey: "admin.system.services.sentry.label",
category: "observability",
configured: hasAny(env, ["SENTRY_DSN", "SENTRY_DSN_WEB", "VITE_SENTRY_DSN_WEB"]),
reachable: null,
detailKey: "admin.system.services.sentry.detail",
},
],
};
}
async function defaultDatabasePing(): Promise<boolean> {
const db = getDbClient();
await db.$queryRaw`SELECT 1`;
return true;
}
function buildDeployInfo(env: NodeJS.ProcessEnv): SystemHealthDeploy {
// Reuses the `/deploys` resolution instead of a second, weaker one. The local
// variant used `??`, which only skips null/undefined: a deploy target that
// sets SENTRY_RELEASE to an empty string won the chain and the header read
// "Release: " with nothing behind it, while /deploys showed the value
// (admin audit 2026-07-26, P0-11).
const metadata = buildDeployMetadata(env);
return {
environment: metadata.environment,
release: metadata.release,
nodeEnv: metadata.nodeEnv,
};
}
function hasAny(env: NodeJS.ProcessEnv, names: readonly string[]): boolean {
return names.some((name) => Boolean(env[name]?.trim()));
}
function hasAll(env: NodeJS.ProcessEnv, names: readonly string[]): boolean {
return names.every((name) => Boolean(env[name]?.trim()));
}
function firstPresent(env: NodeJS.ProcessEnv, names: readonly string[]): string | undefined {
return names.map((name) => env[name]?.trim()).find((value): value is string => Boolean(value));
}
function storageConfigured(env: NodeJS.ProcessEnv): boolean {
if (env.STORAGE_PROVIDER?.trim() === "local") {
return hasAny(env, ["STORAGE_LOCAL_ROOT"]);
}
return hasAll(env, ["STORAGE_ENDPOINT", "STORAGE_REGION", "STORAGE_BUCKET", "STORAGE_ACCESS_KEY_ID", "STORAGE_SECRET_ACCESS_KEY"]);
}