All files / src/components/dashboard MetricsCards.tsx

0% Statements 0/83
0% Branches 0/1
0% Functions 0/1
0% Lines 0/83

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113                                                                                                                                                                                                                                 
import { useMemo } from "react";
import type { DailyBucket, DashboardOverview } from "../../types";
import { Sparkline } from "./Sparkline";
 
type MetricsCardsProps = {
  overview: DashboardOverview;
  daily: DailyBucket[];
};
 
type CardDef = {
  label: string;
  value: string;
  sparkValues: number[];
  accent?: string;
};
 
function formatNumber(n: number): string {
  if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`;
  if (n >= 1_000) return `${(n / 1_000).toFixed(1)}K`;
  return n.toLocaleString();
}
 
function formatCost(usd: number): string {
  if (usd >= 100) return `$${usd.toFixed(0)}`;
  if (usd >= 1) return `$${usd.toFixed(2)}`;
  return `$${usd.toFixed(4)}`;
}
 
function formatDuration(ms: number | null): string {
  if (ms == null) return "--";
  if (ms < 1_000) return `${Math.round(ms)}ms`;
  return `${(ms / 1_000).toFixed(1)}s`;
}
 
export function MetricsCards({ overview, daily }: MetricsCardsProps) {
  const cards = useMemo<CardDef[]>(() => {
    const runsSpark = daily.map((d) => d.runs);
    const tokensSpark = daily.map((d) => d.tokens);
    const costSpark = daily.map((d) => d.cost);
    const errorsSpark = daily.map((d) => d.errors);
 
    return [
      {
        label: "Sessions",
        value: formatNumber(overview.total_sessions),
        sparkValues: runsSpark,
      },
      {
        label: "Runs",
        value: formatNumber(overview.total_runs),
        sparkValues: runsSpark,
      },
      {
        label: "Total Tokens",
        value: formatNumber(
          overview.total_input_tokens + overview.total_output_tokens,
        ),
        sparkValues: tokensSpark,
      },
      {
        label: "Cost",
        value: formatCost(overview.total_cost),
        sparkValues: costSpark,
      },
      {
        label: "API Calls",
        value: formatNumber(overview.total_api_calls),
        sparkValues: runsSpark,
      },
      {
        label: "Tool Calls",
        value: formatNumber(overview.total_tool_calls),
        sparkValues: runsSpark,
      },
      {
        label: "Errors",
        value: formatNumber(overview.total_errors),
        sparkValues: errorsSpark,
        accent: overview.total_errors > 0 ? "var(--color-error)" : undefined,
      },
      {
        label: "Avg Duration",
        value: formatDuration(overview.avg_duration_ms),
        sparkValues: runsSpark,
      },
    ];
  }, [overview, daily]);
 
  return (
    <div className="metrics-grid">
      {cards.map((card) => (
        <div className="metric-card" key={card.label}>
          <div className="metric-card__header">
            <span className="metric-card__label">{card.label}</span>
            <Sparkline
              values={card.sparkValues}
              width={80}
              height={24}
              color={card.accent}
            />
          </div>
          <span
            className="metric-card__value"
            style={card.accent ? { color: card.accent } : undefined}
          >
            {card.value}
          </span>
        </div>
      ))}
    </div>
  );
}