Refactor UsageChart and UsageStats components to support dynamic period selection
This commit is contained in:
parent
11b2fcd643
commit
7195fee2f6
5 changed files with 116 additions and 59 deletions
|
|
@ -429,12 +429,21 @@ async function calculateCost(provider, model, tokens) {
|
|||
}
|
||||
}
|
||||
|
||||
const PERIOD_MS = { "24h": 86400000, "7d": 604800000, "30d": 2592000000, "60d": 5184000000 };
|
||||
|
||||
/**
|
||||
* Get aggregated usage stats
|
||||
* @param {"24h"|"7d"|"30d"|"60d"|"all"} period - Time period to filter
|
||||
*/
|
||||
export async function getUsageStats() {
|
||||
export async function getUsageStats(period = "all") {
|
||||
const db = await getUsageDb();
|
||||
const history = db.data.history || [];
|
||||
let history = db.data.history || [];
|
||||
|
||||
// Filter history by period
|
||||
if (period && PERIOD_MS[period]) {
|
||||
const cutoff = Date.now() - PERIOD_MS[period];
|
||||
history = history.filter((e) => new Date(e.timestamp).getTime() >= cutoff);
|
||||
}
|
||||
|
||||
// Import localDb to get provider connection names and API keys
|
||||
const { getProviderConnections, getApiKeys, getProviderNodes } = await import("@/lib/localDb.js");
|
||||
|
|
@ -571,8 +580,8 @@ export async function getUsageStats() {
|
|||
const completionTokens = entry.tokens?.completion_tokens || 0;
|
||||
const entryTime = new Date(entry.timestamp);
|
||||
|
||||
// Calculate cost for this entry
|
||||
const entryCost = await calculateCost(entry.provider, entry.model, entry.tokens);
|
||||
// Use pre-stored cost (saved at request time), avoid recalculating
|
||||
const entryCost = entry.cost || 0;
|
||||
|
||||
stats.totalPromptTokens += promptTokens;
|
||||
stats.totalCompletionTokens += completionTokens;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue