Repository navigation
feat: report overdue billing worker invoices - #4710
Conversation
📝 WalkthroughWalkthroughChangesThe billing worker now reports overdue collection and advancement invoice metrics. Standard invoice advancement filtering, listing, and counting were added; the auto-advancer uses the consolidated query, and the metrics service is wired into worker startup and shutdown. Invoice advancement query and contract
Overdue invoice metrics
Billing worker integration
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant BillingWorker
participant InvoiceMetricsService
participant InvoiceMetricsAdapter
participant BillingAdapter
participant OpenTelemetry
BillingWorker->>InvoiceMetricsService: Start(ctx)
InvoiceMetricsService->>InvoiceMetricsAdapter: CountOverdueInvoices
InvoiceMetricsAdapter->>BillingAdapter: CountStandardInvoicesPendingAdvancement
BillingAdapter-->>InvoiceMetricsAdapter: Advancement count
InvoiceMetricsAdapter-->>InvoiceMetricsService: Collection and advancement counts
InvoiceMetricsService->>OpenTelemetry: Record gauge measurements
BillingWorker->>InvoiceMetricsService: Stop()
Suggested labels: Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ 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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
openmeter/billing/adapter/invoice_advancement.go (2)
26-40: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winSame "collection window elapsed" predicate logic duplicated across two packages. Both sites independently implement
CollectionAtLTE(cutoff) OR (CollectionAtIsNil() AND UpdatedAtLTE(cutoff))against different statuses — one shared domain rule with two copies to keep in sync.
openmeter/billing/adapter/invoice_advancement.go#L26-L40: extract thisOr(...)block into an exported helper (e.g.,CollectionWindowElapsedPredicate(cutoff time.Time) predicate.BillingInvoice) in this package.openmeter/billing/worker/invoicemetrics/adapter/count.go#L21-L32: import and reuse that same helper here instead of re-declaring theOr(...)block inline.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@openmeter/billing/adapter/invoice_advancement.go` around lines 26 - 40, Extract the shared collection-window predicate from openmeter/billing/adapter/invoice_advancement.go lines 26-40 into an exported helper such as CollectionWindowElapsedPredicate(cutoff time.Time), preserving the CollectionAtLTE OR nil-CollectionAt plus UpdatedAtLTE logic. Update openmeter/billing/worker/invoicemetrics/adapter/count.go lines 21-32 to import and reuse this helper instead of defining the predicate inline; both sites should retain their existing status filters.
16-53: 🚀 Performance & Scalability | 🔵 TrivialWorth confirming index support now that this predicate runs from two periodic jobs.
This predicate (status/time filters + a JSONB
availableActionscontainment check onstatus_details_cache) is now exercised both by the advance worker and, viaCountStandardInvoicesPendingAdvancement, by the new one-minute overdue-invoice gauge. Ifbilling_invoicedoesn't have indexes coveringstatus/collection_at/updated_atand a GIN index onstatus_details_cache, this query could turn into a table scan run every minute as the table grows. Not something visible from this diff alone (no migration files included), so flagging for a quick check.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@openmeter/billing/adapter/invoice_advancement.go` around lines 16 - 53, Verify that the database schema supporting invoicePendingAdvancementPredicate includes indexes for the filtered status, collection_at, and updated_at columns, plus a GIN index for status_details_cache JSONB lookups. If any are missing, add the corresponding migration indexes and ensure they support both the advancement worker and CountStandardInvoicesPendingAdvancement queries.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@openmeter/billing/worker/invoicemetrics/adapter/adapter.go`:
- Around line 21-31: Update Config.Validate in
openmeter/billing/worker/invoicemetrics/adapter/adapter.go to collect both
validation failures in an errs slice, import the models package, and return
errors.Join(errs...) wrapped with models.NewNillableGenericValidationError. In
openmeter/billing/worker/invoicemetrics/service/service.go, update
Config.Validate’s final return to use the same wrapper around
errors.Join(errs...).
---
Nitpick comments:
In `@openmeter/billing/adapter/invoice_advancement.go`:
- Around line 26-40: Extract the shared collection-window predicate from
openmeter/billing/adapter/invoice_advancement.go lines 26-40 into an exported
helper such as CollectionWindowElapsedPredicate(cutoff time.Time), preserving
the CollectionAtLTE OR nil-CollectionAt plus UpdatedAtLTE logic. Update
openmeter/billing/worker/invoicemetrics/adapter/count.go lines 21-32 to import
and reuse this helper instead of defining the predicate inline; both sites
should retain their existing status filters.
- Around line 16-53: Verify that the database schema supporting
invoicePendingAdvancementPredicate includes indexes for the filtered status,
collection_at, and updated_at columns, plus a GIN index for status_details_cache
JSONB lookups. If any are missing, add the corresponding migration indexes and
ensure they support both the advancement worker and
CountStandardInvoicesPendingAdvancement queries.
🪄 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: 86c20ed4-57d4-42e4-976e-d59d03585fae
📒 Files selected for processing (15)
app/common/openmeter_billingworker.gocmd/billing-worker/wire_gen.goopenmeter/billing/adapter.goopenmeter/billing/adapter/invoice_advancement.goopenmeter/billing/service.goopenmeter/billing/service/stdinvoice.goopenmeter/billing/stdinvoice.goopenmeter/billing/worker/advance/advance.goopenmeter/billing/worker/invoicemetrics/adapter.goopenmeter/billing/worker/invoicemetrics/adapter/adapter.goopenmeter/billing/worker/invoicemetrics/adapter/count.goopenmeter/billing/worker/invoicemetrics/metrics.goopenmeter/billing/worker/invoicemetrics/service.goopenmeter/billing/worker/invoicemetrics/service/service.goopenmeter/server/server_test.go
Summary
Summary by CodeRabbit
New Features
Bug Fixes
Summary by CodeRabbit
Greptile Summary
This PR adds overdue invoice reporting to the billing worker. The main changes are:
Confidence Score: 5/5
This looks safe to merge.
Important Files Changed
Reviews (2): Last reviewed commit: "fix: validate invoice metrics configurat..." | Re-trigger Greptile
Context used (3)