mirror of
https://github.com/Wei-Shaw/claude-relay-service.git
synced 2026-01-22 16:43:35 +00:00
fix: user stats in admin panel
This commit is contained in:
@@ -577,8 +577,8 @@ router.get('/:userId/usage-stats', authenticateUserOrAdmin, requireAdmin, async
|
||||
})
|
||||
}
|
||||
|
||||
// 获取用户的API Keys
|
||||
const userApiKeys = await apiKeyService.getUserApiKeys(userId)
|
||||
// 获取用户的API Keys(包括已删除的以保留统计数据)
|
||||
const userApiKeys = await apiKeyService.getUserApiKeys(userId, true)
|
||||
const apiKeyIds = userApiKeys.map((key) => key.id)
|
||||
|
||||
if (apiKeyIds.length === 0) {
|
||||
|
||||
@@ -205,6 +205,23 @@ class UserService {
|
||||
continue
|
||||
}
|
||||
|
||||
// Calculate dynamic usage stats for each user
|
||||
try {
|
||||
const usageStats = await this.calculateUserUsageStats(user.id)
|
||||
user.totalUsage = usageStats.totalUsage
|
||||
user.apiKeyCount = usageStats.apiKeyCount
|
||||
} catch (error) {
|
||||
logger.error(`❌ Error calculating usage for user ${user.id}:`, error)
|
||||
// Fallback to stored values
|
||||
user.totalUsage = user.totalUsage || {
|
||||
requests: 0,
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
totalCost: 0
|
||||
}
|
||||
user.apiKeyCount = user.apiKeyCount || 0
|
||||
}
|
||||
|
||||
users.push(user)
|
||||
}
|
||||
}
|
||||
@@ -448,11 +465,23 @@ class UserService {
|
||||
stats.regularUsers++
|
||||
}
|
||||
|
||||
stats.totalApiKeys += user.apiKeyCount || 0
|
||||
stats.totalUsage.requests += user.totalUsage?.requests || 0
|
||||
stats.totalUsage.inputTokens += user.totalUsage?.inputTokens || 0
|
||||
stats.totalUsage.outputTokens += user.totalUsage?.outputTokens || 0
|
||||
stats.totalUsage.totalCost += user.totalUsage?.totalCost || 0
|
||||
// Calculate dynamic usage stats for each user
|
||||
try {
|
||||
const usageStats = await this.calculateUserUsageStats(user.id)
|
||||
stats.totalApiKeys += usageStats.apiKeyCount
|
||||
stats.totalUsage.requests += usageStats.totalUsage.requests
|
||||
stats.totalUsage.inputTokens += usageStats.totalUsage.inputTokens
|
||||
stats.totalUsage.outputTokens += usageStats.totalUsage.outputTokens
|
||||
stats.totalUsage.totalCost += usageStats.totalUsage.totalCost
|
||||
} catch (error) {
|
||||
logger.error(`❌ Error calculating usage for user ${user.id} in stats:`, error)
|
||||
// Fallback to stored values if calculation fails
|
||||
stats.totalApiKeys += user.apiKeyCount || 0
|
||||
stats.totalUsage.requests += user.totalUsage?.requests || 0
|
||||
stats.totalUsage.inputTokens += user.totalUsage?.inputTokens || 0
|
||||
stats.totalUsage.outputTokens += user.totalUsage?.outputTokens || 0
|
||||
stats.totalUsage.totalCost += user.totalUsage?.totalCost || 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user