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

This commit is contained in:
Andrew Peltekci
2026-03-22 19:14:01 -07:00
committed by GitHub
parent c8d2497423
commit 5fedcad624

View File

@@ -66,7 +66,8 @@ if (!isCloud && fs && typeof fs.existsSync === "function") {
// Default data structure
const defaultData = {
history: []
history: [],
totalRequestsLifetime: 0
};
// Singleton instance
@@ -240,10 +241,14 @@ export async function saveRequestUsage(entry) {
if (!Array.isArray(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);
entry.cost = entryCost;
db.data.history.push(entry);
db.data.totalRequestsLifetime += 1;
// Cap history to prevent unbounded memory/disk growth
const MAX_HISTORY = 10000;
@@ -521,8 +526,12 @@ export async function getUsageStats(period = "all") {
})
.slice(0, 20);
const lifetimeTotalRequests = typeof db.data.totalRequestsLifetime === "number"
? db.data.totalRequestsLifetime
: history.length;
const stats = {
totalRequests: history.length,
totalRequests: lifetimeTotalRequests,
totalPromptTokens: 0,
totalCompletionTokens: 0,
totalCost: 0,