Conversation
📝 WalkthroughWalkthroughThis PR refactors the usage-based rating API by introducing Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
2a30e4b to
15eb1f4
Compare
15eb1f4 to
bdcda88
Compare
bdcda88 to
033cd8b
Compare
033cd8b to
3a4e488
Compare
3a4e488 to
38bd030
Compare
38bd030 to
99c55df
Compare
99c55df to
f951c56
Compare
f951c56 to
f20fe83
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@openmeter/billing/charges/usagebased/service/rating/details.go`:
- Around line 126-159: Loop currently calls snapshotQuantity for every
eligibleRealizations entry even though rateWithLateEvents only consumes values
starting at CurrentPeriod; this is causing unnecessary O(n) meter queries. Fix
by deferring snapshotQuantity until actually needed: replace the eager
priorPeriodQty call with a lazy accessor (e.g., closure or thunk) that captures
snapshotQuantity(ctx, snapshotQuantityInput{...}) and only invokes it on first
use, and pass that accessor into the code that calls rateWithLateEvents (or call
snapshotQuantity conditionally right before any logic that consumes prior-period
quantities). Keep function names intact (eligibleRealizations, snapshotQuantity,
rateWithLateEvents, GetDetailedRatingForUsageResult) so reviewers can find and
verify the change. Ensure errors from the lazy call are propagated the same way
as before.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: ef4b5142-fd21-4136-ae34-bd817e97ec30
📒 Files selected for processing (16)
.agents/skills/charges/SKILL.mdopenmeter/billing/charges/usagebased/rating.goopenmeter/billing/charges/usagebased/service/creditheninvoice.goopenmeter/billing/charges/usagebased/service/creditheninvoice_test.goopenmeter/billing/charges/usagebased/service/creditsonly.goopenmeter/billing/charges/usagebased/service/currenttotals.goopenmeter/billing/charges/usagebased/service/get.goopenmeter/billing/charges/usagebased/service/rating/details.goopenmeter/billing/charges/usagebased/service/rating/quantitysnapshot.goopenmeter/billing/charges/usagebased/service/rating/service.goopenmeter/billing/charges/usagebased/service/rating/service_test.goopenmeter/billing/charges/usagebased/service/rating/totals.goopenmeter/billing/charges/usagebased/service/run/create.goopenmeter/billing/charges/usagebased/service/run/detailedline.goopenmeter/billing/charges/usagebased/service/run/detailedline_test.gopkg/timeutil/closedperiod.go
💤 Files with no reviewable changes (1)
- openmeter/billing/charges/usagebased/service/creditheninvoice_test.go
Overview
This PR refactors usage-based charge rating around explicit run snapshot semantics. Detailed rating now treats
ServicePeriodToas the current run boundary and lets the rating layer decide which realization runs are prior runs. A realization is considered prior only when itsServicePeriodTois before the requested rating boundary, so callers no longer need to mutate the charge by removing the current run before rating.The usage snapshot query model is now clearer: metered quantity is calculated from the charge intent service-period start up to the requested
ServicePeriodTo, capped byStoredAtLTusing an exclusivestored_at < StoredAtLTfilter.RateableIntentnow carries the concrete service period being rated so generated detailed lines and commitment logic operate on the same period boundary as the run.Minimum commitment handling is aligned with the run lifecycle. Detailed rating suppresses minimum commitment for interim snapshots and includes it only for final-period rating. Realtime/current totals also ignore minimum commitment before the charge service period ends and include it at or after the service-period end.
The PR also lays groundwork for late-event support by collecting prior run period snapshots in the rating flow. The actual late-event delta rating is intentionally left as a TODO, with the current implementation preserving existing rating behavior while making the future extension point explicit.
Testing
Added usage-based rating tests backed by the mock streaming connector to cover:
ServicePeriodToas the exclusive event-time upper bound.StoredAtLTas the exclusive stored-at cutoff.GetTotalsForUsageusing the real billing rating service.Summary by CodeRabbit
Refactor
Tests