Skip to content

fix(api/dashboard): tighten committed_monthly aggregation (status + unit + account scope) - #927

Merged
cristim merged 1 commit into
feat/multicloud-web-frontendfrom
fix/committed-monthly
Jun 3, 2026
Merged

cristim merged 1 commit into
feat/multicloud-web-frontendfrom
fix/committed-monthly

Conversation

@cristim

@cristim cristim commented Jun 2, 2026

Copy link
Copy Markdown
Member

Summary

Three bugs were inflating the committed_monthly KPI on the Home page:

  1. Status filter: isActiveCommitment now rejects rows whose Status field is non-empty and not "completed". DB-backed purchase_history rows always read back with Status="" (the column is not persisted — dynamodbav:"-"). Synthesised rows from failed/cancelled/expired executions carry a non-empty status and were being counted as active commitments.

  2. Unit verification: Confirmed and documented that EstimatedSavings is always written in monthly units (populated from PurchaseExecution.EstimatedSavings which derives from monthly recommendation savings at purchase time). No runtime normalisation is needed; comment added to prevent future drift.

  3. Multi-account scope: resolveDashboardAccountID was passing "" to GetPurchaseHistory when account_ids had multiple entries, yielding WHERE account_id = '' which returns no rows at all (silently zero, not all-accounts). Replaced resolveDashboardAccountID with resolveDashboardAccountScope that:

    • Routes to GetPurchaseHistoryFiltered(cloudAccountUUIDs) (matches cloud_account_id = ANY($1)) for UUID-based account_ids filters
    • Routes to GetPurchaseHistory(legacyAccountID) for the legacy singular account_id param
    • Routes to GetAllPurchaseHistory when no filter is set
    • Also fixes the single-UUID case, which was passing a UUID to the VARCHAR account_id column

New test assertions

  • status=failed row is excluded from committedMonthly and activeCommitments
  • Multi-account UUID filter routes to GetPurchaseHistoryFiltered (not GetPurchaseHistory), and the mock asserts GetPurchaseHistory is never called
  • No-filter path uses GetAllPurchaseHistory (updated 5 existing tests to match the new dispatch)

Test plan

  • go test github.com/LeanerCloud/CUDly/internal/api/... -run TestHandler_calculateCommitmentMetrics passes (8 tests including 3 new)
  • go test github.com/LeanerCloud/CUDly/internal/api/... passes (all 1387 tests)
  • go test ./... passes (5016 tests)
  • Home page KPI shows expected value with a few commitments, not inflated total

…nit + account scope)

Three bugs inflated the committed_monthly KPI on the Home page:

1. Status filter: isActiveCommitment now rejects rows whose Status field is
   non-empty and not "completed". DB-backed purchase_history rows always read
   back with Status="" (the column is not persisted); only synthesised rows
   from failed/cancelled/expired executions carry a non-empty status. Those
   were being counted as active commitments.

2. Unit verification: documented that EstimatedSavings is always written in
   monthly units (traces from PurchaseExecution.EstimatedSavings which
   derives from monthly recommendation savings). No runtime normalisation
   needed; comment added to prevent future ambiguity.

3. Multi-account scope: resolveDashboardAccountID was passing empty string
   to GetPurchaseHistory when account_ids had multiple entries, yielding
   WHERE account_id='' which returns no rows. Replaced with
   resolveDashboardAccountScope that routes to GetPurchaseHistoryFiltered
   (cloud_account_id = ANY($1)) for UUID-based filters, GetPurchaseHistory
   for the legacy account_id param, and GetAllPurchaseHistory when no filter
   is set. Also fixes the single-UUID case which was passing a UUID to the
   account_id (VARCHAR) column.

New tests: status=failed row excluded; multi-account UUID filter routes to
GetPurchaseHistoryFiltered and excludes out-of-scope accounts; no-filter
path uses GetAllPurchaseHistory.
@cristim cristim added triaged Item has been triaged priority/p2 Backlog-worthy urgency/this-sprint Within the current sprint impact/all-users Affects every user effort/m Days type/bug Defect labels Jun 2, 2026
@cristim

cristim commented Jun 2, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 2, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@cristim, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 30 minutes and 14 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e929decc-2601-452c-ba54-c0d16ac21bc3

📥 Commits

Reviewing files that changed from the base of the PR and between 1dbeef7 and 16e1e18.

📒 Files selected for processing (4)
  • internal/api/handler_dashboard.go
  • internal/api/handler_dashboard_test.go
  • internal/api/handler_per_account_perms_test.go
  • internal/api/handler_test.go
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/committed-monthly

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

@coderabbitai

coderabbitai Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@cristim

cristim commented Jun 3, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@cristim
cristim merged commit d086575 into feat/multicloud-web-frontend Jun 3, 2026
4 checks passed
@cristim
cristim deleted the fix/committed-monthly branch June 3, 2026 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

effort/m Days impact/all-users Affects every user priority/p2 Backlog-worthy triaged Item has been triaged type/bug Defect urgency/this-sprint Within the current sprint

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant