fix(cleanup): restore usage history cutoff boundary

This commit is contained in:
Halil Tezcan KARABULUT
2026-05-29 23:08:34 +03:00
committed by diegosouzapw
parent 6123e482a9
commit 25741a7fb0
2 changed files with 34 additions and 1 deletions

View File

@@ -28,7 +28,6 @@ export async function cleanupQuotaSnapshots(): Promise<CleanupResult> {
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - retentionDays);
const cutoffISO = cutoffDate.toISOString();
const cutoffDateStr = cutoffISO.split("T")[0];
const result: CleanupResult = { deleted: 0, errors: 0 };
@@ -87,6 +86,7 @@ export async function cleanupUsageHistory(): Promise<CleanupResult> {
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - retentionDays);
const cutoffISO = cutoffDate.toISOString();
const cutoffDateStr = cutoffISO.split("T")[0];
const result: CleanupResult = { deleted: 0, errors: 0 };

View File

@@ -186,3 +186,36 @@ test("usage aggregation upserts replace recomputed totals instead of adding them
assert.equal(hourly.total_output_tokens, 10);
assert.equal(hourly.total_cost, 0.75);
});
test("cleanupUsageHistory rolls up and deletes old rows using the same day boundary", async () => {
const db = core.getDbInstance();
const oldTimestamp = "2024-01-01T12:00:00.000Z";
const recentTimestamp = new Date().toISOString();
databaseSettings.updateDatabaseSettings({
retention: {
...databaseSettings.getUserDatabaseSettings().retention,
usageHistory: 30,
},
});
const insertUsage = db.prepare(
`INSERT INTO usage_history (provider, model, timestamp, tokens_input, tokens_output, success, latency_ms)
VALUES (?, ?, ?, ?, ?, ?, ?)`
);
insertUsage.run("openai", "gpt-test", oldTimestamp, 100, 40, 1, 200);
insertUsage.run("openai", "gpt-test", recentTimestamp, 7, 3, 1, 100);
const result = await cleanup.cleanupUsageHistory();
assert.equal(result.errors, 0);
assert.equal(result.deleted, 1);
const remaining = db.prepare("SELECT COUNT(*) AS count FROM usage_history").get() as CountRow;
assert.equal(remaining.count, 1);
const daily = db.prepare("SELECT * FROM daily_usage_summary").get() as UsageSummaryRow;
assert.equal(daily.total_requests, 1);
assert.equal(daily.total_input_tokens, 100);
assert.equal(daily.total_output_tokens, 40);
});