feat(alerting): add api route for alert changes
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", GetAlertRuleChanges)
|
||||
}
|
||||
|
||||
func GetAlertRuleChanges(query *m.GetAlertChangesQuery) error {
|
||||
alertChanges := make([]m.AlertRuleChange, 0)
|
||||
if err := x.Where("org_id = ?", query.OrgId).Find(&alertChanges); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
query.Result = alertChanges
|
||||
return nil
|
||||
}
|
||||
|
||||
func SaveAlertChange(change string, alert m.AlertRule, sess *xorm.Session) error {
|
||||
_, err := sess.Insert(&m.AlertRuleChange{
|
||||
OrgId: alert.OrgId,
|
||||
Type: change,
|
||||
Created: time.Now(),
|
||||
AlertId: alert.Id,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -46,9 +46,10 @@ func TestAlertRuleChangesDataAccess(t *testing.T) {
|
||||
|
||||
SaveAlerts(&cmd)
|
||||
|
||||
alertChanges, er := GetAlertRuleChanges(FakeOrgId)
|
||||
query := &m.GetAlertChangesQuery{OrgId: FakeOrgId}
|
||||
er := GetAlertRuleChanges(query)
|
||||
So(er, ShouldBeNil)
|
||||
So(len(alertChanges), ShouldEqual, 1)
|
||||
So(len(query.Result), ShouldEqual, 1)
|
||||
|
||||
err = DeleteDashboard(&m.DeleteDashboardCommand{
|
||||
OrgId: FakeOrgId,
|
||||
@@ -66,9 +67,10 @@ func TestAlertRuleChangesDataAccess(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("should add one more alert_rule_change", func() {
|
||||
alertChanges, er := GetAlertRuleChanges(FakeOrgId)
|
||||
query := &m.GetAlertChangesQuery{OrgId: FakeOrgId}
|
||||
er := GetAlertRuleChanges(query)
|
||||
So(er, ShouldBeNil)
|
||||
So(len(alertChanges), ShouldEqual, 2)
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -57,21 +56,6 @@ func DeleteAlertDefinition(dashboardId int64, sess *xorm.Session) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func SaveAlertChange(change string, alert m.AlertRule, sess *xorm.Session) error {
|
||||
_, err := sess.Insert(&m.AlertRuleChange{
|
||||
OrgId: alert.OrgId,
|
||||
Type: change,
|
||||
Created: time.Now(),
|
||||
AlertId: alert.Id,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func alertIsDifferent(rule1, rule2 m.AlertRule) bool {
|
||||
result := false
|
||||
|
||||
@@ -200,14 +184,3 @@ func GetAlertsByDashboardAndPanelId(dashboardId, panelId int64) (m.AlertRule, er
|
||||
|
||||
return alerts[0], nil
|
||||
}
|
||||
|
||||
func GetAlertRuleChanges(orgid int64) ([]m.AlertRuleChange, error) {
|
||||
alertChanges := make([]m.AlertRuleChange, 0)
|
||||
err := x.Where("org_id = ?", orgid).Find(&alertChanges)
|
||||
|
||||
if err != nil {
|
||||
return []m.AlertRuleChange{}, err
|
||||
}
|
||||
|
||||
return alertChanges, nil
|
||||
}
|
||||
|
||||
@@ -43,9 +43,10 @@ func TestAlertingDataAccess(t *testing.T) {
|
||||
Convey("Can create one alert", func() {
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
alertChanges, er := GetAlertRuleChanges(1)
|
||||
query := &m.GetAlertChangesQuery{OrgId: 1}
|
||||
er := GetAlertRuleChanges(query)
|
||||
So(er, ShouldBeNil)
|
||||
So(len(alertChanges), ShouldEqual, 1)
|
||||
So(len(query.Result), ShouldEqual, 1)
|
||||
})
|
||||
|
||||
Convey("Can read properties", func() {
|
||||
@@ -92,9 +93,10 @@ func TestAlertingDataAccess(t *testing.T) {
|
||||
err3 := SaveAlerts(&modifiedCmd)
|
||||
So(err3, ShouldBeNil)
|
||||
|
||||
alertChanges, er := GetAlertRuleChanges(1)
|
||||
query := &m.GetAlertChangesQuery{OrgId: 1}
|
||||
er := GetAlertRuleChanges(query)
|
||||
So(er, ShouldBeNil)
|
||||
So(len(alertChanges), ShouldEqual, 2)
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -129,9 +131,11 @@ func TestAlertingDataAccess(t *testing.T) {
|
||||
alerts, err2 := GetAlertsByDashboardId(testDash.Id)
|
||||
So(err2, ShouldBeNil)
|
||||
So(len(alerts), ShouldEqual, 3)
|
||||
alertChanges, er := GetAlertRuleChanges(1)
|
||||
|
||||
query := &m.GetAlertChangesQuery{OrgId: 1}
|
||||
er := GetAlertRuleChanges(query)
|
||||
So(er, ShouldBeNil)
|
||||
So(len(alertChanges), ShouldEqual, 4)
|
||||
So(len(query.Result), ShouldEqual, 4)
|
||||
})
|
||||
|
||||
Convey("should updated two dashboards and delete one", func() {
|
||||
@@ -147,9 +151,10 @@ func TestAlertingDataAccess(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("should add one more alert_rule_change", func() {
|
||||
alertChanges, er := GetAlertRuleChanges(1)
|
||||
query := &m.GetAlertChangesQuery{OrgId: 1}
|
||||
er := GetAlertRuleChanges(query)
|
||||
So(er, ShouldBeNil)
|
||||
So(len(alertChanges), ShouldEqual, 6)
|
||||
So(len(query.Result), ShouldEqual, 6)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user