Repository navigation
Fix analytics follow-up regressions from #2904 - #2918
diegosouzapw merged 2 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the usage analytics route to prevent double-counting of raw and aggregated rows and to gate off aggregated rows when filtering by API key to prevent data leakage. It also refactors the database cleanup process to abort deletion if the pre-delete rollup fails, and adds settings schema fields for sidebar customization along with corresponding unit tests. Feedback on the changes highlights a critical runtime ReferenceError in cleanup.ts due to the removal of cutoffDateStr from cleanupUsageHistory while still being referenced, as well as its accidental and unused definition in cleanupQuotaSnapshots.
diegosouzapw
left a comment
There was a problem hiding this comment.
Approved after local verification, quality checks and merging into release/v3.8.7.
2467a06
into
cheaper-inference:release/v3.8.7
…ytics-gemini-followup-v387 Fix analytics follow-up regressions from cheaper-inference#2904
Summary
daily_usage_summaryowns dates before the raw cutoff day, andusage_historyowns the cutoff day forward.daily_usage_summaryrows whenever an API-key filter is active, sincedaily_usage_summarydoes not store API-key attribution.sidebarSectionOrder,sidebarItemOrder,sidebarActivePreset) and theSIDEBAR_SECTIONSimport that were accidentally dropped.usage_historydeletion when the pre-delete rollup reports errors.WHERE DATE(timestamp) < ?withWHERE timestamp < ?.Related Issues
Validation
npm run lintnpm run test:unitnpm run test:coverage>= 60%for statements, lines, functions, and branchesTargeted validation run locally:
npx eslint src/app/api/usage/analytics/route.ts src/lib/db/cleanup.ts src/lib/usage/aggregateHistory.ts src/shared/validation/settingsSchemas.ts tests/unit/usage-analytics-route.test.tsnode --import tsx --import ./open-sse/utils/setupPolyfill.ts --test tests/unit/usage-analytics-route.test.ts tests/unit/token-accounting-input-fix.test.ts tests/unit/database-settings-maintenance.test.tsBoth passed.
Full
npm run lint,npm run test:unit, andnpm run test:coveragewere not run for this follow-up.Tests Added Or Updated
tests/unit/usage-analytics-route.test.tsusage_historyrows anddaily_usage_summaryrows are not double-counted.daily_usage_summary.Coverage Notes
src/app/api/usage/analytics/route.tsis covered by the updated analytics route unit tests.src/lib/db/cleanup.tsandsrc/lib/usage/aggregateHistory.tsare covered bytests/unit/database-settings-maintenance.test.tsfor aggregation/maintenance behavior.src/shared/validation/settingsSchemas.tschange restores existing sidebar settings schema fields; no new dedicated schema test was added.Reviewer Notes
daily_usage_summaryrows whenapiKeyIdsfiltering is active. Because summary rows do not store API-key attribution, including them would mix other keys’ aggregate usage into a filtered response.YYYY-MM-DD) rather than a partial timestamp. This keeps daily summary rows and raw rows from sharing the same day and avoids partial-day gaps or overlaps.usage_historyrows in place until the next successful cleanup, but avoids permanent loss of usage data.