Skip to content

fix(platform): aggregate usage chart buckets in SQL - #4474

Merged
yannickmonney merged 3 commits into
mainfrom
fix/usage-chart-sql-buckets
Oct 7, 2026
Merged

yannickmonney merged 3 commits into
mainfrom
fix/usage-chart-sql-buckets

Conversation

@yannickmonney

@yannickmonney yannickmonney commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

A 90-day usage view could return partial totals after hitting the 20,000-row cap on daily ledger rows. Aggregate those rows in SQL by the requested UTC/ISO-week bucket, reporting window and breakdown dimensions before applying the cap. Current and previous windows stay separate even when they share a week or month, and the existing partial flag applies to grouped rows.

Closes #4440.

The Backend integration regression seeds 22,500 daily rows across 90 days and verifies complete, equal daily/weekly/monthly totals, series buckets, prior-window isolation, user/assistant/model breakdowns, filters and tenant isolation. It also checks mixed LLM/voice classification and partial labeling above 20,000 grouped rows. Its fixture is identical at the old and new heads.

Validation:

  • 18 scoped unit/contract tests passed with one worker; scoped lint, strict TypeScript checks and diff checks passed. Commits ran normal hooks.
  • Old fails: Backend integration run 37580889418 / job 112660094839, head 99d76822e627e87bcc5d8688737d7a429becc0dd. The reader exactly matches main's code. All 234 lanes ran; the regression returned 19,999 requests instead of 22,500 and capped: true for all three granularities.
  • New passes: Backend integration run 37581757514 / job 112662863806, head 39bd715c7542a787bf7d13ff5ffa58f0283baf93. All 1,632 checks passed across 234/234 lanes; all 14 usage regression assertions passed. Daily, weekly and monthly each returned 22,500 requests, 337,500 tokens and 45,000 cost cents with capped: false.
  • H1 NOT_RUN: no existing PostgreSQL binaries in the sandbox. No installs, local stack, private PostgreSQL process or data directory were created.

Base: 524e438626f8cf156c9c8b86a5223dc55d5d6b8f. Fixed head: 39bd715c7542a787bf7d13ff5ffa58f0283baf93. Source/tree hashes, complete patch, admission readings and verification logs are retained in the TALE-1084 task's delivery artifacts. Independent review remains required; this PR is not merged.

@yannickmonney

Copy link
Copy Markdown
Contributor Author

ACCEPT — reviewed at head 39bd715c7542a787bf7d13ff5ffa58f0283baf93.

  • DATA — PASS. The query groups by the requested UTC bucket (period_key, ISO IYYY-WIW, or YYYY-MM) and explicitly by (period_key >= currentStart), so a week/month shared by the two reporting windows cannot mix current and previous spend. max(period_key) is only a representative daily key inside the already-separated group; the fold then applies the same UTC/ISO rules, filters, subject normalization, tenant predicate, breakdown classification, and partial semantics. The exact-head Backend integration fixture passed daily, weekly, and monthly totals (22,500 requests, 45,000 cents), prior-window isolation (90 requests, 630 cents), filters/tenant isolation, mixed LLM/voice classification, and the >20,000 grouped-row partial case.
  • PERFORMANCE — PASS. The read remains constrained by org_id, daily period_key >= scanStart, and period_key <= today, with the existing (org_id, period_key) index available; aggregation happens before LIMIT 20,001. For a 90-day view this produces at most 90 daily buckets, about 13 ISO weeks, or 3–4 months per dimension, instead of 90 × API-key rows. I did not run EXPLAIN; CI integration is the observed performance/data proof.
  • CODE / CI — PASS. The three-commit history is intact: the temporary typed-fixture commit 99d76822e6 is followed by the final SQL aggregation fix, with no revert leaving the fix absent. The integration test is registered in integration-check.ts; Backend integration job 112662863806 ran at this exact SHA and passed the usage checks. The job summary reports 1632/1632 checks passed across 234/234 lanes; all seven CI-ready aggregators are green (Checks, Commitlint, SAST, Security, CLI, E2E, Build).
  • SECURITY, A11Y, LOCALE, DOCS — NOT_APPLICABLE for this backend-only read/query and regression registration.

Local PostgreSQL proof: NOT_RUN (no PostgreSQL binaries available; no installs or processes started). No merge performed.

@yannickmonney

Copy link
Copy Markdown
Contributor Author

Merge-lane coverage dispositions at 39bd715c7542a787bf7d13ff5ffa58f0283baf93. From agent #2 (TALE-359 run 9d246852), the merger.

Topic Disposition Basis
DATA PASS C2: rows are grouped by the requested UTC bucket and by reporting window (period_key >= currentStart), with the same filters, tenant predicate and breakdown classification. Backend integration runs on real PostgreSQL at this exact head.
REL-MANUAL PASS; executed old-fails and new-passes Old: the temporary head 99d76822e6 (main plus the regression only) fails 11 usage-bucket checks in Backend integration job 112660094839 (19,999 requests, capped: true; 1621 of 1632 checks). New: job 112662863806 at this head passes 1632 of 1632 checks across 234 lanes.
PERFORMANCE PASS (source); EXPLAIN NOT_RUN Aggregation happens before LIMIT 20,001, using the existing (org_id, period_key) index. A 90-day view reads at most 90 daily, about 13 weekly or 3–4 monthly rows per dimension, instead of daily rows per API key (C2).
RESOURCES PASS (source) Fewer rows cross the wire; no new state.
CODE PASS C2: the three-commit history keeps the regression-first commit, and the integration test is registered in integration-check.ts.
SECURITY NOT_APPLICABLE (C2) The tenant predicate is unchanged, and the regression still covers tenant isolation.
UX-SIMPLICITY, UX-CLARITY, A11Y, UI-RESPONSIVE, LOCALE, DOCS NOT_APPLICABLE No UI, copy or docs change. Charts show complete totals instead of a partial result.
CICD PASS 82 checks: 57 success, 24 by-design skips, 1 neutral (Trivy). Backend integration ran. Tested base = current main 524e4386.
OBSERVABILITY NOT_APPLICABLE The existing partial flag still labels capped results.
DEPLOYMENT, RECOVERY, PROVIDERS, WORKFLOW NOT_APPLICABLE Read query only; no schema or migration.

Merge-gate dry run at 07:58:29Z: open, CLEAN, head matches; no required contexts. checks total=82 not-completed=0 failing=0 [neutral=1 skipped=24 success=57] neutral=[Trivy[github-advanced-security]] statuses=[pending total=0]. Every skip is on the by-design allowlist skip-allow-1545-bi.txt; no check is pending or failing. Merging next with --squash --match-head-commit 39bd715c7542a787bf7d13ff5ffa58f0283baf93.

@yannickmonney
yannickmonney merged commit e011cc8 into main Oct 7, 2026
@yannickmonney
yannickmonney deleted the fix/usage-chart-sql-buckets branch October 7, 2026 07:58
yannickmonney added a commit that referenced this pull request Oct 7, 2026
Cover all ten merges after a89498d through e011cc8: #4464, #4422,
#4468, #4470, #4457, #4465, #4472, #4469 and #4474, plus test-only
#4473. Apply C3's cumulative upgrade path from v0.5.74 and pin its
v0.5.75 notes link to the v0.5.75 tag. Drop the #4438, #4466 and #4440
known issues, now fixed, and list the Environment docs drift (#4454).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(platform): usage charts read daily ledger rows for every granularity, so large orgs hit the 20,000-row cap sooner on 90-day views

1 participant