QuotaMonitor.vue
9.32 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
<template>
<div class="quota-page">
<AppNav />
<div class="quota-layout">
<header class="page-header">
<h1>📊 算力消耗与 Token 监控 (Quota Monitor)</h1>
<p class="subtitle">实时监控企业级 Multi-Agent 系统的大模型 API 成本与耗时</p>
</header>
<main class="dashboard-content" v-if="!loading">
<!-- 核心指标 -->
<section class="overview-grid">
<div class="stat-card sum-card">
<h3>总消耗金额 (USD)</h3>
<div class="stat-value highlight">${{ totalCost.toFixed(4) }}</div>
</div>
<div class="stat-card">
<h3>总输入 Tokens</h3>
<div class="stat-value">{{ totalInputTokens.toLocaleString() }}</div>
</div>
<div class="stat-card">
<h3>总输出 Tokens</h3>
<div class="stat-value">{{ totalOutputTokens.toLocaleString() }}</div>
</div>
<div class="stat-card">
<h3>平均单次推理耗时</h3>
<div class="stat-value">{{ avgDuration.toFixed(1) }}s</div>
</div>
</section>
<div class="charts-row">
<!-- 按照 Agent 的消耗排行榜 -->
<section class="chart-card flex-double">
<h3>🤖 各 Agent 计算力消耗排行榜</h3>
<div class="bar-chart">
<div v-for="agent in agentStats" :key="agent.id" class="bar-row">
<div class="bar-label">
<span class="agent-id">{{ agent.id }}</span>
<span class="agent-calls">{{ agent.calls }} 次调用</span>
</div>
<div class="bar-track-wrapper">
<div class="bar-track">
<div class="bar-fill" :style="{ width: ((agent.cost / maxAgentCost) * 100) + '%' }"></div>
</div>
<div class="bar-value">${{ agent.cost.toFixed(4) }}</div>
</div>
<div class="bar-subtext">
(In: {{ agent.input.toLocaleString() }} / Out: {{ agent.output.toLocaleString() }})
</div>
</div>
<div v-if="agentStats.length === 0" class="empty-state">暂无数据</div>
</div>
</section>
</div>
<!-- 详细调用日志 -->
<section class="chart-card">
<h3>🕒 最近的大模型调用日志</h3>
<div class="log-table-wrapper">
<table class="log-table">
<thead>
<tr>
<th>时间</th>
<th>Agent</th>
<th>对话 ID</th>
<th>耗时</th>
<th>输入 Tokens</th>
<th>输出 Tokens</th>
<th>消耗金额</th>
</tr>
</thead>
<tbody>
<tr v-for="(log, i) in recentLogs" :key="i">
<td class="log-time">{{ formatTime(log.timestamp) }}</td>
<td class="log-agent">{{ log.agentId }}</td>
<td class="log-conv" :title="log.convId">{{ log.convId.substring(0, 12) }}...</td>
<td class="log-duration">{{ (log.durationMs / 1000).toFixed(1) }}s</td>
<td class="log-tokens in">+{{ (log.usage?.input || 0).toLocaleString() }}</td>
<td class="log-tokens out">+{{ (log.usage?.output || 0).toLocaleString() }}</td>
<td class="log-cost">${{ log.costUsd.toFixed(6) }}</td>
</tr>
</tbody>
</table>
<div v-if="recentLogs.length === 0" class="empty-state">暂无日志</div>
</div>
</section>
</main>
<div v-else class="loading-state">
<div class="spinner"></div> 加载监控数据中...
</div>
</div>
</div>
</template>
<script setup>
import { ref, computed, onMounted } from 'vue'
import AppNav from '../components/AppNav.vue'
const loading = ref(true)
const rawData = ref([])
onMounted(async () => {
try {
const res = await fetch('/api/agent/quota')
if (res.ok) {
const data = await res.json()
// Filter out invalid records if any
rawData.value = data.filter(d => d && d.timestamp).sort((a,b) => new Date(b.timestamp) - new Date(a.timestamp))
}
} catch(e) {
console.error('Failed to load quota', e)
} finally {
loading.value = false
}
})
// Overview stats
const totalCost = computed(() => rawData.value.reduce((s, d) => s + (d.costUsd || 0), 0))
const totalInputTokens = computed(() => rawData.value.reduce((s, d) => s + (d.usage?.input || 0), 0))
const totalOutputTokens = computed(() => rawData.value.reduce((s, d) => s + (d.usage?.output || 0), 0))
const avgDuration = computed(() => {
if (rawData.value.length === 0) return 0
const sumMs = rawData.value.reduce((s, d) => s + (d.durationMs || 0), 0)
return (sumMs / rawData.value.length) / 1000
})
// Agent Breakdown
const agentStats = computed(() => {
const map = {}
rawData.value.forEach(d => {
const id = d.agentId || 'unknown'
if (!map[id]) map[id] = { id, cost: 0, input: 0, output: 0, calls: 0 }
map[id].cost += (d.costUsd || 0)
map[id].input += (d.usage?.input || 0)
map[id].output += (d.usage?.output || 0)
map[id].calls += 1
})
return Object.values(map).sort((a,b) => b.cost - a.cost)
})
const maxAgentCost = computed(() => {
if (agentStats.value.length === 0) return 1
return Math.max(...agentStats.value.map(a => a.cost), 0.0001)
})
// Recent Logs table
const recentLogs = computed(() => rawData.value.slice(0, 100))
function formatTime(iso) {
const d = new Date(iso)
return d.toLocaleDateString() + ' ' + d.toLocaleTimeString()
}
</script>
<style scoped>
.quota-page {
height: 100vh;
display: flex;
flex-direction: column;
background: var(--bg-primary);
color: var(--text-primary);
overflow-y: auto;
}
.quota-layout {
max-width: 1200px;
margin: 0 auto;
padding: 32px 40px;
width: 100%;
}
.page-header {
margin-bottom: 32px;
}
.page-header h1 {
font-size: 28px;
font-weight: 700;
margin-bottom: 8px;
color: var(--text-primary);
}
.subtitle {
color: var(--text-secondary);
font-size: 14px;
}
.overview-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
gap: 20px;
margin-bottom: 24px;
}
.stat-card {
background: var(--bg-surface);
border: 1px solid var(--border-color);
border-radius: 12px;
padding: 24px;
display: flex;
flex-direction: column;
box-shadow: 0 4px 6px rgba(0,0,0,0.05);
}
.stat-card h3 {
font-size: 13px;
text-transform: uppercase;
letter-spacing: 0.5px;
color: var(--text-secondary);
margin-bottom: 12px;
}
.stat-value {
font-size: 32px;
font-weight: 700;
color: var(--text-primary);
}
.stat-value.highlight {
color: var(--accent-primary);
}
.sum-card {
background: linear-gradient(145deg, rgba(82, 168, 255, 0.1) 0%, rgba(35, 131, 226, 0.02) 100%);
border-color: rgba(82, 168, 255, 0.2);
}
.charts-row {
display: flex;
gap: 24px;
margin-bottom: 24px;
}
.chart-card {
background: var(--bg-surface);
border: 1px solid var(--border-color);
border-radius: 12px;
padding: 24px;
flex: 1;
}
.chart-card h3 {
font-size: 16px;
margin-bottom: 20px;
color: var(--text-primary);
display: flex;
align-items: center;
gap: 8px;
}
.bar-chart {
display: flex;
flex-direction: column;
gap: 16px;
}
.bar-row {
display: flex;
flex-direction: column;
gap: 6px;
}
.bar-label {
display: flex;
justify-content: space-between;
font-size: 13px;
}
.agent-id { font-weight: 600; color: var(--text-primary); }
.agent-calls { color: var(--text-secondary); font-size: 12px; }
.bar-track-wrapper {
display: flex;
align-items: center;
gap: 12px;
}
.bar-track {
flex: 1;
height: 8px;
background: var(--bg-secondary);
border-radius: 4px;
overflow: hidden;
}
.bar-fill {
height: 100%;
background: var(--accent-primary);
border-radius: 4px;
transition: width 0.5s ease-out;
}
.bar-value {
font-size: 13px;
font-weight: 600;
width: 70px;
text-align: right;
color: var(--text-primary);
}
.bar-subtext {
font-size: 11px;
color: var(--text-faint);
}
.log-table-wrapper {
overflow-x: auto;
}
.log-table {
width: 100%;
border-collapse: collapse;
font-size: 13px;
}
.log-table th {
text-align: left;
padding: 12px;
border-bottom: 1px solid var(--border-color);
color: var(--text-secondary);
font-weight: 500;
}
.log-table td {
padding: 12px;
border-bottom: 1px solid var(--border-color);
color: var(--text-primary);
}
.log-time { color: var(--text-secondary) !important; font-variant-numeric: tabular-nums; }
.log-agent { font-weight: 500; }
.log-conv { color: var(--text-faint) !important; font-family: monospace; }
.log-tokens.in { color: #10b981 !important; }
.log-tokens.out { color: #8b5cf6 !important; }
.log-cost { font-weight: 600; color: var(--accent-primary) !important; }
.empty-state {
padding: 40px;
text-align: center;
color: var(--text-faint);
font-style: italic;
}
.loading-state {
display: flex;
align-items: center;
justify-content: center;
padding: 100px;
color: var(--text-secondary);
gap: 12px;
}
.spinner {
width: 20px; height: 20px;
border: 2px solid var(--border-color);
border-top-color: var(--accent-primary);
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
</style>