import type { ReactNode } from "react"; import { createCorrelationId, fail, type HousekeepingResult, } from "../../../foundation/contracts"; import { HousekeepingPageShell } from "../../../foundation/page/housekeeping-page-shell"; import { HousekeepingPageState } from "../../../foundation/page/housekeeping-page-state"; import type { HousekeepingPageInput } from "../../../route-handlers"; import { type SystemObservabilityQueryData, type SystemObservabilityQueryInput, systemObservabilityQuery, } from "../queries/observability"; interface SystemObservabilityPageProps { readonly result?: HousekeepingResult; } function pageState( kind: "loading" | "empty" | "error", title: string, description: string, ) { return (
); } function forbiddenState() { return (

Access denied

Your account cannot view this observability source.

); } function observabilityContent(data: SystemObservabilityQueryData) { if (data.kind === "devops") { return (

Service health

{data.health ? (
Database
{data.health.dbOk ? "Available" : "Unavailable"}
Redis
{data.health.redisOk === null ? "Not configured" : data.health.redisOk ? "Available" : "Unavailable"}
Emulator
{data.health.emulatorOk ? "Available" : "Unavailable"}
Online users
{data.health.onlineUsers}
) : (

Health data is unavailable.

)}
{observationRows("Recent emulator errors", data.errors)}
); } if (data.kind === "devops-errors") { return observationRows("Emulator errors", data.errors); } return (
{data.metrics.map((metric) => (

{metric.label}

{metric.value}

))}
{observationRows( data.kind === "logs" ? "Recent entries" : "Details", data.rows, )}
); } function observationRows( title: string, rows: readonly { id: string; primary: string; secondary?: string }[], ) { return (

{title}

); } function isEmpty(data: SystemObservabilityQueryData): boolean { if (data.kind === "devops") return data.health === null && data.errors.length === 0; if (data.kind === "devops-errors") return data.errors.length === 0; return data.metrics.length === 0 && data.rows.length === 0; } export function SystemObservabilityPage({ result, }: SystemObservabilityPageProps) { let body: ReactNode; if (!result) { body = pageState( "loading", "Loading observability data", "Reading metrics and operational logs.", ); } else if (!result.ok) { body = result.error.code === "FORBIDDEN" ? forbiddenState() : pageState( "error", "Observability data unavailable", `Request ${result.correlationId} could not be completed.`, ); } else if ( isEmpty(result.data) && result.data.partialDependencies.length === 0 ) { body = pageState( "empty", "No observations", "The selected source returned no records.", ); } else { body = (
0 ? "partial" : "ready" } className="space-y-4" > {result.data.partialDependencies.length > 0 ? ( ) : null} {observabilityContent(result.data)}
); } return ( {body} ); } export async function renderSystemObservabilityPage( input: HousekeepingPageInput, ) { const routeId = input.match .routeId as SystemObservabilityQueryInput["routeId"]; const supported = routeId.startsWith("system.observability."); const result = supported ? await systemObservabilityQuery.run(input.context, { routeId }) : fail("NOT_FOUND", "errors.housekeeping.notFound", createCorrelationId()); return ; }