import React, { useState, useEffect } from 'react';
import { BarChart2, RefreshCw, Zap } from 'lucide-react';
import { UsageService } from '../services/api';
import { useAppContext } from './AppContext';
const TYPE_LABELS = {
plan: 'Plan Generation',
execute_step: 'Step Execution',
analyze: 'Response Analysis',
chat: 'Chat Assistant',
};
const TYPE_COLORS = {
plan: 'var(--accent)',
execute_step: '#22c55e',
analyze: '#f59e0b',
chat: '#a855f7',
};
export default function UsagePage() {
const { sessionId } = useAppContext();
const [usage, setUsage] = useState(null);
const [loading, setLoading] = useState(true);
const [filter, setFilter] = useState('all');
const loadUsage = async () => {
setLoading(true);
try {
const params = filter === 'session' && sessionId ? { session_id: sessionId } : {};
const data = await UsageService.getUsage(params);
setUsage(data);
} catch (err) {
console.error('Failed to load usage', err);
} finally {
setLoading(false);
}
};
useEffect(() => { loadUsage(); }, [filter]);
const maxTokens = usage?.breakdown?.reduce((m, r) => Math.max(m, r.total_tokens), 1) || 1;
return (
{/* Header */}
{['all', 'session'].map(f => (
))}
{loading ? (
) : !usage ? (
Failed to load usage data.
) : (
<>
{/* Totals */}
{[
{ label: 'Prompt tokens', value: usage.totals.prompt_tokens, color: 'var(--fg-2)' },
{ label: 'Completion tokens', value: usage.totals.completion_tokens, color: 'var(--fg-2)' },
{ label: 'Total tokens', value: usage.totals.total_tokens, color: 'var(--accent)' },
].map(({ label, value, color }) => (
{label}
{(value || 0).toLocaleString()}
))}
{/* Breakdown */}
By call type
{usage.breakdown.length === 0 ? (
No usage recorded yet. Run your first API test to see data here.
) : (
usage.breakdown
.sort((a, b) => b.total_tokens - a.total_tokens)
.map(row => {
const barPct = Math.round((row.total_tokens / maxTokens) * 100);
const color = TYPE_COLORS[row.call_type] || 'var(--fg-2)';
return (
{TYPE_LABELS[row.call_type] || row.call_type}
{row.calls} call{row.calls !== 1 ? 's' : ''}
{(row.total_tokens || 0).toLocaleString()} tok
Prompt: {(row.prompt_tokens || 0).toLocaleString()}
Completion: {(row.completion_tokens || 0).toLocaleString()}
);
})
)}
>
)}
);
}