Skip to content

Open-source status page, incident response and uptime monitoring

4.6 KB
Raw Blame
import type { ComponentImpact, UpdateComponentImpact } from "../../types";
import type {
  StatuspageComponent,
  StatuspageGroupComponent,
  StatuspageIncident,
  StatuspagePage,
  StatuspageSubscriber,
} from "./api-types";

export type StatusReportStatus =
  | "investigating"
  | "identified"
  | "monitoring"
  | "resolved";

// `under_maintenance` has no openstatus equivalent — treat as available.
// Unknown values are dropped rather than guessed.
const COMPONENT_IMPACT_MAP: Record<string, ComponentImpact> = {
  operational: "operational",
  degraded_performance: "degraded_performance",
  partial_outage: "partial_outage",
  major_outage: "major_outage",
  under_maintenance: "operational",
};

export function mapPage(page: StatuspagePage, workspaceId: number) {
  return {
    workspaceId,
    title: page.name,
    description: page.page_description ?? "",
    slug: page.subdomain,
    customDomain: page.domain ?? "",
    published: true,
    icon: "",
  };
}

export function mapComponent(
  component: StatuspageComponent,
  workspaceId: number,
  pageId?: number,
) {
  return {
    workspaceId,
    pageId,
    type: "static" as const,
    monitorId: null,
    name: component.name,
    description: component.description ?? null,
    order: component.position ?? 0,
  };
}

export function mapComponentGroup(
  group: StatuspageGroupComponent,
  workspaceId: number,
  pageId?: number,
) {
  return {
    workspaceId,
    pageId,
    name: group.name,
  };
}

const INCIDENT_UPDATE_STATUS_MAP: Record<string, StatusReportStatus> = {
  investigating: "investigating",
  identified: "identified",
  monitoring: "monitoring",
  resolved: "resolved",
  postmortem: "resolved",
  scheduled: "investigating",
  in_progress: "investigating",
  verifying: "monitoring",
  completed: "resolved",
};

export function mapIncidentUpdateStatus(status: string): StatusReportStatus {
  return INCIDENT_UPDATE_STATUS_MAP[status] ?? "investigating";
}

export function isScheduledIncident(incident: StatuspageIncident): boolean {
  return incident.scheduled_for != null;
}

export function mapIncidentToStatusReport(
  incident: StatuspageIncident,
  workspaceId: number,
  pageId?: number,
) {
  const updates = [...(incident.incident_updates ?? [])].sort(
    (a, b) =>
      new Date(a.created_at).getTime() - new Date(b.created_at).getTime(),
  );

  const mappedUpdates = updates.map((u) => ({
    status: mapIncidentUpdateStatus(u.status),
    message: u.body ?? "",
    date: new Date(u.created_at),
    componentImpacts: (u.affected_components ?? []).flatMap(
      (c): UpdateComponentImpact[] => {
        const impact = COMPONENT_IMPACT_MAP[c.new_status];
        // `code` is the component id `componentIdMap` is keyed on
        return impact ? [{ sourceComponentId: c.code, impact }] : [];
      },
    ),
  }));

  // A `postmortem` update already carries the postmortem as its body.
  const hasPostmortemUpdate = updates.some((u) => u.status === "postmortem");
  if (
    incident.postmortem_body &&
    !hasPostmortemUpdate &&
    mappedUpdates.length > 0
  ) {
    const last = mappedUpdates[mappedUpdates.length - 1];
    last.message = `${last.message}\n\n---\n\n**Postmortem**\n\n${incident.postmortem_body}`;
  }

  const lastUpdate = updates[updates.length - 1];
  const reportStatus = lastUpdate
    ? mapIncidentUpdateStatus(lastUpdate.status)
    : "investigating";

  const sourceComponentIds: string[] = [];
  if (incident.components) {
    for (const comp of incident.components) {
      sourceComponentIds.push(comp.id);
    }
  }

  return {
    report: {
      title: incident.name,
      status: reportStatus,
      workspaceId,
      pageId,
    },
    updates: mappedUpdates,
    sourceComponentIds,
  };
}

export function mapIncidentToMaintenance(
  incident: StatuspageIncident,
  workspaceId: number,
  pageId?: number,
) {
  const updates = incident.incident_updates ?? [];
  const message = updates.map((u) => u.body ?? "").join("\n");

  if (!incident.scheduled_for) {
    throw new Error(
      `Incident "${incident.name}" is missing scheduled_for date`,
    );
  }

  return {
    title: incident.name,
    message,
    from: new Date(incident.scheduled_for),
    to: new Date(incident.scheduled_until ?? incident.scheduled_for),
    workspaceId,
    pageId,
  };
}

export function mapSubscriber(
  subscriber: StatuspageSubscriber,
  pageId?: number,
): {
  email: string;
  pageId?: number;
  confirmed: boolean;
  sourceComponentIds: string[];
} | null {
  if (subscriber.mode !== "email") return null;

  return {
    email: subscriber.email ?? "",
    pageId,
    confirmed: true,
    sourceComponentIds: subscriber.components ?? [],
  };
}