File size: 8,322 Bytes
d0dd276
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<script setup>
import { ref, onMounted, onUnmounted, watch, nextTick } from 'vue'
import { useDashboardStore } from '../../../stores/dashboard'
import * as echarts from 'echarts'

// 使用仪表盘存储
const dashboardStore = useDashboardStore()
// 图表DOM引用
const chartContainer = ref(null)
// 图表实例
let chart = null
// 数据
const chartData = ref({
  timestamps: [], // 时间点
  apiCalls: [],   // API调用数
  tokens: []      // Token使用量
})

// 最大显示点数
const MAX_POINTS = 30
// 更新间隔(毫秒)
const UPDATE_INTERVAL = 10000

// 定时器引用
let timer = null

// 初始化图表
function initChart() {
  if (!chartContainer.value) return
  
  // 创建图表实例
  chart = echarts.init(chartContainer.value)
  
  // 获取当前主题模式
  const isDark = dashboardStore.isDarkMode
  const textColor = isDark ? '#e0e0e0' : '#666'
  const axisLineColor = isDark ? '#555' : '#ccc'
  
  // 图表配置
  const option = {
    title: {
      text: 'API实时调用统计',
      left: 'center',
      textStyle: {
        color: textColor
      }
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        label: {
          backgroundColor: '#6a7985'
        }
      }
    },
    legend: {
      data: ['API调用次数', 'Token使用量'],
      top: 30,
      textStyle: {
        color: textColor
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: chartData.value.timestamps,
      axisLabel: {
        rotate: 45,
        color: textColor
      },
      axisLine: {
        lineStyle: {
          color: axisLineColor
        }
      },
      splitLine: {
        lineStyle: {
          color: isDark ? 'rgba(255,255,255,0.1)' : 'rgba(0,0,0,0.1)'
        }
      }
    },
    yAxis: [
      {
        type: 'value',
        name: 'API调用次数',
        position: 'left',
        axisLine: {
          show: true,
          lineStyle: {
            color: '#5470c6'
          }
        },
        axisLabel: {
          formatter: '{value}',
          color: textColor
        },
        splitLine: {
          lineStyle: {
            color: isDark ? 'rgba(255,255,255,0.1)' : 'rgba(0,0,0,0.1)'
          }
        }
      },
      {
        type: 'value',
        name: 'Token使用量',
        position: 'right',
        axisLine: {
          show: true,
          lineStyle: {
            color: '#91cc75'
          }
        },
        axisLabel: {
          formatter: '{value}',
          color: textColor
        },
        splitLine: {
          lineStyle: {
            color: isDark ? 'rgba(255,255,255,0.1)' : 'rgba(0,0,0,0.1)'
          }
        }
      }
    ],
    series: [
      {
        name: 'API调用次数',
        type: 'line',
        smooth: true,
        data: chartData.value.apiCalls,
        itemStyle: {
          color: '#5470c6'
        },
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: 'rgba(84, 112, 198, 0.5)' },
            { offset: 1, color: 'rgba(84, 112, 198, 0.1)' }
          ])
        }
      },
      {
        name: 'Token使用量',
        type: 'line',
        yAxisIndex: 1,
        smooth: true,
        data: chartData.value.tokens,
        itemStyle: {
          color: '#91cc75'
        },
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: 'rgba(145, 204, 117, 0.5)' },
            { offset: 1, color: 'rgba(145, 204, 117, 0.1)' }
          ])
        }
      }
    ]
  }
  
  // 应用配置
  chart.setOption(option)
  
  // 响应窗口大小变化
  window.addEventListener('resize', () => {
    chart && chart.resize()
  })
}

