forked from op7418/CodePilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsageStatsSection.tsx
More file actions
449 lines (405 loc) · 14.1 KB
/
UsageStatsSection.tsx
File metadata and controls
449 lines (405 loc) · 14.1 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
"use client";
import { useState, useEffect, useCallback, useRef } from "react";
import { getLocalDateString } from "@/lib/utils";
import {
BarChart,
Bar,
XAxis,
YAxis,
CartesianGrid,
Tooltip,
Legend,
ResponsiveContainer,
} from "recharts";
import { Button } from "@/components/ui/button";
import { useTranslation } from "@/hooks/useTranslation";
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
interface UsageStatsResponse {
summary: {
total_input_tokens: number;
total_output_tokens: number;
total_cost: number;
total_sessions: number;
cache_read_tokens: number;
cache_creation_tokens: number;
};
daily: Array<{
date: string;
model: string;
input_tokens: number;
output_tokens: number;
cost: number;
}>;
}
// Recharts v3 Payload — only the fields we actually read
interface RechartsPayloadItem {
name?: string | number;
dataKey?: string | number;
value?: number;
color?: string;
fill?: string;
payload?: Record<string, unknown>;
}
// ---------------------------------------------------------------------------
// Number formatting helpers
// ---------------------------------------------------------------------------
/**
* Format a token count for display.
* 0 → "0"
* 999 → "999"
* 1_234 → "1,234"
* 9_999 → "9,999"
* 10_000 → "10.0K"
* 1_234_567 → "1.23M"
* 1_200_000_000 → "1.20B"
*/
function formatTokens(n: number): string {
if (n === 0) return "0";
if (n < 0) return "-" + formatTokens(-n);
if (n < 10_000) return n.toLocaleString("en-US");
if (n < 1_000_000) return (n / 1_000).toFixed(1) + "K";
if (n < 1_000_000_000) return (n / 1_000_000).toFixed(2) + "M";
return (n / 1_000_000_000).toFixed(2) + "B";
}
/**
* Format a USD cost for display.
* 0 → "$0.00"
* 0.00015 → "$0.0002"
* 0.0052 → "$0.0052"
* 0.12 → "$0.12"
* 1.5 → "$1.50"
* 1234.5 → "$1,234.50"
*/
function formatCost(n: number): string {
if (n === 0) return "$0.00";
if (n < 0.01) return "$" + n.toFixed(4);
if (n >= 1000) return "$" + n.toLocaleString("en-US", { minimumFractionDigits: 2, maximumFractionDigits: 2 });
return "$" + n.toFixed(2);
}
/**
* Format a percentage.
* NaN / undefined → "N/A"
* 0 → "0%"
* 0.456 → "0.5%"
* 12.345 → "12.3%"
* 100 → "100%"
*/
function formatPercent(n: number | undefined): string {
if (n === undefined || isNaN(n)) return "N/A";
if (n === 0) return "0%";
if (n === 100) return "100%";
return n.toFixed(1) + "%";
}
/** Short date for chart x-axis: "2/24" */
function shortDate(dateStr: string): string {
const d = new Date(dateStr + "T00:00:00");
return `${d.getMonth() + 1}/${d.getDate()}`;
}
// ---------------------------------------------------------------------------
// Stable model → color mapping
// ---------------------------------------------------------------------------
const COLOR_PALETTE = [
"var(--color-chart-1)",
"var(--color-chart-2)",
"var(--color-chart-3)",
"var(--color-chart-4)",
"var(--color-chart-5)",
];
/**
* Assign a stable color to each unique model key (e.g. "sonnet", "Kimi/sonnet").
* Uses the index in the sorted model list so each distinct key gets its own color.
*/
function getModelColor(_model: string, idx: number): string {
return COLOR_PALETTE[idx % COLOR_PALETTE.length];
}
// ---------------------------------------------------------------------------
// Chart tooltip (recharts v3 compatible)
// ---------------------------------------------------------------------------
function ChartTooltip({ active, payload, label }: {
active?: boolean;
payload?: ReadonlyArray<RechartsPayloadItem>;
label?: string | number;
}) {
if (!active || !payload?.length) return null;
// Filter out entries with zero or missing values
const items = payload.filter((p) => typeof p.value === "number" && p.value > 0);
if (items.length === 0) return null;
return (
<div className="rounded-lg border border-border/50 bg-popover px-3 py-2 text-xs shadow-md">
<p className="mb-1 font-medium text-popover-foreground">{label}</p>
{items.map((p, i) => {
const displayName = String(p.name ?? p.dataKey ?? "unknown");
const displayColor = p.color || p.fill || "var(--color-chart-1)";
return (
<div key={displayName + i} className="flex items-center gap-2 text-popover-foreground/80">
<span
className="inline-block h-2 w-2 shrink-0 rounded-full"
style={{ backgroundColor: displayColor }}
/>
<span>{displayName}</span>
<span className="ml-auto font-mono">{formatTokens(p.value ?? 0)}</span>
</div>
);
})}
</div>
);
}
// ---------------------------------------------------------------------------
// Day range selector
// ---------------------------------------------------------------------------
const RANGE_OPTIONS = [
{ label: "7D", days: 7 },
{ label: "30D", days: 30 },
{ label: "90D", days: 90 },
] as const;
// ---------------------------------------------------------------------------
// Main component
// ---------------------------------------------------------------------------
export function UsageStatsSection() {
const { t } = useTranslation();
const [days, setDays] = useState(30);
const [data, setData] = useState<UsageStatsResponse | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const abortRef = useRef<AbortController | null>(null);
const fetchStats = useCallback(async (d: number) => {
// Abort any in-flight request to avoid stale data overwriting fresh data
abortRef.current?.abort();
const controller = new AbortController();
abortRef.current = controller;
setLoading(true);
setError(null);
try {
const res = await fetch(`/api/usage/stats?days=${d}`, { signal: controller.signal });
if (!res.ok) {
const body = await res.json().catch(() => ({}));
throw new Error(body.error || `HTTP ${res.status}`);
}
setData(await res.json());
} catch (err) {
if (err instanceof DOMException && err.name === 'AbortError') return;
setError(err instanceof Error ? err.message : String(t('usage.loadError')));
} finally {
if (!controller.signal.aborted) {
setLoading(false);
}
}
}, [t]);
useEffect(() => {
fetchStats(days);
return () => abortRef.current?.abort();
}, [days, fetchStats]);
// Derive chart data: pivot daily rows into { date, model1: N, model2: N, ... }
const { chartData, models } = deriveChartData(data?.daily ?? [], days);
const summary = data?.summary;
const totalTokens = summary
? summary.total_input_tokens + summary.total_output_tokens
: 0;
const cacheTotal = summary
? summary.cache_read_tokens + summary.total_input_tokens
: 0;
const cacheRate = summary && cacheTotal > 0
? (summary.cache_read_tokens / cacheTotal) * 100
: undefined;
return (
<div className="max-w-3xl space-y-6">
{/* Day range selector */}
<div className="flex items-center gap-2">
{RANGE_OPTIONS.map((opt) => (
<Button
key={opt.days}
variant={days === opt.days ? "default" : "secondary"}
size="sm"
onClick={() => setDays(opt.days)}
className={`px-3 py-1 text-xs font-medium ${
days !== opt.days ? "text-muted-foreground hover:bg-accent" : ""
}`}
>
{opt.label}
</Button>
))}
</div>
{/* Data cards */}
<div className="grid grid-cols-2 gap-3 sm:grid-cols-4">
<StatCard
label={t('usage.totalTokens')}
value={loading ? "–" : formatTokens(totalTokens)}
sub={
summary
? `${t('usage.input')} ${formatTokens(summary.total_input_tokens)} · ${t('usage.output')} ${formatTokens(summary.total_output_tokens)}`
: undefined
}
/>
<StatCard
label={t('usage.totalCost')}
value={loading ? "–" : formatCost(summary?.total_cost ?? 0)}
/>
<StatCard
label={t('usage.sessions')}
value={loading ? "–" : String(summary?.total_sessions ?? 0)}
/>
<StatCard
label={t('usage.cacheHitRate')}
value={loading ? "–" : formatPercent(cacheRate)}
sub={
summary && summary.cache_read_tokens > 0
? `${formatTokens(summary.cache_read_tokens)} ${t('usage.cached')}`
: undefined
}
/>
</div>
{/* Bar chart */}
<div className="rounded-lg border border-border/50 p-4">
<h3 className="mb-4 text-sm font-medium">{t('usage.dailyChart')}</h3>
{loading && (
<div className="flex h-64 items-center justify-center text-sm text-muted-foreground">
{t('usage.loading')}
</div>
)}
{error && (
<div className="flex h-64 items-center justify-center text-sm text-status-error-foreground">
{error}
</div>
)}
{!loading && !error && chartData.length === 0 && (
<div className="flex h-64 flex-col items-center justify-center gap-2 text-muted-foreground">
<svg
xmlns="http://www.w3.org/2000/svg"
width="32"
height="32"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
className="opacity-40"
>
<path d="M18 20V10" />
<path d="M12 20V4" />
<path d="M6 20v-6" />
</svg>
<p className="text-sm">{t('usage.noData')}</p>
<p className="text-xs">{t('usage.noDataHint')}</p>
</div>
)}
{!loading && !error && chartData.length > 0 && (
<ResponsiveContainer width="100%" height={280}>
<BarChart data={chartData} margin={{ top: 4, right: 4, left: -8, bottom: 0 }}>
<CartesianGrid
strokeDasharray="3 3"
vertical={false}
stroke="var(--color-border)"
opacity={0.5}
/>
<XAxis
dataKey="date"
tick={{ fontSize: 11, fill: "var(--color-muted-foreground)" }}
tickLine={false}
axisLine={false}
/>
<YAxis
tickFormatter={formatTokens}
tick={{ fontSize: 11, fill: "var(--color-muted-foreground)" }}
tickLine={false}
axisLine={false}
width={54}
/>
<Tooltip
content={(props) => <ChartTooltip {...props} />}
cursor={{ fill: "var(--color-accent)", opacity: 0.3 }}
/>
<Legend
wrapperStyle={{ fontSize: 11, paddingTop: 8 }}
iconType="circle"
iconSize={8}
/>
{models.map((model, idx) => (
<Bar
key={model}
name={model}
dataKey={model}
stackId="tokens"
fill={getModelColor(model, idx)}
radius={idx === models.length - 1 ? [3, 3, 0, 0] : [0, 0, 0, 0]}
maxBarSize={40}
/>
))}
</BarChart>
</ResponsiveContainer>
)}
</div>
</div>
);
}
// ---------------------------------------------------------------------------
// Sub-components
// ---------------------------------------------------------------------------
function StatCard({
label,
value,
sub,
}: {
label: string;
value: string;
sub?: string;
}) {
return (
<div className="rounded-lg border border-border/50 p-4 transition-shadow hover:shadow-sm">
<p className="text-xs text-muted-foreground">{label}</p>
<p className="mt-1 text-xl font-semibold tabular-nums">{value}</p>
{sub && <p className="mt-0.5 text-[11px] text-muted-foreground">{sub}</p>}
</div>
);
}
// ---------------------------------------------------------------------------
// Data transform: pivot daily array into chart-friendly format
// ---------------------------------------------------------------------------
function deriveChartData(daily: UsageStatsResponse["daily"], days: number): {
chartData: Array<Record<string, string | number>>;
models: string[];
} {
if (daily.length === 0) return { chartData: [], models: [] };
// Normalise model names: empty string → "unknown"
const normalised = daily.map((row) => ({
...row,
model: row.model || "unknown",
}));
// Collect all unique models
const modelSet = new Set<string>();
for (const row of normalised) {
modelSet.add(row.model);
}
const models = Array.from(modelSet).sort();
// Group by date
const byDate = new Map<string, Record<string, number>>();
for (const row of normalised) {
const total = row.input_tokens + row.output_tokens;
if (!byDate.has(row.date)) {
byDate.set(row.date, {});
}
const entry = byDate.get(row.date)!;
entry[row.model] = (entry[row.model] || 0) + total;
}
// Build a continuous date range covering the full window so x-axis has no gaps
const allDates: string[] = [];
const today = new Date();
today.setHours(0, 0, 0, 0);
for (let i = days - 1; i >= 0; i--) {
const d = new Date(today);
d.setDate(d.getDate() - i);
allDates.push(getLocalDateString(d));
}
// Format for recharts, filling missing dates with zeros
const chartData = allDates.map((date) => {
const modelTokens = byDate.get(date) || {};
const row: Record<string, string | number> = { date: shortDate(date) };
for (const m of models) {
row[m] = modelTokens[m] || 0;
}
return row;
});
return { chartData, models };
}