From ec7c99dd26566b5b161a1dedab104e23485dff36 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Sun, 13 Aug 2023 21:26:55 +0530 Subject: [PATCH] fix: alerts work when "equals to" or "not equals to" used with "all the times" or "at least once" (#3244) --- pkg/query-service/rules/resultTypes.go | 5 ++-- pkg/query-service/rules/thresholdRule.go | 33 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/pkg/query-service/rules/resultTypes.go b/pkg/query-service/rules/resultTypes.go index e7e67bc7bd..de2095eea9 100644 --- a/pkg/query-service/rules/resultTypes.go +++ b/pkg/query-service/rules/resultTypes.go @@ -34,8 +34,9 @@ func (s Sample) MarshalJSON() ([]byte, error) { } type Point struct { - T int64 - V float64 + T int64 + V float64 + Vs []float64 } func (p Point) String() string { diff --git a/pkg/query-service/rules/thresholdRule.go b/pkg/query-service/rules/thresholdRule.go index f6e79a1643..fbd65d9948 100644 --- a/pkg/query-service/rules/thresholdRule.go +++ b/pkg/query-service/rules/thresholdRule.go @@ -550,7 +550,40 @@ func (r *ThresholdRule) runChQuery(ctx context.Context, db clickhouse.Conn, quer } } + + if s, ok := resultMap[labelHash]; ok { + s.Point.Vs = append(s.Point.Vs, s.Point.V) + } } + + for _, s := range resultMap { + if r.matchType() == AllTheTimes && r.compareOp() == ValueIsEq { + for _, v := range s.Point.Vs { + if v != r.targetVal() { // if any of the values is not equal to target, alert shouldn't be sent + s.Point.V = v + } + } + } else if r.matchType() == AllTheTimes && r.compareOp() == ValueIsNotEq { + for _, v := range s.Point.Vs { + if v == r.targetVal() { // if any of the values is equal to target, alert shouldn't be sent + s.Point.V = v + } + } + } else if r.matchType() == AtleastOnce && r.compareOp() == ValueIsEq { + for _, v := range s.Point.Vs { + if v == r.targetVal() { // if any of the values is equal to target, alert should be sent + s.Point.V = v + } + } + } else if r.matchType() == AtleastOnce && r.compareOp() == ValueIsNotEq { + for _, v := range s.Point.Vs { + if v != r.targetVal() { // if any of the values is not equal to target, alert should be sent + s.Point.V = v + } + } + } + } + zap.S().Debugf("ruleid:", r.ID(), "\t resultmap(potential alerts):", len(resultMap)) for _, sample := range resultMap {