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.9 kB ยท 50 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import { type SQL, sql } from "@openstatus/db";import type { MonitorStatus } from "@openstatus/db/src/schema";import { monitor, monitorStatusTable } from "@openstatus/db/src/schema";
export type QuorumParams = { toStatus: MonitorStatus; regionsJson: string; regionCount: number;};
/** * The rule itself: `affected >= total / 2`, without floating point. Every other * expression in this file โ and every caller โ goes through it, so a change to * how quorum is decided happens here and nowhere else. */export function quorumMetSql(affected: SQL, total: SQL): SQL { return sql`${affected} * 2 >= ${total}`;}
/** Regions of this monitor currently reporting `toStatus`. Correlates on `monitor.id`. */export function quorumCountSql({ toStatus, regionsJson,}: Pick<QuorumParams, "toStatus" | "regionsJson">): SQL { return sql`(SELECT count(*) FROM ${monitorStatusTable} WHERE ${monitorStatusTable.monitorId} = ${monitor.id} AND ${monitorStatusTable.status} = ${toStatus} AND ${monitorStatusTable.region} IN (SELECT value FROM json_each(${regionsJson})))`;}
/** Hot path: the region list arrives as a bound JSON array. */export function quorumGuardSql(params: QuorumParams): SQL { return sql`(${params.regionCount} > 0 AND ${quorumMetSql( quorumCountSql(params), sql`${params.regionCount}`, )})`;}
/** * `monitor.regions` is a comma-joined text column. Deriving the count and * membership in SQL is only worth it where the monitor row is not already in * hand โ currently just the drift sweep, which scans every monitor at once. */export function csvRegionCountSql(): SQL { return sql`(length(${monitor.regions}) - length(replace(${monitor.regions}, ',', '')) + 1)`;}
export function csvRegionMemberSql(region: SQL): SQL { return sql`instr(',' || ${monitor.regions} || ',', ',' || ${region} || ',') > 0`;}