Merge branch 'Wei-Shaw:main' into main

This commit is contained in:
千羽
2025-08-04 14:47:32 +09:00
committed by GitHub
52 changed files with 1094 additions and 582 deletions

View File

@@ -269,6 +269,28 @@ router.post('/api/user-stats', async (req, res) => {
}
}
// 获取当前使用量
let currentWindowRequests = 0;
let currentWindowTokens = 0;
let currentDailyCost = 0;
try {
// 获取当前时间窗口的请求次数和Token使用量
if (fullKeyData.rateLimitWindow > 0) {
const client = redis.getClientSafe();
const requestCountKey = `rate_limit:requests:${keyId}`;
const tokenCountKey = `rate_limit:tokens:${keyId}`;
currentWindowRequests = parseInt(await client.get(requestCountKey) || '0');
currentWindowTokens = parseInt(await client.get(tokenCountKey) || '0');
}
// 获取当日费用
currentDailyCost = await redis.getDailyCost(keyId) || 0;
} catch (error) {
logger.warn(`Failed to get current usage for key ${keyId}:`, error);
}
// 构建响应数据只返回该API Key自己的信息确保不泄露其他信息
const responseData = {
id: keyId,
@@ -296,13 +318,17 @@ router.post('/api/user-stats', async (req, res) => {
}
},
// 限制信息(显示配置,不显示当前使用量)
// 限制信息(显示配置当前使用量)
limits: {
tokenLimit: fullKeyData.tokenLimit || 0,
concurrencyLimit: fullKeyData.concurrencyLimit || 0,
rateLimitWindow: fullKeyData.rateLimitWindow || 0,
rateLimitRequests: fullKeyData.rateLimitRequests || 0,
dailyCostLimit: fullKeyData.dailyCostLimit || 0
dailyCostLimit: fullKeyData.dailyCostLimit || 0,
// 当前使用量
currentWindowRequests: currentWindowRequests,
currentWindowTokens: currentWindowTokens,
currentDailyCost: currentDailyCost
},
// 绑定的账户信息只显示ID不显示敏感信息

View File

@@ -192,6 +192,7 @@ class ApiKeyService {
async getAllApiKeys() {
try {
const apiKeys = await redis.getAllApiKeys();
const client = redis.getClientSafe();
// 为每个key添加使用统计和当前并发数
for (const key of apiKeys) {
@@ -207,6 +208,19 @@ class ApiKeyService {
key.permissions = key.permissions || 'all'; // 兼容旧数据
key.dailyCostLimit = parseFloat(key.dailyCostLimit || 0);
key.dailyCost = await redis.getDailyCost(key.id) || 0;
// 获取当前时间窗口的请求次数和Token使用量
if (key.rateLimitWindow > 0) {
const requestCountKey = `rate_limit:requests:${key.id}`;
const tokenCountKey = `rate_limit:tokens:${key.id}`;
key.currentWindowRequests = parseInt(await client.get(requestCountKey) || '0');
key.currentWindowTokens = parseInt(await client.get(tokenCountKey) || '0');
} else {
key.currentWindowRequests = 0;
key.currentWindowTokens = 0;
}
try {
key.restrictedModels = key.restrictedModels ? JSON.parse(key.restrictedModels) : [];
} catch (e) {