mirror of
https://github.com/SigNoz/signoz.git
synced 2026-02-03 08:33:26 +00:00
feat: add abs value function in formula (#9315)
Signed-off-by: “niladrix719” <niladrix719@gmail.com> Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
)
|
||||
|
||||
var SupportedFunctions = []string{
|
||||
"abs",
|
||||
"exp",
|
||||
"log",
|
||||
"ln",
|
||||
|
||||
@@ -197,10 +197,14 @@ func processResults(
|
||||
}, nil
|
||||
}
|
||||
|
||||
var SupportedFunctions = []string{"exp", "log", "ln", "exp2", "log2", "exp10", "log10", "sqrt", "cbrt", "erf", "erfc", "lgamma", "tgamma", "sin", "cos", "tan", "asin", "acos", "atan", "degrees", "radians", "now", "toUnixTimestamp"}
|
||||
var SupportedFunctions = []string{"abs", "exp", "log", "ln", "exp2", "log2", "exp10", "log10", "sqrt", "cbrt", "erf", "erfc", "lgamma", "tgamma", "sin", "cos", "tan", "asin", "acos", "atan", "degrees", "radians", "now", "toUnixTimestamp"}
|
||||
|
||||
func EvalFuncs() map[string]govaluate.ExpressionFunction {
|
||||
GoValuateFuncs := make(map[string]govaluate.ExpressionFunction)
|
||||
// Returns the absolute value of the given argument.
|
||||
GoValuateFuncs["abs"] = func(args ...interface{}) (interface{}, error) {
|
||||
return math.Abs(args[0].(float64)), nil
|
||||
}
|
||||
// Returns e to the power of the given argument.
|
||||
GoValuateFuncs["exp"] = func(args ...interface{}) (interface{}, error) {
|
||||
return math.Exp(args[0].(float64)), nil
|
||||
|
||||
@@ -545,6 +545,9 @@ func EvalFuncs() map[string]govaluate.ExpressionFunction {
|
||||
rad180 := 180 / math.Pi
|
||||
|
||||
// Mathematical functions
|
||||
funcs["abs"] = func(args ...any) (any, error) {
|
||||
return math.Abs(args[0].(float64)), nil
|
||||
}
|
||||
funcs["exp"] = func(args ...any) (any, error) {
|
||||
return math.Exp(args[0].(float64)), nil
|
||||
}
|
||||
@@ -623,7 +626,7 @@ func EvalFuncs() map[string]govaluate.ExpressionFunction {
|
||||
// GetSupportedFunctions returns the list of supported function names
|
||||
func GetSupportedFunctions() []string {
|
||||
return []string{
|
||||
"exp", "log", "ln", "exp2", "log2", "exp10", "log10",
|
||||
"abs", "exp", "log", "ln", "exp2", "log2", "exp10", "log10",
|
||||
"sqrt", "cbrt", "erf", "erfc", "lgamma", "tgamma",
|
||||
"sin", "cos", "tan", "asin", "acos", "atan",
|
||||
"degrees", "radians", "now",
|
||||
|
||||
@@ -863,3 +863,39 @@ func TestComplexExpression(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAbsValueExpression(t *testing.T) {
|
||||
tsData := map[string]*TimeSeriesData{
|
||||
"A": createFormulaTestTimeSeriesData("A", []*TimeSeries{
|
||||
{
|
||||
Labels: createLabels(map[string]string{"service_name": "frontend"}),
|
||||
Values: createValues(map[int64]float64{
|
||||
1: -10,
|
||||
2: 20,
|
||||
}),
|
||||
},
|
||||
}),
|
||||
"B": createFormulaTestTimeSeriesData("B", []*TimeSeries{
|
||||
{
|
||||
Labels: createLabels(map[string]string{"service_name": "frontend"}),
|
||||
Values: createValues(map[int64]float64{
|
||||
1: 5,
|
||||
2: -4,
|
||||
}),
|
||||
},
|
||||
}),
|
||||
}
|
||||
|
||||
evaluator, err := NewFormulaEvaluator("abs(A) + abs(B)", map[string]bool{"A": true, "B": true})
|
||||
require.NoError(t, err)
|
||||
|
||||
result, err := evaluator.EvaluateFormula(tsData)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.Equal(t, 1, len(result))
|
||||
|
||||
series := result[0]
|
||||
require.Equal(t, 2, len(series.Values))
|
||||
assert.Equal(t, 15.0, series.Values[0].Value) // |−10| + |5| = 15
|
||||
assert.Equal(t, 24.0, series.Values[1].Value) // |20| + |−4| = 24
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user