Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
3.4 kB ยท 111 lines
TypeScript
at incident-api
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112import type { RouterOutputs } from "@openstatus/api";
import { componentStatus, flattenComponents, isoOrNull, pageIndicator,} from "./status-vocab";
type Page = NonNullable<RouterOutputs["statusPage"]["get"]>;
export type StatusEndpoint = "summary" | "status" | "incidents";
const ENDPOINTS: Record<string, StatusEndpoint> = { "summary.json": "summary", "current.json": "status", "incidents.json": "incidents",};
/** * The endpoint is always the last path segment; an optional leading segment is a * path-based slug (`/{slug}/summary.json`), mirroring the markdown route. Returns * null for unknown shapes so the handler can 404. */export function matchEndpoint( path: string[],): { endpoint: StatusEndpoint; slug: string | null } | null { if (path.length === 0 || path.length > 2) return null; const endpoint = ENDPOINTS[path[path.length - 1]]; if (!endpoint) return null; return { endpoint, slug: path.length === 2 ? path[0] : null };}
function pageBlock(page: Page, baseUrl: string) { return { name: page.title, url: baseUrl, updated_at: isoOrNull(page.updatedAt), };}
/** `current.json` (Statuspage `status.json` shape) โ the cheapest "is it up?" payload. */export function toStatus(page: Page, baseUrl: string) { return { page: pageBlock(page, baseUrl), status: pageIndicator(page.status), };}
function unresolvedIncidents(page: Page) { return page.statusReports .filter((report) => report.status !== "resolved") .map((report) => ({ id: String(report.id), name: report.title, // openstatus report statuses already match Statuspage's incident enum. status: report.status, updated_at: isoOrNull( report.statusReportUpdates[0]?.date ?? report.createdAt, ), incident_updates: report.statusReportUpdates.map((update) => ({ status: update.status, body: update.message, created_at: isoOrNull(update.date), })), }));}
// `completed` is intentionally absent: scheduledMaintenances only feeds windows// with `to >= now`, so a window is always either upcoming or live.function maintenanceState( from: Date | string | number, now: number,): "scheduled" | "in_progress" { return now < new Date(from).getTime() ? "scheduled" : "in_progress";}
function scheduledMaintenances(page: Page, now: number) { return page.maintenances .filter((m) => m.to && new Date(m.to).getTime() >= now) .map((m) => ({ id: String(m.id), name: m.title, status: maintenanceState(m.from, now), scheduled_for: isoOrNull(m.from), scheduled_until: isoOrNull(m.to), }));}
/** Statuspage `summary.json` โ page status, components, active incidents/maintenance. */export function toSummary(page: Page, baseUrl: string, now = Date.now()) { const components = flattenComponents(page.trackers).map((c) => ({ name: c.name, status: componentStatus(c.status), })); return { page: pageBlock(page, baseUrl), status: pageIndicator(page.status), components, incidents: unresolvedIncidents(page), scheduled_maintenances: scheduledMaintenances(page, now), };}
/** `incidents.json` (Statuspage `incidents/unresolved.json` shape). */export function toUnresolvedIncidents(page: Page, baseUrl: string) { return { page: pageBlock(page, baseUrl), incidents: unresolvedIncidents(page), };}