From ac220a76e87595700885fdb062262810411c02f3 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Wed, 26 May 2021 17:48:36 +0300 Subject: [PATCH] Support $__range_series variable --- pkg/datasource/functions.go | 10 +++++++++- pkg/timeseries/timeseries.go | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/pkg/datasource/functions.go b/pkg/datasource/functions.go index 8771ff6..60fef62 100644 --- a/pkg/datasource/functions.go +++ b/pkg/datasource/functions.go @@ -8,6 +8,8 @@ import ( "github.com/alexanderzobnin/grafana-zabbix/pkg/timeseries" ) +const RANGE_VARIABLE_VALUE = "range_series" + var errFunctionNotSupported = func(name string) error { return fmt.Errorf("function not supported: %s", name) } @@ -75,12 +77,18 @@ func applyFunctions(series []*timeseries.TimeSeriesData, functions []QueryFuncti func applyGroupBy(series timeseries.TimeSeries, params ...string) (timeseries.TimeSeries, error) { pInterval := params[0] pAgg := params[1] + aggFunc := getAggFunc(pAgg) + + if pInterval == RANGE_VARIABLE_VALUE { + s := series.GroupByRange(aggFunc) + return s, nil + } + interval, err := gtime.ParseInterval(pInterval) if err != nil { return nil, errParsingFunctionParam(err) } - aggFunc := getAggFunc(pAgg) s := series.GroupBy(interval, aggFunc) return s, nil } diff --git a/pkg/timeseries/timeseries.go b/pkg/timeseries/timeseries.go index 63b130c..923f27d 100644 --- a/pkg/timeseries/timeseries.go +++ b/pkg/timeseries/timeseries.go @@ -75,6 +75,18 @@ func (ts TimeSeries) GroupBy(interval time.Duration, aggFunc AggFunc) TimeSeries return groupedSeries } +func (ts TimeSeries) GroupByRange(aggFunc AggFunc) TimeSeries { + if ts.Len() == 0 { + return ts + } + + value := aggFunc(ts) + return []TimePoint{ + {Time: ts[0].Time, Value: value}, + {Time: ts[ts.Len()-1].Time, Value: value}, + } +} + func (ts TimeSeries) Transform(transformFunc TransformFunc) TimeSeries { for i, p := range ts { ts[i] = transformFunc(p)