import { useTranslation } from "react-i18next"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip"; import type { SettingsPayload } from "@/lib/types"; import { cn } from "@/lib/utils"; type Usage = NonNullable; const COLORS = ["bg-orange-500", "bg-amber-500", "bg-stone-500", "bg-orange-300", "bg-zinc-400", "bg-neutral-200 dark:bg-neutral-600"]; export function TokenUsageModelTrend({ days, modelDays, models }: { days: { date: string; usage?: Usage["days"][number] }[]; modelDays: NonNullable; models?: Usage["providers_30d"]; }) { const { t, i18n } = useTranslation(); const number = new Intl.NumberFormat(i18n.language); const percent = new Intl.NumberFormat(i18n.language, { style: "percent", maximumFractionDigits: 1 }); const total = days.reduce((sum, day) => sum + (day.usage?.total_tokens ?? 0), 0); const modelUsage = new Map((models ?? []).map(model => [JSON.stringify([model.provider, model.model]), model])); const compact = new Intl.NumberFormat(i18n.language, { notation: "compact", maximumFractionDigits: 1 }); const dates = new Set(days.map(day => day.date)); const totals = new Map(); const daily = new Map>(); for (const row of modelDays) { if (!dates.has(row.date) || row.total_tokens <= 0) continue; const key = JSON.stringify([row.provider, row.model]); const item = totals.get(key) ?? { label: row.model, total: 0 }; item.total += row.total_tokens; totals.set(key, item); const bucket = daily.get(row.date) ?? new Map(); bucket.set(key, (bucket.get(key) ?? 0) + row.total_tokens); daily.set(row.date, bucket); } const series = [...totals].sort((a, b) => b[1].total - a[1].total).slice(0, 5); const otherLabel = t("settings.usage.otherModels", { defaultValue: "Other / unattributed" }); const columns = days.map(day => { const bucket = daily.get(day.date); const values = series.map(([key]) => bucket?.get(key) ?? 0); const total = day.usage?.total_tokens ?? 0; return { date: day.date, total, requests: day.usage?.requests ?? 0, values: [...values, Math.max(0, total - values.reduce((a, b) => a + b, 0))] }; }); const peak = Math.max(0, ...columns.map(column => column.total)); if (!peak || !series.length) return null; const labels = [...series.map(([, item]) => item.label), otherLabel]; const color = (index: number) => index === series.length ? COLORS[5] : COLORS[index]; const hasOther = columns.some(column => column.values[series.length] > 0); const title = t("settings.usage.modelTrend", { defaultValue: "Model usage over time" }); return

{title}

{[peak, peak / 2, 0].map((value, index) =>
{compact.format(value)}
)}
{columns.map(column => column.total === 0 ? ( ) : `${labels[index]}: ${number.format(value)}`).join(", ")}`} className="flex min-w-0 items-end focus-visible:outline focus-visible:outline-2 focus-visible:outline-orange-500"> {column.values.map((value, index) => value > 0 && )}

{column.date}

{column.values.map((value, index) => ({ value, index })).filter(item => item.value > 0).sort((a, b) => b.value - a.value).map(({ value, index }) =>
{labels[index]}
{number.format(value)}
)}
{t("settings.usage.totalTokens", { defaultValue: "Total tokens" })}
{number.format(column.total)}
{t("settings.usage.requests")}
{number.format(column.requests)}
)}
{days[0].date.slice(5)}{days[14].date.slice(5)}{days[days.length - 1].date.slice(5)}
{labels.map((label, index) => { if (index === series.length && !hasOther) return null; const model = index < series.length ? modelUsage.get(series[index][0]) : undefined; const tokens = columns.reduce((sum, column) => sum + column.values[index], 0); const rate = model?.cache_read_observed_input_tokens ? percent.format(model.cache_read_tokens / model.cache_read_observed_input_tokens) : "—"; const parts = [ [t("settings.usage.totalTokens"), `${number.format(tokens)} (${percent.format(tokens / total)})`], [t("settings.usage.cacheHitRate"), rate], ]; return `${name}: ${value}`).join(", ")}`} className="flex min-w-0 items-center gap-1.5 rounded-sm focus-visible:outline focus-visible:outline-2 focus-visible:outline-orange-500"> {label}

{label}

{parts.map(([name, value]) =>
{name}
{value}
)}
; })}
; }