mirror of
https://github.com/Wei-Shaw/claude-relay-service.git
synced 2026-01-23 09:38:02 +00:00
feat: api-stats页面支持多key查询
This commit is contained in:
@@ -407,6 +407,380 @@ router.post('/api/user-stats', async (req, res) => {
|
||||
}
|
||||
})
|
||||
|
||||
// 📊 批量查询统计数据接口
|
||||
router.post('/api/batch-stats', async (req, res) => {
|
||||
try {
|
||||
const { apiIds } = req.body
|
||||
|
||||
// 验证输入
|
||||
if (!apiIds || !Array.isArray(apiIds) || apiIds.length === 0) {
|
||||
return res.status(400).json({
|
||||
error: 'Invalid input',
|
||||
message: 'API IDs array is required'
|
||||
})
|
||||
}
|
||||
|
||||
// 限制最多查询 30 个
|
||||
if (apiIds.length > 30) {
|
||||
return res.status(400).json({
|
||||
error: 'Too many keys',
|
||||
message: 'Maximum 30 API keys can be queried at once'
|
||||
})
|
||||
}
|
||||
|
||||
// 验证所有 ID 格式
|
||||
const uuidRegex = /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i
|
||||
const invalidIds = apiIds.filter((id) => !uuidRegex.test(id))
|
||||
if (invalidIds.length > 0) {
|
||||
return res.status(400).json({
|
||||
error: 'Invalid API ID format',
|
||||
message: `Invalid API IDs: ${invalidIds.join(', ')}`
|
||||
})
|
||||
}
|
||||
|
||||
const client = redis.getClientSafe()
|
||||
const individualStats = []
|
||||
const aggregated = {
|
||||
totalKeys: 0,
|
||||
activeKeys: 0,
|
||||
usage: {
|
||||
requests: 0,
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
cacheCreateTokens: 0,
|
||||
cacheReadTokens: 0,
|
||||
allTokens: 0,
|
||||
cost: 0,
|
||||
formattedCost: '$0.000000'
|
||||
},
|
||||
dailyUsage: {
|
||||
requests: 0,
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
cacheCreateTokens: 0,
|
||||
cacheReadTokens: 0,
|
||||
allTokens: 0,
|
||||
cost: 0,
|
||||
formattedCost: '$0.000000'
|
||||
},
|
||||
monthlyUsage: {
|
||||
requests: 0,
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
cacheCreateTokens: 0,
|
||||
cacheReadTokens: 0,
|
||||
allTokens: 0,
|
||||
cost: 0,
|
||||
formattedCost: '$0.000000'
|
||||
}
|
||||
}
|
||||
|
||||
// 并行查询所有 API Key 数据
|
||||
const results = await Promise.allSettled(
|
||||
apiIds.map(async (apiId) => {
|
||||
const keyData = await redis.getApiKey(apiId)
|
||||
|
||||
if (!keyData || Object.keys(keyData).length === 0) {
|
||||
return { error: 'Not found', apiId }
|
||||
}
|
||||
|
||||
// 检查是否激活
|
||||
if (keyData.isActive !== 'true') {
|
||||
return { error: 'Disabled', apiId }
|
||||
}
|
||||
|
||||
// 检查是否过期
|
||||
if (keyData.expiresAt && new Date() > new Date(keyData.expiresAt)) {
|
||||
return { error: 'Expired', apiId }
|
||||
}
|
||||
|
||||
// 获取使用统计
|
||||
const usage = await redis.getUsageStats(apiId)
|
||||
|
||||
// 获取今日和本月统计
|
||||
const tzDate = redis.getDateInTimezone()
|
||||
const today = redis.getDateStringInTimezone()
|
||||
const currentMonth = `${tzDate.getFullYear()}-${String(tzDate.getMonth() + 1).padStart(2, '0')}`
|
||||
|
||||
// 获取今日模型统计
|
||||
const dailyKeys = await client.keys(`usage:${apiId}:model:daily:*:${today}`)
|
||||
const dailyStats = {
|
||||
requests: 0,
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
cacheCreateTokens: 0,
|
||||
cacheReadTokens: 0,
|
||||
allTokens: 0,
|
||||
cost: 0
|
||||
}
|
||||
|
||||
for (const key of dailyKeys) {
|
||||
const data = await client.hgetall(key)
|
||||
if (data && Object.keys(data).length > 0) {
|
||||
dailyStats.requests += parseInt(data.requests) || 0
|
||||
dailyStats.inputTokens += parseInt(data.inputTokens) || 0
|
||||
dailyStats.outputTokens += parseInt(data.outputTokens) || 0
|
||||
dailyStats.cacheCreateTokens += parseInt(data.cacheCreateTokens) || 0
|
||||
dailyStats.cacheReadTokens += parseInt(data.cacheReadTokens) || 0
|
||||
dailyStats.allTokens += parseInt(data.allTokens) || 0
|
||||
}
|
||||
}
|
||||
|
||||
// 获取本月模型统计
|
||||
const monthlyKeys = await client.keys(`usage:${apiId}:model:monthly:*:${currentMonth}`)
|
||||
const monthlyStats = {
|
||||
requests: 0,
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
cacheCreateTokens: 0,
|
||||
cacheReadTokens: 0,
|
||||
allTokens: 0,
|
||||
cost: 0
|
||||
}
|
||||
|
||||
for (const key of monthlyKeys) {
|
||||
const data = await client.hgetall(key)
|
||||
if (data && Object.keys(data).length > 0) {
|
||||
monthlyStats.requests += parseInt(data.requests) || 0
|
||||
monthlyStats.inputTokens += parseInt(data.inputTokens) || 0
|
||||
monthlyStats.outputTokens += parseInt(data.outputTokens) || 0
|
||||
monthlyStats.cacheCreateTokens += parseInt(data.cacheCreateTokens) || 0
|
||||
monthlyStats.cacheReadTokens += parseInt(data.cacheReadTokens) || 0
|
||||
monthlyStats.allTokens += parseInt(data.allTokens) || 0
|
||||
}
|
||||
}
|
||||
|
||||
// 计算费用
|
||||
const calculateCostForStats = (stats) => {
|
||||
const usageData = {
|
||||
input_tokens: stats.inputTokens,
|
||||
output_tokens: stats.outputTokens,
|
||||
cache_creation_input_tokens: stats.cacheCreateTokens,
|
||||
cache_read_input_tokens: stats.cacheReadTokens
|
||||
}
|
||||
const costResult = CostCalculator.calculateCost(usageData, 'claude-3-5-sonnet-20241022')
|
||||
return costResult.costs.total
|
||||
}
|
||||
|
||||
dailyStats.cost = calculateCostForStats(dailyStats)
|
||||
monthlyStats.cost = calculateCostForStats(monthlyStats)
|
||||
|
||||
return {
|
||||
apiId,
|
||||
name: keyData.name,
|
||||
description: keyData.description || '',
|
||||
isActive: true,
|
||||
createdAt: keyData.createdAt,
|
||||
usage: usage.total || {},
|
||||
dailyStats,
|
||||
monthlyStats
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
// 处理结果并聚合
|
||||
results.forEach((result) => {
|
||||
if (result.status === 'fulfilled' && result.value && !result.value.error) {
|
||||
const stats = result.value
|
||||
aggregated.activeKeys++
|
||||
|
||||
// 聚合总使用量
|
||||
if (stats.usage) {
|
||||
aggregated.usage.requests += stats.usage.requests || 0
|
||||
aggregated.usage.inputTokens += stats.usage.inputTokens || 0
|
||||
aggregated.usage.outputTokens += stats.usage.outputTokens || 0
|
||||
aggregated.usage.cacheCreateTokens += stats.usage.cacheCreateTokens || 0
|
||||
aggregated.usage.cacheReadTokens += stats.usage.cacheReadTokens || 0
|
||||
aggregated.usage.allTokens += stats.usage.allTokens || 0
|
||||
}
|
||||
|
||||
// 聚合今日使用量
|
||||
aggregated.dailyUsage.requests += stats.dailyStats.requests
|
||||
aggregated.dailyUsage.inputTokens += stats.dailyStats.inputTokens
|
||||
aggregated.dailyUsage.outputTokens += stats.dailyStats.outputTokens
|
||||
aggregated.dailyUsage.cacheCreateTokens += stats.dailyStats.cacheCreateTokens
|
||||
aggregated.dailyUsage.cacheReadTokens += stats.dailyStats.cacheReadTokens
|
||||
aggregated.dailyUsage.allTokens += stats.dailyStats.allTokens
|
||||
aggregated.dailyUsage.cost += stats.dailyStats.cost
|
||||
|
||||
// 聚合本月使用量
|
||||
aggregated.monthlyUsage.requests += stats.monthlyStats.requests
|
||||
aggregated.monthlyUsage.inputTokens += stats.monthlyStats.inputTokens
|
||||
aggregated.monthlyUsage.outputTokens += stats.monthlyStats.outputTokens
|
||||
aggregated.monthlyUsage.cacheCreateTokens += stats.monthlyStats.cacheCreateTokens
|
||||
aggregated.monthlyUsage.cacheReadTokens += stats.monthlyStats.cacheReadTokens
|
||||
aggregated.monthlyUsage.allTokens += stats.monthlyStats.allTokens
|
||||
aggregated.monthlyUsage.cost += stats.monthlyStats.cost
|
||||
|
||||
// 添加到个体统计
|
||||
individualStats.push({
|
||||
apiId: stats.apiId,
|
||||
name: stats.name,
|
||||
isActive: true,
|
||||
usage: stats.usage
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
aggregated.totalKeys = apiIds.length
|
||||
|
||||
// 计算总费用
|
||||
const totalUsageData = {
|
||||
input_tokens: aggregated.usage.inputTokens,
|
||||
output_tokens: aggregated.usage.outputTokens,
|
||||
cache_creation_input_tokens: aggregated.usage.cacheCreateTokens,
|
||||
cache_read_input_tokens: aggregated.usage.cacheReadTokens
|
||||
}
|
||||
const totalCostResult = CostCalculator.calculateCost(
|
||||
totalUsageData,
|
||||
'claude-3-5-sonnet-20241022'
|
||||
)
|
||||
aggregated.usage.cost = totalCostResult.costs.total
|
||||
aggregated.usage.formattedCost = totalCostResult.formatted.total
|
||||
|
||||
// 格式化每日和每月费用
|
||||
aggregated.dailyUsage.formattedCost = CostCalculator.formatCost(aggregated.dailyUsage.cost)
|
||||
aggregated.monthlyUsage.formattedCost = CostCalculator.formatCost(aggregated.monthlyUsage.cost)
|
||||
|
||||
logger.api(`📊 Batch stats query for ${apiIds.length} keys from ${req.ip || 'unknown'}`)
|
||||
|
||||
return res.json({
|
||||
success: true,
|
||||
data: {
|
||||
aggregated,
|
||||
individual: individualStats
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
logger.error('❌ Failed to process batch stats query:', error)
|
||||
return res.status(500).json({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to retrieve batch statistics'
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// 📊 批量模型统计查询接口
|
||||
router.post('/api/batch-model-stats', async (req, res) => {
|
||||
try {
|
||||
const { apiIds, period = 'daily' } = req.body
|
||||
|
||||
// 验证输入
|
||||
if (!apiIds || !Array.isArray(apiIds) || apiIds.length === 0) {
|
||||
return res.status(400).json({
|
||||
error: 'Invalid input',
|
||||
message: 'API IDs array is required'
|
||||
})
|
||||
}
|
||||
|
||||
// 限制最多查询 30 个
|
||||
if (apiIds.length > 30) {
|
||||
return res.status(400).json({
|
||||
error: 'Too many keys',
|
||||
message: 'Maximum 30 API keys can be queried at once'
|
||||
})
|
||||
}
|
||||
|
||||
const client = redis.getClientSafe()
|
||||
const tzDate = redis.getDateInTimezone()
|
||||
const today = redis.getDateStringInTimezone()
|
||||
const currentMonth = `${tzDate.getFullYear()}-${String(tzDate.getMonth() + 1).padStart(2, '0')}`
|
||||
|
||||
const modelUsageMap = new Map()
|
||||
|
||||
// 并行查询所有 API Key 的模型统计
|
||||
await Promise.all(
|
||||
apiIds.map(async (apiId) => {
|
||||
const pattern =
|
||||
period === 'daily'
|
||||
? `usage:${apiId}:model:daily:*:${today}`
|
||||
: `usage:${apiId}:model:monthly:*:${currentMonth}`
|
||||
|
||||
const keys = await client.keys(pattern)
|
||||
|
||||
for (const key of keys) {
|
||||
const match = key.match(
|
||||
period === 'daily'
|
||||
? /usage:.+:model:daily:(.+):\d{4}-\d{2}-\d{2}$/
|
||||
: /usage:.+:model:monthly:(.+):\d{4}-\d{2}$/
|
||||
)
|
||||
|
||||
if (!match) {
|
||||
continue
|
||||
}
|
||||
|
||||
const model = match[1]
|
||||
const data = await client.hgetall(key)
|
||||
|
||||
if (data && Object.keys(data).length > 0) {
|
||||
if (!modelUsageMap.has(model)) {
|
||||
modelUsageMap.set(model, {
|
||||
requests: 0,
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
cacheCreateTokens: 0,
|
||||
cacheReadTokens: 0,
|
||||
allTokens: 0
|
||||
})
|
||||
}
|
||||
|
||||
const modelUsage = modelUsageMap.get(model)
|
||||
modelUsage.requests += parseInt(data.requests) || 0
|
||||
modelUsage.inputTokens += parseInt(data.inputTokens) || 0
|
||||
modelUsage.outputTokens += parseInt(data.outputTokens) || 0
|
||||
modelUsage.cacheCreateTokens += parseInt(data.cacheCreateTokens) || 0
|
||||
modelUsage.cacheReadTokens += parseInt(data.cacheReadTokens) || 0
|
||||
modelUsage.allTokens += parseInt(data.allTokens) || 0
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
// 转换为数组并计算费用
|
||||
const modelStats = []
|
||||
for (const [model, usage] of modelUsageMap) {
|
||||
const usageData = {
|
||||
input_tokens: usage.inputTokens,
|
||||
output_tokens: usage.outputTokens,
|
||||
cache_creation_input_tokens: usage.cacheCreateTokens,
|
||||
cache_read_input_tokens: usage.cacheReadTokens
|
||||
}
|
||||
|
||||
const costData = CostCalculator.calculateCost(usageData, model)
|
||||
|
||||
modelStats.push({
|
||||
model,
|
||||
requests: usage.requests,
|
||||
inputTokens: usage.inputTokens,
|
||||
outputTokens: usage.outputTokens,
|
||||
cacheCreateTokens: usage.cacheCreateTokens,
|
||||
cacheReadTokens: usage.cacheReadTokens,
|
||||
allTokens: usage.allTokens,
|
||||
costs: costData.costs,
|
||||
formatted: costData.formatted,
|
||||
pricing: costData.pricing
|
||||
})
|
||||
}
|
||||
|
||||
// 按总 token 数降序排列
|
||||
modelStats.sort((a, b) => b.allTokens - a.allTokens)
|
||||
|
||||
logger.api(`📊 Batch model stats query for ${apiIds.length} keys, period: ${period}`)
|
||||
|
||||
return res.json({
|
||||
success: true,
|
||||
data: modelStats,
|
||||
period
|
||||
})
|
||||
} catch (error) {
|
||||
logger.error('❌ Failed to process batch model stats query:', error)
|
||||
return res.status(500).json({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to retrieve batch model statistics'
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// 📊 用户模型统计查询接口 - 安全的自查询接口
|
||||
router.post('/api/user-model-stats', async (req, res) => {
|
||||
try {
|
||||
|
||||
@@ -1,24 +1,64 @@
|
||||
<template>
|
||||
<div class="api-input-wide-card mb-8 rounded-3xl p-6 shadow-xl">
|
||||
<!-- 标题区域 -->
|
||||
<div class="wide-card-title mb-6 text-center">
|
||||
<h2 class="mb-2 text-2xl font-bold text-gray-900 dark:text-white">
|
||||
<div class="wide-card-title mb-6">
|
||||
<h2 class="mb-2 text-2xl font-bold text-gray-900 dark:text-gray-200">
|
||||
<i class="fas fa-chart-line mr-3" />
|
||||
使用统计查询
|
||||
</h2>
|
||||
<p class="text-base text-gray-600 dark:text-gray-300">查询您的 API Key 使用情况和统计数据</p>
|
||||
<p class="text-base text-gray-600 dark:text-gray-400">查询您的 API Key 使用情况和统计数据</p>
|
||||
</div>
|
||||
|
||||
<!-- 输入区域 -->
|
||||
<div class="mx-auto max-w-4xl">
|
||||
<div class="api-input-grid grid grid-cols-1 lg:grid-cols-4">
|
||||
<!-- 控制栏 -->
|
||||
<div class="control-bar mb-4 flex flex-wrap items-center justify-between gap-3">
|
||||
<!-- API Key 标签 -->
|
||||
<label class="text-sm font-medium text-gray-700 dark:text-gray-300">
|
||||
<i class="fas fa-key mr-2" />
|
||||
{{ multiKeyMode ? '输入您的 API Keys(每行一个或用逗号分隔)' : '输入您的 API Key' }}
|
||||
</label>
|
||||
|
||||
<!-- 模式切换和查询按钮组 -->
|
||||
<div class="button-group flex items-center gap-2">
|
||||
<!-- 模式切换 -->
|
||||
<div
|
||||
class="mode-switch-group flex items-center rounded-lg bg-gray-100 p-1 dark:bg-gray-800"
|
||||
>
|
||||
<button
|
||||
class="mode-switch-btn"
|
||||
:class="{ active: !multiKeyMode }"
|
||||
title="单一模式"
|
||||
@click="multiKeyMode = false"
|
||||
>
|
||||
<i class="fas fa-key" />
|
||||
<span class="ml-2 hidden sm:inline">单一</span>
|
||||
</button>
|
||||
<button
|
||||
class="mode-switch-btn"
|
||||
:class="{ active: multiKeyMode }"
|
||||
title="聚合模式"
|
||||
@click="multiKeyMode = true"
|
||||
>
|
||||
<i class="fas fa-layer-group" />
|
||||
<span class="ml-2 hidden sm:inline">聚合</span>
|
||||
<span
|
||||
v-if="multiKeyMode && parsedApiKeys.length > 0"
|
||||
class="ml-1 rounded-full bg-white/20 px-1.5 py-0.5 text-xs font-semibold"
|
||||
>
|
||||
{{ parsedApiKeys.length }}
|
||||
</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="api-input-grid grid grid-cols-1 gap-4 lg:grid-cols-4">
|
||||
<!-- API Key 输入 -->
|
||||
<div class="lg:col-span-3">
|
||||
<label class="mb-2 block text-sm font-medium text-gray-700 dark:text-gray-200">
|
||||
<i class="fas fa-key mr-2" />
|
||||
输入您的 API Key
|
||||
</label>
|
||||
<!-- 单 Key 模式输入框 -->
|
||||
<input
|
||||
v-if="!multiKeyMode"
|
||||
v-model="apiKey"
|
||||
class="wide-card-input w-full"
|
||||
:disabled="loading"
|
||||
@@ -26,16 +66,33 @@
|
||||
type="password"
|
||||
@keyup.enter="queryStats"
|
||||
/>
|
||||
|
||||
<!-- 多 Key 模式输入框 -->
|
||||
<div v-else class="relative">
|
||||
<textarea
|
||||
v-model="apiKey"
|
||||
class="wide-card-input w-full resize-y"
|
||||
:disabled="loading"
|
||||
placeholder="请输入您的 API Keys,支持以下格式: cr_xxx cr_yyy 或 cr_xxx, cr_yyy"
|
||||
rows="4"
|
||||
@keyup.ctrl.enter="queryStats"
|
||||
/>
|
||||
<button
|
||||
v-if="apiKey && !loading"
|
||||
class="absolute right-2 top-2 text-gray-400 hover:text-gray-600 dark:text-gray-500 dark:hover:text-gray-300"
|
||||
title="清空输入"
|
||||
@click="clearInput"
|
||||
>
|
||||
<i class="fas fa-times-circle" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 查询按钮 -->
|
||||
<div class="lg:col-span-1">
|
||||
<label class="mb-2 hidden text-sm font-medium text-gray-700 dark:text-gray-200 lg:block">
|
||||
|
||||
</label>
|
||||
<button
|
||||
class="btn btn-primary btn-query flex h-full w-full items-center justify-center gap-2"
|
||||
:disabled="loading || !apiKey.trim()"
|
||||
:disabled="loading || !hasValidInput"
|
||||
@click="queryStats"
|
||||
>
|
||||
<i v-if="loading" class="fas fa-spinner loading-spinner" />
|
||||
@@ -48,19 +105,56 @@
|
||||
<!-- 安全提示 -->
|
||||
<div class="security-notice mt-4">
|
||||
<i class="fas fa-shield-alt mr-2" />
|
||||
您的 API Key 仅用于查询自己的统计数据,不会被存储或用于其他用途
|
||||
{{
|
||||
multiKeyMode
|
||||
? '您的 API Keys 仅用于查询统计数据,不会被存储。聚合模式下部分个体化信息将不显示。'
|
||||
: '您的 API Key 仅用于查询自己的统计数据,不会被存储或用于其他用途'
|
||||
}}
|
||||
</div>
|
||||
|
||||
<!-- 多 Key 模式额外提示 -->
|
||||
<div
|
||||
v-if="multiKeyMode"
|
||||
class="mt-2 rounded-lg bg-blue-50 p-3 text-sm text-blue-700 dark:bg-blue-900/20 dark:text-blue-400"
|
||||
>
|
||||
<i class="fas fa-lightbulb mr-2" />
|
||||
<span>提示:最多支持同时查询 30 个 API Keys。使用 Ctrl+Enter 快速查询。</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { computed } from 'vue'
|
||||
import { storeToRefs } from 'pinia'
|
||||
import { useApiStatsStore } from '@/stores/apistats'
|
||||
|
||||
const apiStatsStore = useApiStatsStore()
|
||||
const { apiKey, loading } = storeToRefs(apiStatsStore)
|
||||
const { queryStats } = apiStatsStore
|
||||
const { apiKey, loading, multiKeyMode } = storeToRefs(apiStatsStore)
|
||||
const { queryStats, clearInput } = apiStatsStore
|
||||
|
||||
// 解析输入的 API Keys
|
||||
const parsedApiKeys = computed(() => {
|
||||
if (!multiKeyMode.value || !apiKey.value) return []
|
||||
|
||||
// 支持逗号和换行符分隔
|
||||
const keys = apiKey.value
|
||||
.split(/[,\n]+/)
|
||||
.map((key) => key.trim())
|
||||
.filter((key) => key.length > 0)
|
||||
|
||||
// 去重并限制最多30个
|
||||
const uniqueKeys = [...new Set(keys)]
|
||||
return uniqueKeys.slice(0, 30)
|
||||
})
|
||||
|
||||
// 判断是否有有效输入
|
||||
const hasValidInput = computed(() => {
|
||||
if (multiKeyMode.value) {
|
||||
return parsedApiKeys.value.length > 0
|
||||
}
|
||||
return apiKey.value && apiKey.value.trim().length > 0
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
@@ -101,7 +195,6 @@ const { queryStats } = apiStatsStore
|
||||
|
||||
/* 标题样式 */
|
||||
.wide-card-title h2 {
|
||||
color: #1f2937;
|
||||
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
|
||||
font-weight: 700;
|
||||
}
|
||||
@@ -112,12 +205,12 @@ const { queryStats } = apiStatsStore
|
||||
}
|
||||
|
||||
.wide-card-title p {
|
||||
color: #4b5563;
|
||||
color: #6b7280;
|
||||
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.05);
|
||||
}
|
||||
|
||||
:global(.dark) .wide-card-title p {
|
||||
color: #d1d5db;
|
||||
color: #9ca3af;
|
||||
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
|
||||
}
|
||||
|
||||
@@ -251,6 +344,93 @@ const { queryStats } = apiStatsStore
|
||||
text-shadow: 0 1px 2px rgba(16, 185, 129, 0.2);
|
||||
}
|
||||
|
||||
/* 控制栏 */
|
||||
.control-bar {
|
||||
padding-bottom: 0.5rem;
|
||||
border-bottom: 1px solid rgba(229, 231, 235, 0.3);
|
||||
}
|
||||
|
||||
:global(.dark) .control-bar {
|
||||
border-bottom-color: rgba(75, 85, 99, 0.3);
|
||||
}
|
||||
|
||||
/* 按钮组 */
|
||||
.button-group {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
/* 模式切换组 */
|
||||
.mode-switch-group {
|
||||
display: inline-flex;
|
||||
padding: 4px;
|
||||
background: #f3f4f6;
|
||||
border-radius: 0.5rem;
|
||||
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.05);
|
||||
}
|
||||
|
||||
:global(.dark) .mode-switch-group {
|
||||
background: #1f2937;
|
||||
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.3);
|
||||
}
|
||||
|
||||
/* 模式切换按钮 */
|
||||
.mode-switch-btn {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
padding: 6px 12px;
|
||||
font-size: 0.875rem;
|
||||
font-weight: 500;
|
||||
color: #6b7280;
|
||||
background: transparent;
|
||||
border: none;
|
||||
border-radius: 0.375rem;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
:global(.dark) .mode-switch-btn {
|
||||
color: #9ca3af;
|
||||
}
|
||||
|
||||
.mode-switch-btn:hover:not(.active) {
|
||||
color: #374151;
|
||||
background: rgba(0, 0, 0, 0.05);
|
||||
}
|
||||
|
||||
:global(.dark) .mode-switch-btn:hover:not(.active) {
|
||||
color: #d1d5db;
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
}
|
||||
|
||||
.mode-switch-btn.active {
|
||||
color: white;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
box-shadow: 0 2px 4px rgba(102, 126, 234, 0.2);
|
||||
}
|
||||
|
||||
.mode-switch-btn.active:hover {
|
||||
box-shadow: 0 4px 6px rgba(102, 126, 234, 0.3);
|
||||
}
|
||||
|
||||
.mode-switch-btn i {
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
/* 淡入淡出动画 */
|
||||
.fade-enter-active,
|
||||
.fade-leave-active {
|
||||
transition: all 0.3s ease;
|
||||
}
|
||||
|
||||
.fade-enter-from,
|
||||
.fade-leave-to {
|
||||
opacity: 0;
|
||||
transform: translateX(-10px);
|
||||
}
|
||||
|
||||
/* 加载动画 */
|
||||
.loading-spinner {
|
||||
animation: spin 1s linear infinite;
|
||||
@@ -267,6 +447,18 @@ const { queryStats } = apiStatsStore
|
||||
}
|
||||
|
||||
/* 响应式优化 */
|
||||
@media (max-width: 768px) {
|
||||
.control-bar {
|
||||
flex-direction: column;
|
||||
align-items: stretch;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.button-group {
|
||||
justify-content: center;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.api-input-wide-card {
|
||||
padding: 1.25rem;
|
||||
@@ -304,6 +496,22 @@ const { queryStats } = apiStatsStore
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 480px) {
|
||||
.mode-toggle-btn {
|
||||
padding: 5px 8px;
|
||||
}
|
||||
|
||||
.toggle-icon {
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
}
|
||||
|
||||
.hint-text {
|
||||
font-size: 0.7rem;
|
||||
padding: 4px 8px;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 480px) {
|
||||
.api-input-wide-card {
|
||||
padding: 1rem;
|
||||
|
||||
@@ -1,14 +1,28 @@
|
||||
<template>
|
||||
<div>
|
||||
<!-- 限制配置 -->
|
||||
<!-- 限制配置 / 聚合模式提示 -->
|
||||
<div class="card p-4 md:p-6">
|
||||
<h3
|
||||
class="mb-3 flex items-center text-lg font-bold text-gray-900 dark:text-gray-100 md:mb-4 md:text-xl"
|
||||
>
|
||||
<i class="fas fa-shield-alt mr-2 text-sm text-red-500 md:mr-3 md:text-base" />
|
||||
限制配置
|
||||
{{ multiKeyMode ? '限制配置(聚合查询模式)' : '限制配置' }}
|
||||
</h3>
|
||||
<div class="space-y-4 md:space-y-5">
|
||||
|
||||
<!-- 多 Key 模式下的提示信息 -->
|
||||
<div
|
||||
v-if="multiKeyMode"
|
||||
class="mb-4 rounded-lg bg-yellow-50 p-3 text-sm dark:bg-yellow-900/20"
|
||||
>
|
||||
<i class="fas fa-info-circle mr-2 text-yellow-600 dark:text-yellow-400" />
|
||||
<span class="text-yellow-700 dark:text-yellow-300">
|
||||
聚合查询模式下,限制配置信息不适用于多个 API Key 的组合,因此不显示详细的限制配置。 每个
|
||||
API Key 有独立的限制设置。
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<!-- 仅在单 Key 模式下显示限制配置 -->
|
||||
<div v-if="!multiKeyMode" class="space-y-4 md:space-y-5">
|
||||
<!-- 每日费用限制 -->
|
||||
<div>
|
||||
<div class="mb-2 flex items-center justify-between">
|
||||
@@ -221,7 +235,7 @@ import { useApiStatsStore } from '@/stores/apistats'
|
||||
import WindowCountdown from '@/components/apikeys/WindowCountdown.vue'
|
||||
|
||||
const apiStatsStore = useApiStatsStore()
|
||||
const { statsData } = storeToRefs(apiStatsStore)
|
||||
const { statsData, multiKeyMode } = storeToRefs(apiStatsStore)
|
||||
|
||||
// 获取每日费用进度
|
||||
const getDailyCostProgress = () => {
|
||||
|
||||
@@ -1,14 +1,83 @@
|
||||
<template>
|
||||
<div class="mb-6 grid grid-cols-1 gap-4 md:mb-8 md:gap-6 lg:grid-cols-2">
|
||||
<!-- API Key 基本信息 -->
|
||||
<!-- API Key 基本信息 / 批量查询概要 -->
|
||||
<div class="card p-4 md:p-6">
|
||||
<h3
|
||||
class="mb-3 flex items-center text-lg font-bold text-gray-900 dark:text-gray-100 md:mb-4 md:text-xl"
|
||||
>
|
||||
<i class="fas fa-info-circle mr-2 text-sm text-blue-500 md:mr-3 md:text-base" />
|
||||
API Key 信息
|
||||
<i
|
||||
class="mr-2 text-sm md:mr-3 md:text-base"
|
||||
:class="
|
||||
multiKeyMode ? 'fas fa-layer-group text-purple-500' : 'fas fa-info-circle text-blue-500'
|
||||
"
|
||||
/>
|
||||
{{ multiKeyMode ? '批量查询概要' : 'API Key 信息' }}
|
||||
</h3>
|
||||
<div class="space-y-2 md:space-y-3">
|
||||
|
||||
<!-- 多 Key 模式下的概要信息 -->
|
||||
<div v-if="multiKeyMode && aggregatedStats" class="space-y-2 md:space-y-3">
|
||||
<div class="flex items-center justify-between">
|
||||
<span class="text-sm text-gray-600 dark:text-gray-400 md:text-base">查询 Keys 数</span>
|
||||
<span class="text-sm font-medium text-gray-900 dark:text-gray-100 md:text-base">
|
||||
{{ aggregatedStats.totalKeys }} 个
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex items-center justify-between">
|
||||
<span class="text-sm text-gray-600 dark:text-gray-400 md:text-base">有效 Keys 数</span>
|
||||
<span class="text-sm font-medium text-green-600 md:text-base">
|
||||
<i class="fas fa-check-circle mr-1 text-xs md:text-sm" />
|
||||
{{ aggregatedStats.activeKeys }} 个
|
||||
</span>
|
||||
</div>
|
||||
<div v-if="invalidKeys.length > 0" class="flex items-center justify-between">
|
||||
<span class="text-sm text-gray-600 dark:text-gray-400 md:text-base">无效 Keys 数</span>
|
||||
<span class="text-sm font-medium text-red-600 md:text-base">
|
||||
<i class="fas fa-times-circle mr-1 text-xs md:text-sm" />
|
||||
{{ invalidKeys.length }} 个
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex items-center justify-between">
|
||||
<span class="text-sm text-gray-600 dark:text-gray-400 md:text-base">总请求数</span>
|
||||
<span class="text-sm font-medium text-gray-900 dark:text-gray-100 md:text-base">
|
||||
{{ formatNumber(aggregatedStats.usage.requests) }}
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex items-center justify-between">
|
||||
<span class="text-sm text-gray-600 dark:text-gray-400 md:text-base">总 Token 数</span>
|
||||
<span class="text-sm font-medium text-gray-900 dark:text-gray-100 md:text-base">
|
||||
{{ formatNumber(aggregatedStats.usage.allTokens) }}
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex items-center justify-between">
|
||||
<span class="text-sm text-gray-600 dark:text-gray-400 md:text-base">总费用</span>
|
||||
<span class="text-sm font-medium text-indigo-600 md:text-base">
|
||||
{{ aggregatedStats.usage.formattedCost }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<!-- 各 Key 贡献占比(可选) -->
|
||||
<div
|
||||
v-if="individualStats.length > 1"
|
||||
class="border-t border-gray-200 pt-2 dark:border-gray-700"
|
||||
>
|
||||
<div class="mb-2 text-xs text-gray-500 dark:text-gray-400">各 Key 贡献占比</div>
|
||||
<div class="space-y-1">
|
||||
<div
|
||||
v-for="stat in topContributors"
|
||||
:key="stat.apiId"
|
||||
class="flex items-center justify-between text-xs"
|
||||
>
|
||||
<span class="truncate text-gray-600 dark:text-gray-400">{{ stat.name }}</span>
|
||||
<span class="text-gray-900 dark:text-gray-100"
|
||||
>{{ calculateContribution(stat) }}%</span
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 单 Key 模式下的详细信息 -->
|
||||
<div v-else class="space-y-2 md:space-y-3">
|
||||
<div class="flex items-center justify-between">
|
||||
<span class="text-sm text-gray-600 dark:text-gray-400 md:text-base">名称</span>
|
||||
<span
|
||||
@@ -128,12 +197,38 @@
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
/* eslint-disable no-unused-vars */
|
||||
import { computed } from 'vue'
|
||||
import { storeToRefs } from 'pinia'
|
||||
import { useApiStatsStore } from '@/stores/apistats'
|
||||
import dayjs from 'dayjs'
|
||||
|
||||
const apiStatsStore = useApiStatsStore()
|
||||
const { statsData, statsPeriod, currentPeriodData } = storeToRefs(apiStatsStore)
|
||||
const {
|
||||
statsData,
|
||||
statsPeriod,
|
||||
currentPeriodData,
|
||||
multiKeyMode,
|
||||
aggregatedStats,
|
||||
individualStats,
|
||||
invalidKeys
|
||||
} = storeToRefs(apiStatsStore)
|
||||
|
||||
// 计算前3个贡献最大的 Key
|
||||
const topContributors = computed(() => {
|
||||
if (!individualStats.value || individualStats.value.length === 0) return []
|
||||
|
||||
return [...individualStats.value]
|
||||
.sort((a, b) => (b.usage?.allTokens || 0) - (a.usage?.allTokens || 0))
|
||||
.slice(0, 3)
|
||||
})
|
||||
|
||||
// 计算单个 Key 的贡献占比
|
||||
const calculateContribution = (stat) => {
|
||||
if (!aggregatedStats.value || !aggregatedStats.value.usage.allTokens) return 0
|
||||
const percentage = ((stat.usage?.allTokens || 0) / aggregatedStats.value.usage.allTokens) * 100
|
||||
return percentage.toFixed(1)
|
||||
}
|
||||
|
||||
// 格式化日期
|
||||
const formatDate = (dateString) => {
|
||||
|
||||
@@ -76,6 +76,22 @@ class ApiStatsClient {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 批量查询统计数据
|
||||
async getBatchStats(apiIds) {
|
||||
return this.request('/apiStats/api/batch-stats', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ apiIds })
|
||||
})
|
||||
}
|
||||
|
||||
// 批量查询模型统计
|
||||
async getBatchModelStats(apiIds, period = 'daily') {
|
||||
return this.request('/apiStats/api/batch-model-stats', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ apiIds, period })
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const apiStatsClient = new ApiStatsClient()
|
||||
|
||||
@@ -21,6 +21,14 @@ export const useApiStatsStore = defineStore('apistats', () => {
|
||||
siteIconData: ''
|
||||
})
|
||||
|
||||
// 多 Key 模式相关状态
|
||||
const multiKeyMode = ref(false)
|
||||
const apiKeys = ref([]) // 多个 API Key 数组
|
||||
const apiIds = ref([]) // 对应的 ID 数组
|
||||
const aggregatedStats = ref(null) // 聚合后的统计数据
|
||||
const individualStats = ref([]) // 各个 Key 的独立数据
|
||||
const invalidKeys = ref([]) // 无效的 Keys 列表
|
||||
|
||||
// 计算属性
|
||||
const currentPeriodData = computed(() => {
|
||||
const defaultData = {
|
||||
@@ -69,6 +77,11 @@ export const useApiStatsStore = defineStore('apistats', () => {
|
||||
|
||||
// 查询统计数据
|
||||
async function queryStats() {
|
||||
// 多 Key 模式处理
|
||||
if (multiKeyMode.value) {
|
||||
return queryBatchStats()
|
||||
}
|
||||
|
||||
if (!apiKey.value.trim()) {
|
||||
error.value = '请输入 API Key'
|
||||
return
|
||||
@@ -204,6 +217,12 @@ export const useApiStatsStore = defineStore('apistats', () => {
|
||||
|
||||
statsPeriod.value = period
|
||||
|
||||
// 多 Key 模式下加载批量模型统计
|
||||
if (multiKeyMode.value && apiIds.value.length > 0) {
|
||||
await loadBatchModelStats(period)
|
||||
return
|
||||
}
|
||||
|
||||
// 如果对应时间段的数据还没有加载,则加载它
|
||||
if (
|
||||
(period === 'daily' && !dailyStats.value) ||
|
||||
@@ -297,6 +316,123 @@ export const useApiStatsStore = defineStore('apistats', () => {
|
||||
}
|
||||
}
|
||||
|
||||
// 批量查询统计数据
|
||||
async function queryBatchStats() {
|
||||
const keys = parseApiKeys()
|
||||
if (keys.length === 0) {
|
||||
error.value = '请输入至少一个有效的 API Key'
|
||||
return
|
||||
}
|
||||
|
||||
loading.value = true
|
||||
error.value = ''
|
||||
aggregatedStats.value = null
|
||||
individualStats.value = []
|
||||
invalidKeys.value = []
|
||||
modelStats.value = []
|
||||
apiKeys.value = keys
|
||||
apiIds.value = []
|
||||
|
||||
try {
|
||||
// 批量获取 API Key IDs
|
||||
const idResults = await Promise.allSettled(keys.map((key) => apiStatsClient.getKeyId(key)))
|
||||
|
||||
const validIds = []
|
||||
const validKeys = []
|
||||
|
||||
idResults.forEach((result, index) => {
|
||||
if (result.status === 'fulfilled' && result.value.success) {
|
||||
validIds.push(result.value.data.id)
|
||||
validKeys.push(keys[index])
|
||||
} else {
|
||||
invalidKeys.value.push(keys[index])
|
||||
}
|
||||
})
|
||||
|
||||
if (validIds.length === 0) {
|
||||
throw new Error('所有 API Key 都无效')
|
||||
}
|
||||
|
||||
apiIds.value = validIds
|
||||
apiKeys.value = validKeys
|
||||
|
||||
// 批量查询统计数据
|
||||
const batchResult = await apiStatsClient.getBatchStats(validIds)
|
||||
|
||||
if (batchResult.success) {
|
||||
aggregatedStats.value = batchResult.data.aggregated
|
||||
individualStats.value = batchResult.data.individual
|
||||
statsData.value = batchResult.data.aggregated // 兼容现有组件
|
||||
|
||||
// 加载聚合的模型统计
|
||||
await loadBatchModelStats(statsPeriod.value)
|
||||
|
||||
// 更新 URL
|
||||
updateBatchURL()
|
||||
} else {
|
||||
throw new Error(batchResult.message || '批量查询失败')
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Batch query error:', err)
|
||||
error.value = err.message || '批量查询统计数据失败'
|
||||
aggregatedStats.value = null
|
||||
individualStats.value = []
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// 加载批量模型统计
|
||||
async function loadBatchModelStats(period = 'daily') {
|
||||
if (apiIds.value.length === 0) return
|
||||
|
||||
modelStatsLoading.value = true
|
||||
|
||||
try {
|
||||
const result = await apiStatsClient.getBatchModelStats(apiIds.value, period)
|
||||
|
||||
if (result.success) {
|
||||
modelStats.value = result.data || []
|
||||
} else {
|
||||
throw new Error(result.message || '加载批量模型统计失败')
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Load batch model stats error:', err)
|
||||
modelStats.value = []
|
||||
} finally {
|
||||
modelStatsLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// 解析 API Keys
|
||||
function parseApiKeys() {
|
||||
if (!apiKey.value) return []
|
||||
|
||||
const keys = apiKey.value
|
||||
.split(/[,\n]+/)
|
||||
.map((key) => key.trim())
|
||||
.filter((key) => key.length > 0)
|
||||
|
||||
// 去重并限制最多30个
|
||||
const uniqueKeys = [...new Set(keys)]
|
||||
return uniqueKeys.slice(0, 30)
|
||||
}
|
||||
|
||||
// 更新批量查询 URL
|
||||
function updateBatchURL() {
|
||||
if (apiIds.value.length > 0) {
|
||||
const url = new URL(window.location)
|
||||
url.searchParams.set('apiIds', apiIds.value.join(','))
|
||||
url.searchParams.set('batch', 'true')
|
||||
window.history.pushState({}, '', url)
|
||||
}
|
||||
}
|
||||
|
||||
// 清空输入
|
||||
function clearInput() {
|
||||
apiKey.value = ''
|
||||
}
|
||||
|
||||
// 清除数据
|
||||
function clearData() {
|
||||
statsData.value = null
|
||||
@@ -306,11 +442,18 @@ export const useApiStatsStore = defineStore('apistats', () => {
|
||||
error.value = ''
|
||||
statsPeriod.value = 'daily'
|
||||
apiId.value = null
|
||||
// 清除多 Key 模式数据
|
||||
apiKeys.value = []
|
||||
apiIds.value = []
|
||||
aggregatedStats.value = null
|
||||
individualStats.value = []
|
||||
invalidKeys.value = []
|
||||
}
|
||||
|
||||
// 重置
|
||||
function reset() {
|
||||
apiKey.value = ''
|
||||
multiKeyMode.value = false
|
||||
clearData()
|
||||
}
|
||||
|
||||
@@ -328,6 +471,13 @@ export const useApiStatsStore = defineStore('apistats', () => {
|
||||
dailyStats,
|
||||
monthlyStats,
|
||||
oemSettings,
|
||||
// 多 Key 模式状态
|
||||
multiKeyMode,
|
||||
apiKeys,
|
||||
apiIds,
|
||||
aggregatedStats,
|
||||
individualStats,
|
||||
invalidKeys,
|
||||
|
||||
// Computed
|
||||
currentPeriodData,
|
||||
@@ -335,13 +485,16 @@ export const useApiStatsStore = defineStore('apistats', () => {
|
||||
|
||||
// Actions
|
||||
queryStats,
|
||||
queryBatchStats,
|
||||
loadAllPeriodStats,
|
||||
loadPeriodStats,
|
||||
loadModelStats,
|
||||
loadBatchModelStats,
|
||||
switchPeriod,
|
||||
loadStatsWithApiId,
|
||||
loadOemSettings,
|
||||
clearData,
|
||||
clearInput,
|
||||
reset
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user