Skip to content

The platform for continuously improving AI agents.

4.2 KB
Raw Blame
import type { MetricsRange } from "@/lib/otel-rpc/queries";

export const CHART_COLORS = [
  "var(--foreground)",
  "var(--foreground)",
  "var(--foreground)",
  "var(--foreground)",
  "var(--foreground)",
  "var(--foreground)",
  "var(--foreground)",
  "var(--foreground)",
] as const;

export const METRICS_RANGES: { value: MetricsRange; label: string }[] = [
  { label: "1h", value: "1h" },
  { label: "6h", value: "6h" },
  { label: "24h", value: "24h" },
  { label: "7d", value: "7d" },
  { label: "30d", value: "30d" },
  { label: "All", value: "all" },
];

export function formatCompact(value: number | bigint | undefined): string {
  if (value === undefined) return "—";
  const n = typeof value === "bigint" ? Number(value) : value;
  if (!Number.isFinite(n)) return "—";
  const abs = Math.abs(n);
  if (abs >= 1_000_000_000) return `${trim(n / 1_000_000_000)}B`;
  if (abs >= 1_000_000) return `${trim(n / 1_000_000)}M`;
  if (abs >= 1_000) return `${trim(n / 1_000)}K`;
  if (Number.isInteger(n)) return n.toLocaleString();
  return trim(n);
}

export function formatDurationMs(ms: number | undefined): string {
  if (ms === undefined || !Number.isFinite(ms)) return "—";
  if (ms === 0) return "0ms";
  if (ms < 1) return `${trim(ms * 1000)}µs`;
  if (ms < 1000) return `${trim(ms)}ms`;
  if (ms < 60_000) return `${trim(ms / 1000)}s`;
  return `${trim(ms / 60_000)}m`;
}

export function formatRatePercent(rate: number | undefined): string {
  if (rate === undefined || !Number.isFinite(rate)) return "—";
  const pct = rate * 100;
  if (pct === 0) return "0%";
  if (pct < 0.01) return "<0.01%";
  if (pct < 1) return `${pct.toFixed(2)}%`;
  if (pct < 10) return `${pct.toFixed(1)}%`;
  return `${pct.toFixed(0)}%`;
}

export function formatUsd(cost: number | undefined): string {
  if (cost === undefined || !Number.isFinite(cost)) return "—";
  if (cost === 0) return "$0.00";
  const abs = Math.abs(cost);
  if (abs < 0.0001) return `$${cost.toFixed(6)}`;
  if (abs < 0.01) return `$${cost.toFixed(4)}`;
  if (abs < 1) return `$${cost.toFixed(3)}`;
  return `$${cost.toFixed(2)}`;
}

export function formatMetricNumber(value: number | undefined, unit?: string): string {
  if (value === undefined || !Number.isFinite(value)) return "—";
  const suffix = unit && unit !== "1" ? ` ${unit}` : "";
  if (unit === "ms") return formatDurationMs(value);
  if (unit === "s") return formatDurationMs(value * 1000);
  const abs = Math.abs(value);
  if (abs >= 1_000_000) return `${formatCompact(value)}${suffix}`;
  if (abs >= 1_000) return `${Number(value.toFixed(1)).toLocaleString()}${suffix}`;
  if (Number.isInteger(value)) return `${value.toLocaleString()}${suffix}`;
  if (abs < 0.01 && abs > 0) return `${value.toFixed(4)}${suffix}`;
  return `${trim(value)}${suffix}`;
}

export function formatBucketStep(seconds: bigint | number): string {
  const s = typeof seconds === "bigint" ? Number(seconds) : seconds;
  if (!Number.isFinite(s) || s <= 0) return "";
  if (s < 60) return `${s}s`;
  if (s < 3600) return `${Math.round(s / 60)}m`;
  if (s < 86400) return `${trim(s / 3600)}h`;
  return `${trim(s / 86400)}d`;
}

export function formatBucketTick(ms: number, spanMs: number): string {
  if (!Number.isFinite(ms) || ms <= 0) return "";
  const date = new Date(ms);
  if (spanMs <= 24 * 60 * 60 * 1000) {
    return date.toLocaleTimeString(undefined, {
      hour: "2-digit",
      hour12: false,
      minute: "2-digit",
    });
  }
  if (spanMs <= 7 * 24 * 60 * 60 * 1000) {
    return date.toLocaleString(undefined, {
      day: "numeric",
      hour: "2-digit",
      hour12: false,
      month: "short",
    });
  }
  return date.toLocaleDateString(undefined, { day: "numeric", month: "short" });
}

export function formatBucketFull(ms: number): string {
  if (!Number.isFinite(ms) || ms <= 0) return "—";
  return new Date(ms).toLocaleString(undefined, {
    day: "2-digit",
    hour: "2-digit",
    hour12: false,
    minute: "2-digit",
    month: "short",
    second: "2-digit",
  });
}

export function nsToMs(ns: bigint): number {
  return Number(ns / 1_000_000n);
}

function trim(value: number): string {
  if (Math.abs(value) >= 100) return value.toFixed(0);
  return value.toFixed(2).replace(/0+$/, "").replace(/\.$/, "");
}