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 Graphite invert function #3644

Merged
merged 4 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
23 changes: 21 additions & 2 deletions src/query/graphite/native/builtin_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1265,22 +1265,40 @@ func exclude(_ *common.Context, input singlePathSpec, pattern string) (ts.Series
// and raises the datapoint by the power of the constant provided at each point
// nolint: gocritic
func pow(ctx *common.Context, input singlePathSpec, factor float64) (ts.SeriesList, error) {
r := powHelper(ctx, input, factor, false)
return r, nil
}

// invert takes one metric or a wildcard seriesList, and inverts each datapoint (i.e. 1/x).
func invert(ctx *common.Context, input singlePathSpec) (ts.SeriesList, error) {
r := powHelper(ctx, input, -1, true)
return r, nil
}

func powHelper(ctx *common.Context, input singlePathSpec, factor float64, isInvert bool) ts.SeriesList {
results := make([]*ts.Series, 0, len(input.Values))

renamePrefix := "pow"
if isInvert {
renamePrefix = "invert"
}
for _, series := range input.Values {
numSteps := series.Len()
millisPerStep := series.MillisPerStep()
vals := ts.NewValues(ctx, millisPerStep, numSteps)
for i := 0; i < numSteps; i++ {
vals.SetValueAt(i, math.Pow(series.ValueAt(i), factor))
}
newName := fmt.Sprintf("pow(%s, %f)", series.Name(), factor)
newName := fmt.Sprintf("%s(%s, %f)", renamePrefix, series.Name(), factor)
if isInvert {
newName = fmt.Sprintf("%s(%s)", renamePrefix, series.Name())
}
results = append(results, ts.NewSeries(ctx, newName, series.StartTime(), vals))
}

r := ts.SeriesList(input)
r.Values = results
return r, nil
return r
}

// logarithm takes one metric or a wildcard seriesList, and draws the y-axis in
Expand Down Expand Up @@ -2846,6 +2864,7 @@ func init() {
MustRegisterFunction(interpolate).WithDefaultParams(map[uint8]interface{}{
2: -1, // limit
})
MustRegisterFunction(invert)
MustRegisterFunction(isNonNull)
MustRegisterFunction(keepLastValue).WithDefaultParams(map[uint8]interface{}{
2: -1, // limit
Expand Down
115 changes: 81 additions & 34 deletions src/query/graphite/native/builtin_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -904,12 +904,12 @@ func TestCombineBootstrapWithOriginal(t *testing.T) {
Engine: NewEngine(&common.MovingFunctionStorage{}, CompileOptions{}),
})

originalStart = time.Date(2020, time.October, 5, 1, 16, 00, 0, time.UTC)
originalStart = time.Date(2020, time.October, 5, 1, 16, 0o0, 0, time.UTC)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gofumpt automatically change it. tests still working though

originalValues = []float64{14, 15, 16, 17, 18}
originalSeriesListValues = []*ts.Series{ts.NewSeries(ctx, "original", originalStart, common.NewTestSeriesValues(ctx, 30000, originalValues))}
originalSeriesList = singlePathSpec{Values: originalSeriesListValues}

bootstrappedStart = time.Date(2020, time.October, 5, 1, 15, 00, 0, time.UTC)
bootstrappedStart = time.Date(2020, time.October, 5, 1, 15, 0o0, 0, time.UTC)
bootstrappedValues = []float64{12, 13, 14, 15, 16, 17, 18}
bootstrappedSeriesListValues = []*ts.Series{ts.NewSeries(ctx, "original", bootstrappedStart, common.NewTestSeriesValues(ctx, 30000, bootstrappedValues))}
bootstrappedSeriesList = ts.NewSeriesList()
Expand Down Expand Up @@ -2478,7 +2478,7 @@ func TestAsPercentWithSeriesList(t *testing.T) {
},
}

var inputSeries []*ts.Series
inputSeries := make([]*ts.Series, 0, len(inputs))
for _, input := range inputs {
timeSeries := ts.NewSeries(
ctx,
Expand All @@ -2489,7 +2489,7 @@ func TestAsPercentWithSeriesList(t *testing.T) {
inputSeries = append(inputSeries, timeSeries)
}

var expected []*ts.Series
expected := make([]*ts.Series, 0, len(outputs))
for _, output := range outputs {
timeSeries := ts.NewSeries(
ctx,
Expand Down Expand Up @@ -3234,7 +3234,6 @@ func TestLimitSortStable(t *testing.T) {

require.Equal(t, expectedOrder, order)
}

}

func TestHitcount(t *testing.T) {
Expand Down Expand Up @@ -3394,6 +3393,7 @@ func (*mockStorage) FetchByQuery(
) (*storage.FetchResult, error) {
return storage.NewFetchResult(ctx, nil, block.NewResultMetadata()), nil
}

func (*mockStorage) CompleteTags(
ctx context.Context,
query *querystorage.CompleteTagsQuery,
Expand Down Expand Up @@ -4170,41 +4170,88 @@ func TestPow(t *testing.T) {
var (
ctx = common.NewTestContext()
millisPerStep = 10000
output = []float64{1.0, 4.0, 9.0, 16.0, 25.0}
output2 = []float64{0.0, 4.0, 16.0, 36.0, 64.0}
)

defer func() { _ = ctx.Close() }()

series := ts.NewSeries(
ctx,
"foo",
ctx.StartTime,
common.NewTestSeriesValues(ctx, millisPerStep, []float64{1.0, 2.0, 3.0, 4.0, 5.0}),
)
results, err := pow(ctx, singlePathSpec{
Values: []*ts.Series{series},
}, 2)
require.Nil(t, err)
expected := common.TestSeries{Name: `pow(foo, 2.000000)`, Data: output}
require.Nil(t, err)
common.CompareOutputsAndExpected(t, millisPerStep, ctx.StartTime,
[]common.TestSeries{expected}, results.Values)
inputs := []struct {
name string
values []float64
pow float64
expected []float64
}{
{
"foo",
[]float64{1.0, 2.0, 3.0, 4.0, 5.0},
2,
[]float64{1.0, 4.0, 9.0, 16.0, 25.0},
},
{
"bar",
[]float64{0.0, 2.0, 4.0, 6.0, 8.0},
2,
[]float64{0.0, 4.0, 16.0, 36.0, 64.0},
},
}

series2 := ts.NewSeries(
ctx,
"foo",
ctx.StartTime,
common.NewTestSeriesValues(ctx, millisPerStep, []float64{0.0, 2.0, 4.0, 6.0, 8.0}),
for _, input := range inputs {
series := ts.NewSeries(
ctx,
input.name,
ctx.StartTime,
common.NewTestSeriesValues(ctx, millisPerStep, input.values),
)
results, err := pow(ctx, singlePathSpec{
Values: []*ts.Series{series},
}, input.pow)
require.NoError(t, err)
expected := common.TestSeries{
Name: fmt.Sprintf("pow(%s, %f)", input.name, input.pow),
Data: input.expected,
}
common.CompareOutputsAndExpected(t, millisPerStep, ctx.StartTime,
[]common.TestSeries{expected}, results.Values)
}
}

func TestInvert(t *testing.T) {
var (
ctx = common.NewTestContext()
millisPerStep = 10000
)
results2, err := pow(ctx, singlePathSpec{
Values: []*ts.Series{series, series2},
}, 2)
require.Nil(t, err)
expected2 := common.TestSeries{Name: `pow(foo, 2.000000)`, Data: output2}
require.Nil(t, err)
common.CompareOutputsAndExpected(t, millisPerStep, ctx.StartTime,
[]common.TestSeries{expected, expected2}, results2.Values)

defer func() { _ = ctx.Close() }()

inputs := []struct {
name string
values []float64
expected []float64
}{
{
"foo",
[]float64{1.0, 2.0, 4.0},
[]float64{1.0, 1 / 2.0, 1 / 4.0},
},
}

for _, input := range inputs {
series := ts.NewSeries(
ctx,
input.name,
ctx.StartTime,
common.NewTestSeriesValues(ctx, millisPerStep, input.values),
)
results, err := invert(ctx, singlePathSpec{
Values: []*ts.Series{series},
})
require.NoError(t, err)
expected := common.TestSeries{
Name: fmt.Sprintf("invert(%s)", input.name),
Data: input.expected,
}
common.CompareOutputsAndExpected(t, millisPerStep, ctx.StartTime,
[]common.TestSeries{expected}, results.Values)
}
}

func TestCumulative(t *testing.T) {
Expand Down