Skip to content

The platform for continuously improving AI agents.

4.2 KB
Raw Blame
import { isMessage, type JsonObject, type JsonValue, toJson } from "@bufbuild/protobuf";
import {
  type ListValue,
  ListValueSchema,
  type Struct,
  StructSchema,
  type Value,
  ValueSchema,
} from "@bufbuild/protobuf/wkt";

import type { MetricPoint, Span } from "@/lib/otel-rpc/gen/v1/trace_pb";

const KINDS = ["unspecified", "internal", "server", "client", "producer", "consumer"];

export function formatTime(ns: bigint): string {
  return formatTimeMs(Number(ns / 1_000_000n));
}

export function formatTimeMs(ms: number | bigint): string {
  const value = typeof ms === "bigint" ? Number(ms) : ms;
  if (!Number.isFinite(value) || value <= 0) return "—";
  return new Intl.DateTimeFormat(undefined, {
    day: "2-digit",
    hour: "2-digit",
    minute: "2-digit",
    month: "short",
    second: "2-digit",
  }).format(value);
}

export function formatDuration(ns: bigint): string {
  const value = Number(ns);
  if (!Number.isFinite(value)) return "—";
  if (value < 1_000) return `${value}ns`;
  if (value < 1_000_000) return `${trim(value / 1_000)}µs`;
  if (value < 1_000_000_000) return `${trim(value / 1_000_000)}ms`;
  return `${trim(value / 1_000_000_000)}s`;
}

export function formatTokens(tokens: bigint | undefined): string {
  if (tokens === undefined) return "—";
  return new Intl.NumberFormat().format(tokens);
}

export function formatCost(cost: number | undefined): string {
  if (cost === undefined) return "—";
  if (cost === 0) return "$0";
  if (Math.abs(cost) < 0.01) return `$${cost.toFixed(6)}`;
  return `$${cost.toFixed(4)}`;
}

export function spanKind(kind: number): string {
  return KINDS[kind] ?? "unspecified";
}

export function serviceHue(name: string): number {
  let hash = 0;
  for (const char of name) hash = (hash * 33 + char.charCodeAt(0)) % 360;
  return hash;
}

export function listJson(value: ListValue | undefined): JsonValue[] {
  const events = value as unknown;
  if (!events) return [];
  if (Array.isArray(events)) return events as JsonValue[];
  if (isMessage(events, ListValueSchema)) return toJson(ListValueSchema, events);
  return [];
}

export function valueJson(value: Value | undefined): JsonValue | undefined {
  const raw = value as unknown;
  if (raw === undefined || raw === null) return undefined;
  if (isMessage(raw, ValueSchema)) return toJson(ValueSchema, raw);
  return raw as JsonValue;
}

export function structJson(value: Struct | JsonObject | undefined): JsonObject {
  const raw = value as unknown;
  if (!raw || typeof raw !== "object") return {};
  if (isMessage(raw, StructSchema)) return toJson(StructSchema, raw);
  return raw as JsonObject;
}

export function formatJsonText(value: JsonValue | undefined): string {
  if (value === undefined || value === null) return "";
  if (typeof value === "string") return value;
  if (typeof value === "number" || typeof value === "boolean") return String(value);
  return JSON.stringify(value, null, 2);
}

export function traceBounds(spans: Span[]): { start: bigint; end: bigint } {
  let start = spans[0]?.startTimeNs ?? 0n;
  let end = spans[0]?.endTimeNs ?? 0n;
  for (const span of spans) {
    if (span.startTimeNs < start) start = span.startTimeNs;
    if (span.endTimeNs > end) end = span.endTimeNs;
  }
  return { end, start };
}

export function percent(part: bigint, whole: bigint): number {
  if (whole <= 0n) return 0;
  return Number((part * 10_000n) / whole) / 100;
}

export function formatMetricValue(point: MetricPoint): string {
  const unit = point.unit && point.unit !== "1" ? ` ${point.unit}` : "";
  if (point.intValue !== undefined) return `${point.intValue.toLocaleString()}${unit}`;
  if (point.doubleValue !== undefined) return `${Number(point.doubleValue.toFixed(4))}${unit}`;
  if (point.count !== undefined && point.sum !== undefined) {
    return `n=${point.count} sum=${Number(point.sum.toFixed(3))}${unit}`;
  }
  if (point.count !== undefined) return `n=${point.count}`;
  return "—";
}

export function severityVariant(number: number): "error" | "warning" | "info" | "neutral" {
  if (number >= 17) return "error";
  if (number >= 13) return "warning";
  if (number >= 9) return "info";
  return "neutral";
}

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