From 245c1ee3d3afbf22797ab0ea85b227796efa934b Mon Sep 17 00:00:00 2001 From: Yuriy Tseretyan Date: Fri, 21 Oct 2022 10:03:43 -0400 Subject: [PATCH] Chore: Add `last` downsampling function to Resample expression (#57379) --- pkg/expr/mathexp/resample.go | 4 +++ pkg/expr/mathexp/resample_test.go | 32 ++++++++++++++++++++++++ public/app/features/expressions/types.ts | 1 + 3 files changed, 37 insertions(+) diff --git a/pkg/expr/mathexp/resample.go b/pkg/expr/mathexp/resample.go index 13cd4b0914a..f6239be59f9 100644 --- a/pkg/expr/mathexp/resample.go +++ b/pkg/expr/mathexp/resample.go @@ -54,6 +54,8 @@ func (s Series) Resample(refID string, interval time.Duration, downsampler strin default: return s, fmt.Errorf("upsampling %v not implemented", upsampler) } + } else if len(vals) == 1 { + value = vals[0] } else { // downsampling fVec := data.NewField("", s.GetLabels(), vals) ff := Float64Field(*fVec) @@ -67,6 +69,8 @@ func (s Series) Resample(refID string, interval time.Duration, downsampler strin tmp = Min(&ff) case "max": tmp = Max(&ff) + case "last": + tmp = Last(&ff) default: return s, fmt.Errorf("downsampling %v not implemented", downsampler) } diff --git a/pkg/expr/mathexp/resample_test.go b/pkg/expr/mathexp/resample_test.go index 352d979ed6c..a09ae962266 100644 --- a/pkg/expr/mathexp/resample_test.go +++ b/pkg/expr/mathexp/resample_test.go @@ -245,6 +245,38 @@ func TestResampleSeries(t *testing.T) { time.Unix(10, 0), nil, }), }, + { + name: "resample series: downsampling (last / pad )", + interval: time.Second * 3, + downsampler: "last", + upsampler: "pad", + timeRange: backend.TimeRange{ + From: time.Unix(0, 0), + To: time.Unix(11, 0), + }, + seriesToResample: makeSeries("", nil, tp{ + time.Unix(0, 0), float64Pointer(0), + }, tp{ + time.Unix(2, 0), float64Pointer(2), + }, tp{ + time.Unix(4, 0), float64Pointer(3), + }, tp{ + time.Unix(6, 0), float64Pointer(4), + }, tp{ + time.Unix(8, 0), float64Pointer(0), + }, tp{ + time.Unix(10, 0), float64Pointer(1), + }), + series: makeSeries("", nil, tp{ + time.Unix(0, 0), float64Pointer(0), + }, tp{ + time.Unix(3, 0), float64Pointer(2), + }, tp{ + time.Unix(6, 0), float64Pointer(4), + }, tp{ + time.Unix(9, 0), float64Pointer(0), + }), + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/public/app/features/expressions/types.ts b/public/app/features/expressions/types.ts index 0fbbff79d7e..b67b9fe6a81 100644 --- a/public/app/features/expressions/types.ts +++ b/public/app/features/expressions/types.ts @@ -75,6 +75,7 @@ export const reducerMode: Array> = [ ]; export const downsamplingTypes: Array> = [ + { value: ReducerID.last, label: 'Last', description: 'Fill with the last value' }, { value: ReducerID.min, label: 'Min', description: 'Fill with the minimum value' }, { value: ReducerID.max, label: 'Max', description: 'Fill with the maximum value' }, { value: ReducerID.mean, label: 'Mean', description: 'Fill with the average value' },