Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

TypeScript · 88 lines · 4.5 KiB · at 83c2f03

// The same rules in TSUS v2: plain .ts, Option is T | undefined, match is a typed function. No// adapter: the watcher's .ts code reads `undefined` the way it already reads optional values.// `tsus check --v2 bench/oncall/v2` passes on this file.import { fromNullable, isSome, matchLiteral, tryCatch, type Option } from "@tsus/v2";import type { ApiEvent, HandledEntry, JobKind, WatchConfig } from "../types.ts";const HOUR = 3_600_000;export const TRIAGE_ONLY_PREFIXES = ["bug_report:", "bug_report_reply:", "bug_reports:", "ux_issues:"] as const;export type Job = {  readonly id: string;  readonly kind: JobKind;  readonly key: string;  readonly alertKey: Option<string>;  readonly incident: Option<number>;  readonly rank: number;  readonly event: Record<string, unknown>;  readonly queuedAt: number;};type AlertClass = "critical" | "page" | "ticket" | "direct";const isClass = (s: string): s is AlertClass => s === "critical" || s === "page" || s === "ticket" || s === "direct";// The queue order of an alert class: critical first, then pages, then tickets. An unknown class// ranks as a ticket. Written the natural way, with the arms inline (allocated per call).export const rankOf = (cls: string): number =>  isClass(cls) ? matchLiteral(cls, { critical: () => 0, page: () => 1, ticket: () => 2, direct: () => 3 }) : 2;// The same order as an exhaustive switch (TSUS501 checks it covers every class): what a hot path// should use, since a match call allocates its arms.export const rankOfSwitch = (cls: string): number => {  if (!isClass(cls)) return 2;  switch (cls) {    case "critical":      return 0;    case "page":      return 1;    case "ticket":      return 2;    case "direct":      return 3;  }};const isRecord = (v: unknown): v is Record<string, unknown> => typeof v === "object" && v !== null && !Array.isArray(v);// An event's data column: a JSON object, or {} for anything else.export const eventData = (raw: Option<string>): Record<string, unknown> => {  if (raw === undefined || raw === "") return {};  const parsed = tryCatch((): unknown => JSON.parse(raw));  return parsed.ok && isRecord(parsed.value) ? parsed.value : {};};// The API's columns are null or a string; an empty string counts as absent, as it always has.const present = (s: Option<string>): Option<string> => (s === "" ? undefined : s);const watched = (environment: Option<string>, environments: readonly string[]): boolean =>  environment === undefined || environments.includes(environment);const ownAlert = (key: string): boolean => key.startsWith("oncall:") && !key.startsWith("oncall:drill:");const triageOnly = (key: string): boolean => TRIAGE_ONLY_PREFIXES.some((p) => key.startsWith(p));export const jobFor = (e: ApiEvent, cfg: WatchConfig, now: number): Option<Job> => jobWith(rankOf, e, cfg, now);export const jobForSwitch = (e: ApiEvent, cfg: WatchConfig, now: number): Option<Job> => jobWith(rankOfSwitch, e, cfg, now);const jobWith = (rank: (cls: string) => number, e: ApiEvent, cfg: WatchConfig, now: number): Option<Job> => {  if (!watched(present(fromNullable(e.environment)), cfg.environments)) return undefined;  const data = eventData(fromNullable(e.data));  const key = present(fromNullable(e.key));  if ((e.kind === "alert_opened" || e.kind === "alert_severity_rose") && key !== undefined) {    if (ownAlert(key) || triageOnly(key)) return undefined;    const cls = typeof data.class === "string" ? data.class : "ticket";    if (cls === "direct") return undefined;    return { id: `e${e.id}`, kind: "incident", key, alertKey: key, incident: e.kind === "alert_opened" ? e.at : undefined, rank: rank(cls), event: { ...e, data }, queuedAt: now };  }  if (e.kind === "abuse_flag") {    const subject = typeof data.subject_id === "string" ? data.subject_id : String(e.id);    return { id: `e${e.id}`, kind: "incident", key: `abuse:${subject}`, alertKey: undefined, incident: undefined, rank: 2, event: { ...e, data }, queuedAt: now };  }  return undefined;};// A job without an incident time, or a handled run without one, is the same incident; otherwise an// incident that opened after the key was handled is a new one.const sameIncident = (incident: Option<number>, handled: HandledEntry): boolean =>  incident === undefined || handled.incident === null || incident <= handled.at;export const isDuplicate = (handled: Option<HandledEntry>, incident: Option<number>, now: number, dedupeHours: number): boolean =>  isSome(handled) && now - handled.at < dedupeHours * HOUR && sameIncident(incident, handled);