Skip to content

Commit

Permalink
[query] Implemented the Graphite cumulative function
Browse files Browse the repository at this point in the history
  • Loading branch information
teddywahle authored Sep 28, 2020
1 parent 13290be commit a7b499d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/query/graphite/native/builtin_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2079,6 +2079,11 @@ func consolidateBy(_ *common.Context, seriesList singlePathSpec, consolidationAp
return r, nil
}

// cumulative is an alias for consolidateBy(series, 'sum')
func cumulative(ctx *common.Context, seriesList singlePathSpec) (ts.SeriesList, error) {
return consolidateBy(ctx, seriesList, "sum")
}

// offsetToZero offsets a metric or wildcard seriesList by subtracting the minimum
// value in the series from each data point.
func offsetToZero(ctx *common.Context, seriesList singlePathSpec) (ts.SeriesList, error) {
Expand Down Expand Up @@ -2181,6 +2186,7 @@ func init() {
MustRegisterFunction(consolidateBy)
MustRegisterFunction(constantLine)
MustRegisterFunction(countSeries)
MustRegisterFunction(cumulative)
MustRegisterFunction(currentAbove)
MustRegisterFunction(currentBelow)
MustRegisterFunction(dashed).WithDefaultParams(map[uint8]interface{}{
Expand Down
34 changes: 34 additions & 0 deletions src/query/graphite/native/builtin_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3015,6 +3015,39 @@ func TestConsolidateBy(t *testing.T) {
require.NotNil(t, err)
}

func TestCumulative(t *testing.T) {
ctx := common.NewTestContext()
defer ctx.Close()

stepSize := 10000
input := struct {
name string
startTime time.Time
stepInMilli int
values []float64
}{
"foo",
ctx.StartTime,
stepSize,
[]float64{1.0, 2.0, 3.0, 4.0, math.NaN()},
}

series := ts.NewSeries(
ctx,
input.name,
input.startTime,
common.NewTestSeriesValues(ctx, input.stepInMilli, input.values),
)

results, err := cumulative(ctx, singlePathSpec{
Values: []*ts.Series{series},
})
expected := common.TestSeries{Name: `consolidateBy(foo,"sum")`, Data: input.values}
require.Nil(t, err)
common.CompareOutputsAndExpected(t, input.stepInMilli, input.startTime,
[]common.TestSeries{expected}, results.Values)
}

func TestOffsetToZero(t *testing.T) {
ctx := common.NewTestContext()
defer ctx.Close()
Expand Down Expand Up @@ -3263,6 +3296,7 @@ func TestFunctionsRegistered(t *testing.T) {
"consolidateBy",
"constantLine",
"countSeries",
"cumulative",
"currentAbove",
"currentBelow",
"dashed",
Expand Down

0 comments on commit a7b499d

Please sign in to comment.