File size: 6,936 Bytes
1e24842
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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>
  );
}