Further consolidate and improve ccms query builder

Entire-Checkpoint: d10e6221ee4f
This commit is contained in:
2026-03-04 17:31:36 +01:00
parent 26982088c3
commit 845d0111af
4 changed files with 69 additions and 152 deletions

View File

@@ -79,17 +79,8 @@ func (ccms *CCMetricStore) buildQueries(
}
// Skip if metric is removed for subcluster
if len(mc.SubClusters) != 0 {
isRemoved := false
for _, scConfig := range mc.SubClusters {
if scConfig.Name == job.SubCluster && scConfig.Remove {
isRemoved = true
break
}
}
if isRemoved {
continue
}
if len(mc.SubClusters) != 0 && metricstore.IsMetricRemovedForSubCluster(mc, job.SubCluster) {
continue
}
// Avoid duplicates...
@@ -123,7 +114,7 @@ func (ccms *CCMetricStore) buildQueries(
)
if !ok {
return nil, nil, fmt.Errorf("METRICDATA/EXTERNAL-CCMS > TODO: unhandled case: native-scope=%s, requested-scope=%s", nativeScope, requestedScope)
return nil, nil, fmt.Errorf("METRICDATA/EXTERNAL-CCMS > unsupported scope transformation: native-scope=%s, requested-scope=%s", nativeScope, requestedScope)
}
for _, sr := range scopeResults {
@@ -175,17 +166,8 @@ func (ccms *CCMetricStore) buildNodeQueries(
}
// Skip if metric is removed for subcluster
if mc.SubClusters != nil {
isRemoved := false
for _, scConfig := range mc.SubClusters {
if scConfig.Name == subCluster && scConfig.Remove {
isRemoved = true
break
}
}
if isRemoved {
continue
}
if mc.SubClusters != nil && metricstore.IsMetricRemovedForSubCluster(mc, subCluster) {
continue
}
// Avoid duplicates...
@@ -234,7 +216,7 @@ func (ccms *CCMetricStore) buildNodeQueries(
)
if !ok {
return nil, nil, fmt.Errorf("METRICDATA/EXTERNAL-CCMS > TODO: unhandled case: native-scope=%s, requested-scope=%s", nativeScope, requestedScope)
return nil, nil, fmt.Errorf("METRICDATA/EXTERNAL-CCMS > unsupported scope transformation: native-scope=%s, requested-scope=%s", nativeScope, requestedScope)
}
for _, sr := range scopeResults {

View File

@@ -275,13 +275,6 @@ func (ccms *CCMetricStore) LoadData(
}
for i, row := range resBody.Results {
// Safety check to prevent index out of range errors
if i >= len(req.Queries) || i >= len(assignedScope) {
cclog.Warnf("Index out of range prevented: i=%d, queries=%d, assignedScope=%d",
i, len(req.Queries), len(assignedScope))
continue
}
query := req.Queries[i]
metric := query.Metric
scope := assignedScope[i]
@@ -318,18 +311,7 @@ func (ccms *CCMetricStore) LoadData(
continue
}
id := (*string)(nil)
if query.Type != nil {
// Check if ndx is within the bounds of TypeIds slice
if ndx < len(query.TypeIds) {
id = new(string)
*id = query.TypeIds[ndx]
} else {
// Log the error but continue processing
cclog.Warnf("TypeIds index out of range: %d with length %d for metric %s on host %s",
ndx, len(query.TypeIds), query.Metric, query.Hostname)
}
}
id := ms.ExtractTypeID(query.Type, query.TypeIds, ndx, query.Metric, query.Hostname)
ms.SanitizeStats(&res.Avg, &res.Min, &res.Max)
@@ -393,6 +375,10 @@ func (ccms *CCMetricStore) LoadStats(
stats := make(map[string]map[string]schema.MetricStatistics, len(metrics))
for i, res := range resBody.Results {
if i >= len(req.Queries) {
cclog.Warnf("LoadStats: result index %d exceeds queries length %d", i, len(req.Queries))
break
}
if len(res) == 0 {
// No Data Found For Metric, Logged in FetchData to Warn
continue
@@ -481,18 +467,7 @@ func (ccms *CCMetricStore) LoadScopedStats(
continue
}
id := (*string)(nil)
if query.Type != nil {
// Check if ndx is within the bounds of TypeIds slice
if ndx < len(query.TypeIds) {
id = new(string)
*id = query.TypeIds[ndx]
} else {
// Log the error but continue processing
cclog.Warnf("TypeIds index out of range: %d with length %d for metric %s on host %s",
ndx, len(query.TypeIds), query.Metric, query.Hostname)
}
}
id := ms.ExtractTypeID(query.Type, query.TypeIds, ndx, query.Metric, query.Hostname)
ms.SanitizeStats(&res.Avg, &res.Min, &res.Max)
@@ -582,6 +557,13 @@ func (ccms *CCMetricStore) LoadNodeData(
qdata := res[0]
if qdata.Error != nil {
errors = append(errors, fmt.Sprintf("fetching %s for node %s failed: %s", metric, query.Hostname, *qdata.Error))
continue
}
mc := archive.GetMetricConfig(cluster, metric)
if mc == nil {
cclog.Warnf("Metric config not found for %s on cluster %s", metric, cluster)
continue
}
ms.SanitizeStats(&qdata.Avg, &qdata.Min, &qdata.Max)
@@ -592,7 +574,6 @@ func (ccms *CCMetricStore) LoadNodeData(
data[query.Hostname] = hostdata
}
mc := archive.GetMetricConfig(cluster, metric)
hostdata[metric] = append(hostdata[metric], &schema.JobMetric{
Unit: mc.Unit,
Timestep: mc.Timestep,
@@ -680,13 +661,6 @@ func (ccms *CCMetricStore) LoadNodeListData(
}
for i, row := range resBody.Results {
// Safety check to prevent index out of range errors
if i >= len(req.Queries) || i >= len(assignedScope) {
cclog.Warnf("Index out of range prevented: i=%d, queries=%d, assignedScope=%d",
i, len(req.Queries), len(assignedScope))
continue
}
var query APIQuery
if resBody.Queries != nil {
if i < len(resBody.Queries) {
@@ -743,18 +717,7 @@ func (ccms *CCMetricStore) LoadNodeListData(
continue
}
id := (*string)(nil)
if query.Type != nil {
// Check if ndx is within the bounds of TypeIds slice
if ndx < len(query.TypeIds) {
id = new(string)
*id = query.TypeIds[ndx]
} else {
// Log the error but continue processing
cclog.Warnf("TypeIds index out of range: %d with length %d for metric %s on host %s",
ndx, len(query.TypeIds), query.Metric, query.Hostname)
}
}
id := ms.ExtractTypeID(query.Type, query.TypeIds, ndx, query.Metric, query.Hostname)
ms.SanitizeStats(&res.Avg, &res.Min, &res.Max)