fix(heartbeat): record cost_cents for subscription_included events#200
Open
om952 wants to merge 1 commit into
Open
fix(heartbeat): record cost_cents for subscription_included events#200om952 wants to merge 1 commit into
om952 wants to merge 1 commit into
Conversation
- Remove early return that forced cost_cents=0 for subscription_included. - normalizeBilledCostCents now rounds costUsd*100 for all billing types. - Add focused unit tests for the normalization helper. - Add migration 0087 to backfill historical cost_events from heartbeat_runs.result_json. - Add embedded-Postgres tests for the backfill migration. Fixes OpenScanAI#27
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.
Summary
Heartbeat cost events with billing type
subscription_includedwere incorrectly stored withcost_cents = 0becausenormalizeBilledCostCentsreturned 0 for that billing type. This fix removes the early return and adds a historical backfill migration.Changes
server/src/services/heartbeat.ts
subscription_includedearly return innormalizeBilledCostCents.normalizeBilledCostCentsfor focused unit testing.Math.max(0, Math.round(costUsd * 100)).server/src/tests/heartbeat-cost-cents.test.ts
metered_api,subscription_included,subscription_overage, negative clamping, and NaN/Infinity.packages/db/src/migrations/0087_backfill_subscription_included_cost_cents.sql
cost_events.cost_centsfromheartbeat_runs.result_jsonfor rows wherebilling_type = 'subscription_included'andcost_cents = 0.costUsd,cost_usd,total_cost_usd.ROUND(cost * 100)and clamps to>= 0.packages/db/src/migrations/meta/_journal.json
packages/db/src/backfill-subscription-included-cost-cents.test.ts
Verification
heartbeat-cost-cents.test.ts: 6/6 passedcosts-service.test.ts: 16/16 passedmonthly-spend-service.test.ts: 2/2 passedbackfill-subscription-included-cost-cents.test.ts: 3/3 passedpackages/dbmigration numbering check and build: passedNotes
agents.spent_monthly_centsandcompanies.spent_monthly_centsare hydrated from current-monthcost_eventson read, so correcting historicalcost_eventsautomatically corrects displayed spend.Fixes #27