From 75a6c2b6129f87d1896ff1e068de84bca9bf3853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 18 Aug 2016 09:03:46 +0200 Subject: [PATCH] feat(alerting): added validation that checks valid data source and checks for template variables in graphite query, #5841 --- pkg/services/sqlstore/alert.go | 1 - .../app/features/alerting/alert_tab_ctrl.ts | 61 +++++++++++++++++-- .../features/alerting/partials/alert_tab.html | 4 ++ public/sass/components/_alerts.scss | 2 +- public/sass/components/_panel_graph.scss | 4 +- 5 files changed, 63 insertions(+), 9 deletions(-) diff --git a/pkg/services/sqlstore/alert.go b/pkg/services/sqlstore/alert.go index 38975c9e2e9..93bd950979d 100644 --- a/pkg/services/sqlstore/alert.go +++ b/pkg/services/sqlstore/alert.go @@ -87,7 +87,6 @@ func HandleAlertsQuery(query *m.GetAlertsQuery) error { sql.WriteString(")") } - sqlog.Error(sql.String(), "params", params) alerts := make([]*m.Alert, 0) if err := x.Sql(sql.String(), params...).Find(&alerts); err != nil { return err diff --git a/public/app/features/alerting/alert_tab_ctrl.ts b/public/app/features/alerting/alert_tab_ctrl.ts index 0dd228e7040..cdba3efb7be 100644 --- a/public/app/features/alerting/alert_tab_ctrl.ts +++ b/public/app/features/alerting/alert_tab_ctrl.ts @@ -4,6 +4,7 @@ import _ from 'lodash'; import {ThresholdMapper} from './threshold_mapper'; import {QueryPart} from 'app/core/components/query_part/query_part'; import alertDef from './alert_def'; +import config from 'app/core/config'; export class AlertTabCtrl { panel: any; @@ -19,9 +20,17 @@ export class AlertTabCtrl { addNotificationSegment; notifications; alertNotifications; + error: string; /** @ngInject */ - constructor(private $scope, private $timeout, private backendSrv, private dashboardSrv, private uiSegmentSrv, private $q) { + constructor(private $scope, + private $timeout, + private backendSrv, + private dashboardSrv, + private uiSegmentSrv, + private $q, + private datasourceSrv, + private templateSrv) { this.panelCtrl = $scope.ctrl; this.panel = this.panelCtrl.panel; this.$scope.ctrl = this; @@ -35,6 +44,7 @@ export class AlertTabCtrl { this.addNotificationSegment = this.uiSegmentSrv.newPlusButton(); this.initModel(); + this.validateModel(); // set panel alert edit mode this.$scope.$on("$destroy", () => { @@ -144,6 +154,47 @@ export class AlertTabCtrl { }; } + validateModel() { + let firstTarget; + var fixed = false; + let foundTarget = null; + + for (var condition of this.alert.conditions) { + if (condition.type !== 'query') { + continue; + } + + for (var target of this.panel.targets) { + if (!firstTarget) { + firstTarget = target; + } + if (condition.query.params[0] === target.refId) { + foundTarget = target; + break; + } + } + + if (!foundTarget) { + if (firstTarget) { + condition.query.params[0] = firstTarget.refId; + foundTarget = firstTarget; + fixed = true; + } else { + this.error = "Could not find any metric queries"; + } + } + + var datasourceName = foundTarget.datasource || this.panel.datasource; + this.datasourceSrv.get(datasourceName).then(ds => { + if (ds.meta.id !== 'graphite') { + this.error = 'Currently the alerting backend only supports Graphite queries'; + } else if (this.templateSrv.variableExists(foundTarget.target)) { + this.error = 'Template variables are not supported in alert queries'; + } + }); + } + } + buildConditionModel(source) { var cm: any = {source: source, type: source.type}; @@ -187,7 +238,7 @@ export class AlertTabCtrl { addCondition(type) { var condition = this.buildDefaultCondition(); // add to persited model - this.panelCtrl.conditions.push(condition); + this.alert.conditions.push(condition); // add to view model this.conditionModels.push(this.buildConditionModel(condition)); } @@ -198,7 +249,7 @@ export class AlertTabCtrl { } delete() { - this.panel.alert = {enabled: false}; + this.alert = this.panel.alert = {enabled: false}; this.panel.thresholds = []; this.conditionModels = []; this.panelCtrl.render(); @@ -223,12 +274,12 @@ export class AlertTabCtrl { // ensure params array is correct length switch (evaluator.type) { case "lt": - case "gt": { + case "gt": { evaluator.params = [evaluator.params[0]]; break; } case "within_range": - case "outside_range": { + case "outside_range": { evaluator.params = [evaluator.params[0], evaluator.params[1]]; break; } diff --git a/public/app/features/alerting/partials/alert_tab.html b/public/app/features/alerting/partials/alert_tab.html index 39a76b2a6a3..a386d23945b 100644 --- a/public/app/features/alerting/partials/alert_tab.html +++ b/public/app/features/alerting/partials/alert_tab.html @@ -20,6 +20,10 @@
+
+ {{ctrl.error}} +
+
Alert Config
diff --git a/public/sass/components/_alerts.scss b/public/sass/components/_alerts.scss index 2c538c1c1c0..4434fa680f0 100644 --- a/public/sass/components/_alerts.scss +++ b/public/sass/components/_alerts.scss @@ -9,7 +9,7 @@ .alert { - padding: 8px 35px 13px 14px; + padding: 0.5rem 2rem 0.5rem 1rem; margin-bottom: $line-height-base; text-shadow: 0 1px 0 rgba(255,255,255,.5); background-color: $state-warning-bg; diff --git a/public/sass/components/_panel_graph.scss b/public/sass/components/_panel_graph.scss index f6d564c8264..d5cfedf03db 100644 --- a/public/sass/components/_panel_graph.scss +++ b/public/sass/components/_panel_graph.scss @@ -392,10 +392,10 @@ z-index: 0; position: relative; - &--crit { + &--critical { background-color: rgba(237, 46, 24, 0.60); } - &--warn { + &--warning { background-color: rgba(247, 149, 32, 0.60); } }