'use client' import { useEffect, useState } from 'react' import { ChevronLeft, ChevronRight, RefreshCw } from 'lucide-react' import { useRouter, useSearchParams } from 'next/navigation' import { useTranslation } from 'react-i18next' import { SettingsPageHeader } from '@/components/settings/shared' import { fetchUsageStatistics, usageYear, type UsageStatistics } from '@/lib/usage-statistics' import UsageActivity from '../components/UsageActivity' import { isApiError } from '@/shared/api/errors' export default function UsageSettingsSection() { const { t, i18n } = useTranslation() const router = useRouter() const search = useSearchParams() const currentYear = new Date().getFullYear() const year = usageYear(search.get('year'), currentYear) const [snapshot, setSnapshot] = useState<{ key: string data: UsageStatistics | null error: string | null } | null>(null) const [revision, setRevision] = useState(0) const requestKey = `${year}:${revision}` const data = snapshot?.data const loading = snapshot?.key !== requestKey const error = !loading && Boolean(snapshot?.error) useEffect(() => { const controller = new AbortController() fetchUsageStatistics(year, controller.signal) .then(value => { if (!controller.signal.aborted) setSnapshot({ key: requestKey, data: value, error: null }) }) .catch((failure: unknown) => { if (!controller.signal.aborted) setSnapshot(previous => ({ key: requestKey, data: previous?.data ?? null, error: failure instanceof Error ? `${failure.message}${isApiError(failure) && failure.status ? ` (HTTP ${failure.status})` : ''}` : 'Unable to load usage statistics.', })) }) return () => controller.abort() }, [year, requestKey]) const changeYear = (next: number) => { const query = new URLSearchParams(search.toString()) query.set('year', String(next)) router.push(`/settings/usage?${query}`, { scroll: false }) } const n = (value?: number | null) => (value == null ? '—' : value.toLocaleString(i18n.language)) const percent = (value?: number | null) => (value == null ? '—' : `${(value * 100).toFixed(1)}%`) const visible = data?.year === year ? data : null const stats = visible?.totals const models = visible?.models.filter(model => model.model) ?? [] const historical = visible?.models.filter(model => !model.model) ?? [] return (
{year}
{error && (
{t('Unable to load usage statistics.')} {snapshot?.error && {t(snapshot.error)}}
)} {loading && !visible && (
{t('Loading usage…')}
)} {visible && (
{[ [t('Total tokens'), visible.tracked_turns ? n(stats?.total_tokens) : '—'], [t('Input tokens'), visible.tracked_turns ? n(stats?.prompt_tokens) : '—'], [t('Output tokens'), visible.tracked_turns ? n(stats?.completion_tokens) : '—'], [t('Cache hit rate'), percent(stats?.cache_hit_rate)], ].map(([label, value]) => (
{label}
{value}
))}

{t('Usage by model')}

{t('{{calls}} LLM calls · {{sessions}} sessions', { calls: n(stats?.total_calls), sessions: n(visible.sessions), })}
{models.length ? (
{[ t('Model'), t('Total tokens'), t('Input tokens'), t('Output tokens'), t('Cache hit rate'), t('LLM calls'), t('TTFT'), t('TPS'), ].map((label, index) => ( ))} {models.map(model => ( ))}
{label}
{model.model} {model.provider && ( {model.provider} )} {n(model.total_tokens)} {n(model.prompt_tokens)} {n(model.completion_tokens)} {percent(model.cache_hit_rate)} {n(model.total_calls)} {model.ttft_seconds == null ? '—' : t('{{value}} s', { value: model.ttft_seconds.toFixed(2) })} {model.tokens_per_second == null ? '—' : t('{{value}} tok/s', { value: model.tokens_per_second.toFixed(1) })}
) : (
{t('No recorded model usage for this year.')}
)} {historical.length > 0 && (
{t('Historical usage without recoverable model details')} {n(historical.reduce((sum, model) => sum + model.total_tokens, 0))} tokens
)}
)}
) }