Skip to content

Commit

Permalink
Fix top() and bottom() functions by fixing sorting, closes #1313
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderzobnin committed Sep 23, 2021
1 parent 7502922 commit 09c91cc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
3 changes: 3 additions & 0 deletions pkg/timeseries/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ type TimeSeriesMeta struct {

// Item update interval. nil means not supported intervals (flexible, schedule, etc)
Interval *time.Duration

// AggValue is using for sorting purposes
AggValue *float64
}

type AggFunc = func(points []TimePoint) *float64
Expand Down
11 changes: 5 additions & 6 deletions pkg/timeseries/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ import (

// SortBy sorts series by value calculated with provided aggFunc in given order
func SortBy(series []*TimeSeriesData, order string, aggFunc AggFunc) []*TimeSeriesData {
aggregatedSeries := make([]TimeSeries, len(series))
for i, s := range series {
aggregatedSeries[i] = s.TS.GroupByRange(aggFunc)
for _, s := range series {
s.Meta.AggValue = aggFunc(s.TS)
}

// Sort by aggregated value
sort.Slice(series, func(i, j int) bool {
if len(aggregatedSeries[i]) > 0 && len(aggregatedSeries[j]) > 0 {
return *aggregatedSeries[i][0].Value < *aggregatedSeries[j][0].Value
} else if len(aggregatedSeries[j]) > 0 {
if series[i].Meta.AggValue != nil && series[j].Meta.AggValue != nil {
return *series[i].Meta.AggValue < *series[j].Meta.AggValue
} else if series[j].Meta.AggValue != nil {
return true
}
return false
Expand Down

0 comments on commit 09c91cc

Please sign in to comment.