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.2 kB ยท 76 lines
TypeScript
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677import { z } from "zod";
import { db, eq, schema } from "../src";import { selectMonitorSchema } from "../src/schema";import type { monitorRegionSchema } from "../src/schema/constants";
type MonitorRegion = z.infer<typeof monitorRegionSchema>;
// Every deprecated region and its replacement. Earlier entries already ran;// they stay because re-applying them is a no-op.export const REGION_REMAPS: ReadonlyArray<[MonitorRegion, MonitorRegion]> = [ // Asia Pacific ["hkg", "sin"], ["bom", "sin"], // North America ["atl", "dfw"], ["mia", "dfw"], ["gdl", "dfw"], ["qro", "dfw"], ["bos", "ewr"], ["phx", "lax"], ["sea", "sjc"], ["yul", "yyz"], ["den", "dfw"], // Europe ["waw", "ams"], ["mad", "cdg"], ["otp", "fra"], // South America ["bog", "gru"], ["gig", "gru"], ["scl", "gru"], ["eze", "gru"],];
export function applyRegionRemaps(regions: MonitorRegion[]) { for (const [from, to] of REGION_REMAPS) { // `updateRegion` handles one occurrence; a region can be listed twice. while (regions.includes(from)) updateRegion(from, to, regions); }}
// Only run against the DB when executed directly, not when imported by tests.if (import.meta.main) { const rawMonitors = await db.select().from(schema.monitor);
const monitors = z.array(selectMonitorSchema).parse(rawMonitors); for (const monitor of monitors) { const regions = monitor.regions.slice(); applyRegionRemaps(regions); const newRegions = regions.join(","); if (newRegions === monitor.regions.join(",")) continue; await db .update(schema.monitor) .set({ regions: newRegions }) .where(eq(schema.monitor.id, monitor.id)) .execute(); }}
export function updateRegion( oldRegion: z.infer<typeof monitorRegionSchema>, newRegion: z.infer<typeof monitorRegionSchema>, regions: z.infer<typeof monitorRegionSchema>[],) { const regionIndex = regions.indexOf(oldRegion); if (regionIndex !== -1) { const newRegionIndex = regions.indexOf(newRegion); if (newRegionIndex === -1) { regions[regionIndex] = newRegion; } if (newRegionIndex !== -1) { regions.splice(regionIndex, 1); } }}