fix:调整去重策略

- 调整去重策略(src/routes/admin/usageStats.js):账户筛选改为按 accountId 聚合记录所有出现的 accountType,构建 options 时依次按历史类型解析账号,失败再全量回退,无法解析也保留为筛选项并带 rawTypes,避免渠道改名/删除导致选项被“去
    重”丢失。
  - 解析兜底(src/routes/admin/usageStats.js):resolveAccountInfo 在传入未知类型或过滤后为空时回退尝试全部服务,减轻渠道改名解析不到的问题。
This commit is contained in:
atoz03
2025-12-05 19:19:52 +08:00
parent bfa3f528a2
commit fbb660138c

View File

@@ -1942,10 +1942,15 @@ router.get('/api-keys/:keyId/usage-records', authenticateAdmin, async (req, res)
return accountCache.get(cacheKey) return accountCache.get(cacheKey)
} }
const servicesToTry = type let servicesToTry = type
? accountServices.filter((svc) => svc.type === type) ? accountServices.filter((svc) => svc.type === type)
: accountServices : accountServices
// 若渠道改名或传入未知类型,回退尝试全量服务,避免漏解析历史账号
if (!servicesToTry.length) {
servicesToTry = accountServices
}
for (const service of servicesToTry) { for (const service of servicesToTry) {
try { try {
const account = await service.getter(id) const account = await service.getter(id)
@@ -2055,12 +2060,14 @@ router.get('/api-keys/:keyId/usage-records', authenticateAdmin, async (req, res)
} }
if (record.accountId) { if (record.accountId) {
const key = `${record.accountId}:${record.accountType || 'unknown'}` const normalizedType = record.accountType || 'unknown'
if (!accountOptionMap.has(key)) { if (!accountOptionMap.has(record.accountId)) {
accountOptionMap.set(key, { accountOptionMap.set(record.accountId, {
id: record.accountId, id: record.accountId,
accountType: record.accountType || 'unknown' accountTypes: new Set([normalizedType])
}) })
} else {
accountOptionMap.get(record.accountId).accountTypes.add(normalizedType)
} }
} }
@@ -2133,23 +2140,37 @@ router.get('/api-keys/:keyId/usage-records', authenticateAdmin, async (req, res)
} }
const accountOptions = [] const accountOptions = []
const accountIdAdded = new Set()
for (const option of accountOptionMap.values()) { for (const option of accountOptionMap.values()) {
const info = await resolveAccountInfo(option.id, option.accountType) const types = Array.from(option.accountTypes || [])
if (info && info.name) {
if (accountIdAdded.has(option.id)) { // 优先按历史出现的 accountType 解析,若失败则回退全量解析
continue let resolvedInfo = null
for (const type of types) {
resolvedInfo = await resolveAccountInfo(option.id, type)
if (resolvedInfo && resolvedInfo.name) {
break
} }
accountIdAdded.add(option.id)
accountOptions.push({
id: option.id,
name: info.name,
accountType: info.type,
accountTypeName: accountTypeNames[info.type] || '未知渠道'
})
} else {
logger.warn(`⚠️ Skipping deleted/invalid account in filter options: ${option.id}`)
} }
if (!resolvedInfo) {
resolvedInfo = await resolveAccountInfo(option.id)
}
const chosenType = resolvedInfo?.type || types[0] || 'unknown'
const chosenTypeName = accountTypeNames[chosenType] || '未知渠道'
if (!resolvedInfo) {
logger.warn(
`⚠️ 保留无法解析的账户筛选项: ${option.id}, types=${types.join(',') || 'none'}`
)
}
accountOptions.push({
id: option.id,
name: resolvedInfo?.name || option.id,
accountType: chosenType,
accountTypeName: chosenTypeName,
rawTypes: types
})
} }
return res.json({ return res.json({