Spaces:
Runtime error
Runtime error
Download frontend-react/src/components/UsagePage.jsx from Amitkasar/ai-api-testing: direct link, hf CLI and curl.
- Browser
- Download file 6.94 kB
-
https://huggingface.co/spaces/Amitkasar/ai-api-testing/resolve/main/frontend-react/src/components/UsagePage.jsx
- Command line
-
hf download hf://spaces/Amitkasar/ai-api-testing/frontend-react/src/components/UsagePage.jsx
-
curl -L -o UsagePage.jsx https://huggingface.co/spaces/Amitkasar/ai-api-testing/resolve/main/frontend-react/src/components/UsagePage.jsx
6.94 kB
| 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 ( | |
| <div className="page"> | |
| <div className="page-inner"> | |
| {/* Header */} | |
| <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 4 }}> | |
| <div> | |
| <div className="h-eyebrow">Analytics</div> | |
| <h1 style={{ fontSize: 18, fontWeight: 600, color: 'var(--fg-0)', letterSpacing: '-0.01em', margin: '4px 0 0', display: 'flex', alignItems: 'center', gap: 8 }}> | |
| <BarChart2 size={18} style={{ color: 'var(--accent)' }}/> | |
| Token Usage | |
| </h1> | |
| </div> | |
| <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}> | |
| <div style={{ display: 'flex', gap: 4 }}> | |
| {['all', 'session'].map(f => ( | |
| <button | |
| key={f} | |
| onClick={() => setFilter(f)} | |
| className={`btn${filter === f ? ' btn-primary' : ''}`} | |
| style={{ fontSize: 11 }} | |
| > | |
| {f === 'all' ? 'All time' : 'This session'} | |
| </button> | |
| ))} | |
| </div> | |
| <button className="btn" onClick={loadUsage} disabled={loading}> | |
| <RefreshCw size={13} style={loading ? { animation: 'spin 1s linear infinite' } : {}}/> | |
| Refresh | |
| </button> | |
| </div> | |
| </div> | |
| {loading ? ( | |
| <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', minHeight: 200, gap: 12 }}> | |
| <RefreshCw size={22} style={{ color: 'var(--fg-4)', animation: 'spin 1s linear infinite' }}/> | |
| </div> | |
| ) : !usage ? ( | |
| <div style={{ textAlign: 'center', padding: 48, color: 'var(--fg-3)', fontSize: 12 }}>Failed to load usage data.</div> | |
| ) : ( | |
| <> | |
| {/* Totals */} | |
| <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 12, marginTop: 20, marginBottom: 24 }}> | |
| {[ | |
| { 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 }) => ( | |
| <div key={label} style={{ background: 'var(--bg-2)', border: '1px solid var(--line-2)', borderRadius: 8, padding: '14px 16px' }}> | |
| <div style={{ fontSize: 10, textTransform: 'uppercase', letterSpacing: '0.1em', color: 'var(--fg-3)', fontWeight: 700, marginBottom: 6 }}>{label}</div> | |
| <div style={{ fontSize: 22, fontWeight: 700, color, fontVariantNumeric: 'tabular-nums' }}> | |
| {(value || 0).toLocaleString()} | |
| </div> | |
| </div> | |
| ))} | |
| </div> | |
| {/* Breakdown */} | |
| <div style={{ marginBottom: 8 }}> | |
| <span className="h-eyebrow">By call type</span> | |
| </div> | |
| <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}> | |
| {usage.breakdown.length === 0 ? ( | |
| <div style={{ textAlign: 'center', padding: 32, color: 'var(--fg-3)', fontSize: 12 }}> | |
| No usage recorded yet. Run your first API test to see data here. | |
| </div> | |
| ) : ( | |
| 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 ( | |
| <div key={row.call_type} style={{ background: 'var(--bg-2)', border: '1px solid var(--line-2)', borderRadius: 8, padding: '12px 16px' }}> | |
| <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 8 }}> | |
| <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}> | |
| <Zap size={13} style={{ color }}/> | |
| <span style={{ fontSize: 13, fontWeight: 600, color: 'var(--fg-0)' }}> | |
| {TYPE_LABELS[row.call_type] || row.call_type} | |
| </span> | |
| <span style={{ fontSize: 11, color: 'var(--fg-3)', background: 'var(--bg-3)', padding: '1px 6px', borderRadius: 3 }}> | |
| {row.calls} call{row.calls !== 1 ? 's' : ''} | |
| </span> | |
| </div> | |
| <span style={{ fontSize: 13, fontWeight: 700, color, fontVariantNumeric: 'tabular-nums' }}> | |
| {(row.total_tokens || 0).toLocaleString()} tok | |
| </span> | |
| </div> | |
| <div style={{ height: 5, background: 'var(--line-1)', borderRadius: 99, overflow: 'hidden' }}> | |
| <div style={{ height: '100%', width: `${barPct}%`, background: color, borderRadius: 99, transition: 'width 500ms ease' }}/> | |
| </div> | |
| <div style={{ display: 'flex', gap: 16, marginTop: 6, fontSize: 11, color: 'var(--fg-3)' }}> | |
| <span>Prompt: {(row.prompt_tokens || 0).toLocaleString()}</span> | |
| <span>Completion: {(row.completion_tokens || 0).toLocaleString()}</span> | |
| </div> | |
| </div> | |
| ); | |
| }) | |
| )} | |
| </div> | |
| </> | |
| )} | |
| </div> | |
| </div> | |
| ); | |
| } | |