Implement the following plan: Plan: Improve buildQuery implementation and related code…
- User
Implement the following plan:
Plan: Improve buildQuery implementation and related code
Context
The
buildQueries/buildNodeQueriesfunctions andLoad*methods exist in two packages with near-identical logic:pkg/metricstore/query.go(internal, in-memory store)internal/metricstoreclient/cc-metric-store.go+cc-metric-store-queries.go(external, HTTP client)
Both share
BuildScopeQueriesandSanitizeStatsfrompkg/metricstore/scopequery.go. The review identified bugs, dead code, duplicated patterns, and inconsistencies across both packages.Changes
1. Bug fix:
LoadNodeDatanil metric config panic (both packages)archive.GetMetricConfig()can return nil, butLoadNodeDatadereferencesmc.Unit/mc.Timestepwithout checking. Every other Load function checks.Files:
pkg/metricstore/query.go:600— add nil check +continueinternal/metricstoreclient/cc-metric-store.go:595— add nil check +continue
2. Bug fix:
LoadNodeDatamissingcontinueafter error (both packages)When
qdata.Error != nil, the error is appended but execution falls through to append invalid data. Shouldcontinue.Files:
pkg/metricstore/query.go:589-590internal/metricstoreclient/cc-metric-store.go:583-585
3. Extract
ExtractTypeIDhelper toscopequery.goThe ID-extraction block is copy-pasted 6 times (3x in each package). Extract to shared helper in
pkg/metricstore/scopequery.go.gofunc ExtractTypeID(queryType *string, typeIds []string, ndx int, metric, hostname string) *string { if queryType == nil { return nil } if ndx < len(typeIds) { id := typeIds[ndx] return &id } cclog.Warnf("TypeIds index out of range: %d with length %d for metric %s on host %s", ndx, len(typeIds), metric, hostname) return nil } Assistant
I'll start by reading the key files to understand the current code.
Assistant
I'll implement all 8 changes. Let me start with the shared helper functions in
scopequery.go, then update both packages.Assistant
Step 1: Add helper functions to
scopequery.goAssistant
Step 2: Fix
pkg/metricstore/query.goAssistant
Fix 6: Remove redundant inner bounds check in
LoadData:Assistant
Fix 4 + 5 + 7:
buildQueries— useIsMetricRemovedForSubCluster, fix TODO, add capacity hint:Assistant
Fix TODO in
buildQueries:Assistant
Fix 8: Add bounds check to
LoadStats:Assistant
Fix 3: Replace TypeID extraction in
LoadScopedStats:Assistant
Fix 1+2:
LoadNodeData— nil check for mc andcontinueafter error:Assistant
Fix 6: Remove redundant inner bounds check in
LoadNodeListData:Assistant
Fix 3: Replace TypeID extraction in
LoadNodeListData:Assistant
Fix 4+5+7:
buildNodeQueries— use helper, fix TODO, add capacity hint:Assistant
Fix TODO in
buildNodeQueries:Assistant
Step 3: Fix external metricstoreclient files
Assistant
Fix 6: Remove redundant inner bounds check in
LoadData:Assistant
Fix 8: Add bounds check to
LoadStats:Assistant
Fix 3: Replace TypeID extraction in
LoadScopedStats:Assistant
Fix 1+2:
LoadNodeData— nil check for mc andcontinueafter error:Assistant
Fix 6: Remove redundant inner bounds check in
LoadNodeListData:Assistant
Fix 3: Replace TypeID extraction in
LoadNodeListData:Assistant
Now fix the
cc-metric-store-queries.gofile — useIsMetricRemovedForSubClusterand fix TODOs:Assistant
Fix the second TODO in
buildNodeQueries:Assistant
Step 4: Build and test
Assistant
All passing. Let me also check the unused import situation —
cclogwas used inLoadScopedStatsofcc-metric-store.goand now the inline TypeID code is removed, butcclogis still used elsewhere so it should be fine.Assistant
All changes implemented and verified. Here's a summary:
- Bug fix:
LoadNodeDatanil metric config — Added nil check formcbefore dereferencing in both packages - Bug fix:
LoadNodeDatamissingcontinueafter error — Addedcontinueafter appending error in both packages - Extracted
ExtractTypeIDhelper — Added toscopequery.go, replaced 6 inline occurrences (3 per package) - Extracted
IsMetricRemovedForSubClusterhelper — Added toscopequery.go, replaced 4 inline occurrences (2 per package) - Fixed TODO error messages — Replaced "TODO: unhandled case" with "unsupported scope transformation" in all 4 occurrences
- Removed redundant inner bounds checks — Removed 4 dead-code safety checks that were already guarded by outer truncation
- Added capacity hint for
assignedScope— Changed from[]schema.MetricScope{}tomake(...)with matching capacity in internalbuildQueries/buildNodeQueries - — Added check in both packages
- Bug fix: