Skip to content

fix(recommendations/aws): plumb on_demand_cost for canonical Effective Savings % (closes #303)#312

Merged
cristim merged 1 commit intofeat/multicloud-web-frontendfrom
fix/issue-303-aws-savings-pct
May 5, 2026
Merged

fix(recommendations/aws): plumb on_demand_cost for canonical Effective Savings % (closes #303)#312
cristim merged 1 commit intofeat/multicloud-web-frontendfrom
fix/issue-303-aws-savings-pct

Conversation

@cristim
Copy link
Copy Markdown
Member

@cristim cristim commented May 5, 2026

Summary

Acceptance criteria check

  • AWS recommendations carry a non-null on_demand_cost from the provider all the way to RecommendationRecord and the API payload — RI: existing parseAWSCostDetails; SP: this PR.
  • effectiveSavingsPct() resolves to the same value as the underlying provider report for AWS RIs and Savings Plans — formula unchanged from bug(frontend/recs): Effective % shows implausible values (85.9% on Azure 1y RI, 54% on savings=$0 row) #274; this PR ensures the provider-supplied denominator reaches the frontend for SP.
  • Unit test in frontend/src/__tests__/recommendations.test.ts pinning the expected effective % for at least one AWS RI fixture and one SP fixture — added in this PR.
  • Regression test in the AWS provider mapping asserting OnDemandCost is populated — TestParseRecommendationDetail_WithAccountAndCosts (RI, pre-existing) + TestParseSavingsPlanDetail_OnDemandCost (SP, this PR).

Reference

Pattern mirrors PR #277 (commit 66a993fa5) which fixed the same bug for Azure 1y RIs.

Closes #303

Summary by CodeRabbit

  • Bug Fixes

    • Improved accuracy of AWS Savings Plans recommendation calculations by properly computing on-demand cost baselines, ensuring effective savings percentages align with AWS Cost Explorer data for EC2 RI, Compute Savings Plans, and other plan types.
  • Tests

    • Enhanced test coverage validating on-demand cost calculations across multiple AWS recommendation scenarios.

closes #303)

The AWS Savings Plans parser did not populate common.Recommendation.OnDemandCost,
so effectiveSavingsPct() fell back to reconstructing the on-demand denominator from
monthly_cost + savings + amortized. For SP rows monthly_cost reflects only the
recurring commitment charge, not the full on-demand baseline, so the reconstruction
is inaccurate.

Fix: derive OnDemandCost from CurrentAverageHourlyOnDemandSpend × 730 in
parseSavingsPlanDetail — the same field AWS Cost Explorer uses internally when
computing EstimatedSavingsPercentage. The scheduler's existing nonZeroPtr helper
(added in #277) converts 0 → nil so the frontend falls back to reconstruction when
the field is absent, preserving backward compatibility.

The AWS RI parser already populated OnDemandCost (from EstimatedMonthlyOnDemandCost
via parseAWSCostDetails) and the scheduler already plumbs it through to
RecommendationRecord — both landed in #277. This commit closes the SP gap and adds
the AWS-specific frontend and provider regression tests the AC list requires.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 5, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0a1178cb-57f2-406c-9e9e-f248004677b7

📥 Commits

Reviewing files that changed from the base of the PR and between f83c914 and 4df8dab.

📒 Files selected for processing (3)
  • frontend/src/__tests__/recommendations.test.ts
  • providers/aws/recommendations/parser_sp.go
  • providers/aws/recommendations/parser_sp_additional_test.go

📝 Walkthrough

Walkthrough

This PR plumbs the AWS provider-supplied on_demand_cost from the Savings Plans parser through to the frontend to enable correct Effective Savings % calculations. Backend parsing now computes OnDemandCost from hourly on-demand spend; frontend and backend tests validate the computation and usage.

Changes

AWS On-Demand Cost Plumbing

Layer / File(s) Summary
Data Shape & Backend Parsing
providers/aws/recommendations/parser_sp.go
parseSavingsPlanDetail computes OnDemandCost from CurrentAverageHourlyOnDemandSpend * hoursPerMonth and populates the returned common.Recommendation struct with this value, with explanatory comments on its semantics.
Backend Tests
providers/aws/recommendations/parser_sp_additional_test.go
TestParseSavingsPlanDetail_OnDemandCost validates that rec.OnDemandCost is correctly computed from hourly on-demand spend when present, and defaults to 0 when absent.
Frontend Tests
frontend/src/__tests__/recommendations.test.ts
Added AWS provider fixtures test suite covering EC2 RI, Compute Savings Plans, and Savings Plans scenarios; verifies effectiveSavingsPct uses provider-supplied on_demand_cost as the denominator and matches expected percentage values.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • LeanerCloud/CUDly#277: Fixed the same issue for Azure RIs by plumbing on_demand_cost to the frontend; this PR applies the same pattern to AWS Savings Plans.
  • LeanerCloud/CUDly#254: Earlier changes to AWS Savings Plans parsing logic and frontend effectiveSavingsPct behavior that are related to the data flow modified here.

Poem

🐰 A parser that once forgot the cost,
Now multiplies hourly, like dew drops lost;
On-demand at last! The savings shine bright,
Percentages true from backend to sight!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.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 clearly summarizes the main change: plumbing on_demand_cost for AWS Savings Plans to fix Effective Savings % calculations, matching the primary objective stated in issue #303.
Linked Issues check ✅ Passed All acceptance criteria from #303 are met: on_demand_cost is plumbed for AWS SP via parser_sp.go, frontend fixtures added for AWS RI and SP, regression test TestParseSavingsPlanDetail_OnDemandCost validates OnDemandCost population, and effectiveSavingsPct() usage is enabled by the plumbing.
Out of Scope Changes check ✅ Passed All changes are within scope: backend plumbing of OnDemandCost for AWS Savings Plans, frontend test fixtures for AWS RI and SP, and regression tests directly support closing #303 without extraneous modifications.

✏️ 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 fix/issue-303-aws-savings-pct

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

@cristim cristim added priority/p1 Next up; this sprint severity/high Significant harm urgency/this-sprint Within the current sprint impact/all-users Affects every user effort/m Days type/bug Defect triaged Item has been triaged labels May 5, 2026
@cristim
Copy link
Copy Markdown
Member Author

cristim commented May 5, 2026

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 5, 2026

✅ 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 merged commit c94c44a into feat/multicloud-web-frontend May 5, 2026
5 of 6 checks passed
cristim added a commit that referenced this pull request May 6, 2026
…isibility (closes #321) (#324)

* fix(db/aws): flush stale recs cache missing on_demand_cost (migration 000049)

PR #312 added on_demand_cost population for AWS SP rows but shipped no
cache-invalidation migration. Pre-#312 AWS rows in recommendations.payload
lack the on_demand_cost key, so the frontend falls back to the broken
reconstruction formula (which double-counts amortization), rendering
implausibly high Effective % values (e.g. ~86% instead of ~22%).

Migration 000049 follows #256's pattern: set monthly_cost to null on
AWS rows that lack on_demand_cost, so the frontend renders "—" until
the next scheduler tick repopulates with correct values. The WHERE clause
is scoped strictly to AWS rows missing the field (idempotent on re-run).

Closes #321

* fix(recommendations/aws): warn-log when on-demand cost inputs are nil

Add structured warn-logs in the RI and SP parsers when the AWS CE API
fields that populate OnDemandCost are absent from the response:

- parseAWSCostDetails (parser_ri.go): logs when EstimatedMonthlyOnDemandCost
  is nil, which causes OnDemandCost=0 → stored as nil by the scheduler's
  nonZeroPtr helper → frontend reconstruction fallback.

- parseSavingsPlanDetail (parser_sp.go): logs when
  CurrentAverageHourlyOnDemandSpend is nil, same downstream effect.

Both logs follow the respective file's existing logging style (fmt.Printf
for parser_ri.go, log.Printf for parser_sp.go). The logs make it
observable when the fallback path is taken, aiding diagnosis for issue #321
and future regression detection.

* fix: apply CodeRabbit auto-fixes

Fixed 1 file(s) based on 1 unresolved review comment.

Co-authored-by: CodeRabbit <noreply@coderabbit.ai>

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: CodeRabbit <noreply@coderabbit.ai>
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/p1 Next up; this sprint severity/high Significant harm 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