mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-09-12 21:58:58 -04:00
fix(search): merge top-level metric aggregations across spaces
The cross-space merge only carried buckets, dropping metric results (value/metricKind) from the per-space responses. Reduce metrics with their kind's reducer, keyed by field and kind.
This commit is contained in:
1 file changed
+24
@@ -287,6 +287,7 @@ func (s *Service) Search(ctx context.Context, req *searchsvc.SearchRequest) (*se
|
||||
}
|
||||
|
||||
mergedAggregations := map[string]map[string]*searchmsgBucket{}
|
||||
mergedMetrics := map[string]*searchsvc.AggregationResult{}
|
||||
for _, res := range responses {
|
||||
if res == nil {
|
||||
continue
|
||||
@@ -296,6 +297,23 @@ func (s *Service) Search(ctx context.Context, req *searchsvc.SearchRequest) (*se
|
||||
matches = append(matches, match)
|
||||
}
|
||||
for _, agg := range res.GetAggregations() {
|
||||
// Top-level metric: reduce across spaces; keyed by field+kind so
|
||||
// several metrics on the same field stay separate.
|
||||
if kind := agg.GetMetricKind(); kind != searchsvc.MetricKind_METRIC_KIND_UNSPECIFIED {
|
||||
key := agg.GetField() + "|" + kind.String()
|
||||
existing, ok := mergedMetrics[key]
|
||||
if !ok {
|
||||
mergedMetrics[key] = agg
|
||||
continue
|
||||
}
|
||||
if kind == searchsvc.MetricKind_METRIC_KIND_AVG {
|
||||
existing.Sum += agg.GetSum()
|
||||
existing.Count += agg.GetCount()
|
||||
} else {
|
||||
existing.Value = reduceMetric(kind, existing.GetValue(), agg.GetValue())
|
||||
}
|
||||
continue
|
||||
}
|
||||
field := agg.GetField()
|
||||
if _, ok := mergedAggregations[field]; !ok {
|
||||
mergedAggregations[field] = map[string]*searchmsgBucket{}
|
||||
@@ -330,6 +348,12 @@ func (s *Service) Search(ctx context.Context, req *searchsvc.SearchRequest) (*se
|
||||
aggregations := make([]*searchsvc.AggregationResult, 0, len(req.GetAggregations()))
|
||||
for _, opt := range req.GetAggregations() {
|
||||
field := opt.GetField()
|
||||
if kind := opt.GetMetricKind(); kind != searchsvc.MetricKind_METRIC_KIND_UNSPECIFIED {
|
||||
if m, ok := mergedMetrics[field+"|"+kind.String()]; ok {
|
||||
aggregations = append(aggregations, m)
|
||||
}
|
||||
continue
|
||||
}
|
||||
bucketMap := mergedAggregations[field]
|
||||
buckets := make([]*searchsvc.Bucket, 0, len(bucketMap))
|
||||
for _, b := range bucketMap {
|
||||
|
||||
Reference in new issue
Block a user