Skip to content

Open-source status page, incident response and uptime monitoring

3.1 KB
Raw Blame
import { asc, eq } from "@openstatus/db";
import { statusReport, statusReportUpdate } from "@openstatus/db/src/schema";

import type { DB } from "../context";
import type { StatusReport } from "../types";
import type { StatusReportStatus } from "./schemas";

type DerivableUpdate = {
  id: number;
  status: StatusReportStatus;
  date: Date;
};

/**
 * Latest update wins; `id` breaks date ties — must match
 * `getCurrentImpactsForReport` or status and impacts can come from
 * different updates. Null when there are no updates.
 */
export function deriveReportStatus(
  updates: ReadonlyArray<DerivableUpdate>,
): StatusReportStatus | null {
  return findLatestUpdate(updates)?.status ?? null;
}

function findLatestUpdate(
  updates: ReadonlyArray<DerivableUpdate>,
): DerivableUpdate | null {
  let latest: DerivableUpdate | null = null;
  for (const update of updates) {
    if (
      latest === null ||
      update.date.getTime() > latest.date.getTime() ||
      (update.date.getTime() === latest.date.getTime() && update.id > latest.id)
    ) {
      latest = update;
    }
  }
  return latest;
}

/**
 * Persist `status_report.status` from the report's updates. Every mutation
 * touching the update set must end with this — the column is a cache, never
 * an independently settable field. Null when no updates remain.
 */
export async function recomputeReportStatus(
  tx: DB,
  statusReportId: number,
  options?: {
    /** Bump `updatedAt` even if the status is unchanged, when this update is the latest one. */
    touchIfLatestIs?: number;
    /** Stamped on the report whenever the row is written. */
    updatedBy?: number | null;
    /** The update just deleted; forces the write when it was the latest one. */
    removed?: DerivableUpdate;
  },
): Promise<StatusReport | null> {
  const rows = await tx
    .select({
      id: statusReportUpdate.id,
      status: statusReportUpdate.status,
      date: statusReportUpdate.date,
    })
    .from(statusReportUpdate)
    .where(eq(statusReportUpdate.statusReportId, statusReportId))
    .orderBy(asc(statusReportUpdate.date), asc(statusReportUpdate.id))
    .all();

  const latest = findLatestUpdate(rows);
  if (latest === null) return null;

  const current = await tx
    .select()
    .from(statusReport)
    .where(eq(statusReport.id, statusReportId))
    .get();
  if (!current) return null;

  const isNewLatest =
    options?.touchIfLatestIs !== undefined &&
    options.touchIfLatestIs === latest.id;
  const removedLatest =
    options?.removed !== undefined &&
    findLatestUpdate([...rows, options.removed])?.id === options.removed.id;

  // no-op edits (message, impacts) must not bump updatedAt — the RSS/Atom
  // feed dates and sorts items by it. Adding or removing the latest update
  // always counts, even when the status it leaves behind is the same.
  if (current.status === latest.status && !isNewLatest && !removedLatest)
    return current;

  return tx
    .update(statusReport)
    .set({
      status: latest.status,
      updatedAt: new Date(),
      ...(options?.updatedBy !== undefined && { updatedBy: options.updatedBy }),
    })
    .where(eq(statusReport.id, statusReportId))
    .returning()
    .get();
}