From 7c339f07941ef5f48c5499bbfb87288381f7b2cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Fri, 30 Sep 2016 17:37:47 +0200 Subject: [PATCH] feat(alerting): show alertin state in panel header, closes #6136 --- pkg/api/alerting.go | 19 ++++++ pkg/api/api.go | 1 + pkg/models/alert.go | 15 +++++ pkg/services/alerting/extractor.go | 4 +- pkg/services/alerting/extractor_test.go | 2 - pkg/services/sqlstore/alert.go | 17 ++++++ .../app/features/alerting/alert_tab_ctrl.ts | 59 ++++++++++--------- .../features/alerting/partials/alert_tab.html | 4 +- .../features/annotations/annotations_srv.ts | 44 ++++++++++++-- .../app/features/dashboard/dashnav/dashnav.ts | 2 +- .../app/features/panel/metrics_panel_ctrl.ts | 4 +- public/app/features/panel/panel_directive.ts | 22 ++++++- public/app/features/panel/panel_menu.js | 1 + public/app/partials/panelgeneral.html | 4 +- public/app/plugins/panel/graph/graph.ts | 2 +- public/app/plugins/panel/graph/module.ts | 10 +++- .../plugins/panel/graph/thresholds_form.ts | 2 +- public/app/plugins/panel/table/editor.html | 5 +- public/sass/pages/_alerting.scss | 30 ++++++++++ 19 files changed, 194 insertions(+), 53 deletions(-) diff --git a/pkg/api/alerting.go b/pkg/api/alerting.go index 1233e09d504..460ad56e1ab 100644 --- a/pkg/api/alerting.go +++ b/pkg/api/alerting.go @@ -25,6 +25,25 @@ func ValidateOrgAlert(c *middleware.Context) { } } +func GetAlertStatesForDashboard(c *middleware.Context) Response { + dashboardId := c.QueryInt64("dashboardId") + + if dashboardId == 0 { + return ApiError(400, "Missing query parameter dashboardId", nil) + } + + query := models.GetAlertStatesForDashboardQuery{ + OrgId: c.OrgId, + DashboardId: c.QueryInt64("dashboardId"), + } + + if err := bus.Dispatch(&query); err != nil { + return ApiError(500, "Failed to fetch alert states", err) + } + + return Json(200, query.Result) +} + // GET /api/alerts func GetAlerts(c *middleware.Context) Response { query := models.GetAlertsQuery{ diff --git a/pkg/api/api.go b/pkg/api/api.go index bac3db429d2..deb29d730eb 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -254,6 +254,7 @@ func Register(r *macaron.Macaron) { r.Post("/test", bind(dtos.AlertTestCommand{}), wrap(AlertTest)) r.Get("/:alertId", ValidateOrgAlert, wrap(GetAlert)) r.Get("/", wrap(GetAlerts)) + r.Get("/states-for-dashboard", wrap(GetAlertStatesForDashboard)) }) r.Get("/alert-notifications", wrap(GetAlertNotifications)) diff --git a/pkg/models/alert.go b/pkg/models/alert.go index bf21b8e5ec6..938be8ddbd4 100644 --- a/pkg/models/alert.go +++ b/pkg/models/alert.go @@ -135,3 +135,18 @@ type GetAlertByIdQuery struct { Result *Alert } + +type GetAlertStatesForDashboardQuery struct { + OrgId int64 + DashboardId int64 + + Result []*AlertStateInfoDTO +} + +type AlertStateInfoDTO struct { + Id int64 `json:"id"` + DashboardId int64 `json:"dashboardId"` + PanelId int64 `json:"panelId"` + State AlertStateType `json:"state"` + NewStateDate time.Time `json:"newStateDate"` +} diff --git a/pkg/services/alerting/extractor.go b/pkg/services/alerting/extractor.go index cf516786412..6a8be4f6deb 100644 --- a/pkg/services/alerting/extractor.go +++ b/pkg/services/alerting/extractor.go @@ -74,9 +74,9 @@ func (e *DashAlertExtractor) GetAlerts() ([]*m.Alert, error) { continue } + // backward compatability check, can be removed later enabled, hasEnabled := jsonAlert.CheckGet("enabled") - - if !hasEnabled || !enabled.MustBool() { + if hasEnabled && enabled.MustBool() == false { continue } diff --git a/pkg/services/alerting/extractor_test.go b/pkg/services/alerting/extractor_test.go index b25ce313b54..f82210f1b1a 100644 --- a/pkg/services/alerting/extractor_test.go +++ b/pkg/services/alerting/extractor_test.go @@ -42,7 +42,6 @@ func TestAlertRuleExtraction(t *testing.T) { "name": "name1", "message": "desc1", "handler": 1, - "enabled": true, "frequency": "60s", "conditions": [ { @@ -66,7 +65,6 @@ func TestAlertRuleExtraction(t *testing.T) { "name": "name2", "message": "desc2", "handler": 0, - "enabled": true, "frequency": "60s", "severity": "warning", "conditions": [ diff --git a/pkg/services/sqlstore/alert.go b/pkg/services/sqlstore/alert.go index 64a4a6b3d8a..61397017943 100644 --- a/pkg/services/sqlstore/alert.go +++ b/pkg/services/sqlstore/alert.go @@ -17,6 +17,7 @@ func init() { bus.AddHandler("sql", DeleteAlertById) bus.AddHandler("sql", GetAllAlertQueryHandler) bus.AddHandler("sql", SetAlertState) + bus.AddHandler("sql", GetAlertStatesForDashboard) } func GetAlertById(query *m.GetAlertByIdQuery) error { @@ -241,3 +242,19 @@ func SetAlertState(cmd *m.SetAlertStateCommand) error { return nil }) } + +func GetAlertStatesForDashboard(query *m.GetAlertStatesForDashboardQuery) error { + var rawSql = `SELECT + id, + dashboard_id, + panel_id, + state, + new_state_date + FROM alert + WHERE org_id = ? AND dashboard_id = ?` + + query.Result = make([]*m.AlertStateInfoDTO, 0) + err := x.Sql(rawSql, query.OrgId, query.DashboardId).Find(&query.Result) + + return err +} diff --git a/public/app/features/alerting/alert_tab_ctrl.ts b/public/app/features/alerting/alert_tab_ctrl.ts index 7ae8477c5bf..0ea5d5fd804 100644 --- a/public/app/features/alerting/alert_tab_ctrl.ts +++ b/public/app/features/alerting/alert_tab_ctrl.ts @@ -48,19 +48,18 @@ export class AlertTabCtrl { $onInit() { this.addNotificationSegment = this.uiSegmentSrv.newPlusButton(); - this.initModel(); - this.validateModel(); + // subscribe to graph threshold handle changes + var thresholdChangedEventHandler = this.graphThresholdChanged.bind(this); + this.panelCtrl.events.on('threshold-changed', thresholdChangedEventHandler); - // set panel alert edit mode + // set panel alert edit mode this.$scope.$on("$destroy", () => { + this.panelCtrl.events.off("threshold-changed", thresholdChangedEventHandler); this.panelCtrl.editingThresholds = false; this.panelCtrl.render(); }); - // subscribe to graph threshold handle changes - this.panelCtrl.events.on('threshold-changed', this.graphThresholdChanged.bind(this)); - - // build notification model + // build notification model this.notifications = []; this.alertNotifications = []; this.alertHistory = []; @@ -68,21 +67,8 @@ export class AlertTabCtrl { return this.backendSrv.get('/api/alert-notifications').then(res => { this.notifications = res; - _.each(this.alert.notifications, item => { - var model = _.find(this.notifications, {id: item.id}); - if (model) { - model.iconClass = this.getNotificationIcon(model.type); - this.alertNotifications.push(model); - } - }); - - _.each(this.notifications, item => { - if (item.isDefault) { - item.iconClass = this.getNotificationIcon(item.type); - item.bgColor = "#00678b"; - this.alertNotifications.push(item); - } - }); + this.initModel(); + this.validateModel(); }); } @@ -143,9 +129,8 @@ export class AlertTabCtrl { } initModel() { - var alert = this.alert = this.panel.alert = this.panel.alert || {enabled: false}; - - if (!this.alert.enabled) { + var alert = this.alert = this.panel.alert; + if (!alert) { return; } @@ -169,6 +154,22 @@ export class AlertTabCtrl { ThresholdMapper.alertToGraphThresholds(this.panel); + for (let addedNotification of alert.notifications) { + var model = _.find(this.notifications, {id: addedNotification.id}); + if (model) { + model.iconClass = this.getNotificationIcon(model.type); + this.alertNotifications.push(model); + } + } + + for (let notification of this.notifications) { + if (notification.isDefault) { + notification.iconClass = this.getNotificationIcon(notification.type); + notification.bgColor = "#00678b"; + this.alertNotifications.push(notification); + } + } + this.panelCtrl.editingThresholds = true; this.panelCtrl.render(); } @@ -193,7 +194,7 @@ export class AlertTabCtrl { } validateModel() { - if (!this.alert.enabled) { + if (!this.alert) { return; } @@ -310,17 +311,17 @@ export class AlertTabCtrl { icon: 'fa-trash', yesText: 'Delete', onConfirm: () => { - this.alert = this.panel.alert = {enabled: false}; + delete this.panel.alert; + this.alert = null; this.panel.thresholds = []; this.conditionModels = []; this.panelCtrl.render(); } }); - } enable() { - this.alert.enabled = true; + this.panel.alert = {}; this.initModel(); } diff --git a/public/app/features/alerting/partials/alert_tab.html b/public/app/features/alerting/partials/alert_tab.html index 3d6e183d93f..bb6fe7547b2 100644 --- a/public/app/features/alerting/partials/alert_tab.html +++ b/public/app/features/alerting/partials/alert_tab.html @@ -1,4 +1,4 @@ -
+
-
+