Functional Weave
Code in Python

monitor.status-page@1.0.0

impl/typescript.ts

1,489 bytes · the TypeScript implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

import { type ComponentState } from "./monitor_component_state.ts";  ← from monitor.component-state ^1.0.0 · built alongside by fune
import { type Component, type StatusSummary } from "./monitor_status_page_types.ts";

// Worst first. Maintenance ranks below every real problem: an outage elsewhere
// during planned work is still the headline.
const RANK: readonly ComponentState[] = ["operational", "maintenance", "degraded", "partial-outage", "major-outage"];
const HEADLINE: Readonly<Record<ComponentState, string>> = {
  "operational": "All systems operational",
  "maintenance": "Scheduled maintenance",
  "degraded": "Degraded performance",
  "partial-outage": "Partial outage",
  "major-outage": "Major outage",
};

/** The banner for a whole status page: the worst component state wins. */
export function statusPage(components: readonly Component[]): StatusSummary {
  if (components.length === 0) throw new RangeError("components must not be empty");
  const seen = new Set<string>();
  let worst = 0;
  const affected: string[] = [];
  for (const c of components) {
    if (c.name === "") throw new RangeError("component name must not be empty");
    if (seen.has(c.name)) throw new RangeError(`duplicate component name: ${c.name}`);
    seen.add(c.name);
    const rank = RANK.indexOf(c.state);
    if (rank < 0) throw new RangeError(`unknown component state: ${String(c.state)}`);
    if (rank > worst) worst = rank;
    if (rank > 0) affected.push(c.name);
  }
  return { state: RANK[worst], headline: HEADLINE[RANK[worst]], affected };
}