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.
1.7 kB ยท 58 lines
TypeScript
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859import { db, schema } from "@openstatus/db";import { and, eq, inArray, isNull, ne } from "drizzle-orm";import { Hono } from "hono";
import { env } from "../env";
export const incidentRoute = new Hono({ strict: false });
incidentRoute.use("*", async (c, next) => { if (c.req.header("authorization") !== env().CRON_SECRET) { return c.text("Unauthorized", 401); }
return next();});
incidentRoute.get("/cleanup", async (c) => { // Find monitors that have unresolved incidents but are active const unresolvedIncidentMonitorIds = db .select({ monitorId: schema.monitorIncidentTable.monitorId }) .from(schema.monitorIncidentTable) .where(isNull(schema.monitorIncidentTable.resolvedAt));
const activeMonitorsWithUnresolvedIncidents = await db .select({ id: schema.monitor.id }) .from(schema.monitor) .where( and( inArray(schema.monitor.id, unresolvedIncidentMonitorIds), eq(schema.monitor.active, true), ne(schema.monitor.status, "error"), ), ) .all();
const monitorIds = activeMonitorsWithUnresolvedIncidents.map((m) => m.id);
if (monitorIds.length === 0) { return c.json({ status: "ok", updated: 0 }); }
// Update incidents for these monitors: set resolvedAt to now and autoResolved to true const result = await db .update(schema.monitorIncidentTable) .set({ resolvedAt: new Date(), autoResolved: true, }) .where( and( inArray(schema.monitorIncidentTable.monitorId, monitorIds), isNull(schema.monitorIncidentTable.resolvedAt), ), ) .returning({ id: schema.monitorIncidentTable.id });
return c.json({ status: "ok", updated: result.length });});