Alerting: added possibility to preview grafana managed alert rules. (#34600)

* starting to add eval logic.

* wip

* first version of test rule.

* reverted file.

* add info colum to result to show error or (with CC evalmatches)

* fix labels in evalmatch

* fix be test

* refactored using observables.

* moved widht/height div to outside panel rendere.

* adding docs api level.

* adding container styles to error div.

* increasing size of preview.

Co-authored-by: kyle <kyle@grafana.com>
This commit is contained in:
Marcus Andersson
2021-05-26 10:06:28 +02:00
committed by GitHub
co-authored by kyle
parent 42f33630c7
commit e19b3df1a9
13 changed files with 362 additions and 12 deletions
+3 -2
View File
@@ -5,6 +5,7 @@ import (
"encoding/json"
"testing"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana/pkg/expr/mathexp"
"github.com/stretchr/testify/require"
ptr "github.com/xorcare/pointer"
@@ -202,7 +203,7 @@ func TestConditionsCmdExecute(t *testing.T) {
vars: mathexp.Vars{
"A": mathexp.Results{
Values: []mathexp.Value{
valBasedSeries(ptr.Float64(30), ptr.Float64(40)),
valBasedSeriesWithLabels(data.Labels{"h": "1"}, ptr.Float64(30), ptr.Float64(40)),
valBasedSeries(ptr.Float64(0), ptr.Float64(10)),
},
},
@@ -218,7 +219,7 @@ func TestConditionsCmdExecute(t *testing.T) {
}},
resultNumber: func() mathexp.Number {
v := valBasedNumber(ptr.Float64(1))
v.SetMeta([]EvalMatch{{Value: ptr.Float64(35)}})
v.SetMeta([]EvalMatch{{Value: ptr.Float64(35), Labels: data.Labels{"h": "1"}}})
return v
},
},
+5
View File
@@ -24,6 +24,11 @@ func (cr classicReducer) ValidReduceFunc() bool {
//nolint: gocyclo
func (cr classicReducer) Reduce(series mathexp.Series) mathexp.Number {
num := mathexp.NewNumber("", nil)
if series.GetLabels() != nil {
num.SetLabels(series.GetLabels().Copy())
}
num.SetValue(nil)
if series.Len() == 0 {
+12
View File
@@ -5,6 +5,7 @@ import (
"testing"
"time"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana/pkg/expr/mathexp"
"github.com/stretchr/testify/require"
ptr "github.com/xorcare/pointer"
@@ -408,6 +409,17 @@ func valBasedSeries(vals ...*float64) mathexp.Series {
return newSeries
}
func valBasedSeriesWithLabels(l data.Labels, vals ...*float64) mathexp.Series {
newSeries := mathexp.NewSeries("", l, 0, false, 1, true, len(vals))
for idx, f := range vals {
err := newSeries.SetPoint(idx, unixTimePointer(int64(idx)), f)
if err != nil {
panic(err)
}
}
return newSeries
}
func unixTimePointer(sec int64) *time.Time {
t := time.Unix(sec, 0)
return &t
+9
View File
@@ -331,12 +331,21 @@ func (evalResults Results) AsDataFrame() data.Frame {
frame.Fields = append(frame.Fields, data.NewField(lKey, nil, make([]string, fieldLen)))
}
frame.Fields = append(frame.Fields, data.NewField("State", nil, make([]string, fieldLen)))
frame.Fields = append(frame.Fields, data.NewField("Info", nil, make([]string, fieldLen)))
for evalIdx, evalResult := range evalResults {
for lIdx, v := range labelColumns {
frame.Set(lIdx, evalIdx, evalResult.Instance[v])
}
frame.Set(len(labelColumns), evalIdx, evalResult.State.String())
switch {
case evalResult.Error != nil:
frame.Set(len(labelColumns)+1, evalIdx, evalResult.Error.Error())
case evalResult.EvaluationString != "":
frame.Set(len(labelColumns)+1, evalIdx, evalResult.EvaluationString)
}
}
return *frame
}
@@ -1597,6 +1597,13 @@ func TestEval(t *testing.T) {
"typeInfo": {
"frame": "string"
}
},
{
"name": "Info",
"type": "string",
"typeInfo": {
"frame": "string"
}
}
]
},
@@ -1604,6 +1611,9 @@ func TestEval(t *testing.T) {
"values": [
[
"Alerting"
],
[
""
]
]
}
@@ -1648,6 +1658,13 @@ func TestEval(t *testing.T) {
"typeInfo": {
"frame": "string"
}
},
{
"name": "Info",
"type": "string",
"typeInfo": {
"frame": "string"
}
}
]
},
@@ -1655,6 +1672,9 @@ func TestEval(t *testing.T) {
"values": [
[
"Normal"
],
[
""
]
]
}