From ef7681f68970fc800b78ce127a732252ec6b7847 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Thu, 27 May 2021 16:47:18 +0300 Subject: [PATCH] Implement transformNull --- pkg/datasource/functions.go | 21 ++++++++++++++++----- pkg/timeseries/transform_functions.go | 13 +++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/pkg/datasource/functions.go b/pkg/datasource/functions.go index 6e00539..2af7a98 100644 --- a/pkg/datasource/functions.go +++ b/pkg/datasource/functions.go @@ -57,11 +57,12 @@ var frontendFuncMap map[string]bool func init() { seriesFuncMap = map[string]DataProcessingFunc{ - "groupBy": applyGroupBy, - "scale": applyScale, - "offset": applyOffset, - "percentile": applyPercentile, - "timeShift": applyTimeShiftPost, + "groupBy": applyGroupBy, + "scale": applyScale, + "offset": applyOffset, + "transformNull": applyTransformNull, + "percentile": applyPercentile, + "timeShift": applyTimeShiftPost, } aggFuncMap = map[string]AggDataProcessingFunc{ @@ -202,6 +203,16 @@ func applyOffset(series timeseries.TimeSeries, params ...interface{}) (timeserie return series.Transform(transformFunc), nil } +func applyTransformNull(series timeseries.TimeSeries, params ...interface{}) (timeseries.TimeSeries, error) { + nullValue, err := MustFloat64(params[0]) + if err != nil { + return nil, errParsingFunctionParam(err) + } + + transformFunc := timeseries.TransformNull(nullValue) + return series.Transform(transformFunc), nil +} + func applyAggregateBy(series []*timeseries.TimeSeriesData, params ...interface{}) ([]*timeseries.TimeSeriesData, error) { pInterval, err := MustString(params[0]) pAgg, err := MustString(params[1]) diff --git a/pkg/timeseries/transform_functions.go b/pkg/timeseries/transform_functions.go index d989b95..f7c0eb6 100644 --- a/pkg/timeseries/transform_functions.go +++ b/pkg/timeseries/transform_functions.go @@ -14,6 +14,12 @@ func TransformOffset(offset float64) TransformFunc { } } +func TransformNull(nullValue float64) TransformFunc { + return func(point TimePoint) TimePoint { + return transformNull(point, nullValue) + } +} + func TransformShiftTime(interval time.Duration) TransformFunc { return func(point TimePoint) TimePoint { return transformShiftTime(point, interval) @@ -41,3 +47,10 @@ func transformShiftTime(point TimePoint, interval time.Duration) TimePoint { point.Time = shiftedTime return point } + +func transformNull(point TimePoint, nullValue float64) TimePoint { + if point.Value == nil { + point.Value = &nullValue + } + return point +}