Functional Weave
Code in TypeScript

monitor.status-code-summary@1.0.0

impl/typescript.ts

1,063 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 { roundDiv } from "./math_round_div.ts";  ← from math.round-div ^1.0.0 · built alongside by fune
import { type StatusCodeSummary } from "./monitor_status_code_summary_types.ts";

/**
 * HTTP status codes counted by class, with the server error rate (5xx) and the
 * client error rate (4xx) kept apart: only 5xx is the server's failure.
 */
export function summariseStatusCodes(statuses: readonly number[]): StatusCodeSummary {
  const counts = [0, 0, 0, 0, 0];
  for (const s of statuses) {
    if (!Number.isInteger(s)) throw new RangeError("status code must be a whole number");
    if (s < 100 || s > 599) throw new RangeError(`status code must be 100 to 599, received ${s}`);
    counts[Math.floor(s / 100) - 1] += 1;
  }
  const total = statuses.length;
  const rate = (n: number): number => (total === 0 ? 0 : roundDiv(n * 10000, total, "half-up"));
  return {
    total,
    informational: counts[0],
    success: counts[1],
    redirect: counts[2],
    clientError: counts[3],
    serverError: counts[4],
    errorRateBasisPoints: rate(counts[4]),
    clientErrorRateBasisPoints: rate(counts[3]),
  };
}