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.
TypeScript
at incident-api
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105import { getLogger } from "@logtape/logtape";import { db, eq } from "@openstatus/db";import { page } from "@openstatus/db/src/schema";import { Hono } from "hono";import { endTime, setMetric, startTime } from "hono/timing";
const logger = getLogger("api-server");import { Status, Tracker } from "@openstatus/tracker";
import { cacheKeys } from "../../libs/cache-keys";import { redis } from "../../libs/clients";
// TODO: include ratelimiting
export const status = new Hono();
status.get("/:slug", async (c) => { try { const { slug } = c.req.param();
// Only public pages are ever written under this prefix, so a hit needs no // access check; a page made private can stay cached for up to the 60s TTL. const cache = await redis.get(cacheKeys.pageStatus(slug));
if (cache) { setMetric(c, "OpenStatus-Cache", "HIT"); return c.json({ status: cache }); }
startTime(c, "database");
// Single query with all relations const currentPage = await db.query.page.findFirst({ where: eq(page.slug, slug), with: { pageComponents: { with: { monitor: { with: { monitorIncidents: true, }, }, }, }, statusReports: true, maintenances: true, }, });
endTime(c, "database");
if (!currentPage) { return c.json({ status: Status.Unknown }); }
if (currentPage.accessType !== "public") { return c.json({ status: Status.Unknown }); }
// Extract active monitor components const monitorComponents = currentPage.pageComponents.filter( (c) => c.type === "monitor" && c.monitor && c.monitor.active && !c.monitor.deletedAt, );
// Extract all ongoing incidents from active monitors const ongoingIncidents = monitorComponents.flatMap( (c) => c.monitor?.monitorIncidents?.filter((inc) => !inc.resolvedAt) ?? [], );
// Filter for unresolved status reports const unresolvedStatusReports = currentPage.statusReports.filter( (report) => report.status !== "resolved", );
// Filter for ongoing maintenances const now = new Date(); const ongoingMaintenances = currentPage.maintenances.filter( (m) => m.from <= now && m.to >= now, );
// Use the tracker to determine status const tracker = new Tracker({ incidents: ongoingIncidents, statusReports: unresolvedStatusReports, maintenances: ongoingMaintenances, });
const status = tracker.currentStatus; await redis.set(cacheKeys.pageStatus(slug), status, { ex: 60 }); // 1m cache
return c.json({ status }); } catch (e) { logger.error("Error in public status page", { error: e instanceof Error ? e.message : String(e), stack: e instanceof Error ? e.stack : undefined, }); return c.json({ status: Status.Unknown }); }});