mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(alerting): adds api endpoints for alerts per dashboard and panel
This commit is contained in:
@@ -136,3 +136,34 @@ func PutAlertState(c *middleware.Context, cmd models.UpdateAlertStateCommand) Re
|
||||
|
||||
return Json(200, cmd.Result)
|
||||
}
|
||||
|
||||
// GET /api/alerts-dashboard/:dashboardId
|
||||
func GetAlertsForDashboard(c *middleware.Context) Response {
|
||||
dashboardId := c.ParamsInt64(":dashboardId")
|
||||
query := &models.GetAlertsForDashboardQuery{
|
||||
DashboardId: dashboardId,
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return ApiError(500, "Failed get alert ", err)
|
||||
}
|
||||
|
||||
return Json(200, query.Result)
|
||||
}
|
||||
|
||||
// GET /api/alerts-dashboard/:dashboardId/:panelId
|
||||
func GetAlertsForPanel(c *middleware.Context) Response {
|
||||
dashboardId := c.ParamsInt64(":dashboardId")
|
||||
panelId := c.ParamsInt64(":panelId")
|
||||
|
||||
query := &models.GetAlertForPanelQuery{
|
||||
DashboardId: dashboardId,
|
||||
PanelId: panelId,
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return ApiError(500, "Failed get alert ", err)
|
||||
}
|
||||
|
||||
return Json(200, query.Result)
|
||||
}
|
||||
|
||||
@@ -246,6 +246,9 @@ func Register(r *macaron.Macaron) {
|
||||
r.Get("/:id", ValidateOrgAlert, wrap(GetAlert))
|
||||
})
|
||||
|
||||
r.Get("/alerts-dashboard/:dashboardId", wrap(GetAlertsForDashboard))
|
||||
r.Get("/alerts-dashboard/:dashboardId/:panelId", wrap(GetAlertsForPanel))
|
||||
|
||||
}, reqSignedIn)
|
||||
|
||||
// admin api
|
||||
|
||||
@@ -102,3 +102,16 @@ type GetAlertChangesQuery struct {
|
||||
|
||||
Result []AlertRuleChange
|
||||
}
|
||||
|
||||
type GetAlertsForDashboardQuery struct {
|
||||
DashboardId int64
|
||||
|
||||
Result []AlertRule
|
||||
}
|
||||
|
||||
type GetAlertForPanelQuery struct {
|
||||
DashboardId int64
|
||||
PanelId int64
|
||||
|
||||
Result AlertRule
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@ func init() {
|
||||
bus.AddHandler("sql", SaveAlerts)
|
||||
bus.AddHandler("sql", GetAllAlertsForOrg)
|
||||
bus.AddHandler("sql", GetAlertById)
|
||||
bus.AddHandler("sql", GetAlertsByDashboardId)
|
||||
bus.AddHandler("sql", GetAlertsByDashboardAndPanelId)
|
||||
}
|
||||
|
||||
func GetAlertById(query *m.GetAlertByIdQuery) error {
|
||||
@@ -160,28 +162,29 @@ func GetAlertsByDashboardId2(dashboardId int64, sess *xorm.Session) ([]m.AlertRu
|
||||
return alerts, nil
|
||||
}
|
||||
|
||||
func GetAlertsByDashboardId(dashboardId int64) ([]m.AlertRule, error) {
|
||||
func GetAlertsByDashboardId(cmd *m.GetAlertsForDashboardQuery) error {
|
||||
alerts := make([]m.AlertRule, 0)
|
||||
err := x.Where("dashboard_id = ?", dashboardId).Find(&alerts)
|
||||
err := x.Where("dashboard_id = ?", cmd.DashboardId).Find(&alerts)
|
||||
|
||||
if err != nil {
|
||||
return []m.AlertRule{}, err
|
||||
return err
|
||||
}
|
||||
|
||||
return alerts, nil
|
||||
cmd.Result = alerts
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetAlertsByDashboardAndPanelId(dashboardId, panelId int64) (m.AlertRule, error) {
|
||||
func GetAlertsByDashboardAndPanelId(cmd *m.GetAlertForPanelQuery) error {
|
||||
alerts := make([]m.AlertRule, 0)
|
||||
err := x.Where("dashboard_id = ? and panel_id = ?", dashboardId, panelId).Find(&alerts)
|
||||
err := x.Where("dashboard_id = ? and panel_id = ?", cmd.DashboardId, cmd.PanelId).Find(&alerts)
|
||||
|
||||
if err != nil {
|
||||
return m.AlertRule{}, err
|
||||
return err
|
||||
}
|
||||
|
||||
if len(alerts) != 1 {
|
||||
return m.AlertRule{}, err
|
||||
return err
|
||||
}
|
||||
|
||||
return alerts[0], nil
|
||||
cmd.Result = alerts[0]
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -59,11 +59,12 @@ func TestAlertRuleChangesDataAccess(t *testing.T) {
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("Alerts should be removed", func() {
|
||||
alerts, err2 := GetAlertsByDashboardId(testDash.Id)
|
||||
query := m.GetAlertsForDashboardQuery{DashboardId: testDash.Id}
|
||||
err2 := GetAlertsByDashboardId(&query)
|
||||
|
||||
So(testDash.Id, ShouldEqual, 1)
|
||||
So(err2, ShouldBeNil)
|
||||
So(len(alerts), ShouldEqual, 0)
|
||||
So(len(query.Result), ShouldEqual, 0)
|
||||
})
|
||||
|
||||
Convey("should add one more alert_rule_change", func() {
|
||||
|
||||
@@ -51,19 +51,23 @@ func TestAlertingDataAccess(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("Can read properties", func() {
|
||||
alert, err2 := GetAlertsByDashboardAndPanelId(testDash.Id, 1)
|
||||
query := m.GetAlertForPanelQuery{
|
||||
DashboardId: testDash.Id,
|
||||
PanelId: 1,
|
||||
}
|
||||
err2 := GetAlertsByDashboardAndPanelId(&query)
|
||||
|
||||
So(err2, ShouldBeNil)
|
||||
So(alert.Interval, ShouldEqual, "10")
|
||||
So(alert.WarnLevel, ShouldEqual, "> 30")
|
||||
So(alert.CritLevel, ShouldEqual, "> 50")
|
||||
So(alert.Query, ShouldEqual, "Query")
|
||||
So(alert.QueryRefId, ShouldEqual, "A")
|
||||
So(alert.Title, ShouldEqual, "Alerting title")
|
||||
So(alert.Description, ShouldEqual, "Alerting description")
|
||||
So(alert.QueryRange, ShouldEqual, "5m")
|
||||
So(alert.Aggregator, ShouldEqual, "avg")
|
||||
So(alert.State, ShouldEqual, "OK")
|
||||
So(query.Result.Interval, ShouldEqual, "10")
|
||||
So(query.Result.WarnLevel, ShouldEqual, "> 30")
|
||||
So(query.Result.CritLevel, ShouldEqual, "> 50")
|
||||
So(query.Result.Query, ShouldEqual, "Query")
|
||||
So(query.Result.QueryRefId, ShouldEqual, "A")
|
||||
So(query.Result.Title, ShouldEqual, "Alerting title")
|
||||
So(query.Result.Description, ShouldEqual, "Alerting description")
|
||||
So(query.Result.QueryRange, ShouldEqual, "5m")
|
||||
So(query.Result.Aggregator, ShouldEqual, "avg")
|
||||
So(query.Result.State, ShouldEqual, "OK")
|
||||
})
|
||||
|
||||
Convey("Alerts with same dashboard id and panel id should update", func() {
|
||||
@@ -85,14 +89,15 @@ func TestAlertingDataAccess(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("Alerts should be updated", func() {
|
||||
alerts, err2 := GetAlertsByDashboardId(testDash.Id)
|
||||
query := m.GetAlertsForDashboardQuery{DashboardId: testDash.Id}
|
||||
err2 := GetAlertsByDashboardId(&query)
|
||||
|
||||
So(err2, ShouldBeNil)
|
||||
So(len(alerts), ShouldEqual, 1)
|
||||
So(alerts[0].Query, ShouldEqual, "Updated Query")
|
||||
So(len(query.Result), ShouldEqual, 1)
|
||||
So(query.Result[0].Query, ShouldEqual, "Updated Query")
|
||||
|
||||
Convey("Alert state should not be updated", func() {
|
||||
So(alerts[0].State, ShouldEqual, "OK")
|
||||
So(query.Result[0].State, ShouldEqual, "OK")
|
||||
})
|
||||
})
|
||||
|
||||
@@ -135,9 +140,11 @@ func TestAlertingDataAccess(t *testing.T) {
|
||||
Convey("Should save 3 dashboards", func() {
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
alerts, err2 := GetAlertsByDashboardId(testDash.Id)
|
||||
queryForDashboard := m.GetAlertsForDashboardQuery{DashboardId: testDash.Id}
|
||||
err2 := GetAlertsByDashboardId(&queryForDashboard)
|
||||
|
||||
So(err2, ShouldBeNil)
|
||||
So(len(alerts), ShouldEqual, 3)
|
||||
So(len(queryForDashboard.Result), ShouldEqual, 3)
|
||||
|
||||
query := &m.GetAlertChangesQuery{OrgId: 1}
|
||||
er := GetAlertRuleChanges(query)
|
||||
@@ -152,9 +159,10 @@ func TestAlertingDataAccess(t *testing.T) {
|
||||
err = SaveAlerts(&cmd)
|
||||
|
||||
Convey("should delete the missing alert", func() {
|
||||
alerts, err2 := GetAlertsByDashboardId(testDash.Id)
|
||||
query := m.GetAlertsForDashboardQuery{DashboardId: testDash.Id}
|
||||
err2 := GetAlertsByDashboardId(&query)
|
||||
So(err2, ShouldBeNil)
|
||||
So(len(alerts), ShouldEqual, 2)
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
})
|
||||
|
||||
Convey("should add one more alert_rule_change", func() {
|
||||
@@ -200,11 +208,12 @@ func TestAlertingDataAccess(t *testing.T) {
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("Alerts should be removed", func() {
|
||||
alerts, err2 := GetAlertsByDashboardId(testDash.Id)
|
||||
query := m.GetAlertsForDashboardQuery{DashboardId: testDash.Id}
|
||||
err2 := GetAlertsByDashboardId(&query)
|
||||
|
||||
So(testDash.Id, ShouldEqual, 1)
|
||||
So(err2, ShouldBeNil)
|
||||
So(len(alerts), ShouldEqual, 0)
|
||||
So(len(query.Result), ShouldEqual, 0)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -21,6 +21,7 @@ export class AlertLogCtrl {
|
||||
|
||||
loadAlertLogs() {
|
||||
this.backendSrv.get('/api/alerts/events/' + this.alertId).then(result => {
|
||||
console.log(result);
|
||||
this.alertLogs = result;
|
||||
});
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
<table class="filter-table">
|
||||
<thead>
|
||||
<th style="width: 68px"></th>
|
||||
<th style="width: 68px">Status</th>
|
||||
<th><strong>Time</strong></th>
|
||||
<th>Description</th>
|
||||
</thead>
|
||||
|
||||
Reference in New Issue
Block a user