Implement sortSeries

This commit is contained in:
Alexander Zobnin
2021-05-27 12:57:09 +03:00
parent 67289c91f7
commit 24e9129fbe
3 changed files with 41 additions and 10 deletions

View File

@@ -64,8 +64,9 @@ func init() {
} }
filterFuncMap = map[string]AggDataProcessingFunc{ filterFuncMap = map[string]AggDataProcessingFunc{
"top": applyTop, "top": applyTop,
"bottom": applyBottom, "bottom": applyBottom,
"sortSeries": applySortSeries,
} }
// Functions processing on the frontend // Functions processing on the frontend
@@ -244,6 +245,17 @@ func applyBottom(series []*timeseries.TimeSeriesData, params ...interface{}) ([]
return filteredSeries, nil return filteredSeries, nil
} }
func applySortSeries(series []*timeseries.TimeSeriesData, params ...interface{}) ([]*timeseries.TimeSeriesData, error) {
order, err := MustString(params[0])
if err != nil {
return nil, errParsingFunctionParam(err)
}
aggFunc := timeseries.AggAvg
sorted := timeseries.SortBy(series, order, aggFunc)
return sorted, nil
}
func getAggFunc(agg string) timeseries.AggFunc { func getAggFunc(agg string) timeseries.AggFunc {
switch agg { switch agg {
case "avg": case "avg":
@@ -266,3 +278,9 @@ func getAggFunc(agg string) timeseries.AggFunc {
return timeseries.AggAvg return timeseries.AggAvg
} }
} }
func sortSeriesPoints(series []*timeseries.TimeSeriesData) {
for _, s := range series {
s.TS.Sort()
}
}

View File

@@ -74,7 +74,6 @@ func (ds *ZabbixDatasourceInstance) queryNumericDataForItems(ctx context.Context
} }
series := convertHistoryToTimeSeries(history, items) series := convertHistoryToTimeSeries(history, items)
// TODO: handle time series functions
series, err = applyFunctions(series, query.Functions) series, err = applyFunctions(series, query.Functions)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@@ -95,6 +95,21 @@ func (ts TimeSeries) Transform(transformFunc TransformFunc) TimeSeries {
} }
func Filter(series []*TimeSeriesData, n int, order string, aggFunc AggFunc) []*TimeSeriesData { func Filter(series []*TimeSeriesData, n int, order string, aggFunc AggFunc) []*TimeSeriesData {
SortBy(series, "asc", aggFunc)
filteredSeries := make([]*TimeSeriesData, n)
for i := 0; i < n; i++ {
if order == "top" {
filteredSeries[i] = series[len(series)-1-i]
} else if order == "bottom" {
filteredSeries[i] = series[i]
}
}
return filteredSeries
}
func SortBy(series []*TimeSeriesData, order string, aggFunc AggFunc) []*TimeSeriesData {
aggregatedSeries := make([]TimeSeries, len(series)) aggregatedSeries := make([]TimeSeries, len(series))
for i, s := range series { for i, s := range series {
aggregatedSeries[i] = s.TS.GroupByRange(aggFunc) aggregatedSeries[i] = s.TS.GroupByRange(aggFunc)
@@ -110,16 +125,15 @@ func Filter(series []*TimeSeriesData, n int, order string, aggFunc AggFunc) []*T
return false return false
}) })
filteredSeries := make([]*TimeSeriesData, n) if order == "desc" {
for i := 0; i < n; i++ { reverseSeries := make([]*TimeSeriesData, len(series))
if order == "top" { for i := 0; i < len(series); i++ {
filteredSeries[i] = series[len(aggregatedSeries)-1-i] reverseSeries[i] = series[len(series)-1-i]
} else if order == "bottom" {
filteredSeries[i] = series[i]
} }
series = reverseSeries
} }
return filteredSeries return series
} }
func AggregateBy(series []*TimeSeriesData, interval time.Duration, aggFunc AggFunc) *TimeSeriesData { func AggregateBy(series []*TimeSeriesData, interval time.Duration, aggFunc AggFunc) *TimeSeriesData {