Skip to content

Open-source status page, incident response and uptime monitoring

50.0 KB
Raw Blame
import { Events } from "@openstatus/analytics";
import { and, eq, inArray, isNull, sql } from "@openstatus/db";
import {
  maintenance,
  page,
  pageComponent,
  pageConfigurationSchema,
  privateLocationToMonitors,
  selectMaintenancePageSchema,
  selectPageComponentWithMonitorRelation,
  selectPageSchema,
  selectPublicMonitorSchema,
  selectPublicPageLightSchemaWithRelation,
  selectPublicPageSchemaWithRelation,
  selectStatusReportPageSchema,
  selectWorkspaceSchema,
  statusReport,
  timeZoneSchema,
} from "@openstatus/db/src/schema";
import { getStatusUpdateTypes } from "@openstatus/services/page";
import { constantTimeEqual } from "@openstatus/services/page-access";
import {
  getSubscriberByToken,
  hasPendingSubscriber,
  unsubscribeSubscriber,
  updateSubscriberScope,
  upsertSelfSignupSubscriber,
  verifySelfSignupSubscriber,
} from "@openstatus/services/page-subscriber";
import { TRPCError } from "@trpc/server";
import { endOfDay, startOfDay, subDays } from "date-fns";
import { z } from "zod";

import {
  assertPageAccess,
  resolvePageAccess,
  visitorFromCtx,
} from "../lib/page-access";
import { sendSubscriberVerification } from "../lib/subscriber-verification";
import { createTRPCRouter, publicProcedure } from "../trpc";
import {
  type StatusData,
  activeReportStatus,
  fetchByJobType,
  fillStatusDataFor45Days,
  fillStatusDataFor45DaysNoop,
  getEvents,
  getUptime,
  getWorstVariant,
  isMonitorComponent,
  setDataByType,
  withTinybirdFallback,
} from "./statusPage.utils";
import {
  getMetricsLatencyMultiProcedure,
  getMetricsLatencyProcedure,
  getMetricsRegionsProcedure,
  getStatusInTimeZoneProcedure,
  getStatusProcedure,
  getUptimeProcedure,
} from "./tinybird";

// NOTE: publicProcedure is used to get the status page
// TODO: improve performance of SQL query (make a single query with joins)

// IMPORTANT: we cannot use the tinybird procedure because it has protectedProcedure
// instead, we should add TB logic in here!!!!

// NOTE: this router is used on status pages only - do not confuse with the page router which is used in the dashboard for the config

// Gate fields for getGate, reusing selectPageSchema's stringToArray transforms
// so authEmailDomains / allowedIpRanges come back as arrays like getLight.
const gateFieldsSchema = selectPageSchema.pick({
  slug: true,
  customDomain: true,
  accessType: true,
  authEmailDomains: true,
  allowedIpRanges: true,
  homepageUrl: true,
  contactUrl: true,
});

// Password for cookie-less server callers (feeds, `?pw=` links).
const queryPasswordSchema = z.string().nullish();

// A host reaches its page through either the slug or the custom domain.
const bySlugOrDomain = (slug: string) =>
  sql`lower(${page.slug}) = ${slug} OR lower(${page.customDomain}) = ${slug}`;

export function sortStatusReportsByLatestUpdate<
  T extends { id: number; statusReportUpdates: { date: Date }[] },
>(a: T, b: T): number {
  // Sort reports without updates to the beginning
  if (a.statusReportUpdates.length === 0 && b.statusReportUpdates.length === 0)
    return b.id - a.id;
  if (a.statusReportUpdates.length === 0) return -1;
  if (b.statusReportUpdates.length === 0) return 1;
  return (
    b.statusReportUpdates[0].date.getTime() -
      a.statusReportUpdates[0].date.getTime() || b.id - a.id
  );
}

// Status rows grouped into `timeZone` days, with the zone they ended up in.
async function fetchStatusDays(
  monitors: Parameters<typeof fetchByJobType>[0],
  timeZone: string,
) {
  const readUtc = async () => ({
    timeZone: "UTC",
    rows: await fetchByJobType(monitors, (type) =>
      getStatusProcedure("45d", type),
    ),
  });
  // UTC pages keep the 45d pipes; other zones regroup 15m buckets into their days
  if (timeZone === "UTC") return readUtc();
  try {
    const rows = await fetchByJobType(
      monitors,
      (type) =>
        ({ monitorIds }) =>
          getStatusInTimeZoneProcedure(type)({
            monitorIds,
            timezone: timeZone,
          }),
    );
    return { timeZone, rows };
  } catch (err) {
    // ClickHouse's tz database can reject a zone Node accepts: keep real bars
    // in UTC days; other errors still drop the page to manual mode
    if (!(err instanceof Error) || !/time ?zone/i.test(err.message)) throw err;
    console.error(
      `[status-page] zone ${timeZone} failed, using UTC days:`,
      err,
    );
    return readUtc();
  }
}

