Skip to content

Commit 62e8a03

Browse files
committed
refactor(alerting): refactoring PR for OR conditions, grafana#6579
1 parent 457ae74 commit 62e8a03

File tree

8 files changed

+32
-30
lines changed

8 files changed

+32
-30
lines changed

pkg/api/alerting.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ func AlertTest(c *middleware.Context, dto dtos.AlertTestCommand) Response {
119119
res := backendCmd.Result
120120

121121
dtoRes := &dtos.AlertTestResult{
122-
Firing: res.Firing,
123-
FiringEval: res.FiringEval,
122+
Firing: res.Firing,
123+
ConditionEvals: res.ConditionEvals,
124124
}
125125

126126
if res.Error != nil {

pkg/api/dtos/alerting.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ type AlertTestCommand struct {
3535
}
3636

3737
type AlertTestResult struct {
38-
Firing bool `json:"firing"`
39-
FiringEval string `json:"firingEvaluation"`
40-
TimeMs string `json:"timeMs"`
41-
Error string `json:"error,omitempty"`
42-
EvalMatches []*EvalMatch `json:"matches,omitempty"`
43-
Logs []*AlertTestResultLog `json:"logs,omitempty"`
38+
Firing bool `json:"firing"`
39+
ConditionEvals string `json:"conditionEvals"`
40+
TimeMs string `json:"timeMs"`
41+
Error string `json:"error,omitempty"`
42+
EvalMatches []*EvalMatch `json:"matches,omitempty"`
43+
Logs []*AlertTestResultLog `json:"logs,omitempty"`
4444
}
4545

4646
type AlertTestResultLog struct {

pkg/services/alerting/conditions/query.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func NewQueryCondition(model *simplejson.Json, index int) (*QueryCondition, erro
173173
condition.Evaluator = evaluator
174174

175175
operatorJson := model.Get("operator")
176-
operator := operatorJson.Get("type").MustString()
176+
operator := operatorJson.Get("type").MustString("and")
177177
condition.Operator = operator
178178

179179
return &condition, nil

pkg/services/alerting/eval_context.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ type EvalContext struct {
1717
EvalMatches []*EvalMatch
1818
Logs []*ResultLogEntry
1919
Error error
20-
Description string
21-
FiringEval string
20+
ConditionEvals string
2221
StartTime time.Time
2322
EndTime time.Time
2423
Rule *Rule

pkg/services/alerting/eval_handler.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package alerting
22

33
import (
44
"strconv"
5+
"strings"
56
"time"
67

78
"github.com/grafana/grafana/pkg/log"
@@ -22,7 +23,8 @@ func NewEvalHandler() *DefaultEvalHandler {
2223

2324
func (e *DefaultEvalHandler) Eval(context *EvalContext) {
2425
firing := true
25-
firingEval := ""
26+
conditionEvals := ""
27+
2628
for i := 0; i < len(context.Rule.Conditions); i++ {
2729
condition := context.Rule.Conditions[i]
2830
cr, err := condition.Eval(context)
@@ -36,24 +38,22 @@ func (e *DefaultEvalHandler) Eval(context *EvalContext) {
3638
}
3739

3840
// calculating Firing based on operator
39-
operator := "AND"
4041
if cr.Operator == "or" {
4142
firing = firing || cr.Firing
42-
operator = "OR"
4343
} else {
4444
firing = firing && cr.Firing
4545
}
4646

4747
if i > 0 {
48-
firingEval = "[" + firingEval + " " + operator + " " + strconv.FormatBool(cr.Firing) + "]"
48+
conditionEvals = "[" + conditionEvals + " " + strings.ToUpper(cr.Operator) + " " + strconv.FormatBool(cr.Firing) + "]"
4949
} else {
50-
firingEval = strconv.FormatBool(firing)
50+
conditionEvals = strconv.FormatBool(firing)
5151
}
5252

5353
context.EvalMatches = append(context.EvalMatches, cr.EvalMatches...)
5454
}
5555

56-
context.FiringEval = firingEval + " = " + strconv.FormatBool(firing)
56+
context.ConditionEvals = conditionEvals + " = " + strconv.FormatBool(firing)
5757
context.Firing = firing
5858
context.EndTime = time.Now()
5959
elapsedTime := context.EndTime.Sub(context.StartTime) / time.Millisecond

pkg/services/alerting/eval_handler_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestAlertingExecutor(t *testing.T) {
3030

3131
handler.Eval(context)
3232
So(context.Firing, ShouldEqual, true)
33-
So(context.FiringEval, ShouldEqual, "true = true")
33+
So(context.ConditionEvals, ShouldEqual, "true = true")
3434
})
3535

3636
Convey("Show return false with not passing asdf", func() {
@@ -43,7 +43,7 @@ func TestAlertingExecutor(t *testing.T) {
4343

4444
handler.Eval(context)
4545
So(context.Firing, ShouldEqual, false)
46-
So(context.FiringEval, ShouldEqual, "[true AND false] = false")
46+
So(context.ConditionEvals, ShouldEqual, "[true AND false] = false")
4747
})
4848

4949
Convey("Show return true if any of the condition is passing with OR operator", func() {
@@ -56,7 +56,7 @@ func TestAlertingExecutor(t *testing.T) {
5656

5757
handler.Eval(context)
5858
So(context.Firing, ShouldEqual, true)
59-
So(context.FiringEval, ShouldEqual, "[true OR false] = true")
59+
So(context.ConditionEvals, ShouldEqual, "[true OR false] = true")
6060
})
6161

6262
Convey("Show return false if any of the condition is failing with AND operator", func() {
@@ -69,7 +69,7 @@ func TestAlertingExecutor(t *testing.T) {
6969

7070
handler.Eval(context)
7171
So(context.Firing, ShouldEqual, false)
72-
So(context.FiringEval, ShouldEqual, "[true AND false] = false")
72+
So(context.ConditionEvals, ShouldEqual, "[true AND false] = false")
7373
})
7474

7575
Convey("Show return true if one condition is failing with nested OR operator", func() {
@@ -83,7 +83,7 @@ func TestAlertingExecutor(t *testing.T) {
8383

8484
handler.Eval(context)
8585
So(context.Firing, ShouldEqual, true)
86-
So(context.FiringEval, ShouldEqual, "[[true AND true] OR false] = true")
86+
So(context.ConditionEvals, ShouldEqual, "[[true AND true] OR false] = true")
8787
})
8888

8989
Convey("Show return false if one condition is passing with nested OR operator", func() {
@@ -97,7 +97,7 @@ func TestAlertingExecutor(t *testing.T) {
9797

9898
handler.Eval(context)
9999
So(context.Firing, ShouldEqual, false)
100-
So(context.FiringEval, ShouldEqual, "[[true AND false] OR false] = false")
100+
So(context.ConditionEvals, ShouldEqual, "[[true AND false] OR false] = false")
101101
})
102102

103103
Convey("Show return false if a condition is failing with nested AND operator", func() {
@@ -111,7 +111,7 @@ func TestAlertingExecutor(t *testing.T) {
111111

112112
handler.Eval(context)
113113
So(context.Firing, ShouldEqual, false)
114-
So(context.FiringEval, ShouldEqual, "[[true AND false] AND true] = false")
114+
So(context.ConditionEvals, ShouldEqual, "[[true AND false] AND true] = false")
115115
})
116116

117117
Convey("Show return true if a condition is passing with nested OR operator", func() {
@@ -125,7 +125,7 @@ func TestAlertingExecutor(t *testing.T) {
125125

126126
handler.Eval(context)
127127
So(context.Firing, ShouldEqual, true)
128-
So(context.FiringEval, ShouldEqual, "[[true OR false] OR true] = true")
128+
So(context.ConditionEvals, ShouldEqual, "[[true OR false] OR true] = true")
129129
})
130130
})
131131
}

public/app/features/alerting/partials/alert_tab.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,23 @@ <h5 class="section-heading">Alert Config</h5>
3838
<h5 class="section-heading">Conditions</h5>
3939
<div class="gf-form-inline" ng-repeat="conditionModel in ctrl.conditionModels">
4040
<div class="gf-form">
41-
<metric-segment-model css-class="query-keyword" ng-if="$index" property="conditionModel.operator.type" options="ctrl.evalOperators" custom="false"></metric-segment-model>
41+
<metric-segment-model css-class="query-keyword width-5" ng-if="$index" property="conditionModel.operator.type" options="ctrl.evalOperators" custom="false"></metric-segment-model>
4242
<span class="gf-form-label query-keyword width-5" ng-if="$index===0">WHEN</span>
4343
</div>
4444
<div class="gf-form">
45-
<query-part-editor class="gf-form-label query-part" part="conditionModel.reducerPart" handle-event="ctrl.handleReducerPartEvent(conditionModel, $event)">
45+
<query-part-editor class="gf-form-label query-part width-5" part="conditionModel.reducerPart" handle-event="ctrl.handleReducerPartEvent(conditionModel, $event)">
4646
</query-part-editor>
4747
<span class="gf-form-label query-keyword">OF</span>
4848
</div>
4949
<div class="gf-form">
50-
<query-part-editor class="gf-form-label query-part" part="conditionModel.queryPart" handle-event="ctrl.handleQueryPartEvent(conditionModel, $event)">
50+
<query-part-editor class="gf-form-label query-part width-10" part="conditionModel.queryPart" handle-event="ctrl.handleQueryPartEvent(conditionModel, $event)">
5151
</query-part-editor>
5252
</div>
5353
<div class="gf-form">
5454
<metric-segment-model property="conditionModel.evaluator.type" options="ctrl.evalFunctions" custom="false" css-class="query-keyword" on-change="ctrl.evaluatorTypeChanged(conditionModel.evaluator)"></metric-segment-model>
55-
<input class="gf-form-input max-width-7" type="number" step="any" ng-hide="conditionModel.evaluator.params.length === 0" ng-model="conditionModel.evaluator.params[0]" ng-change="ctrl.evaluatorParamsChanged()"></input>
55+
<input class="gf-form-input max-width-9" type="number" step="any" ng-hide="conditionModel.evaluator.params.length === 0" ng-model="conditionModel.evaluator.params[0]" ng-change="ctrl.evaluatorParamsChanged()"></input>
5656
<label class="gf-form-label query-keyword" ng-show="conditionModel.evaluator.params.length === 2">TO</label>
57-
<input class="gf-form-input max-width-7" type="number" step="any" ng-if="conditionModel.evaluator.params.length === 2" ng-model="conditionModel.evaluator.params[1]" ng-change="ctrl.evaluatorParamsChanged()"></input>
57+
<input class="gf-form-input max-width-9" type="number" step="any" ng-if="conditionModel.evaluator.params.length === 2" ng-model="conditionModel.evaluator.params[1]" ng-change="ctrl.evaluatorParamsChanged()"></input>
5858
</div>
5959
<div class="gf-form">
6060
<label class="gf-form-label">

public/app/features/dashboard/row/row_model.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export class DashboardRow {
3434

3535
getSaveModel() {
3636
assignModelProperties(this.model, this, this.defaults);
37+
38+
// remove properties that dont server persisted purpose
39+
delete this.model.isNew;
3740
return this.model;
3841
}
3942

0 commit comments

Comments
 (0)