refactor: centralize usage stats and refine api key cache

This commit is contained in:
hkfires
2025-11-17 14:55:57 +08:00
parent ad520b7b26
commit fea36b1ca9
4 changed files with 15 additions and 43 deletions

View File

@@ -297,38 +297,7 @@ export const connectionModule = {
const response = await this.makeRequest('/usage');
usageData = response?.usage || null;
if (usageData) {
// 从usage数据中提取keyStats
const sourceStats = {};
const apis = usageData.apis || {};
Object.values(apis).forEach(apiEntry => {
const models = apiEntry.models || {};
Object.values(models).forEach(modelEntry => {
const details = modelEntry.details || [];
details.forEach(detail => {
const source = detail.source;
if (!source) return;
if (!sourceStats[source]) {
sourceStats[source] = {
success: 0,
failure: 0
};
}
const isFailed = detail.failed === true;
if (isFailed) {
sourceStats[source].failure += 1;
} else {
sourceStats[source].success += 1;
}
});
});
});
keyStats = sourceStats;
keyStats = await this.getKeyStats(usageData);
}
} catch (error) {
console.warn('获取usage统计失败:', error);
@@ -438,4 +407,3 @@ export const connectionModule = {
}
}
};