fix(usage): compute credit balance from all-time usage, not a 30-day window#44
Merged
ralyodio merged 1 commit intoJul 2, 2026
Conversation
…window /api/usage computed balanceUsd = totalCredits - totalUsage where totalCredits summed ALL confirmed/forwarded credit_deposits (no date filter) but totalUsage summed only the usage_events from the last 30 days (and capped at 1000 rows, since that query also feeds the history/breakdowns). So any spend older than 30 days effectively 'came back' and the remaining balance was overstated (e.g. top up $100, spend $90 in January, return in March -> balance shows ~$100 again). Subtract an all-time usage total for the balance via a DB-side aggregate, falling back to the recent-window sum if PostgREST aggregates are unavailable (so it can never overstate more than before). The 30-day query is kept for history/daily/module breakdowns.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
GET /api/usagecomputedbalanceUsd = totalCredits − totalUsage, wheretotalCreditssums all-time confirmed/forwardedcredit_depositsbuttotalUsagesums only the last-30-daysusage_events(that query is windowed +.limit(1000)because it also feeds the history/breakdowns). So any spend older than 30 days is never subtracted and the balance is overstated — e.g. top up $100, spend $90 in January, return in March → balance shows ~$100 again (see #43).Fix
Subtract an all-time usage total for the balance, computed via a DB-side aggregate (
cost_usd.sum()), while keeping the existing 30-day query for thehistory/daily/module breakdowns. It falls back to the recent-window sum if PostgREST aggregates aren't available, so it can never overstate more than the current behavior:Note
If you'd prefer a dedicated
usage_total_cost(user_id)SQL function (the repo already uses.rpc(...)elsewhere) over the PostgREST aggregate, I'm happy to switch to a migration + RPC — just say the word. The aggregate keeps this PR code-only and safe by construction.Fixes #43.