Fix top() and bottom() functions by fixing sorting, closes #1313

This commit is contained in:
Alexander Zobnin
2021-09-23 17:44:02 +03:00
parent 750292296c
commit 09c91ccbe4
2 changed files with 8 additions and 6 deletions

View File

@@ -49,6 +49,9 @@ type TimeSeriesMeta struct {
// Item update interval. nil means not supported intervals (flexible, schedule, etc) // Item update interval. nil means not supported intervals (flexible, schedule, etc)
Interval *time.Duration Interval *time.Duration
// AggValue is using for sorting purposes
AggValue *float64
} }
type AggFunc = func(points []TimePoint) *float64 type AggFunc = func(points []TimePoint) *float64

View File

@@ -7,16 +7,15 @@ import (
// SortBy sorts series by value calculated with provided aggFunc in given order // SortBy sorts series by value calculated with provided aggFunc in given order
func SortBy(series []*TimeSeriesData, order string, aggFunc AggFunc) []*TimeSeriesData { func SortBy(series []*TimeSeriesData, order string, aggFunc AggFunc) []*TimeSeriesData {
aggregatedSeries := make([]TimeSeries, len(series)) for _, s := range series {
for i, s := range series { s.Meta.AggValue = aggFunc(s.TS)
aggregatedSeries[i] = s.TS.GroupByRange(aggFunc)
} }
// Sort by aggregated value // Sort by aggregated value
sort.Slice(series, func(i, j int) bool { sort.Slice(series, func(i, j int) bool {
if len(aggregatedSeries[i]) > 0 && len(aggregatedSeries[j]) > 0 { if series[i].Meta.AggValue != nil && series[j].Meta.AggValue != nil {
return *aggregatedSeries[i][0].Value < *aggregatedSeries[j][0].Value return *series[i].Meta.AggValue < *series[j].Meta.AggValue
} else if len(aggregatedSeries[j]) > 0 { } else if series[j].Meta.AggValue != nil {
return true return true
} }
return false return false