Skip to content

chore: detailed line conversion owned by rating - #4254

Merged
turip merged 3 commits into
mainfrom
refactor/detailed-line-convert-ownership
Apr 29, 2026
Merged

turip merged 3 commits into
mainfrom
refactor/detailed-line-convert-ownership

Conversation

@turip

@turip turip commented Apr 29, 2026 •

Copy link
Copy Markdown
Member

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

  • Refactor
    • Reworked how detailed usage lines are loaded and stored across rating, run creation, and invoicing—centralized fetching and switched to an upsert-based persistence flow.
  • Bug Fixes
    • Improved error wrapping for detailed-line persistence and fetch failures to make problems clearer.
  • Tests
    • Added and adjusted unit tests covering detailed-line mapping, fetch behavior, and error wrapping.

@turip
turip requested a review from a team as a code owner April 29, 2026 13:24
@turip turip added release-note/misc Miscellaneous changes area/billing labels Apr 29, 2026
@coderabbitai

coderabbitai Bot commented Apr 29, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Introduces a DetailedLinesFetcher into the rating layer, moves detailed-line loading/mapping from run/state-machine to rating, and switches run detailed-line persistence from Runs.PersistRunDetailedLines to Adapter.UpsertRunDetailedLines across creation/finalization flows.

Changes

Cohort / File(s) Summary
Rating: detailed-lines fetch & mapping
rating/detailedline.go, rating/service.go, rating/details.go, rating/service_test.go
Adds mapper from billing rating lines → usagebased detailed lines and a new DetailedLinesFetcher dependency; wiring and tests added to validate mapping and fetcher behavior.
Run workflows — consume rating & persist lines
creditheninvoice.go, creditsonly.go, run/create.go
Calls into rating without preloading lines; replaces Runs.PersistRunDetailedLines usage with Adapter.UpsertRunDetailedLines and assigns currentRun.DetailedLines from rating results. Error wrap messages updated to upsert run detailed lines.
Removed run-level detailed-line helpers
run/detailedline.go, run/detailedline_test.go
Deletes old run-level ensure/map/persist helpers and their unit test that previously expanded, mapped, and persisted detailed lines.
State machine & service wiring
statemachine.go, service.go
Removes state-machine helper that expanded detailed lines; injects adapter as DetailedLinesFetcher into rater via service.New and Config.

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
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~30 minutes

Possibly related PRs

Suggested reviewers

  • tothandras
  • GAlexIHU
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main refactoring: moving detailed line conversion responsibility from the state machine/run layer into the rating service, which is the core objective of this PR.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/detailed-line-convert-ownership

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.

❤️ Share
Review rate limit: 6/8 reviews remaining, refill in 13 minutes and 13 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 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 error pattern 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

📥 Commits

Reviewing files that changed from the base of the PR and between f6bdaa2 and e33f7a5.

📒 Files selected for processing (11)
  • openmeter/billing/charges/usagebased/service/creditheninvoice.go
  • openmeter/billing/charges/usagebased/service/creditsonly.go
  • openmeter/billing/charges/usagebased/service/rating/detailedline.go
  • openmeter/billing/charges/usagebased/service/rating/details.go
  • openmeter/billing/charges/usagebased/service/rating/service.go
  • openmeter/billing/charges/usagebased/service/rating/service_test.go
  • openmeter/billing/charges/usagebased/service/run/create.go
  • openmeter/billing/charges/usagebased/service/run/detailedline.go
  • openmeter/billing/charges/usagebased/service/run/detailedline_test.go
  • openmeter/billing/charges/usagebased/service/service.go
  • openmeter/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

turip added 3 commits April 29, 2026 15:41
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.
@turip
turip force-pushed the refactor/detailed-line-convert-ownership branch from 7a26903 to 3405351 Compare April 29, 2026 13:41

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 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

📥 Commits

Reviewing files that changed from the base of the PR and between e33f7a5 and 3405351.

📒 Files selected for processing (11)
  • openmeter/billing/charges/usagebased/service/creditheninvoice.go
  • openmeter/billing/charges/usagebased/service/creditsonly.go
  • openmeter/billing/charges/usagebased/service/rating/detailedline.go
  • openmeter/billing/charges/usagebased/service/rating/details.go
  • openmeter/billing/charges/usagebased/service/rating/service.go
  • openmeter/billing/charges/usagebased/service/rating/service_test.go
  • openmeter/billing/charges/usagebased/service/run/create.go
  • openmeter/billing/charges/usagebased/service/run/detailedline.go
  • openmeter/billing/charges/usagebased/service/run/detailedline_test.go
  • openmeter/billing/charges/usagebased/service/service.go
  • openmeter/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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Namespace not populated for a reason?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, namespace is populated by the adapter (always).

This branch was previously deployed

1 inactive deployment
dev — 34053515 Deployed Apr 29, 2026 by turip via Migration Checks #13942
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants