修复 Token 显示为 0:API 返回 prompt_tokens+completion_tokens,非 token_used

This commit is contained in:
wangxiaoji 2026-06-30 14:50:49 +08:00
parent 65c28251f9
commit 44356571a6
1 changed files with 6 additions and 3 deletions

View File

@ -28,6 +28,9 @@ const isSuccess = (l) =>
const sum = (arr, key) =>
arr.reduce((s, l) => s + (Number(l[key]) || 0), 0);
const sumTokens = (arr) =>
arr.reduce((s, l) => s + (Number(l?.prompt_tokens) || 0) + (Number(l?.completion_tokens) || 0), 0);
export const useDashboardData = (userState, rangeDays = 7) => {
const initialized = useRef(false);
const [loading, setLoading] = useState(false);
@ -116,7 +119,7 @@ export const useDashboardData = (userState, rangeDays = 7) => {
return {
today: {
requests: todayLogs.length,
tokens: sum(todayLogs, 'token_used'),
tokens: sumTokens(todayLogs),
cost: sum(todayLogs, 'quota'),
avgLatency:
todayLatencies.length > 0
@ -128,7 +131,7 @@ export const useDashboardData = (userState, rangeDays = 7) => {
},
month: {
requests: monthLogs.length,
tokens: sum(monthLogs, 'token_used'),
tokens: sumTokens(monthLogs),
cost: sum(monthLogs, 'quota'),
},
};
@ -155,7 +158,7 @@ export const useDashboardData = (userState, rangeDays = 7) => {
date: d,
count: dayLogs.length,
cost: sum(dayLogs, 'quota'),
tokens: sum(dayLogs, 'token_used'),
tokens: sumTokens(dayLogs),
models,
});
}