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

@@ -66,6 +66,7 @@ func init() {
filterFuncMap = map[string]AggDataProcessingFunc{
"top": applyTop,
"bottom": applyBottom,
"sortSeries": applySortSeries,
}
// Functions processing on the frontend
@@ -244,6 +245,17 @@ func applyBottom(series []*timeseries.TimeSeriesData, params ...interface{}) ([]
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 {
switch agg {
case "avg":
@@ -266,3 +278,9 @@ func getAggFunc(agg string) timeseries.AggFunc {
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)
// TODO: handle time series functions
series, err = applyFunctions(series, query.Functions)
if err != nil {
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 {
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))
for i, s := range series {
aggregatedSeries[i] = s.TS.GroupByRange(aggFunc)
@@ -110,16 +125,15 @@ func Filter(series []*TimeSeriesData, n int, order string, aggFunc AggFunc) []*T
return false
})
filteredSeries := make([]*TimeSeriesData, n)
for i := 0; i < n; i++ {
if order == "top" {
filteredSeries[i] = series[len(aggregatedSeries)-1-i]
} else if order == "bottom" {
filteredSeries[i] = series[i]
if order == "desc" {
reverseSeries := make([]*TimeSeriesData, len(series))
for i := 0; i < len(series); i++ {
reverseSeries[i] = series[len(series)-1-i]
}
series = reverseSeries
}
return filteredSeries
return series
}
func AggregateBy(series []*TimeSeriesData, interval time.Duration, aggFunc AggFunc) *TimeSeriesData {