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.
2.3 kB ยท 76 lines
TypeScript
at incident-api
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677import { and, eq, inArray, isNull } from "@openstatus/db";import { monitor } from "@openstatus/db/src/schema";import type { OSTinybird } from "@openstatus/tinybird";
import type { DB } from "../context";import type { StatusData } from "../status-timeline";
type SupportedJobType = "http" | "tcp" | "dns" | "icmp" | "grpc";
/** * Raw daily status buckets (one row per monitor per day) from the 45d Tinybird * pipes, grouped by jobType. Ids that are missing / soft-deleted / of an * unsupported jobType are skipped โ callers want empty data for those, not a * throw. The pipe rows already match `StatusData` (day ISO, monitorId string). */export async function fetchMonitorDailyStats(args: { db: DB; tb: OSTinybird; monitorIds: number[]; workspaceId: number;}): Promise<StatusData[]> { const ids = Array.from(new Set(args.monitorIds)); if (ids.length === 0) return [];
const rows = await args.db .select({ id: monitor.id, jobType: monitor.jobType }) .from(monitor) .where( and( inArray(monitor.id, ids), eq(monitor.workspaceId, args.workspaceId), isNull(monitor.deletedAt), ), ) .all();
const idsByJobType: Record<SupportedJobType, string[]> = { http: [], tcp: [], dns: [], icmp: [], grpc: [], }; for (const row of rows) { if ( row.jobType === "http" || row.jobType === "tcp" || row.jobType === "dns" || row.jobType === "icmp" || row.jobType === "grpc" ) { idsByJobType[row.jobType].push(String(row.id)); } }
const results = await Promise.all( (["http", "tcp", "dns", "icmp", "grpc"] as const) .filter((jobType) => idsByJobType[jobType].length > 0) .map((jobType) => { const monitorIds = idsByJobType[jobType]; const pipe = jobType === "http" ? args.tb.httpStatus45d : jobType === "tcp" ? args.tb.tcpStatus45d : jobType === "dns" ? args.tb.dnsStatus45d : jobType === "icmp" ? args.tb.icmpStatus45d : args.tb.grpcStatus45d; return pipe({ monitorIds }); }), );
return results.flatMap((result) => result.data);}