mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(alerting): begin alert rule storage refactoring
This commit is contained in:
@@ -186,7 +186,7 @@ func upsertAlerts(alerts []*m.AlertRule, posted []*m.AlertRule, sess *xorm.Sessi
|
||||
}
|
||||
|
||||
if update {
|
||||
if alertToUpdate.Equals(alert) {
|
||||
if alertToUpdate.ContainsUpdates(alert) {
|
||||
alert.Updated = time.Now()
|
||||
alert.State = alertToUpdate.State
|
||||
_, err := sess.Id(alert.Id).Update(alert)
|
||||
|
||||
@@ -22,20 +22,11 @@ func TestAlertRuleChangesDataAccess(t *testing.T) {
|
||||
Convey("When dashboard is removed", func() {
|
||||
items := []*m.AlertRule{
|
||||
{
|
||||
PanelId: 1,
|
||||
DashboardId: testDash.Id,
|
||||
Query: "Query",
|
||||
QueryRefId: "A",
|
||||
WarnLevel: 30,
|
||||
CritLevel: 50,
|
||||
WarnOperator: ">",
|
||||
CritOperator: ">",
|
||||
Frequency: 10,
|
||||
Name: "Alerting title",
|
||||
Description: "Alerting description",
|
||||
QueryRange: 3600,
|
||||
Aggregator: "avg",
|
||||
OrgId: FakeOrgId,
|
||||
PanelId: 1,
|
||||
DashboardId: testDash.Id,
|
||||
Name: "Alerting title",
|
||||
Description: "Alerting description",
|
||||
OrgId: FakeOrgId,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
)
|
||||
|
||||
func TestAlertingDataAccess(t *testing.T) {
|
||||
|
||||
Convey("Testing Alerting data access", t, func() {
|
||||
InitTestDB(t)
|
||||
|
||||
@@ -16,21 +15,11 @@ func TestAlertingDataAccess(t *testing.T) {
|
||||
|
||||
items := []*m.AlertRule{
|
||||
{
|
||||
PanelId: 1,
|
||||
DashboardId: testDash.Id,
|
||||
OrgId: testDash.OrgId,
|
||||
Query: "Query",
|
||||
QueryRefId: "A",
|
||||
WarnLevel: 30,
|
||||
CritLevel: 50,
|
||||
WarnOperator: ">",
|
||||
CritOperator: ">",
|
||||
Frequency: 10,
|
||||
Name: "Alerting title",
|
||||
Description: "Alerting description",
|
||||
QueryRange: 3600,
|
||||
Aggregator: "avg",
|
||||
DatasourceId: 42,
|
||||
PanelId: 1,
|
||||
DashboardId: testDash.Id,
|
||||
OrgId: testDash.OrgId,
|
||||
Name: "Alerting title",
|
||||
Description: "Alerting description",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -58,25 +47,15 @@ func TestAlertingDataAccess(t *testing.T) {
|
||||
|
||||
alert := alertQuery.Result[0]
|
||||
So(err2, ShouldBeNil)
|
||||
So(alert.Frequency, ShouldEqual, 10)
|
||||
So(alert.WarnLevel, ShouldEqual, 30)
|
||||
So(alert.CritLevel, ShouldEqual, 50)
|
||||
So(alert.WarnOperator, ShouldEqual, ">")
|
||||
So(alert.CritOperator, ShouldEqual, ">")
|
||||
So(alert.Query, ShouldEqual, "Query")
|
||||
So(alert.QueryRefId, ShouldEqual, "A")
|
||||
So(alert.Name, ShouldEqual, "Alerting title")
|
||||
So(alert.Description, ShouldEqual, "Alerting description")
|
||||
So(alert.QueryRange, ShouldEqual, 3600)
|
||||
So(alert.Aggregator, ShouldEqual, "avg")
|
||||
So(alert.State, ShouldEqual, "OK")
|
||||
So(alert.DatasourceId, ShouldEqual, 42)
|
||||
})
|
||||
|
||||
Convey("Alerts with same dashboard id and panel id should update", func() {
|
||||
modifiedItems := items
|
||||
modifiedItems[0].Query = "Updated Query"
|
||||
modifiedItems[0].State = "ALERT"
|
||||
modifiedItems[0].Name = "New name"
|
||||
//modifiedItems[0].State = "ALERT"
|
||||
|
||||
modifiedCmd := m.SaveAlertsCommand{
|
||||
DashboardId: testDash.Id,
|
||||
@@ -97,7 +76,7 @@ func TestAlertingDataAccess(t *testing.T) {
|
||||
|
||||
So(err2, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 1)
|
||||
So(query.Result[0].Query, ShouldEqual, "Updated Query")
|
||||
So(query.Result[0].Name, ShouldEqual, "Name")
|
||||
|
||||
Convey("Alert state should not be updated", func() {
|
||||
So(query.Result[0].State, ShouldEqual, "OK")
|
||||
@@ -120,19 +99,19 @@ func TestAlertingDataAccess(t *testing.T) {
|
||||
{
|
||||
DashboardId: testDash.Id,
|
||||
PanelId: 1,
|
||||
Query: "1",
|
||||
Name: "1",
|
||||
OrgId: 1,
|
||||
},
|
||||
{
|
||||
DashboardId: testDash.Id,
|
||||
PanelId: 2,
|
||||
Query: "2",
|
||||
Name: "2",
|
||||
OrgId: 1,
|
||||
},
|
||||
{
|
||||
DashboardId: testDash.Id,
|
||||
PanelId: 3,
|
||||
Query: "3",
|
||||
Name: "3",
|
||||
OrgId: 1,
|
||||
},
|
||||
}
|
||||
@@ -180,19 +159,10 @@ func TestAlertingDataAccess(t *testing.T) {
|
||||
Convey("When dashboard is removed", func() {
|
||||
items := []*m.AlertRule{
|
||||
{
|
||||
PanelId: 1,
|
||||
DashboardId: testDash.Id,
|
||||
Query: "Query",
|
||||
QueryRefId: "A",
|
||||
WarnLevel: 30,
|
||||
CritLevel: 50,
|
||||
WarnOperator: ">",
|
||||
CritOperator: ">",
|
||||
Frequency: 10,
|
||||
Name: "Alerting title",
|
||||
Description: "Alerting description",
|
||||
QueryRange: 3600,
|
||||
Aggregator: "avg",
|
||||
PanelId: 1,
|
||||
DashboardId: testDash.Id,
|
||||
Name: "Alerting title",
|
||||
Description: "Alerting description",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -15,20 +15,11 @@ func TestAlertingStateAccess(t *testing.T) {
|
||||
|
||||
items := []*m.AlertRule{
|
||||
{
|
||||
PanelId: 1,
|
||||
DashboardId: testDash.Id,
|
||||
OrgId: testDash.OrgId,
|
||||
Query: "Query",
|
||||
QueryRefId: "A",
|
||||
WarnLevel: 30,
|
||||
CritLevel: 50,
|
||||
WarnOperator: ">",
|
||||
CritOperator: ">",
|
||||
Frequency: 10,
|
||||
Name: "Alerting title",
|
||||
Description: "Alerting description",
|
||||
QueryRange: 3600,
|
||||
Aggregator: "avg",
|
||||
PanelId: 1,
|
||||
DashboardId: testDash.Id,
|
||||
OrgId: testDash.OrgId,
|
||||
Name: "Alerting title",
|
||||
Description: "Alerting description",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -101,17 +101,45 @@ func TestAlertModel(t *testing.T) {
|
||||
"timeShift": null,
|
||||
"aliasColors": {},
|
||||
"seriesOverrides": [],
|
||||
|
||||
|
||||
"alerting": {
|
||||
"queryRef": "A",
|
||||
"warnLevel": 30,
|
||||
"critLevel": 50,
|
||||
"warnOperator": ">",
|
||||
"critOperator": ">",
|
||||
"aggregator": "sum",
|
||||
"queryRange": 3600,
|
||||
"frequency": 10,
|
||||
"name": "active desktop users",
|
||||
"description": "restart webservers"
|
||||
"warning": {
|
||||
"op": ">",
|
||||
"level": 10
|
||||
},
|
||||
"critical": {
|
||||
"op": ">",
|
||||
"level": 20
|
||||
},
|
||||
"function": "static",
|
||||
"valueQuery": {
|
||||
"queryRefId": "A",
|
||||
"from": "5m",
|
||||
"to": "now",
|
||||
"agg": "avg",
|
||||
"params": [
|
||||
"#A",
|
||||
"5m",
|
||||
"now",
|
||||
"avg"
|
||||
]
|
||||
},
|
||||
"evalQuery": {
|
||||
"queryRefId": "A",
|
||||
"from": "5m",
|
||||
"to": "now",
|
||||
"agg": "avg",
|
||||
"params": [
|
||||
"#A",
|
||||
"5m",
|
||||
"now",
|
||||
"avg"
|
||||
]
|
||||
},
|
||||
"evalStringParam1": "",
|
||||
"name": "Alerting Panel Title alert"
|
||||
},
|
||||
"links": []
|
||||
},
|
||||
@@ -189,16 +217,42 @@ func TestAlertModel(t *testing.T) {
|
||||
},
|
||||
"seriesOverrides": [],
|
||||
"alerting": {
|
||||
"queryRef": "A",
|
||||
"warnOperator": ">",
|
||||
"critOperator": ">",
|
||||
"warnLevel": 300,
|
||||
"critLevel": 500,
|
||||
"aggregator": "avg",
|
||||
"queryRange": 3600,
|
||||
"frequency": 10,
|
||||
"name": "active mobile users",
|
||||
"description": "restart itunes"
|
||||
"warning": {
|
||||
"op": ">",
|
||||
"level": 10
|
||||
},
|
||||
"critical": {
|
||||
"op": ">",
|
||||
"level": 20
|
||||
},
|
||||
"function": "static",
|
||||
"valueQuery": {
|
||||
"queryRefId": "A",
|
||||
"from": "5m",
|
||||
"to": "now",
|
||||
"agg": "avg",
|
||||
"params": [
|
||||
"#A",
|
||||
"5m",
|
||||
"now",
|
||||
"avg"
|
||||
]
|
||||
},
|
||||
"evalQuery": {
|
||||
"queryRefId": "A",
|
||||
"from": "5m",
|
||||
"to": "now",
|
||||
"agg": "avg",
|
||||
"params": [
|
||||
"#A",
|
||||
"5m",
|
||||
"now",
|
||||
"avg"
|
||||
]
|
||||
},
|
||||
"evalStringParam1": "",
|
||||
"name": "Alerting Panel Title alert"
|
||||
},
|
||||
"links": []
|
||||
}
|
||||
@@ -379,37 +433,13 @@ func TestAlertModel(t *testing.T) {
|
||||
So(v.DashboardId, ShouldEqual, 1)
|
||||
So(v.PanelId, ShouldNotEqual, 0)
|
||||
|
||||
So(v.WarnLevel, ShouldNotBeEmpty)
|
||||
So(v.CritLevel, ShouldNotBeEmpty)
|
||||
|
||||
So(v.Aggregator, ShouldNotBeEmpty)
|
||||
So(v.Query, ShouldNotBeEmpty)
|
||||
So(v.QueryRefId, ShouldNotBeEmpty)
|
||||
So(v.QueryRange, ShouldNotEqual, 0)
|
||||
So(v.Frequency, ShouldNotEqual, 0)
|
||||
So(v.Name, ShouldNotBeEmpty)
|
||||
So(v.Description, ShouldNotBeEmpty)
|
||||
|
||||
expr := simplejson.NewFromAny(v.Expression)
|
||||
So(expr.Get("valueQuery").Get("query").MustString(), ShouldNotEqual, "")
|
||||
So(expr.Get("valueQuery").Get("datsourceId").MustInt64(), ShouldNotEqual, 0)
|
||||
}
|
||||
|
||||
So(alerts[0].WarnLevel, ShouldEqual, 30)
|
||||
So(alerts[1].WarnLevel, ShouldEqual, 300)
|
||||
|
||||
So(alerts[0].Frequency, ShouldEqual, 10)
|
||||
So(alerts[1].Frequency, ShouldEqual, 10)
|
||||
|
||||
So(alerts[0].CritLevel, ShouldEqual, 50)
|
||||
So(alerts[1].CritLevel, ShouldEqual, 500)
|
||||
|
||||
So(alerts[0].CritOperator, ShouldEqual, ">")
|
||||
So(alerts[1].CritOperator, ShouldEqual, ">")
|
||||
So(alerts[0].WarnOperator, ShouldEqual, ">")
|
||||
So(alerts[1].WarnOperator, ShouldEqual, ">")
|
||||
|
||||
So(alerts[0].Query, ShouldEqual, `{"refId":"A","target":"aliasByNode(statsd.fakesite.counters.session_start.desktop.count, 4)"}`)
|
||||
So(alerts[1].Query, ShouldEqual, `{"refId":"A","target":"aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"}`)
|
||||
|
||||
So(alerts[0].DatasourceId, ShouldEqual, 2)
|
||||
So(alerts[1].DatasourceId, ShouldEqual, 1)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -11,21 +11,12 @@ func addAlertMigrations(mg *Migrator) {
|
||||
Columns: []*Column{
|
||||
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
||||
{Name: "dashboard_id", Type: DB_BigInt, Nullable: false},
|
||||
{Name: "datasource_id", Type: DB_BigInt, Nullable: false},
|
||||
{Name: "panel_id", Type: DB_BigInt, Nullable: false},
|
||||
{Name: "org_id", Type: DB_BigInt, Nullable: false},
|
||||
{Name: "query", Type: DB_Text, Nullable: false},
|
||||
{Name: "query_ref_id", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "warn_level", Type: DB_Float, Nullable: false},
|
||||
{Name: "warn_operator", Type: DB_NVarchar, Length: 10, Nullable: false},
|
||||
{Name: "crit_level", Type: DB_Float, Nullable: false},
|
||||
{Name: "crit_operator", Type: DB_NVarchar, Length: 10, Nullable: false},
|
||||
{Name: "frequency", Type: DB_BigInt, Nullable: false},
|
||||
{Name: "name", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "description", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "query_range", Type: DB_Int, Nullable: false},
|
||||
{Name: "aggregator", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "state", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "expression", Type: DB_Text, Nullable: false},
|
||||
{Name: "created", Type: DB_DateTime, Nullable: false},
|
||||
{Name: "updated", Type: DB_DateTime, Nullable: false},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user