mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(alerting): add api route for alert changes
This commit is contained in:
parent
2e097f4af1
commit
973db1ac38
@ -21,6 +21,19 @@ func ValidateOrgAlert(c *middleware.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// GET /api/alert_rule
|
||||
func GetAlertChanges(c *middleware.Context) Response {
|
||||
query := models.GetAlertChangesQuery{
|
||||
OrgId: c.OrgId,
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return ApiError(500, "List alerts failed", err)
|
||||
}
|
||||
|
||||
return Json(200, query.Result)
|
||||
}
|
||||
|
||||
// GET /api/alert_rule
|
||||
func GetAlerts(c *middleware.Context) Response {
|
||||
query := models.GetAlertsQuery{
|
||||
|
@ -237,6 +237,7 @@ func Register(r *macaron.Macaron) {
|
||||
r.Get("/metrics/test", GetTestMetrics)
|
||||
|
||||
r.Group("/alert_rule", func() {
|
||||
r.Get("/changes", wrap(GetAlertChanges))
|
||||
r.Get("/", wrap(GetAlerts))
|
||||
r.Get("/:id", ValidateOrgAlert, wrap(GetAlert))
|
||||
})
|
||||
|
@ -95,3 +95,9 @@ type GetAlertById struct {
|
||||
|
||||
Result AlertRule
|
||||
}
|
||||
|
||||
type GetAlertChangesQuery struct {
|
||||
OrgId int64
|
||||
|
||||
Result []AlertRuleChange
|
||||
}
|
||||
|
37
pkg/services/sqlstore/alert_rule_changes.go
Normal file
37
pkg/services/sqlstore/alert_rule_changes.go
Normal file
@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user