// 更新图表数据
function updateChartData() {
  // 清空之前的数据
  chartData.value.timestamps = []
  chartData.value.apiCalls = []
  chartData.value.tokens = []
  
  // 获取当前时间
  const now = new Date()
  const timeString = now.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit', second: '2-digit' })
  
  // 使用后端提供的时间序列数据
  if (dashboardStore.timeSeriesData.calls.length > 0 && dashboardStore.timeSeriesData.tokens.length > 0) {
    // 使用后端的时间序列数据
    chartData.value.timestamps = dashboardStore.timeSeriesData.calls.map(point => point.time)
    chartData.value.apiCalls = dashboardStore.timeSeriesData.calls.map(point => point.value)
    chartData.value.tokens = dashboardStore.timeSeriesData.tokens.map(point => point.value)
  } else {
    // 后备方案:使用最新的调用次数
    const apiCalls = dashboardStore.status.minuteCalls
    const tokenSum = calculateTokenSum()
    
    // 添加数据点
    chartData.value.timestamps.push(timeString)
    chartData.value.apiCalls.push(apiCalls)
    chartData.value.tokens.push(tokenSum)
  }
  
  // 限制显示点数
  if (chartData.value.timestamps.length > MAX_POINTS) {
    const toRemove = chartData.value.timestamps.length - MAX_POINTS
    chartData.value.timestamps.splice(0, toRemove)
    chartData.value.apiCalls.splice(0, toRemove)
    chartData.value.tokens.splice(0, toRemove)
  }
  
  // 更新图表
  if (chart) {
    chart.setOption({
      xAxis: {
        data: chartData.value.timestamps
      },
      series: [
        { data: chartData.value.apiCalls },
        { data: chartData.value.tokens }
      ]
    })
  }
}

// 计算最近一分钟内的Token总量
function calculateTokenSum() {
  let sum = 0
  
  if (dashboardStore.apiKeyStats.length > 0) {
    // 这里简化处理,显示最近的token总量变化
    // 实际项目中可能需要更精确的统计逻辑
    sum = dashboardStore.apiKeyStats.reduce((total, key) => {
      // 计算该密钥下所有模型的token总和
      const keyTokens = Object.values(key.model_stats || {}).reduce((sum, model) => {
        return sum + (model.tokens || 0)
      }, 0)
      return total + keyTokens / 100 // 缩放数值以便在图表中更好显示
    }, 0)
  }
  
  return Math.round(sum)
}

// 监听夜间模式变化
watch(() => dashboardStore.isDarkMode, (newValue) => {
  if (chart) {
    // 重新初始化图表以适应主题变化
    chart.dispose()
    nextTick(() => {
      initChart()
      // 重新填充数据
      if (chartData.value.timestamps.length > 0) {
        chart.setOption({
          xAxis: {
            data: chartData.value.timestamps
          },
          series: [
            { data: chartData.value.apiCalls },
            { data: chartData.value.tokens }
          ]
        })
      }
    })
  }
}, { immediate: false })

// 组件挂载时初始化
onMounted(() => {
  // 初始化图表
  initChart()
  
  // 第一次更新数据
  updateChartData()
  
  // 设置定时更新
  timer = setInterval(() => {
    // 刷新仪表盘数据
    dashboardStore.fetchDashboardData().then(() => {
      // 更新图表
      updateChartData()
    })
  }, UPDATE_INTERVAL)
})

// 组件卸载时清理
onUnmounted(() => {
  // 清除定时器
  if (timer) {
    clearInterval(timer)
    timer = null
  }
  
  // 销毁图表实例
  if (chart) {
    chart.dispose()
    chart = null
  }
  
  // 移除事件监听
  window.removeEventListener('resize', () => {
    chart && chart.resize()
  })
})
</script>

<template>
  <div class="api-calls-chart-container">
    <div ref="chartContainer" class="chart-container"></div>
  </div>
</template>

<style scoped>
.api-calls-chart-container {
  margin: 20px 0;
  border-radius: var(--radius-lg);
  background-color: var(--stats-item-bg);
  padding: 15px;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--card-border);
  transition: all 0.3s ease;
}

.api-calls-chart-container:hover {
  box-shadow: var(--shadow-md);
  border-color: var(--button-primary);
  transform: translateY(-3px);
}

.chart-title {
  margin-top: 0;
  margin-bottom: 15px;
  color: var(--color-heading);
  font-weight: 600;
  text-align: center;
}

.chart-container {
  width: 100%;
  height: 350px;
}

@media (max-width: 768px) {
  .chart-container {
    height: 300px;
  }
}

@media (max-width: 480px) {
  .chart-container {
    height: 250px;
  }
}
</style>