Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | import { useEffect, useState } from 'react'; import { useAgentStore } from '../../store/agentStore'; import { useConnectionStore } from '../../store/connectionStore'; import { BarChart3, TrendingUp, Clock, Zap } from 'lucide-react'; export function UsageStats() { const usageStats = useAgentStore((s) => s.usageStats); const loadUsageStats = useAgentStore((s) => s.loadUsageStats); const connectionState = useConnectionStore((s) => s.connectionState); const [timeRange, setTimeRange] = useState<'7d' | '30d' | 'all'>('7d'); useEffect(() => { if (connectionState === 'connected') { loadUsageStats(); } }, [connectionState]); const stats = usageStats || { totalSessions: 0, totalMessages: 0, totalTokens: 0, byModel: {} }; const models = Object.entries(stats.byModel || {}); const formatTokens = (n: number) => { if (n >= 1_000_000) return `~${(n / 1_000_000).toFixed(1)} M`; if (n >= 1_000) return `~${(n / 1_000).toFixed(1)} k`; return `${n}`; }; // 计算总输入和输出 Token const totalInputTokens = models.reduce((sum, [_, data]) => sum + data.inputTokens, 0); const totalOutputTokens = models.reduce((sum, [_, data]) => sum + data.outputTokens, 0); return ( <div className="max-w-3xl"> <div className="flex justify-between items-center mb-6"> <h1 className="text-xl font-bold text-gray-900">用量统计</h1> <div className="flex items-center gap-2"> <div className="flex items-center bg-gray-100 rounded-lg p-0.5"> {(['7d', '30d', 'all'] as const).map((range) => ( <button key={range} onClick={() => setTimeRange(range)} className={`px-3 py-1 text-xs rounded-md transition-colors ${ timeRange === range ? 'bg-white text-gray-900 shadow-sm' : 'text-gray-500 hover:text-gray-700' }`} > {range === '7d' ? '近 7 天' : range === '30d' ? '近 30 天' : '全部'} </button> ))} </div> <button onClick={() => loadUsageStats()} className="text-xs text-gray-500 hover:text-gray-700 px-3 py-1.5 border border-gray-200 rounded-lg transition-colors" > 刷新 </button> </div> </div> <div className="text-xs text-gray-500 mb-4">本设备所有已保存对话的 Token 用量汇总。</div> {/* 主要统计卡片 */} <div className="grid grid-cols-4 gap-4 mb-8"> <StatCard icon={BarChart3} label="会话数" value={stats.totalSessions} color="text-blue-500" /> <StatCard icon={Zap} label="消息数" value={stats.totalMessages} color="text-purple-500" /> <StatCard icon={TrendingUp} label="输入 Token" value={formatTokens(totalInputTokens)} color="text-green-500" /> <StatCard icon={Clock} label="输出 Token" value={formatTokens(totalOutputTokens)} color="text-orange-500" /> </div> {/* 总 Token 使用量概览 */} <div className="bg-white rounded-xl border border-gray-200 p-5 shadow-sm mb-6"> <h3 className="text-sm font-semibold mb-4 text-gray-900">Token 使用概览</h3> <div className="flex items-center gap-4"> <div className="flex-1"> <div className="flex justify-between text-xs text-gray-500 mb-1"> <span>输入</span> <span>输出</span> </div> <div className="h-3 bg-gray-100 rounded-full overflow-hidden flex"> <div className="bg-gradient-to-r from-green-400 to-green-500 h-full transition-all" style={{ width: `${(totalInputTokens / Math.max(totalInputTokens + totalOutputTokens, 1)) * 100}%` }} /> <div className="bg-gradient-to-r from-orange-400 to-orange-500 h-full transition-all" style={{ width: `${(totalOutputTokens / Math.max(totalInputTokens + totalOutputTokens, 1)) * 100}%` }} /> </div> </div> <div className="text-right flex-shrink-0"> <div className="text-lg font-bold text-gray-900">{formatTokens(stats.totalTokens)}</div> <div className="text-xs text-gray-500">总计</div> </div> </div> </div> {/* 按模型分组 */} <h2 className="text-sm font-semibold mb-4 text-gray-900">按模型</h2> <div className="bg-white rounded-xl border border-gray-200 divide-y divide-gray-100 shadow-sm"> {models.length === 0 ? ( <div className="p-8 text-center"> <div className="w-12 h-12 bg-gray-100 rounded-full flex items-center justify-center mx-auto mb-3"> <BarChart3 className="w-6 h-6 text-gray-400" /> </div> <p className="text-sm text-gray-400">暂无使用数据</p> <p className="text-xs text-gray-300 mt-1">开始对话后将自动记录用量统计</p> </div> ) : ( models.map(([model, data]) => { const total = data.inputTokens + data.outputTokens; const inputPct = (data.inputTokens / Math.max(total, 1)) * 100; const outputPct = (data.outputTokens / Math.max(total, 1)) * 100; return ( <div key={model} className="p-4"> <div className="flex justify-between items-center mb-2"> <span className="font-medium text-gray-900">{model}</span> <span className="text-xs text-gray-500">{data.messages} 条消息</span> </div> <div className="h-2 bg-gray-100 rounded-full overflow-hidden mb-2 flex"> <div className="bg-orange-500 h-full" style={{ width: `${inputPct}%` }} /> <div className="bg-orange-200 h-full" style={{ width: `${outputPct}%` }} /> </div> <div className="flex justify-between text-xs text-gray-500"> <span>输入: {formatTokens(data.inputTokens)}</span> <span>输出: {formatTokens(data.outputTokens)}</span> <span>总计: {formatTokens(total)}</span> </div> </div> ); }) )} </div> </div> ); } function StatCard({ icon: Icon, label, value, color, }: { icon: typeof BarChart3; label: string; value: string | number; color: string; }) { return ( <div className="bg-white rounded-xl border border-gray-200 p-4 shadow-sm"> <div className="flex items-center gap-2 mb-2"> <Icon className={`w-4 h-4 ${color}`} /> <span className="text-xs text-gray-500">{label}</span> </div> <div className="text-2xl font-bold text-gray-900">{value}</div> </div> ); } |