From 709675a9a1d089d5ae95b548372353e4f8451894 Mon Sep 17 00:00:00 2001 From: Evan Yin Date: Sat, 31 Jul 2021 11:57:52 -0700 Subject: [PATCH] [query] Add Graphite averageBelow function (#3641) --- src/query/graphite/native/builtin_functions.go | 8 ++++++++ src/query/graphite/native/builtin_functions_test.go | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/src/query/graphite/native/builtin_functions.go b/src/query/graphite/native/builtin_functions.go index 4ed51d9a69..f885cc6499 100644 --- a/src/query/graphite/native/builtin_functions.go +++ b/src/query/graphite/native/builtin_functions.go @@ -273,6 +273,13 @@ func averageAbove(ctx *common.Context, series singlePathSpec, n float64) (ts.Ser return aboveByFunction(ctx, series, sr, n) } +// averageBelow takes one metric or a wildcard seriesList followed by an floating point number n, +// returns only the metrics with an average value below n. +func averageBelow(ctx *common.Context, series singlePathSpec, n float64) (ts.SeriesList, error) { + sr := ts.SeriesReducerAvg.Reducer() + return belowByFunction(ctx, series, sr, n) +} + // currentAbove takes one metric or a wildcard seriesList followed by an floating point number n, // returns only the metrics with the last value above n. func currentAbove(ctx *common.Context, series singlePathSpec, n float64) (ts.SeriesList, error) { @@ -2790,6 +2797,7 @@ func init() { 2: nil, // total }) MustRegisterFunction(averageAbove) + MustRegisterFunction(averageBelow) MustRegisterFunction(averageSeries) MustRegisterFunction(averageSeriesWithWildcards).WithDefaultParams(map[uint8]interface{}{ 2: -1, // positions diff --git a/src/query/graphite/native/builtin_functions_test.go b/src/query/graphite/native/builtin_functions_test.go index d34493fb8e..c6793dd359 100644 --- a/src/query/graphite/native/builtin_functions_test.go +++ b/src/query/graphite/native/builtin_functions_test.go @@ -2008,6 +2008,12 @@ func TestAverageAbove(t *testing.T) { testComparatorFunc(t, averageAbove, 12000, nil) } +func TestAverageBelow(t *testing.T) { + testComparatorFunc(t, averageBelow, 0, nil) + testComparatorFunc(t, averageBelow, 600, []int{0, 2, 3}) + testComparatorFunc(t, averageBelow, 12000, []int{0, 2, 3, 4}) +} + func TestCurrentAbove(t *testing.T) { testComparatorFunc(t, currentAbove, -10, []int{0, 2, 3, 4}) testComparatorFunc(t, currentAbove, -5, []int{0, 3, 4})