mirror of
https://github.com/Wei-Shaw/claude-relay-service.git
synced 2026-01-23 00:53:33 +00:00
fix: user stats again
This commit is contained in:
@@ -138,27 +138,9 @@ class UserService {
|
|||||||
// 📊 计算用户使用统计(通过聚合API Keys)
|
// 📊 计算用户使用统计(通过聚合API Keys)
|
||||||
async calculateUserUsageStats(userId) {
|
async calculateUserUsageStats(userId) {
|
||||||
try {
|
try {
|
||||||
// Use redis directly to avoid circular dependency
|
// Use the existing apiKeyService method which already includes usage stats
|
||||||
const client = redis.getClientSafe()
|
const apiKeyService = require('./apiKeyService')
|
||||||
const pattern = 'api_key:*'
|
const userApiKeys = await apiKeyService.getUserApiKeys(userId)
|
||||||
const keys = await client.keys(pattern)
|
|
||||||
|
|
||||||
const userApiKeys = []
|
|
||||||
for (const key of keys) {
|
|
||||||
const keyData = await client.get(key)
|
|
||||||
if (keyData) {
|
|
||||||
const apiKey = JSON.parse(keyData)
|
|
||||||
if (apiKey.userId === userId) {
|
|
||||||
// Get usage stats for this API key
|
|
||||||
const usage = await redis.getUsageStats(apiKey.id)
|
|
||||||
userApiKeys.push({
|
|
||||||
id: apiKey.id,
|
|
||||||
name: apiKey.name,
|
|
||||||
usage
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const totalUsage = {
|
const totalUsage = {
|
||||||
requests: 0,
|
requests: 0,
|
||||||
@@ -176,6 +158,10 @@ class UserService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
`📊 Calculated user ${userId} usage: ${totalUsage.requests} requests, ${totalUsage.inputTokens} input tokens, $${totalUsage.totalCost.toFixed(4)} total cost from ${userApiKeys.length} API keys`
|
||||||
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
totalUsage,
|
totalUsage,
|
||||||
apiKeyCount: userApiKeys.length
|
apiKeyCount: userApiKeys.length
|
||||||
|
|||||||
Reference in New Issue
Block a user