Draft
Conversation
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 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 |
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
Adds UnitConfig — a unit conversion layer that transforms raw metered quantities into billing-ready units before pricing. This replaces the need for separate PackagePrice and DynamicPrice types by combining a generic conversion operation with standard pricing (Unit, Tiered, etc.).
What it does
UnitConfig sits between the meter and the pricer:
Core fields:
operation:divideormultiplyconversion_factor: positive non-zero decimalrounding:ceiling,floor,half_up, ornone(default)precision: decimal places for rounding (default 0)display_unit: human-readable label (e.g., "GB", "M tokens")Examples:
divideby 1,000,000 +ceilingrounding + UnitPrice → package-style billingmultiplyby 1.2 +nonerounding + UnitPrice → dynamic pricingdivideby 1e9 +ceiling+displayUnit: "GB"Changes
API layer:
UnitConfig,UnitConfigOperation,UnitConfigRoundingMode,InvoiceUsageQuantityDetailunitConfigfield onFeatureUnitConfig(features),RateCardUsageBased(rate cards), and invoice linesDomain layer (
openmeter/productcatalog):UnitConfigtype withConvert()(precise, for entitlements) andConvertAndRound()(for invoicing)Billing integration:
UnitConfigConversionmutator: applies conversion before pricingUnitConfigRoundingmutator: applies rounding for invoiced quantitiesUsageQuantityDetailon invoice lines: full audit trail (raw → converted → invoiced)Database:
unit_configJSONB column added to:features,plan_rate_cards,addon_rate_cards,subscription_items,billing_invoice_usage_based_line_configsTests:
Live validation against running server
Validated end-to-end on
localhost:8888with the sandbox billing app.Setup: Created customer → subscribed to
plan_ppm(pay-per-million tokens) → ingested 1,500,001 raw tokens → triggered invoicing.Plan rate card config:
{ "unitConfig": { "operation": "divide", "conversion_factor": "1000000", "display_unit": "M tokens", "rounding": "ceiling" }, "price": { "amount": "1.5", "type": "unit" } }Invoice line result:
{ "quantity": "2", "totals": { "total": "3" }, "usageQuantityDetail": { "rawQuantity": "1500001", "convertedQuantity": "1.500001", "invoicedQuantity": "2", "appliedUnitConfig": { "operation": "divide", "conversion_factor": "1000000", "display_unit": "M tokens", "rounding": "ceiling" } } }Validation:
rawQuantity: 1500001convertedQuantity: 1.500001invoicedQuantity: 2total: 3appliedUnitConfigpresentTest plan
openmeter/productcatalog/unitconfig_test.go— conversion, rounding, validationrate/unit_test.go,rate/tieredgraduated_test.go— UnitConfig with pricingtest/billing/unitconfig_test.go— full gathering → invoice pipeline