perf(common): extend ComputeDetails with VCPU + MemoryGB#97
Conversation
Adds optional VCPU (int) and MemoryGB (float64) fields to common.ComputeDetails so per-provider catalogue lookups can enrich compute recommendations with sizing data. Both fields use json omitempty — converters that don't yet wire a catalogue leave them at the zero value and the API payload stays clean. GetDetailDescription appends " (N vCPU / M GB)" when both values are > 0, otherwise returns the existing "platform/tenancy" form. Format uses %g so 16 GB renders as "16 GB" (not "16.000000 GB") while 0.5 GB SKUs render as "0.5 GB". Scope decision — schema-only foundation: - Azure: PR #81 (perf/azure-batched-sku-lookup) introduces the cachedSKULookup helper for cache/cosmos/database. Compute was scoped back from #81 because these fields didn't exist. With the schema in place, a follow-up PR can wire compute on top of #81 once it merges (filed as a separate issue post-merge to avoid an unmerged-PR dependency in this changeset). - AWS: ce.EC2InstanceDetails (Cost-Explorer) has no vCPU/Memory fields; populating would require a new ec2:DescribeInstanceTypes caller + cache + mocks. Substantial net-new code, tracked as a separate follow-up. - GCP: no compute recommendation converter exists today. N/A. - Frontend: api.Recommendation has no `details` field and the detail drawer renders a hard-coded list. Surfacing the new fields needs separate API + UI work; tracked as follow-up once at least one provider populates the fields. Tests: extend pkg/common/types_test.go::TestComputeDetails_GetDetail Description with 4 new table rows — VCPU-only, MemoryGB-only, integer-GB, and fractional-GB (Azure 0.5 GB SKU shape). Refs #82
|
@coderabbitai review |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThe Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related issues
Poem
🚥 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. Comment |
✅ Actions performedReview triggered.
|
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
closes #148) (#229) PR #97 added VCPU + MemoryGB fields to common.ComputeDetails (with omitempty JSON tags). PR #81 introduced the lazy SKU-catalogue pattern for cache/cosmosdb/database but explicitly scoped back compute because the destination fields didn't exist. Both prerequisites are now in place; this wires Azure compute to the same pattern. Implementation mirrors cache/database verbatim for consistency: - New unexported vmSKUEntry{vCPUs, memoryGB}. - New skuCacheOnce sync.Once + skuCacheMap field on ComputeClient. - cachedSKULookup(ctx, skuName) lazily triggers the catalogue fetch on first call; subsequent calls are O(1) map lookups. ok=false on miss or fetch error. - fetchSKUCatalogue reuses the existing createResourceSKUsPager and isAvailableInRegion helpers (already used by GetValidResourceTypes), walks every page, and reduces virtualMachines SKUs into the map. Returns nil on error so the sync.Once-gated cache stays nil and converters fall back to the empty-fields path with a one-time WARN. - extractVMSKUCapabilities pulls vCPUs (Atoi) and MemoryGB (ParseFloat) from the SKU's Capabilities name/value list. Unparseable or missing capabilities → 0, treated as "unknown". - populateVMSKUMapFromPage was extracted out of fetchSKUCatalogue to stay under the project's gocyclo=10 threshold (matches the cache/database extraction pattern from PR #81). - convertAzureVMRecommendation now takes ctx (was _) and populates Details.VCPU/MemoryGB from the cache when both >0. The single caller GetRecommendations already passes ctx; no public API change. Behaviour preserved on failure: a transient ResourceSKUsClient error no longer breaks the conversion — VCPU/MemoryGB stay at 0 (omitempty hides them from API payloads), the rest of Details is populated from the recommendation payload as before, and a WARN is logged once per client lifetime. UX improvement: common.ComputeDetails.GetDetailDescription appends " (<vcpu> vCPU / <memory> GB)" when both fields are >0, so Azure VM recommendation summaries now include the size string instead of the SKU name alone. Tests added in providers/azure/services/compute/client_test.go (file-scoped vmSKUCatalogueMockPager keeps the shared mocks.MockResourceSKUsPager surface untouched, mirroring the cosmosdb / cache test convention): - TestComputeClient_ConvertAzureVMRecommendation_PopulatesVCPUAndMemoryFromSKUCache: catalogue hit populates VCPU=2, MemoryGB=8 for Standard_D2s_v3. - TestComputeClient_ConvertAzureVMRecommendation_PagerErrorFallsBack: fetch error leaves both fields at 0; conversion succeeds. - TestComputeClient_ConvertAzureVMRecommendation_NoMatchLeavesFieldsZero: catalogue miss leaves both fields at 0; conversion succeeds. - TestComputeClient_CachedSKULookup_FetchedOnce: 10 lookups trigger exactly 1 NextPage call (pins the N+1 invariant per PR #81). Out of scope (separate follow-ups, per the issue body): - AWS / GCP compute converter wiring. - Platform / Tenancy / Scope enrichment (require non-ResourceSKUs Azure data sources). Verification: gofmt clean; go vet ./... clean; gocyclo -over 10 finds no offenders in the modified file; go test github.com/LeanerCloud/CUDly/providers/azure/services/compute → 42 passing (4 new + all existing). The pre-existing providers/azure/services/search build failure (missing go.sum entry) is unrelated to this change. Closes #148
Summary
Adds
VCPU int+MemoryGB float64(bothomitempty) tocommon.ComputeDetailsso per-provider catalogue lookups can enrich computerecommendations with sizing data. Foundation work for the deferred Azure /
AWS / GCP compute SKU enrichment tracked in #82.
GetDetailDescriptionnow appends" (N vCPU / M GB)"when both valuesare populated; the existing
"platform/tenancy"form is preserved wheneither is zero (so today's call sites stay unchanged).
Scope decision — schema-only foundation
This PR intentionally lands only the shared-type schema + matcher
formatting + tests. Per-provider catalogue wiring is out of scope:
perf/azure-batched-sku-lookup) introduces thecachedSKULookuphelper for cache/cosmos/database. Compute was scopedback from perf(azure): batched SKU catalogue lookup eliminates N+1 in recommendation converters #81 because these struct fields didn't exist yet. With the
schema in place, a follow-up PR can wire compute on top of perf(azure): batched SKU catalogue lookup eliminates N+1 in recommendation converters #81 once it
merges. Filing the wiring against an unmerged PR here would create a
rebase/merge dependency that's better tracked as a separate issue.
ce.EC2InstanceDetails(Cost-Explorer) has no vCPU/Memoryfields. Populating would require a new
ec2:DescribeInstanceTypescaller + region-scoped catalogue cache + mocks/tests — substantial
net-new code, separate follow-up.
is added.
api.Recommendation(TS) has nodetailsfield and thedetail drawer renders a hard-coded list. Surfacing the new fields needs
separate API serializer + UI changes; tracked as a follow-up once at
least one provider populates the fields.
The schema change is the prerequisite that unblocks every one of those
follow-ups.
Changes
pkg/common/types.go::ComputeDetailsgainsVCPU+MemoryGB(
omitempty) with a doc comment explaining the catalogue-sourcecontract.
ComputeDetails.GetDetailDescription()appends the size suffix whenboth fields are > 0; uses
%gso16 GBrenders without trailingzeros while
0.5 GB(smallest Azure SKU) renders verbatim.pkg/common/types_test.goadds 4 table rows covering VCPU-only,MemoryGB-only (both fall back to base form), integer GB, and
fractional GB.
Test plan
go build ./...(root +pkg/)go test ./...(root)go test ./...frompkg/ComputeDetailsconsumers (Azure converter, AWS parser,cmd/main.go switch) continue compiling — no field is removed and
omitemptykeeps the JSON payload unchanged when fields aren'tpopulated
Follow-up issues (to file post-merge)
cachedSKULookupafter perf(azure): batched SKU catalogue lookup eliminates N+1 in recommendation converters #81 mergesDescribeInstanceTypesdetailstoapi.Recommendation+ render in the detaildrawer
Refs #82
Summary by CodeRabbit