Repository navigation
chore: detailed line conversion owned by rating - #4254
Conversation
📝 WalkthroughWalkthroughIntroduces a DetailedLinesFetcher into the rating layer, moves detailed-line loading/mapping from run/state-machine to rating, and switches run detailed-line persistence from Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant RunSvc as Run Service
participant Rater as Rating Service
participant Adapter as Adapter (DetailedLinesFetcher / Upsert)
RunSvc->>Rater: GetDetailedRatingForUsage(charge)
Rater->>Adapter: FetchDetailedLines(charge) rgba(70,130,180,0.5)
Adapter-->>Rater: charge with DetailedLines
Rater->>Rater: rate -> totals + detailedLines
Rater-->>RunSvc: ratingResult{Totals, DetailedLines}
RunSvc->>Adapter: UpsertRunDetailedLines(chargeID, runID, DetailedLines) rgba(34,139,34,0.5)
Adapter-->>RunSvc: upsert result / error
RunSvc->>RunSvc: currentRun.DetailedLines = ratingResult.DetailedLines
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~30 minutes Possibly related PRs
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. Review rate limit: 6/8 reviews remaining, refill in 13 minutes and 13 seconds.Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
openmeter/billing/charges/usagebased/service/run/create.go (1)
163-167: Optional cleanup: consider a shared helper for detailed-line persistence.This exact
UpsertRunDetailedLines + set currentRun.DetailedLines + wrapped errorpattern now exists in multiple flows. A small helper could reduce drift risk later.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@openmeter/billing/charges/usagebased/service/run/create.go` around lines 163 - 167, Extract the repeated pattern of calling s.adapter.UpsertRunDetailedLines, wrapping any error, and assigning currentRun.DetailedLines into a small shared helper (e.g., a method on service like s.upsertAndSetDetailedLines(ctx, chargeID string, run *Run, lines []DetailedLine) or similar) and replace the duplicated blocks with calls to that helper; the helper should call s.adapter.UpsertRunDetailedLines(ctx, chargeID, run.ID, lines), return a wrapped error on failure (fmt.Errorf("upsert run detailed lines: %w", err)), and on success set run.DetailedLines = mo.Some(lines).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@openmeter/billing/charges/usagebased/service/run/create.go`:
- Around line 163-167: Extract the repeated pattern of calling
s.adapter.UpsertRunDetailedLines, wrapping any error, and assigning
currentRun.DetailedLines into a small shared helper (e.g., a method on service
like s.upsertAndSetDetailedLines(ctx, chargeID string, run *Run, lines
[]DetailedLine) or similar) and replace the duplicated blocks with calls to that
helper; the helper should call s.adapter.UpsertRunDetailedLines(ctx, chargeID,
run.ID, lines), return a wrapped error on failure (fmt.Errorf("upsert run
detailed lines: %w", err)), and on success set run.DetailedLines =
mo.Some(lines).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: e6a67294-e20d-4251-b82b-f3a5730d7bd6
📒 Files selected for processing (11)
openmeter/billing/charges/usagebased/service/creditheninvoice.goopenmeter/billing/charges/usagebased/service/creditsonly.goopenmeter/billing/charges/usagebased/service/rating/detailedline.goopenmeter/billing/charges/usagebased/service/rating/details.goopenmeter/billing/charges/usagebased/service/rating/service.goopenmeter/billing/charges/usagebased/service/rating/service_test.goopenmeter/billing/charges/usagebased/service/run/create.goopenmeter/billing/charges/usagebased/service/run/detailedline.goopenmeter/billing/charges/usagebased/service/run/detailedline_test.goopenmeter/billing/charges/usagebased/service/service.goopenmeter/billing/charges/usagebased/service/statemachine.go
💤 Files with no reviewable changes (3)
- openmeter/billing/charges/usagebased/service/statemachine.go
- openmeter/billing/charges/usagebased/service/run/detailedline_test.go
- openmeter/billing/charges/usagebased/service/run/detailedline.go
Rating now owns detailed line conversion so that calculations can use the usagebased detailed line format. This patch also unifies detailed line fetching by making rating expand the detailed lines as needed. This removes the redundant codepath from statemachine/run.
7a26903 to
3405351
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
openmeter/billing/charges/usagebased/service/creditheninvoice.go (1)
330-334: Consider extracting a shared helper for “upsert detailed lines + set run.DetailedLines”.This exact sequence is now repeated in multiple paths (
creditheninvoice.go,creditsonly.go,run/create.go). A small helper would reduce drift and keep future changes safer.As per coding guidelines: "In general when reviewing the Golang code make readability and maintainability a priority, even potentially suggest restructuring the code to improve them."
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@openmeter/billing/charges/usagebased/service/creditheninvoice.go` around lines 330 - 334, Extract the repeated "upsert detailed lines + set run.DetailedLines" sequence into a single helper (for example a method named UpsertAndSetDetailedLines on the service or a private function) that accepts ctx, chargeID, runID and detailedLines and performs s.Adapter.UpsertRunDetailedLines(...) and then sets currentRun.DetailedLines = mo.Some(...); replace the inline sequence in creditheninvoice.go, creditsonly.go and run/create.go with calls to that helper (use existing symbols UpsertRunDetailedLines, s.Adapter, s.Charge.GetChargeID(), currentRun.ID and ratingResult.DetailedLines to locate the code paths). Ensure the helper returns and wraps errors the same way so callers preserve existing error handling.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@openmeter/billing/charges/usagebased/service/creditheninvoice.go`:
- Around line 330-334: Extract the repeated "upsert detailed lines + set
run.DetailedLines" sequence into a single helper (for example a method named
UpsertAndSetDetailedLines on the service or a private function) that accepts
ctx, chargeID, runID and detailedLines and performs
s.Adapter.UpsertRunDetailedLines(...) and then sets currentRun.DetailedLines =
mo.Some(...); replace the inline sequence in creditheninvoice.go, creditsonly.go
and run/create.go with calls to that helper (use existing symbols
UpsertRunDetailedLines, s.Adapter, s.Charge.GetChargeID(), currentRun.ID and
ratingResult.DetailedLines to locate the code paths). Ensure the helper returns
and wraps errors the same way so callers preserve existing error handling.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 6ba27ade-b073-4707-9be6-e5a258395ddb
📒 Files selected for processing (11)
openmeter/billing/charges/usagebased/service/creditheninvoice.goopenmeter/billing/charges/usagebased/service/creditsonly.goopenmeter/billing/charges/usagebased/service/rating/detailedline.goopenmeter/billing/charges/usagebased/service/rating/details.goopenmeter/billing/charges/usagebased/service/rating/service.goopenmeter/billing/charges/usagebased/service/rating/service_test.goopenmeter/billing/charges/usagebased/service/run/create.goopenmeter/billing/charges/usagebased/service/run/detailedline.goopenmeter/billing/charges/usagebased/service/run/detailedline_test.goopenmeter/billing/charges/usagebased/service/service.goopenmeter/billing/charges/usagebased/service/statemachine.go
💤 Files with no reviewable changes (3)
- openmeter/billing/charges/usagebased/service/statemachine.go
- openmeter/billing/charges/usagebased/service/run/detailedline_test.go
- openmeter/billing/charges/usagebased/service/run/detailedline.go
🚧 Files skipped from review as they are similar to previous changes (4)
- openmeter/billing/charges/usagebased/service/service.go
- openmeter/billing/charges/usagebased/service/rating/detailedline.go
- openmeter/billing/charges/usagebased/service/rating/service.go
- openmeter/billing/charges/usagebased/service/rating/service_test.go
| ) | ||
|
|
||
| require.Len(t, out, 2) | ||
| require.Empty(t, out[0].Namespace) |
There was a problem hiding this comment.
Namespace not populated for a reason?
There was a problem hiding this comment.
Yes, namespace is populated by the adapter (always).
Overview
Rating now owns detailed line conversion so that calculations can use the usagebased detailed line format.
This patch also unifies detailed line fetching by making rating expand the detailed lines as needed.
This removes the redundant codepath from statemachine/run.
Notes for reviewer
Summary by CodeRabbit