From 72a67b39f1ca9bf3810ec97e7b8869e7f5aa2af1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 11 Aug 2016 15:18:21 +0200 Subject: [PATCH] feat(thresholds): more work thresholds --- public/app/features/alerting/alert_def.ts | 45 +++++++- .../app/features/alerting/alert_tab_ctrl.ts | 107 ++++++------------ .../features/alerting/partials/alert_tab.html | 7 +- .../alerting/specs/threshold_mapper_specs.ts | 78 +++++++++++++ .../app/features/alerting/threshold_mapper.ts | 72 ++++++++++++ public/app/plugins/panel/graph/graph.js | 4 +- .../plugins/panel/graph/specs/graph_specs.ts | 4 +- .../app/plugins/panel/graph/tab_display.html | 2 +- 8 files changed, 237 insertions(+), 82 deletions(-) create mode 100644 public/app/features/alerting/specs/threshold_mapper_specs.ts create mode 100644 public/app/features/alerting/threshold_mapper.ts diff --git a/public/app/features/alerting/alert_def.ts b/public/app/features/alerting/alert_def.ts index e1d996f1013..84c1bc7cb05 100644 --- a/public/app/features/alerting/alert_def.ts +++ b/public/app/features/alerting/alert_def.ts @@ -10,6 +10,49 @@ function getSeverityIconClass(alertState) { return alertSeverityIconMap[alertState]; } +import { + QueryPartDef, + QueryPart, +} from 'app/core/components/query_part/query_part'; + +var alertQueryDef = new QueryPartDef({ + type: 'query', + params: [ + {name: "queryRefId", type: 'string', options: ['A', 'B', 'C', 'D', 'E', 'F']}, + {name: "from", type: "string", options: ['1s', '10s', '1m', '5m', '10m', '15m', '1h']}, + {name: "to", type: "string", options: ['now']}, + ], + defaultParams: ['#A', '5m', 'now', 'avg'] +}); + +var reducerAvgDef = new QueryPartDef({ + type: 'avg', + params: [], + defaultParams: [] +}); + +var conditionTypes = [ + {text: 'Query', value: 'query'}, +]; + +var evalFunctions = [ + {text: 'IS ABOVE', value: 'gt'}, + {text: 'IS BELOW', value: 'lt'}, + {text: 'IS OUTSIDE RANGE', value: 'outside_range'}, + {text: 'IS WITHIN RANGE', value: 'within_range'}, + {text: 'HAS NO VALUE' , value: 'no_value'} +]; + +var severityLevels = [ + {text: 'Critical', value: 'critical'}, + {text: 'Warning', value: 'warning'}, +]; + export default { - getSeverityIconClass, + alertQueryDef: alertQueryDef, + reducerAvgDef: reducerAvgDef, + getSeverityIconClass: getSeverityIconClass, + conditionTypes: conditionTypes, + evalFunctions: evalFunctions, + severityLevels: severityLevels, }; diff --git a/public/app/features/alerting/alert_tab_ctrl.ts b/public/app/features/alerting/alert_tab_ctrl.ts index 403a4a0fb8a..a91fa5e0724 100644 --- a/public/app/features/alerting/alert_tab_ctrl.ts +++ b/public/app/features/alerting/alert_tab_ctrl.ts @@ -1,27 +1,9 @@ /// import _ from 'lodash'; - -import { - QueryPartDef, - QueryPart, -} from 'app/core/components/query_part/query_part'; - -var alertQueryDef = new QueryPartDef({ - type: 'query', - params: [ - {name: "queryRefId", type: 'string', options: ['A', 'B', 'C', 'D', 'E', 'F']}, - {name: "from", type: "string", options: ['1s', '10s', '1m', '5m', '10m', '15m', '1h']}, - {name: "to", type: "string", options: ['now']}, - ], - defaultParams: ['#A', '5m', 'now', 'avg'] -}); - -var reducerAvgDef = new QueryPartDef({ - type: 'avg', - params: [], - defaultParams: [] -}); +import {ThresholdMapper} from './threshold_mapper'; +import {QueryPart} from 'app/core/components/query_part/query_part'; +import alertDef from './alert_def'; export class AlertTabCtrl { panel: any; @@ -29,21 +11,11 @@ export class AlertTabCtrl { testing: boolean; testResult: any; subTabIndex: number; - - handlers = [{text: 'Grafana', value: 1}, {text: 'External', value: 0}]; - conditionTypes = [ - {text: 'Query', value: 'query'}, - ]; + conditionTypes: any; alert: any; conditionModels: any; - evalFunctions = [ - {text: '>', value: '>'}, - {text: '<', value: '<'}, - ]; - severityLevels = [ - {text: 'Critical', value: 'critical'}, - {text: 'Warning', value: 'warning'}, - ]; + evalFunctions: any; + severityLevels: any; addNotificationSegment; notifications; alertNotifications; @@ -54,6 +26,9 @@ export class AlertTabCtrl { this.panel = this.panelCtrl.panel; this.$scope.ctrl = this; this.subTabIndex = 0; + this.evalFunctions = alertDef.evalFunctions; + this.conditionTypes = alertDef.conditionTypes; + this.severityLevels = alertDef.severityLevels; } $onInit() { @@ -101,6 +76,27 @@ export class AlertTabCtrl { })); } + evaluatorTypeChanged(evaluator) { + // ensure params array is correct length + switch (evaluator.type) { + case "lt": + case "gt": { + evaluator.params = [evaluator.params[0]]; + break; + } + case "within_range": + case "outside_range": { + evaluator.params = [evaluator.params[0], evaluator.params[1]]; + break; + } + case "no_value": { + evaluator.params = []; + } + } + + this.thresholdUpdated(); + } + notificationAdded() { var model = _.findWhere(this.notifications, {name: this.addNotificationSegment.value}); if (!model) { @@ -146,45 +142,10 @@ export class AlertTabCtrl { this.panelCtrl.editingThresholds = true; } - this.syncThresholds(); + ThresholdMapper.alertToGraphThresholds(this.panel); this.panelCtrl.render(); } - syncThresholds() { - if (this.panel.type !== 'graph') { - return; - } - - var threshold: any = {}; - if (this.panel.thresholds && this.panel.thresholds.length > 0) { - threshold = this.panel.thresholds[0]; - } else { - this.panel.thresholds = [threshold]; - } - - var updated = false; - for (var condition of this.conditionModels) { - if (condition.type === 'query') { - var value = condition.evaluator.params[0]; - if (!_.isNumber(value)) { - continue; - } - - if (value !== threshold.value) { - threshold.value = value; - updated = true; - } - - if (condition.evaluator.type !== threshold.op) { - threshold.op = condition.evaluator.type; - updated = true; - } - } - } - - return updated; - } - graphThresholdChanged(evt) { for (var condition of this.alert.conditions) { if (condition.type === 'query') { @@ -206,8 +167,8 @@ export class AlertTabCtrl { buildConditionModel(source) { var cm: any = {source: source, type: source.type}; - cm.queryPart = new QueryPart(source.query, alertQueryDef); - cm.reducerPart = new QueryPart({params: []}, reducerAvgDef); + cm.queryPart = new QueryPart(source.query, alertDef.alertQueryDef); + cm.reducerPart = new QueryPart({params: []}, alertDef.reducerAvgDef); cm.evaluator = source.evaluator; return cm; @@ -240,7 +201,7 @@ export class AlertTabCtrl { } thresholdUpdated() { - if (this.syncThresholds()) { + if (ThresholdMapper.alertToGraphThresholds(this.panel)) { this.panelCtrl.render(); } } diff --git a/public/app/features/alerting/partials/alert_tab.html b/public/app/features/alerting/partials/alert_tab.html index d58e374d1cb..892cc32e6d3 100644 --- a/public/app/features/alerting/partials/alert_tab.html +++ b/public/app/features/alerting/partials/alert_tab.html @@ -58,9 +58,10 @@
- Value - - + + + +