fix: 修复API Keys页面时间窗口进度条和Token数值显示问题

- 修复时间窗口限制的请求次数和Token使用量进度条不更新的问题
  - 在apiKeyService.getAllApiKeys()中添加获取当前窗口统计数据的逻辑
  - 从Redis读取rate_limit:requests和rate_limit:tokens键的值

- 优化Token数值展示,添加K/M单位格式化
  - UsageDetailModal组件中5处Token数值改用formatTokenCount
  - ApiKeysView模型统计中5处Token数值改用formatTokenCount
  - 统一使用K/M单位简化大数字显示

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
shaw
2025-08-04 10:54:57 +08:00
parent 52718ef608
commit 4e3c826b6c
3 changed files with 26 additions and 12 deletions

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) {