Implement groupBy
This commit is contained in:
@@ -2,8 +2,8 @@ package datasource
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/alexanderzobnin/grafana-zabbix/pkg/gtime"
|
||||
"github.com/alexanderzobnin/grafana-zabbix/pkg/timeseries"
|
||||
)
|
||||
|
||||
@@ -11,20 +11,26 @@ var errFunctionNotSupported = func(name string) error {
|
||||
return fmt.Errorf("function not supported: %s", name)
|
||||
}
|
||||
|
||||
type DataProcessingFunc = func(series timeseries.TimeSeries, params ...string) timeseries.TimeSeries
|
||||
type DataProcessingFunc = func(series timeseries.TimeSeries, params ...string) (timeseries.TimeSeries, error)
|
||||
|
||||
var funcMap map[string]DataProcessingFunc
|
||||
|
||||
func init() {
|
||||
funcMap = make(map[string]DataProcessingFunc)
|
||||
funcMap["groupBy"] = applyGroupBy
|
||||
funcMap = map[string]DataProcessingFunc{
|
||||
"groupBy": applyGroupBy,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func applyFunctions(series []*timeseries.TimeSeriesData, functions []QueryFunction) ([]*timeseries.TimeSeriesData, error) {
|
||||
for _, f := range functions {
|
||||
if applyFunc, ok := funcMap[f.Def.Name]; ok {
|
||||
for _, s := range series {
|
||||
s.TS = applyFunc(s.TS, f.Params...)
|
||||
result, err := applyFunc(s.TS, f.Params...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.TS = result
|
||||
}
|
||||
} else {
|
||||
err := errFunctionNotSupported(f.Def.Name)
|
||||
@@ -34,7 +40,32 @@ func applyFunctions(series []*timeseries.TimeSeriesData, functions []QueryFuncti
|
||||
return series, nil
|
||||
}
|
||||
|
||||
func applyGroupBy(series timeseries.TimeSeries, params ...string) timeseries.TimeSeries {
|
||||
s := series.GroupBy(time.Minute, "avg")
|
||||
return s
|
||||
func applyGroupBy(series timeseries.TimeSeries, params ...string) (timeseries.TimeSeries, error) {
|
||||
pInterval := params[0]
|
||||
pAgg := params[1]
|
||||
interval, err := gtime.ParseInterval(pInterval)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
aggFunc := getAggFunc(pAgg)
|
||||
s := series.GroupBy(interval, aggFunc)
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func getAggFunc(agg string) timeseries.AggFunc {
|
||||
switch agg {
|
||||
case "avg":
|
||||
return timeseries.AggAvg
|
||||
case "max":
|
||||
return timeseries.AggMax
|
||||
case "min":
|
||||
return timeseries.AggMin
|
||||
case "first":
|
||||
return timeseries.AggFirst
|
||||
case "last":
|
||||
return timeseries.AggLast
|
||||
default:
|
||||
return timeseries.AggAvg
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user