chore: format usage stats route

This commit is contained in:
atoz03
2025-12-04 15:02:07 +08:00
parent 9fe2918a54
commit 6a3dce523b
2 changed files with 17 additions and 11 deletions

View File

@@ -371,7 +371,9 @@ router.get('/usage-trend', authenticateAdmin, async (req, res) => {
logger.info(` endDate (raw): ${endDate}`) logger.info(` endDate (raw): ${endDate}`)
logger.info(` startTime (parsed): ${startTime.toISOString()}`) logger.info(` startTime (parsed): ${startTime.toISOString()}`)
logger.info(` endTime (parsed): ${endTime.toISOString()}`) logger.info(` endTime (parsed): ${endTime.toISOString()}`)
logger.info(` System timezone offset: ${require('../../../config/config').system.timezoneOffset || 8}`) logger.info(
` System timezone offset: ${require('../../../config/config').system.timezoneOffset || 8}`
)
} else { } else {
// 默认最近24小时 // 默认最近24小时
endTime = new Date() endTime = new Date()
@@ -1841,7 +1843,10 @@ router.get('/api-keys/:keyId/usage-records', authenticateAdmin, async (req, res)
const startTime = startDate ? new Date(startDate) : null const startTime = startDate ? new Date(startDate) : null
const endTime = endDate ? new Date(endDate) : null const endTime = endDate ? new Date(endDate) : null
if ((startDate && Number.isNaN(startTime?.getTime())) || (endDate && Number.isNaN(endTime?.getTime()))) { if (
(startDate && Number.isNaN(startTime?.getTime())) ||
(endDate && Number.isNaN(endTime?.getTime()))
) {
return res.status(400).json({ success: false, error: 'Invalid date range' }) return res.status(400).json({ success: false, error: 'Invalid date range' })
} }
@@ -2071,14 +2076,13 @@ router.get('/api-keys/:keyId/usage-records', authenticateAdmin, async (req, res)
record.costFormatted || record.costFormatted ||
costData?.formatted?.total || costData?.formatted?.total ||
CostCalculator.formatCost(computedCost), CostCalculator.formatCost(computedCost),
costBreakdown: costBreakdown: record.costBreakdown || {
record.costBreakdown || { input: costData?.costs?.input || 0,
input: costData?.costs?.input || 0, output: costData?.costs?.output || 0,
output: costData?.costs?.output || 0, cacheCreate: costData?.costs?.cacheWrite || 0,
cacheCreate: costData?.costs?.cacheWrite || 0, cacheRead: costData?.costs?.cacheRead || 0,
cacheRead: costData?.costs?.cacheRead || 0, total: costData?.costs?.total || computedCost
total: costData?.costs?.total || computedCost },
},
responseTime: record.responseTime || null responseTime: record.responseTime || null
}) })
} }

View File

@@ -33,7 +33,9 @@ function mapExpiryField(updates, accountType, accountId) {
if ('expiresAt' in mappedUpdates) { if ('expiresAt' in mappedUpdates) {
mappedUpdates.subscriptionExpiresAt = mappedUpdates.expiresAt mappedUpdates.subscriptionExpiresAt = mappedUpdates.expiresAt
delete mappedUpdates.expiresAt delete mappedUpdates.expiresAt
logger.info(`Mapping expiresAt to subscriptionExpiresAt for ${accountType} account ${accountId}`) logger.info(
`Mapping expiresAt to subscriptionExpiresAt for ${accountType} account ${accountId}`
)
} }
return mappedUpdates return mappedUpdates
} }