mod: 补充API KEY详情页进度条

This commit is contained in:
sczheng
2026-01-23 09:54:00 +08:00
parent 2180c42b84
commit c0c944f904
6 changed files with 158 additions and 27 deletions

View File

@@ -1029,6 +1029,7 @@ router.post('/api-keys/batch-stats', authenticateAdmin, async (req, res) => {
cost: 0,
formattedCost: '$0.00',
dailyCost: 0,
weeklyOpusCost: 0,
currentWindowCost: 0,
windowRemainingSeconds: null,
allTimeCost: 0,
@@ -1109,6 +1110,7 @@ async function calculateKeyStats(keyId, timeRange, startDate, endDate) {
// 获取实时限制数据(窗口数据不受时间范围筛选影响,始终获取当前窗口状态)
let dailyCost = 0
let weeklyOpusCost = 0 // 字段名沿用 weeklyOpusCost*语义为“Claude 周费用”
let currentWindowCost = 0
let windowRemainingSeconds = null
let windowStartTime = null
@@ -1121,6 +1123,7 @@ async function calculateKeyStats(keyId, timeRange, startDate, endDate) {
const rateLimitWindow = parseInt(apiKey?.rateLimitWindow) || 0
const dailyCostLimit = parseFloat(apiKey?.dailyCostLimit) || 0
const totalCostLimit = parseFloat(apiKey?.totalCostLimit) || 0
const weeklyOpusCostLimit = parseFloat(apiKey?.weeklyOpusCostLimit) || 0
// 只在启用了每日费用限制时查询
if (dailyCostLimit > 0) {
@@ -1133,6 +1136,11 @@ async function calculateKeyStats(keyId, timeRange, startDate, endDate) {
allTimeCost = parseFloat((await client.get(totalCostKey)) || '0')
}
// 只在启用了 Claude 周费用限制时查询(字段名沿用 weeklyOpusCostLimit
if (weeklyOpusCostLimit > 0) {
weeklyOpusCost = await redis.getWeeklyOpusCost(keyId)
}
// 🔧 FIX: 对于 "全部时间" 时间范围,直接使用 allTimeCost
// 因为 usage:*:model:daily:* 键有 30 天 TTL旧数据已经过期
if (timeRange === 'all' && allTimeCost > 0) {
@@ -1149,6 +1157,7 @@ async function calculateKeyStats(keyId, timeRange, startDate, endDate) {
formattedCost: CostCalculator.formatCost(allTimeCost),
// 实时限制数据(始终返回,不受时间范围影响)
dailyCost,
weeklyOpusCost,
currentWindowCost,
windowRemainingSeconds,
windowStartTime,
@@ -1199,6 +1208,7 @@ async function calculateKeyStats(keyId, timeRange, startDate, endDate) {
formattedCost: '$0.00',
// 实时限制数据(始终返回,不受时间范围影响)
dailyCost,
weeklyOpusCost,
currentWindowCost,
windowRemainingSeconds,
windowStartTime,
@@ -1317,6 +1327,7 @@ async function calculateKeyStats(keyId, timeRange, startDate, endDate) {
formattedCost: CostCalculator.formatCost(totalCost),
// 实时限制数据
dailyCost,
weeklyOpusCost,
currentWindowCost,
windowRemainingSeconds,
windowStartTime,

View File

@@ -762,7 +762,7 @@ class ApiKeyService {
for (const key of apiKeys) {
key.usage = await redis.getUsageStats(key.id)
const costStats = await redis.getCostStats(key.id)
// Add cost information to usage object for frontend compatibility
// 为前端兼容性:把费用信息同步到 usage 对象里
if (key.usage && costStats) {
key.usage.total = key.usage.total || {}
key.usage.total.cost = costStats.total