mirror of
https://github.com/Wei-Shaw/claude-relay-service.git
synced 2026-01-23 00:53:33 +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
|
// 获取用户的API Keys(包括已删除的以保留统计数据)
|
||||||
const userApiKeys = await apiKeyService.getUserApiKeys(userId)
|
const userApiKeys = await apiKeyService.getUserApiKeys(userId, true)
|
||||||
const apiKeyIds = userApiKeys.map((key) => key.id)
|
const apiKeyIds = userApiKeys.map((key) => key.id)
|
||||||
|
|
||||||
if (apiKeyIds.length === 0) {
|
if (apiKeyIds.length === 0) {
|
||||||
|
|||||||
@@ -205,6 +205,23 @@ class UserService {
|
|||||||
continue
|
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)
|
users.push(user)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -448,11 +465,23 @@ class UserService {
|
|||||||
stats.regularUsers++
|
stats.regularUsers++
|
||||||
}
|
}
|
||||||
|
|
||||||
stats.totalApiKeys += user.apiKeyCount || 0
|
// Calculate dynamic usage stats for each user
|
||||||
stats.totalUsage.requests += user.totalUsage?.requests || 0
|
try {
|
||||||
stats.totalUsage.inputTokens += user.totalUsage?.inputTokens || 0
|
const usageStats = await this.calculateUserUsageStats(user.id)
|
||||||
stats.totalUsage.outputTokens += user.totalUsage?.outputTokens || 0
|
stats.totalApiKeys += usageStats.apiKeyCount
|
||||||
stats.totalUsage.totalCost += user.totalUsage?.totalCost || 0
|
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