2024-10-03 16:48:32 +05:30
|
|
|
package rules
|
|
|
|
|
|
|
|
|
|
import (
|
2025-10-03 19:47:15 +05:30
|
|
|
"github.com/stretchr/testify/require"
|
2024-10-03 16:48:32 +05:30
|
|
|
"testing"
|
|
|
|
|
|
2025-03-20 21:01:41 +05:30
|
|
|
v3 "github.com/SigNoz/signoz/pkg/query-service/model/v3"
|
2025-04-18 00:04:25 +05:30
|
|
|
ruletypes "github.com/SigNoz/signoz/pkg/types/ruletypes"
|
2024-10-03 16:48:32 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestBaseRule_RequireMinPoints(t *testing.T) {
|
|
|
|
|
threshold := 1.0
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
rule *BaseRule
|
|
|
|
|
shouldAlert bool
|
|
|
|
|
series *v3.Series
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "test should skip if less than min points",
|
|
|
|
|
rule: &BaseRule{
|
2025-04-18 00:04:25 +05:30
|
|
|
ruleCondition: &ruletypes.RuleCondition{
|
2024-10-03 16:48:32 +05:30
|
|
|
RequireMinPoints: true,
|
|
|
|
|
RequiredNumPoints: 4,
|
|
|
|
|
},
|
2025-10-03 19:47:15 +05:30
|
|
|
|
|
|
|
|
Threshold: ruletypes.BasicRuleThresholds{
|
|
|
|
|
{
|
|
|
|
|
Name: "test-threshold",
|
|
|
|
|
TargetValue: &threshold,
|
|
|
|
|
CompareOp: ruletypes.ValueIsAbove,
|
|
|
|
|
MatchType: ruletypes.AtleastOnce,
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-10-03 16:48:32 +05:30
|
|
|
},
|
|
|
|
|
series: &v3.Series{
|
|
|
|
|
Points: []v3.Point{
|
|
|
|
|
{Value: 1},
|
|
|
|
|
{Value: 2},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
shouldAlert: false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "test should alert if more than min points",
|
|
|
|
|
rule: &BaseRule{
|
2025-04-18 00:04:25 +05:30
|
|
|
ruleCondition: &ruletypes.RuleCondition{
|
2024-10-03 16:48:32 +05:30
|
|
|
RequireMinPoints: true,
|
|
|
|
|
RequiredNumPoints: 4,
|
2025-04-18 00:04:25 +05:30
|
|
|
CompareOp: ruletypes.ValueIsAbove,
|
|
|
|
|
MatchType: ruletypes.AtleastOnce,
|
2024-10-03 16:48:32 +05:30
|
|
|
Target: &threshold,
|
|
|
|
|
},
|
2025-10-03 19:47:15 +05:30
|
|
|
Threshold: ruletypes.BasicRuleThresholds{
|
|
|
|
|
{
|
|
|
|
|
Name: "test-threshold",
|
|
|
|
|
TargetValue: &threshold,
|
|
|
|
|
CompareOp: ruletypes.ValueIsAbove,
|
|
|
|
|
MatchType: ruletypes.AtleastOnce,
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-10-03 16:48:32 +05:30
|
|
|
},
|
|
|
|
|
series: &v3.Series{
|
|
|
|
|
Points: []v3.Point{
|
|
|
|
|
{Value: 1},
|
|
|
|
|
{Value: 2},
|
|
|
|
|
{Value: 3},
|
|
|
|
|
{Value: 4},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
shouldAlert: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2025-10-03 19:47:15 +05:30
|
|
|
_, err := test.rule.Threshold.ShouldAlert(*test.series, "")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, len(test.series.Points) >= test.rule.ruleCondition.RequiredNumPoints, test.shouldAlert)
|
2024-10-03 16:48:32 +05:30
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|