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

5
app.js
View File

@@ -24,7 +24,8 @@ import {
MIN_AUTH_FILES_PAGE_SIZE, MIN_AUTH_FILES_PAGE_SIZE,
MAX_AUTH_FILES_PAGE_SIZE, MAX_AUTH_FILES_PAGE_SIZE,
OAUTH_CARD_IDS, OAUTH_CARD_IDS,
STORAGE_KEY_AUTH_FILES_PAGE_SIZE STORAGE_KEY_AUTH_FILES_PAGE_SIZE,
NOTIFICATION_DURATION_MS
} from './src/utils/constants.js'; } from './src/utils/constants.js';
// 核心服务导入 // 核心服务导入
@@ -514,7 +515,7 @@ class CLIProxyManager {
setTimeout(() => { setTimeout(() => {
notification.classList.remove('show'); notification.classList.remove('show');
}, 3000); }, NOTIFICATION_DURATION_MS);
} }
// 密钥可见性切换 // 密钥可见性切换

View File

@@ -297,38 +297,7 @@ export const connectionModule = {
const response = await this.makeRequest('/usage'); const response = await this.makeRequest('/usage');
usageData = response?.usage || null; usageData = response?.usage || null;
if (usageData) { if (usageData) {
// 从usage数据中提取keyStats keyStats = await this.getKeyStats(usageData);
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;
} }
} catch (error) { } catch (error) {
console.warn('获取usage统计失败:', error); console.warn('获取usage统计失败:', error);
@@ -438,4 +407,3 @@ export const connectionModule = {
} }
} }
}; };

View File

@@ -271,7 +271,7 @@ export const apiKeysModule = {
body: JSON.stringify(currentKeys) body: JSON.stringify(currentKeys)
}); });
this.clearCache(); // 清除缓存 this.clearCache('api-keys'); // 清除 api-keys 段缓存
this.closeModal(); this.closeModal();
this.loadApiKeys(); this.loadApiKeys();
this.showNotification(i18n.t('notification.api_key_added'), 'success'); this.showNotification(i18n.t('notification.api_key_added'), 'success');
@@ -315,7 +315,7 @@ export const apiKeysModule = {
body: JSON.stringify({ index, value: newKey }) body: JSON.stringify({ index, value: newKey })
}); });
this.clearCache(); // 清除缓存 this.clearCache('api-keys'); // 清除 api-keys 段缓存
this.closeModal(); this.closeModal();
this.loadApiKeys(); this.loadApiKeys();
this.showNotification(i18n.t('notification.api_key_updated'), 'success'); this.showNotification(i18n.t('notification.api_key_updated'), 'success');
@@ -330,7 +330,7 @@ export const apiKeysModule = {
try { try {
await this.makeRequest(`/api-keys?index=${index}`, { method: 'DELETE' }); await this.makeRequest(`/api-keys?index=${index}`, { method: 'DELETE' });
this.clearCache(); // 清除缓存 this.clearCache('api-keys'); // 清除 api-keys 段缓存
this.loadApiKeys(); this.loadApiKeys();
this.showNotification(i18n.t('notification.api_key_deleted'), 'success'); this.showNotification(i18n.t('notification.api_key_deleted'), 'success');
} catch (error) { } catch (error) {

View File

@@ -1,9 +1,12 @@
// 获取API密钥的统计信息 // 获取API密钥的统计信息
export async function getKeyStats() { export async function getKeyStats(usageData = null) {
try { try {
const response = await this.makeRequest('/usage'); let usage = usageData;
const usage = response?.usage || null; if (!usage) {
const response = await this.makeRequest('/usage');
usage = response?.usage || null;
}
if (!usage) { if (!usage) {
return {}; return {};
} }
@@ -20,7 +23,7 @@ export async function getKeyStats() {
details.forEach(detail => { details.forEach(detail => {
const source = detail.source; const source = detail.source;
if (!source) return; if (!source) return;
if (!sourceStats[source]) { if (!sourceStats[source]) {
sourceStats[source] = { sourceStats[source] = {
success: 0, success: 0,