Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1774 from grafana/groupbynode-respect-order
Browse files Browse the repository at this point in the history
groupByNode[s]: implement ordering like graphite does
  • Loading branch information
Dieterbe authored Apr 15, 2020
2 parents 34784be + 52c7b9d commit cf39498
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
14 changes: 12 additions & 2 deletions expr/func_groupbynodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,21 @@ func (s *FuncGroupByNodes) Exec(dataMap DataMap) ([]models.Series, error) {
s []models.Series
m models.SeriesMeta
}

groups := make(map[string]Group)

// list of aggregation keys in order they were seen.
// we need to return series in this order
var keyList []string

// Group series by nodes, this is mostly similar to GroupByTags,
// except that the group keys are different.
for _, serie := range series {
key := aggKey(serie, s.nodes)
group := groups[key]
group, ok := groups[key]
if !ok {
keyList = append(keyList, key)
}
group.s = append(group.s, serie)
group.m = group.m.Merge(serie.Meta)
groups[key] = group
Expand All @@ -70,7 +79,8 @@ func (s *FuncGroupByNodes) Exec(dataMap DataMap) ([]models.Series, error) {
output := make([]models.Series, 0, len(groups))
aggFunc := getCrossSeriesAggFunc(s.aggregator)

for key, group := range groups {
for _, key := range keyList {
group := groups[key]
consolidator, queryConsolidator := summarizeCons(group.s)
outSeries := models.Series{
Target: key,
Expand Down
8 changes: 0 additions & 8 deletions expr/func_groupbynodes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package expr

import (
"math"
"sort"
"strconv"
"testing"

Expand Down Expand Up @@ -241,13 +240,6 @@ func testGroupByNodes(name string, in []models.Series, expected []models.Series,
t.Fatalf("case %q: output length expected to be %d but got %d", name, len(expected), len(got))
}

sort.Slice(got, func(i, j int) bool {
return got[i].Target < got[j].Target
})
sort.Slice(expected, func(i, j int) bool {
return expected[i].Target < expected[j].Target
})

for i, g := range got {
o := expected[i]
if g.Target != o.Target {
Expand Down

0 comments on commit cf39498

Please sign in to comment.