fix(usage): track lifetime request total beyond history cap (#366)

This commit is contained in:
Andrew Peltekci
2026-03-23 09:14:01 +07:00
committed by GitHub
parent c8d2497423
commit 5fedcad624
+11 -2
View File
@@ -66,7 +66,8 @@ if (!isCloud && fs && typeof fs.existsSync === "function") {
// Default data structure // Default data structure
const defaultData = { const defaultData = {
history: [] history: [],
totalRequestsLifetime: 0
}; };
// Singleton instance // Singleton instance
@@ -240,10 +241,14 @@ export async function saveRequestUsage(entry) {
if (!Array.isArray(db.data.history)) { if (!Array.isArray(db.data.history)) {
db.data.history = []; db.data.history = [];
} }
if (typeof db.data.totalRequestsLifetime !== "number") {
db.data.totalRequestsLifetime = db.data.history.length;
}
const entryCost = await calculateCost(entry.provider, entry.model, entry.tokens); const entryCost = await calculateCost(entry.provider, entry.model, entry.tokens);
entry.cost = entryCost; entry.cost = entryCost;
db.data.history.push(entry); db.data.history.push(entry);
db.data.totalRequestsLifetime += 1;
// Cap history to prevent unbounded memory/disk growth // Cap history to prevent unbounded memory/disk growth
const MAX_HISTORY = 10000; const MAX_HISTORY = 10000;
@@ -521,8 +526,12 @@ export async function getUsageStats(period = "all") {
}) })
.slice(0, 20); .slice(0, 20);
const lifetimeTotalRequests = typeof db.data.totalRequestsLifetime === "number"
? db.data.totalRequestsLifetime
: history.length;
const stats = { const stats = {
totalRequests: history.length, totalRequests: lifetimeTotalRequests,
totalPromptTokens: 0, totalPromptTokens: 0,
totalCompletionTokens: 0, totalCompletionTokens: 0,
totalCost: 0, totalCost: 0,