export const statusPageRouter = createTRPCRouter({
  get: publicProcedure
    .input(
      z.object({
        slug: z.string().toLowerCase(),
        pw: queryPasswordSchema,
        // NOTE: override the defaults we are getting from the page configuration
        cardType: z
          .enum(["requests", "duration", "dominant", "manual"])
          .nullish(),
        barType: z.enum(["absolute", "dominant", "manual"]).nullish(),
      }),
    )
    .output(selectPublicPageSchemaWithRelation.nullish())
    .query(async (opts) => {
      if (!opts.input.slug) return null;

      const _page = await opts.ctx.db.query.page.findFirst({
        where: bySlugOrDomain(opts.input.slug),
        with: {
          workspace: true,
          statusReports: {
            // TODO: we need to order the based on statusReportUpdates instead
            // orderBy: (reports, { desc }) => desc(reports.createdAt),
            with: {
              statusReportUpdates: {
                orderBy: (reports, { desc }) => desc(reports.date),
                with: { statusReportUpdateToPageComponents: true },
              },
              statusReportsToPageComponents: { with: { pageComponent: true } },
            },
          },
          maintenances: {
            with: {
              maintenanceUpdates: {
                orderBy: (updates, { desc }) => [
                  desc(updates.date),
                  desc(updates.id),
                ],
              },
              maintenancesToPageComponents: { with: { pageComponent: true } },
            },
            orderBy: (maintenances, { desc }) => desc(maintenances.from),
          },
          pageComponents: {
            with: {
              monitor: {
                with: {
                  monitorIncidents: true,
                },
              },
              group: true,
            },
            orderBy: (pageComponents, { asc }) => asc(pageComponents.order),
          },
          pageComponentGroups: true,
        },
      });

      if (!_page) return null;

      // Denied visitors still need the page chrome (login, layout, OG).
      if (!resolvePageAccess(opts.ctx, _page, opts.input.pw).ok) {
        _page.statusReports = [];
        _page.maintenances = [];
        _page.pageComponents = [];
        _page.pageComponentGroups = [];
      }

      const ws = selectWorkspaceSchema.safeParse(_page.workspace);
      const pageComponents = selectPageComponentWithMonitorRelation
        .array()
        .parse(_page.pageComponents);

      const configuration = pageConfigurationSchema.safeParse(
        _page.configuration ?? {},
      );

      if (!configuration.success) {
        console.error("Invalid configuration", configuration.error);
        return null;
      }

      const barType = opts.input.barType ?? configuration.data.type;
      // const cardType = opts.input.cardType ?? configuration.data.value;

      const monitorComponents = pageComponents.filter(isMonitorComponent);

      // Transform all page components (both monitor and static types)
      const components = pageComponents.map((c) => {
        const events = getEvents({
          maintenances: _page.maintenances,
          incidents: c.monitor?.monitorIncidents ?? [],
          reports: _page.statusReports,
          pageComponentId: c.id,
          monitorId: c.monitorId ?? undefined,
          componentType: c.type,
        });

        // Calculate status based on component type
        let status: "success" | "degraded" | "error" | "info";

        // impact-aware: an active report colors the component by its derived
        // status (major ⇒ error); legacy reports keep flat degraded
        const reportStatus = activeReportStatus(events);

        if (c.type === "static") {
          // Static: only reports and maintenances affect status
          status =
            reportStatus ??
            (events.some(
              (e) =>
                e.type === "maintenance" &&
                e.to &&
                e.from.getTime() <= new Date().getTime() &&
                e.to.getTime() >= new Date().getTime(),
            )
              ? "info"
              : "success");
        } else {
          // Monitor: incidents, reports, and maintenances affect status
          status =
            events.some((e) => e.type === "incident" && !e.to) &&
            barType !== "manual"
              ? "error"
              : (reportStatus ??
                (events.some(
                  (e) =>
                    e.type === "maintenance" &&
                    e.to &&
                    e.from.getTime() <= new Date().getTime() &&
                    e.to.getTime() >= new Date().getTime(),
                )
                  ? "info"
                  : "success"));
        }

        return {
          ...c,
          status,
          events,
        };
      });

      // Keep monitors for backward compatibility with existing fields
      const monitors = monitorComponents.map((c) => {
        const events = getEvents({
          maintenances: _page.maintenances,
          incidents: c.monitor.monitorIncidents ?? [],
          reports: _page.statusReports,
          monitorId: c.monitor.id,
        });
        const status =
          events.some((e) => e.type === "incident" && !e.to) &&
          barType !== "manual"
            ? "error"
            : (activeReportStatus(events) ??
              (events.some(
                (e) =>
                  e.type === "maintenance" &&
                  e.to &&
                  e.from.getTime() <= new Date().getTime() &&
                  e.to.getTime() >= new Date().getTime(),
              )
                ? "info"
                : "success"));
        return {
          ...c.monitor,
          status,
          events,
          monitorGroupId: c.groupId,
          order: c.order,
          groupOrder: c.groupOrder,
        };
      });

      // Add privateLocationCount to each monitor
      const privateLocationCounts = new Map<number, number>();
      if (monitors.length > 0) {
        const monitorIds = monitors.map((m) => m.id);
        const privateLocations =
          await opts.ctx.db.query.privateLocationToMonitors.findMany({
            where: and(
              inArray(privateLocationToMonitors.monitorId, monitorIds),
              isNull(privateLocationToMonitors.deletedAt),
            ),
            columns: {
              monitorId: true,
            },
          });

        // Count private locations per monitor
        for (const pl of privateLocations) {
          if (pl.monitorId === null) continue;
          privateLocationCounts.set(
            pl.monitorId,
            (privateLocationCounts.get(pl.monitorId) ?? 0) + 1,
          );
        }
      }

      // Create new array with privateLocationCount included (no mutation/cast)
      const monitorsWithPrivateLocationCount = monitors.map((m) => ({
        ...m,
        privateLocationCount: privateLocationCounts.get(m.id) ?? 0,
      }));

      // Sort monitors to match trackers behavior: group by monitorGroupId, order
      // groups by minimum order, sort within groups by groupOrder, and sort
      // ungrouped monitors by order
      const groupMinOrderMap = new Map<number, number>();
      for (const monitor of monitorsWithPrivateLocationCount) {
        const groupId = monitor.monitorGroupId;
        if (groupId !== null) {
          const order = monitor.order ?? 0;
          const currentMin =
            groupMinOrderMap.get(groupId) ?? Number.MAX_SAFE_INTEGER;
          groupMinOrderMap.set(groupId, Math.min(currentMin, order));
        }
      }

      monitorsWithPrivateLocationCount.sort((a, b) => {
        const aGroupId = a.monitorGroupId ?? null;
        const bGroupId = b.monitorGroupId ?? null;

        // If both monitors are in the same group (or both ungrouped with null)
        if (aGroupId === bGroupId) {
          if (aGroupId === null) {
            // Both ungrouped - sort by order
            return (a.order ?? 0) - (b.order ?? 0);
          }
          // Both in same group - sort by groupOrder within the group
          return (a.groupOrder ?? 0) - (b.groupOrder ?? 0);
        }

        // Different groups or one is ungrouped - sort by group position
        // For grouped monitors, use precomputed minimum order of the group
        // For ungrouped monitors, use their own order
        const aGroupMinOrder =
          aGroupId !== null
            ? (groupMinOrderMap.get(aGroupId) ?? 0)
            : (a.order ?? 0);
        const bGroupMinOrder =
          bGroupId !== null
            ? (groupMinOrderMap.get(bGroupId) ?? 0)
            : (b.order ?? 0);

        return aGroupMinOrder - bGroupMinOrder;
      });

      // no barType gate: incident-driven error is already suppressed per
      // monitor in manual mode; report-driven error (major_outage) must show
      const status = monitorsWithPrivateLocationCount.some(
        (m) => m.status === "error",
      )
        ? "error"
        : monitorsWithPrivateLocationCount.some((m) => m.status === "degraded")
          ? "degraded"
          : monitorsWithPrivateLocationCount.some((m) => m.status === "info")
            ? "info"
            : "success";

      // Get page-wide events (not tied to specific monitors)
      const pageEvents = getEvents({
        maintenances: _page.maintenances,
        incidents: monitorComponents.flatMap(
          (c) => c.monitor.monitorIncidents ?? [],
        ),
        reports: _page.statusReports,
        // No monitorId provided, so we get all events for the page
      });

      const threshold = new Date().getTime() - 7 * 24 * 60 * 60 * 1000;
      const lastEvents = pageEvents
        .filter((e) => {
          if (e.type === "incident") return false;
          if (!e.from || e.from.getTime() >= threshold) return true;
          if (e.type === "report" && e.status !== "success") return true;
          return false;
        })
        .sort((a, b) => a.from.getTime() - b.from.getTime());

      const openEvents = pageEvents.filter((event) => {
        if (event.type === "incident" && barType !== "manual") {
          if (!event.to) return true;
          if (event.to < new Date()) return false;
          return false;
        }
        if (event.type === "report") {
          if (!event.to) return true;
          if (event.to < new Date()) return false;
          return false;
        }
        if (event.type === "maintenance") {
          if (!event.to) return false; // NOTE: this never happens
          if (event.from <= new Date() && event.to >= new Date()) return true;
          return false;
        }
        return false;
      });

      const monitorGroups = _page.pageComponentGroups;

      // Create trackers array with grouped and ungrouped components
      const groupedMap = new Map<
        number | null,
        {
          groupId: number | null;
          groupName: string | null;
          defaultOpen: boolean;
          components: typeof components;
          minOrder: number;
        }
      >();

      components.forEach((component) => {
        const groupId = component.groupId ?? null;
        const group = groupId
          ? monitorGroups.find((g) => g?.id === groupId)
          : null;
        const groupName = group?.name ?? null;
        const defaultOpen = group?.defaultOpen ?? false;

        if (!groupedMap.has(groupId)) {
          groupedMap.set(groupId, {
            groupId,
            groupName,
            defaultOpen,
            components: [],
            minOrder: component.order ?? 0,
          });
        }
        const currentGroup = groupedMap.get(groupId);
        if (currentGroup) {
          currentGroup.components.push(component);
          currentGroup.minOrder = Math.min(
            currentGroup.minOrder,
            component.order ?? 0,
          );
        }
      });

      // Convert to trackers array
      type PageComponentTracker = {
        type: "component";
        component: (typeof components)[number];
        order: number;
      };

      type GroupTracker = {
        type: "group";
        groupId: number;
        groupName: string;
        defaultOpen: boolean;
        components: typeof components;
        status: "success" | "degraded" | "error" | "info" | "empty";
        order: number;
      };

      type Tracker = PageComponentTracker | GroupTracker;

      const trackers: Tracker[] = Array.from(groupedMap.values())
        .flatMap((group): Tracker[] => {
          if (group.groupId === null) {
            // Ungrouped components - return as individual trackers
            return group.components.map((component): PageComponentTracker => ({
              type: "component",
              component,
              order: component.order ?? 0,
            }));
          }
          // Grouped components - return as single group tracker
          const sortedComponents = group.components.sort(
            (a, b) => (a.groupOrder ?? 0) - (b.groupOrder ?? 0),
          );
          return [
            {
              type: "group",
              groupId: group.groupId,
              groupName: group.groupName ?? "",
              defaultOpen: group.defaultOpen,
              components: sortedComponents,
              status: getWorstVariant(
                group.components.map(
                  (c) => c.status as "success" | "degraded" | "error" | "info",
                ),
              ),
              order: group.minOrder,
            },
          ];
        })
        .sort((a, b) => a.order - b.order);

      const whiteLabel = ws.data?.limits["white-label"] ?? false;
      // stored custom theme stops applying when the plan no longer includes it
      const customTheme = ws.data?.limits["custom-theme"]
        ? _page.customTheme
        : null;

      const statusReports = _page.statusReports.sort(
        sortStatusReportsByLatestUpdate,
      );

      const maintenances = _page.maintenances.sort(
        (a, b) => b.from.getTime() - a.from.getTime(),
      );

      // In "manual" mode the page only surfaces user-authored events, so drop
      // monitor-derived incidents from the components consumers read (e.g. the
      // calendar). Mirrors the bar/uptime gating in statusPage.utils.ts.
      const publicPageComponents =
        barType === "manual"
          ? pageComponents.map((c) =>
              c.monitor
                ? { ...c, monitor: { ...c.monitor, monitorIncidents: [] } }
                : c,
            )
          : pageComponents;

      return selectPublicPageSchemaWithRelation.parse({
        ..._page,
        customTheme,
        monitors: monitorsWithPrivateLocationCount,
        monitorGroups,
        trackers,
        incidents:
          monitorsWithPrivateLocationCount.flatMap((m) => m.monitorIncidents) ??
          [],
        statusReports,
        maintenances,
        workspacePlan: _page.workspace.plan,
        statusUpdateTypes: getStatusUpdateTypes({
          accessType: _page.accessType ?? "public",
          emailSubscriptionsEnabled:
            ws.data?.limits["status-subscribers"] ?? false,
        }),
        status,
        lastEvents,
        openEvents,
        pageComponents: publicPageComponents,
        pageComponentGroups: _page.pageComponentGroups,
        whiteLabel,
      });
    }),

  getLight: publicProcedure
    .input(
      z.object({
        slug: z.string().toLowerCase(),
        pw: queryPasswordSchema,
      }),
    )
    .query(async (opts) => {
      if (!opts.input.slug) return null;

      // Single query with all relations
      const _page = await opts.ctx.db.query.page.findFirst({
        where: bySlugOrDomain(opts.input.slug),
        with: {
          workspace: true,
          statusReports: {
            with: {
              statusReportUpdates: {
                orderBy: (reports, { desc }) => desc(reports.date),
                with: { statusReportUpdateToPageComponents: true },
              },
              statusReportsToPageComponents: { with: { pageComponent: true } },
            },
          },
          maintenances: {
            with: {
              maintenanceUpdates: {
                orderBy: (updates, { desc }) => [
                  desc(updates.date),
                  desc(updates.id),
                ],
              },
              maintenancesToPageComponents: { with: { pageComponent: true } },
            },
            orderBy: (maintenances, { desc }) => desc(maintenances.from),
          },
          pageComponents: {
            with: {
              monitor: { with: { monitorIncidents: true } },
              group: true,
            },
            orderBy: (pageComponents, { asc }) => asc(pageComponents.order),
          },
          pageComponentGroups: true,
        },
      });

      if (!_page) return null;

      // Denied visitors still need the page chrome (login, layout, OG).
      if (!resolvePageAccess(opts.ctx, _page, opts.input.pw).ok) {
        _page.statusReports = [];
        _page.maintenances = [];
        _page.pageComponents = [];
        _page.pageComponentGroups = [];
      }

      // Extract monitor components for backwards compatibility
      const monitorComponents = _page.pageComponents.filter(
        (c) =>
          c.type === "monitor" &&
          c.monitor &&
          c.monitor.active &&
          !c.monitor.deletedAt,
      );

      // Build legacy monitors array (sorted by order)
      const monitors = monitorComponents
        .map((c) => ({
          ...c.monitor,
          // the page component carries the public-facing name/description;
          // clear externalName so the schema transform keeps the override.
          // description: NULL means never backfilled → fall back to the
          // monitor's own; "" is a deliberately cleared field → stays blank.
          name: c.name,
          description: c.description ?? c.monitor?.description ?? "",
          externalName: null,
        }))
        .sort((a, b) => {
          const aComp = monitorComponents.find((m) => m.monitor?.id === a.id);
          const bComp = monitorComponents.find((m) => m.monitor?.id === b.id);
          return (aComp?.order ?? 0) - (bComp?.order ?? 0);
        });

      // Extract all incidents from monitor components
      const incidents = monitorComponents.flatMap(
        (c) => c.monitor?.monitorIncidents ?? [],
      );

      const ws = selectWorkspaceSchema.safeParse(_page.workspace);
      const whiteLabel = ws.data?.limits["white-label"] ?? false;
      // stored custom theme stops applying when the plan no longer includes it
      const customTheme = ws.data?.limits["custom-theme"]
        ? _page.customTheme
        : null;

      const statusReports = _page.statusReports.sort(
        sortStatusReportsByLatestUpdate,
      );

      return selectPublicPageLightSchemaWithRelation.parse({
        ..._page,
        customTheme,
        monitors,
        incidents,
        statusReports,
        maintenances: _page.maintenances,
        pageComponents: _page.pageComponents,
        pageComponentGroups: _page.pageComponentGroups,
        workspacePlan: _page.workspace.plan,
        whiteLabel,
      });
    }),

  // Narrow access-check query for the markdown detail routes: returns only the
  // gate + chrome fields, skipping the full reports/maintenances/components graph
  // that getLight loads.
  getGate: publicProcedure
    .input(z.object({ slug: z.string().toLowerCase() }))
    .query(async (opts) => {
      if (!opts.input.slug) return null;

      const _page = await opts.ctx.db.query.page.findFirst({
        where: bySlugOrDomain(opts.input.slug),
        columns: {
          slug: true,
          customDomain: true,
          accessType: true,
          authEmailDomains: true,
          allowedIpRanges: true,
          homepageUrl: true,
          contactUrl: true,
        },
        with: { workspace: true },
      });

      if (!_page) return null;

      const ws = selectWorkspaceSchema.safeParse(_page.workspace);
      const whiteLabel = ws.data?.limits["white-label"] ?? false;

      const { workspace: _workspace, ...rest } = _page;
      const gate = gateFieldsSchema.parse(rest);
      return { ...gate, whiteLabel };
    }),

  getMaintenance: publicProcedure
    .input(
      z.object({
        slug: z.string().toLowerCase(),
        id: z.number(),
        pw: queryPasswordSchema,
      }),
    )
    .query(async (opts) => {
      if (!opts.input.slug) return null;

      const _page = await opts.ctx.db
        .select()
        .from(page)
        .where(bySlugOrDomain(opts.input.slug))
        .get();

      if (!_page) return null;

      assertPageAccess(opts.ctx, _page, opts.input.pw);

      const _maintenance = await opts.ctx.db.query.maintenance.findFirst({
        where: and(
          eq(maintenance.id, opts.input.id),
          eq(maintenance.pageId, _page.id),
        ),
        with: {
          maintenanceUpdates: {
            orderBy: (updates, { desc }) => [
              desc(updates.date),
              desc(updates.id),
            ],
          },
          maintenancesToPageComponents: {
            with: { pageComponent: { with: { monitor: true } } },
          },
        },
      });

      if (!_maintenance) return null;

      const props: z.infer<typeof selectMaintenancePageSchema> = _maintenance;
      return selectMaintenancePageSchema.parse(props);
    }),

  getUptime: publicProcedure
    .input(
      z.object({
        slug: z.string().toLowerCase(),
        pw: queryPasswordSchema,
        pageComponentIds: z.string().array(),
        cardType: z
          .enum(["requests", "duration", "dominant", "manual"])
          .prefault("requests"),
        barType: z
          .enum(["absolute", "dominant", "manual"])
          .prefault("dominant"),
        // preview override for the floating-button config; falls back to the
        // page's stored `configuration.days` when omitted
        days: z.union([z.literal(30), z.literal(45)]).optional(),
      }),
    )
    .query(async (opts) => {
      const input = opts.input;
      if (!input.slug) return null;

      const _page = await opts.ctx.db.query.page.findFirst({
        where: bySlugOrDomain(input.slug),
        with: {
          maintenances: {
            with: {
              maintenancesToPageComponents: { with: { pageComponent: true } },
            },
          },
          statusReports: {
            with: {
              statusReportsToPageComponents: { with: { pageComponent: true } },
              statusReportUpdates: {
                with: { statusReportUpdateToPageComponents: true },
              },
            },
          },
          pageComponents: {
            where: inArray(
              pageComponent.id,
              input.pageComponentIds.map(Number),
            ),
            with: {
              monitor: {
                with: {
                  monitorIncidents: true,
                },
              },
            },
          },
        },
      });

      if (!_page) return null;

      assertPageAccess(opts.ctx, _page, input.pw);

      const pageComponents = selectPageComponentWithMonitorRelation
        .array()
        .parse(_page.pageComponents);

      // Early return if no components to process
      if (pageComponents.length === 0) return [];

      const monitors = pageComponents.filter(isMonitorComponent);

      // a zone this runtime cannot resolve renders the page in UTC instead of failing it
      const pageTimeZone = timeZoneSchema
        .catch("UTC")
        .parse(_page.defaultTimezone);

      // Manual mode never touches Tinybird. Otherwise race the reads against
      // the fallback budget: a slow (>5s) or erroring Tinybird degrades the
      // whole page to manual mode so bars still render from DB events.
      const tinybird = await withTinybirdFallback(() =>
        input.barType === "manual"
          ? Promise.resolve(null)
          : fetchStatusDays(monitors, pageTimeZone),
      );

      const tinybirdUnhealthy = !tinybird.ok;
      const timeZone = tinybird.data?.timeZone ?? pageTimeZone;
      const statusDataByMonitorId =
        tinybird.data?.rows ?? new Map<string, never[]>();

      const parsedConfiguration = pageConfigurationSchema.safeParse(
        _page.configuration ?? {},
      );
      const lookbackPeriod =
        input.days ??
        (parsedConfiguration.success ? parsedConfiguration.data.days : 45);

      // No public regions alone doesn't make a monitor private-only: it may
      // have no locations at all, so confirm an active private assignment.
      const noRegionMonitorIds = monitors
        .filter((c) => c.monitor.regions.length === 0)
        .map((c) => c.monitor.id);
      const privateOnlyMonitorIds = new Set<number>();
      if (noRegionMonitorIds.length > 0) {
        const rows = await opts.ctx.db.query.privateLocationToMonitors.findMany(
          {
            where: and(
              inArray(privateLocationToMonitors.monitorId, noRegionMonitorIds),
              isNull(privateLocationToMonitors.deletedAt),
            ),
            columns: { monitorId: true },
          },
        );
        for (const row of rows) {
          if (row.monitorId !== null) privateOnlyMonitorIds.add(row.monitorId);
        }
      }

      return pageComponents.map((c) => {
        const events = getEvents({
          maintenances: _page.maintenances,
          incidents: c.monitor?.monitorIncidents ?? [],
          reports: _page.statusReports,
          pageComponentId: c.id,
          monitorId: c.monitorId ?? undefined,
          componentType: c.type,
        });

        // Determine whether to use real Tinybird data or synthetic data
        const shouldUseRealData =
          c.type === "monitor" &&
          c.monitor &&
          input.barType !== "manual" &&
          !tinybirdUnhealthy &&
          process.env.NOOP_UPTIME !== "true";

        let filledData: StatusData[];
        if (shouldUseRealData) {
          // Monitor components with real data: use Tinybird data
          const monitorId = c.monitor?.id.toString() || "";
          const rawData = statusDataByMonitorId.get(monitorId) || [];
          filledData = fillStatusDataFor45Days(
            rawData,
            monitorId,
            lookbackPeriod,
            timeZone,
          );
        } else {
          // Static components, manual mode, or NOOP mode: use synthetic data
          filledData = fillStatusDataFor45DaysNoop({
            errorDays: [],
            degradedDays: [],
            lookbackPeriod,
            timeZone,
          });
        }

        // Static components have no monitoring data; a degraded Tinybird forces
        // every component into manual mode so bars/cards still render.
        const forceManual = c.type === "static" || tinybirdUnhealthy;
        const effectiveBarType = forceManual ? "manual" : input.barType;
        const effectiveCardType = forceManual ? "manual" : input.cardType;

        const processedData = setDataByType({
          events,
          data: filledData,
          cardType: effectiveCardType,
          barType: effectiveBarType,
          timeZone,
        });
        const uptime = getUptime({
          data: filledData,
          events,
          barType: effectiveBarType,
          cardType: effectiveCardType,
          timeZone,
          privateLocationOnly:
            c.monitor != null && privateOnlyMonitorIds.has(c.monitor.id),
        });

        return {
          id: c.id,
          pageComponentId: c.id,
          name: c.name,
          description: c.description,
          type: c.type,
          // For monitor-type components, include monitor fields
          ...(c.monitor ? { monitor: c.monitor } : {}),
          data: processedData,
          uptime,
          // the zone the days were grouped in, UTC if the page's was rejected
          timeZone,
        };
      });
    }),

  // NOTE: used for the theme store
  getNoopUptime: publicProcedure.query(async () => {
    const data = fillStatusDataFor45DaysNoop({
      errorDays: [4],
      degradedDays: [40],
    });
    const processedData = setDataByType({
      events: [
        {
          type: "maintenance",
          from: new Date(new Date().setDate(new Date().getDate() - 10)),
          to: new Date(new Date().setDate(new Date().getDate() - 10)),
          name: "DB migration",
          id: 1,
          status: "info",
        },
      ],
      data,
      cardType: "requests",
      barType: "dominant",
    });
    return {
      data: processedData,
      uptime: "100%",
    };
  }),

  getReport: publicProcedure
    .input(
      z.object({
        slug: z.string().toLowerCase(),
        id: z.number(),
        pw: queryPasswordSchema,
      }),
    )
    .query(async (opts) => {
      if (!opts.input.slug) return null;

      const _page = await opts.ctx.db
        .select()
        .from(page)
        .where(bySlugOrDomain(opts.input.slug))
        .get();

      if (!_page) return null;

      assertPageAccess(opts.ctx, _page, opts.input.pw);

      const _report = await opts.ctx.db.query.statusReport.findFirst({
        where: and(
          eq(statusReport.id, opts.input.id),
          eq(statusReport.pageId, _page.id),
        ),
        with: {
          statusReportsToPageComponents: {
            with: { pageComponent: { with: { monitor: true } } },
          },
          statusReportUpdates: {
            orderBy: (reports, { desc }) => desc(reports.date),
            with: { statusReportUpdateToPageComponents: true },
          },
        },
      });

      if (!_report) return null;

      const result: z.infer<typeof selectStatusReportPageSchema> = _report;
      return selectStatusReportPageSchema.parse(result);
    }),

  getNoopReport: publicProcedure.query(async () => {
    const date = new Date(new Date().setDate(new Date().getDate() - 4));

    const resolvedDate = new Date(date.setMinutes(date.getMinutes() - 81));
    const monitoringDate = new Date(date.setMinutes(date.getMinutes() - 54));
    const identifiedDate = new Date(date.setMinutes(date.getMinutes() - 32));
    const investigatingDate = new Date(date.setMinutes(date.getMinutes() - 4));

    const props: z.input<typeof selectStatusReportPageSchema> = {
      id: 1,
      pageId: 1,
      workspaceId: 1,
      status: "investigating" as const,
      title: "API Latency Issues",
      createdAt: new Date(new Date().setDate(new Date().getDate() - 2)),
      updatedAt: new Date(new Date().setDate(new Date().getDate() - 1)),
      statusReportsToPageComponents: [
        {
          pageComponentId: 1,
          statusReportId: 1,
          pageComponent: {
            workspaceId: 1,
            pageId: 1,
            id: 1,
            name: "API Monitor",
            type: "monitor" as const,
            monitorId: 1,
            order: 1,
            groupId: null,
            groupOrder: null,
            description: "Main API endpoint",
            createdAt: new Date(new Date().setDate(new Date().getDate() - 30)),
            updatedAt: new Date(new Date().setDate(new Date().getDate() - 30)),
          },
        },
      ],
      statusReportUpdates: [
        {
          id: 4,
          statusReportId: 1,
          status: "resolved" as const,
          message:
            "All systems are operating normally. The issue has been fully resolved.",
          date: resolvedDate,
          createdAt: resolvedDate,
          updatedAt: resolvedDate,
        },
        {
          id: 3,
          statusReportId: 1,
          status: "monitoring" as const,
          message:
            "We are continuing to monitor the situation to ensure that the issue is resolved.",
          date: monitoringDate,
          createdAt: monitoringDate,
          updatedAt: monitoringDate,
        },
        {
          id: 2,
          statusReportId: 1,
          status: "identified" as const,
          message: "The issue has been identified and a fix is being deployed.",
          date: identifiedDate,
          createdAt: identifiedDate,
          updatedAt: identifiedDate,
        },
        {
          id: 1,
          statusReportId: 1,
          status: "investigating" as const,
          message:
            "We are investigating reports of increased latency on our API endpoints.",
          date: investigatingDate,
          createdAt: investigatingDate,
          updatedAt: investigatingDate,
        },
      ],
    };

    return selectStatusReportPageSchema.parse(props);
  }),

  getMonitors: publicProcedure
    .input(
      z.object({ slug: z.string().toLowerCase(), pw: queryPasswordSchema }),
    )
    .query(async (opts) => {
      if (!opts.input.slug) return null;

      // NOTE: revalidate the public monitors first
      const _page = await opts.ctx.db.query.page.findFirst({
        where: bySlugOrDomain(opts.input.slug),
        with: {
          pageComponents: {
            with: {
              monitor: true,
            },
          },
        },
      });

      if (!_page) return null;

      assertPageAccess(opts.ctx, _page, opts.input.pw);

      const pageComponents = selectPageComponentWithMonitorRelation
        .array()
        .parse(_page.pageComponents);

      const publicMonitors = pageComponents
        .filter(isMonitorComponent)
        .filter((c) => c.monitor?.public);

      // Slow/erroring Tinybird → empty latency data so the page still renders.
      const metrics = await withTinybirdFallback(() =>
        fetchByJobType(publicMonitors, (type) =>
          getMetricsLatencyMultiProcedure("1d", type),
        ),
      );
      const metricsDataByMonitorId = metrics.data ?? new Map<string, never[]>();

      return publicMonitors.map((c) => {
        const monitorId = c.monitor.id.toString();
        const data = metricsDataByMonitorId.get(monitorId) || [];

        return {
          ...selectPublicMonitorSchema.parse(c.monitor),
          data,
        };
      });
    }),

  getMonitor: publicProcedure
    .input(
      z.object({
        slug: z.string().toLowerCase(),
        id: z.number(),
        pw: queryPasswordSchema,
      }),
    )
    .query(async (opts) => {
      if (!opts.input.slug) return null;

      const _page = await opts.ctx.db.query.page.findFirst({
        where: bySlugOrDomain(opts.input.slug),
        with: {
          pageComponents: {
            where: eq(pageComponent.monitorId, opts.input.id),
            with: {
              monitor: true,
            },
          },
        },
      });

      if (!_page) return null;

      assertPageAccess(opts.ctx, _page, opts.input.pw);

      const pageComponents = selectPageComponentWithMonitorRelation
        .array()
        .parse(_page.pageComponents);

      const monitorComponents = pageComponents.filter(isMonitorComponent);

      const _monitor = monitorComponents.find(
        (c) => c.monitor.id === opts.input.id,
      )?.monitor;

      if (!_monitor) return null;
      if (!_monitor.public) return null;
      if (_monitor.deletedAt) return null;

      const proceduresByType = {
        http: {
          latency: getMetricsLatencyProcedure("7d", "http"),
          regions: getMetricsRegionsProcedure("7d", "http"),
          uptime: getUptimeProcedure("7d", "http"),
        },
        tcp: {
          latency: getMetricsLatencyProcedure("7d", "tcp"),
          regions: getMetricsRegionsProcedure("7d", "tcp"),
          uptime: getUptimeProcedure("7d", "tcp"),
        },
        dns: {
          latency: getMetricsLatencyProcedure("7d", "dns"),
          regions: getMetricsRegionsProcedure("7d", "dns"),
          uptime: getUptimeProcedure("7d", "dns"),
        },
        icmp: {
          latency: getMetricsLatencyProcedure("7d", "icmp"),
          regions: getMetricsRegionsProcedure("7d", "icmp"),
          uptime: getUptimeProcedure("7d", "icmp"),
        },
        grpc: {
          latency: getMetricsLatencyProcedure("7d", "grpc"),
          regions: getMetricsRegionsProcedure("7d", "grpc"),
          uptime: getUptimeProcedure("7d", "grpc"),
        },
      };

      // `udp` and `ssl` are monitor job types with no Tinybird pipes. Looking the
      // key up instead of asserting the type means such a monitor renders with
      // empty charts — the same shape a Tinybird outage produces — rather than
      // throwing on a missing key.
      const procedures =
        proceduresByType[_monitor.jobType as keyof typeof proceduresByType] ??
        null;

      const fromDate = startOfDay(subDays(new Date(), 7)).toISOString();
      const toDate = endOfDay(new Date()).toISOString();

      // Slow/erroring Tinybird → empty chart data so the page still renders.
      const metrics = !procedures
        ? { ok: false as const, data: null }
        : await withTinybirdFallback(() =>
            Promise.all([
              procedures.latency({
                monitorId: _monitor.id.toString(),
                fromDate,
                toDate,
              }),
              procedures.regions({
                monitorId: _monitor.id.toString(),
                fromDate,
                toDate,
              }),
              procedures.uptime({
                monitorId: _monitor.id.toString(),
                interval: 240,
                fromDate,
                toDate,
              }),
            ]),
          );

      const [latency, regions, uptime] = metrics.data ?? [
        { data: [] },
        { data: [] },
        { data: [] },
      ];

      return {
        ...selectPublicMonitorSchema.parse(_monitor),
        data: {
          latency,
          regions,
          uptime,
        },
      };
    }),

  subscribe: publicProcedure
    .meta({ track: Events.SubscribePage, trackProps: ["slug"] })
    .input(
      z.object({
        slug: z.string().toLowerCase(),
        email: z.email(),
        subscribeComponents: z.boolean(),
        pageComponents: z.array(z.number().int().positive()),
      }),
    )
    .mutation(async (opts) => {
      if (!opts.input.slug) return null;

      const _page = await opts.ctx.db.query.page.findFirst({
        where: bySlugOrDomain(opts.input.slug),
        with: {
          workspace: true,
        },
      });

      if (!_page) {
        throw new TRPCError({
          code: "NOT_FOUND",
          message: "Page not found",
        });
      }

      assertPageAccess(opts.ctx, _page);

      const workspace = selectWorkspaceSchema.safeParse(_page.workspace);

      if (!workspace.success) {
        throw new TRPCError({
          code: "BAD_REQUEST",
          message: "Workspace data is invalid",
        });
      }

      if (!workspace.data.limits["status-subscribers"]) {
        throw new TRPCError({
          code: "FORBIDDEN",
          message: "Upgrade to use status subscribers",
        });
      }

      // Guard against email spam: reject if a pending (unverified, unexpired) subscription exists
      const isPending = await hasPendingSubscriber({
        input: { email: opts.input.email, pageId: _page.id },
        // gated by `assertPageAccess` above
        visitor: null,
      });
      if (isPending) {
        throw new TRPCError({
          code: "BAD_REQUEST",
          message:
            "A confirmation link was already sent. Please check your email or wait until it expires to request a new one.",
        });
      }

      const subscription = await upsertSelfSignupSubscriber({
        input: {
          email: opts.input.email,
          pageId: _page.id,
          componentIds: opts.input.subscribeComponents
            ? opts.input.pageComponents
            : [],
        },
        visitor: visitorFromCtx(opts.ctx),
      });

      // Already verified — no need to send another verification email
      if (subscription.acceptedAt) {
        throw new TRPCError({
          code: "BAD_REQUEST",
          message: "Email already subscribed",
        });
      }

      await sendSubscriberVerification(opts.ctx.db, subscription.id);

      return { id: subscription.id };
    }),

  getSubscriptionByToken: publicProcedure
    .input(z.object({ slug: z.string().toLowerCase(), token: z.uuid() }))
    .query(async (opts) => {
      if (!opts.input.slug) return null;

      const subscription = await getSubscriberByToken({
        input: { token: opts.input.token, domain: opts.input.slug },
      });

      if (!subscription) {
        throw new TRPCError({
          code: "NOT_FOUND",
          message: "Subscription not found",
        });
      }

      return subscription;
    }),

  updateSubscription: publicProcedure
    .input(
      z.object({
        slug: z.string().toLowerCase(),
        token: z.uuid(),
        subscribeComponents: z.boolean(),
        pageComponents: z.array(z.number().int().positive()),
      }),
    )
    .mutation(async (opts) => {
      if (!opts.input.slug) return null;

      try {
        await updateSubscriberScope({
          input: {
            token: opts.input.token,
            componentIds: opts.input.subscribeComponents
              ? opts.input.pageComponents
              : [],
            domain: opts.input.slug,
          },
        });
      } catch (error) {
        if (error instanceof Error) {
          const code = error.message.toLowerCase().includes("not found")
            ? "NOT_FOUND"
            : "BAD_REQUEST";
          throw new TRPCError({ code, message: error.message });
        }
        throw error;
      }

      return { success: true };
    }),

  validateEmailDomain: publicProcedure
    .meta({ track: Events.ValidateEmailDomain, trackProps: ["slug"] })
    .input(z.object({ slug: z.string().toLowerCase(), email: z.string() }))
    .query(async (opts) => {
      if (!opts.input.slug) return null;

      const _page = await opts.ctx.db.query.page.findFirst({
        where: bySlugOrDomain(opts.input.slug),
      });

      if (!_page) {
        throw new TRPCError({
          code: "NOT_FOUND",
          message: "Page not found",
        });
      }

      if (_page.accessType !== "email-domain") {
        throw new TRPCError({
          code: "BAD_REQUEST",
          message:
            "Page is not configured to allow email domain authentication",
        });
      }

      const allowedDomains = _page.authEmailDomains?.split(",") ?? [];

      if (!allowedDomains.includes(opts.input.email.split("@")[1])) {
        throw new TRPCError({
          code: "BAD_REQUEST",
          message: "Invalid email domain",
        });
      }

      // Public procedure: never return the raw row (password, IP allow-list).
      return {
        email: opts.input.email,
        slug: opts.input.slug,
        page: { title: _page.title },
      };
    }),

  verifyEmail: publicProcedure
    .meta({ track: Events.VerifySubscribePage, trackProps: ["slug"] })
    .input(z.object({ slug: z.string().toLowerCase(), token: z.uuid() }))
    .mutation(async (opts) => {
      if (!opts.input.slug) return null;

      try {
        const subscription = await verifySelfSignupSubscriber({
          input: { token: opts.input.token, domain: opts.input.slug },
        });

        if (!subscription) {
          throw new TRPCError({
            code: "NOT_FOUND",
            message: "Subscription not found",
          });
        }

        return subscription;
      } catch (error) {
        if (error instanceof TRPCError) throw error;
        if (error instanceof Error) {
          throw new TRPCError({ code: "BAD_REQUEST", message: error.message });
        }
        throw error;
      }
    }),

  verifyPassword: publicProcedure
    .input(z.object({ slug: z.string().toLowerCase(), password: z.string() }))
    .mutation(async (opts) => {
      if (!opts.input.slug) return null;

      const _page = await opts.ctx.db.query.page.findFirst({
        where: bySlugOrDomain(opts.input.slug),
      });

      if (!_page) {
        throw new TRPCError({
          code: "NOT_FOUND",
          message: "Page not found",
        });
      }

      if (_page.accessType !== "password") {
        throw new TRPCError({
          code: "BAD_REQUEST",
          message: "Page is not configured to allow password authentication",
        });
      }

      if (!constantTimeEqual(_page.password, opts.input.password)) {
        throw new TRPCError({
          code: "BAD_REQUEST",
          message: "Invalid password",
        });
      }

      return true;
    }),

  // Server-side password gate for the public `/api/*` routes. Returns a boolean
  // so the stored password never leaves the server (the `get` output omits it).
  isPasswordAuthorized: publicProcedure
    .input(
      z.object({
        slug: z.string().toLowerCase(),
        queryPassword: z.string().nullish(),
        cookiePassword: z.string().nullish(),
      }),
    )
    .query(async (opts) => {
      const _page = await opts.ctx.db.query.page.findFirst({
        where: bySlugOrDomain(opts.input.slug),
        columns: { password: true, accessType: true },
      });
      if (!_page || _page.accessType !== "password") return false;
      // TODO: rate-limit — an unauthenticated caller can brute-force guesses here.
      // Query param wins over cookie: a present-but-wrong `?pw=` must not fall
      // through to a valid cookie. Mirrors isPasswordAuthorized on the proxy.
      const submitted = opts.input.queryPassword ?? opts.input.cookiePassword;
      return constantTimeEqual(_page.password, submitted);
    }),

  getSubscriberByToken: publicProcedure
    .input(z.object({ token: z.uuid(), domain: z.string().toLowerCase() }))
    .query(async (opts) => {
      const subscription = await getSubscriberByToken({
        input: { token: opts.input.token, domain: opts.input.domain },
      });

      if (
        !subscription ||
        subscription.unsubscribedAt ||
        subscription.channelType !== "email"
      ) {
        return null;
      }

      return {
        pageName: subscription.pageName,
        maskedEmail: subscription.email,
      };
    }),

  unsubscribe: publicProcedure
    .input(z.object({ token: z.uuid(), domain: z.string().toLowerCase() }))
    .mutation(async (opts) => {
      try {
        await unsubscribeSubscriber({
          input: { token: opts.input.token, domain: opts.input.domain },
        });
        return { success: true };
      } catch (error) {
        if (error instanceof Error) {
          throw new TRPCError({ code: "BAD_REQUEST", message: error.message });
        }
        throw error;
      }
    }),
});