Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[query] add AverageBelow function #3641

Merged
merged 2 commits into from
Jul 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/query/graphite/native/builtin_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -2790,6 +2797,7 @@ func init() {
2: nil, // total
})
MustRegisterFunction(averageAbove)
MustRegisterFunction(averageBelow)
MustRegisterFunction(averageSeries)
MustRegisterFunction(averageSeriesWithWildcards).WithDefaultParams(map[uint8]interface{}{
2: -1, // positions
Expand Down
6 changes: 6 additions & 0 deletions src/query/graphite/native/builtin_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down