mirror of https://github.com/grafana/grafana
Alerting: Copy rule definitions into state history (#62032)
* Copy rules instead of accepting pointer * Deep-copy the rule, for even more guarantees * Create struct just for needed fields * Move RuleMeta to historian/model package, iron out package dependencies * Move tests for dash ID parsing to model package along with codepull/59621/head
parent
1f55a54543
commit
046a9bb7c1
@ -0,0 +1,46 @@ |
||||
package model |
||||
|
||||
import ( |
||||
"strconv" |
||||
|
||||
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger" |
||||
"github.com/grafana/grafana/pkg/infra/log" |
||||
"github.com/grafana/grafana/pkg/services/ngalert/models" |
||||
) |
||||
|
||||
// RuleMeta is the metadata about a rule that is needed by state history.
|
||||
type RuleMeta struct { |
||||
ID int64 |
||||
OrgID int64 |
||||
UID string |
||||
Title string |
||||
Group string |
||||
NamespaceUID string |
||||
DashboardUID string |
||||
PanelID int64 |
||||
} |
||||
|
||||
func NewRuleMeta(r *models.AlertRule, log log.Logger) RuleMeta { |
||||
dashUID, ok := r.Annotations[models.DashboardUIDAnnotation] |
||||
var panelID int64 |
||||
if ok { |
||||
panelAnno := r.Annotations[models.PanelIDAnnotation] |
||||
pid, err := strconv.ParseInt(panelAnno, 10, 64) |
||||
if err != nil { |
||||
logger.Error("Error parsing panelUID for alert annotation", "ruleID", r.ID, "dash", dashUID, "actual", panelAnno, "error", err) |
||||
pid = 0 |
||||
dashUID = "" |
||||
} |
||||
panelID = pid |
||||
} |
||||
return RuleMeta{ |
||||
ID: r.ID, |
||||
OrgID: r.OrgID, |
||||
UID: r.UID, |
||||
Title: r.Title, |
||||
Group: r.RuleGroup, |
||||
NamespaceUID: r.NamespaceUID, |
||||
DashboardUID: dashUID, |
||||
PanelID: panelID, |
||||
} |
||||
} |
@ -0,0 +1,77 @@ |
||||
package model |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log" |
||||
"github.com/grafana/grafana/pkg/services/ngalert/models" |
||||
"github.com/stretchr/testify/require" |
||||
) |
||||
|
||||
func TestNewRuleMeta(t *testing.T) { |
||||
logger := log.NewNopLogger() |
||||
|
||||
type testCase struct { |
||||
name string |
||||
in models.AlertRule |
||||
expDash string |
||||
expPanel int64 |
||||
} |
||||
|
||||
cases := []testCase{ |
||||
{ |
||||
name: "no dash UID", |
||||
in: models.AlertRule{ |
||||
OrgID: 1, |
||||
Annotations: map[string]string{ |
||||
models.PanelIDAnnotation: "123", |
||||
}, |
||||
}, |
||||
expDash: "", |
||||
expPanel: 0, |
||||
}, |
||||
{ |
||||
name: "no panel ID", |
||||
in: models.AlertRule{ |
||||
OrgID: 1, |
||||
Annotations: map[string]string{ |
||||
models.DashboardUIDAnnotation: "abcd-uid", |
||||
}, |
||||
}, |
||||
expDash: "", |
||||
expPanel: 0, |
||||
}, |
||||
{ |
||||
name: "invalid panel ID", |
||||
in: models.AlertRule{ |
||||
OrgID: 1, |
||||
Annotations: map[string]string{ |
||||
models.DashboardUIDAnnotation: "abcd-uid", |
||||
models.PanelIDAnnotation: "bad-id", |
||||
}, |
||||
}, |
||||
expDash: "", |
||||
expPanel: 0, |
||||
}, |
||||
{ |
||||
name: "success", |
||||
in: models.AlertRule{ |
||||
OrgID: 1, |
||||
Annotations: map[string]string{ |
||||
models.DashboardUIDAnnotation: "abcd-uid", |
||||
models.PanelIDAnnotation: "123", |
||||
}, |
||||
}, |
||||
expDash: "abcd-uid", |
||||
expPanel: 123, |
||||
}, |
||||
} |
||||
|
||||
for _, tc := range cases { |
||||
t.Run(tc.name, func(t *testing.T) { |
||||
res := NewRuleMeta(&tc.in, logger) |
||||
require.Equal(t, tc.expDash, res.DashboardUID) |
||||
require.Equal(t, tc.expPanel, res.PanelID) |
||||
}) |
||||
} |
||||
} |
Loading…
Reference in new issue