From faa3b5f4e808606d86901ef1fe7ef03181a37262 Mon Sep 17 00:00:00 2001 From: bergquist Date: Thu, 15 Dec 2016 16:29:40 +0100 Subject: [PATCH] feat(alertlist): adds sorting on state closes #6676 --- public/app/features/alerting/alert_def.ts | 9 +++++++ .../app/plugins/panel/alertlist/editor.html | 6 +++++ public/app/plugins/panel/alertlist/module.ts | 27 ++++++++++++++++--- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/public/app/features/alerting/alert_def.ts b/public/app/features/alerting/alert_def.ts index fd08823c4a9..ce6ae23be5b 100644 --- a/public/app/features/alerting/alert_def.ts +++ b/public/app/features/alerting/alert_def.ts @@ -20,6 +20,14 @@ var conditionTypes = [ {text: 'Query', value: 'query'}, ]; +var alertStateSortScore = { + alerting: 1, + no_data: 2, + pending: 3, + ok: 4, + paused: 5, +}; + var evalFunctions = [ {text: 'IS ABOVE', value: 'gt'}, {text: 'IS BELOW', value: 'lt'}, @@ -129,4 +137,5 @@ export default { reducerTypes: reducerTypes, createReducerPart: createReducerPart, joinEvalMatches: joinEvalMatches, + alertStateSortScore: alertStateSortScore, }; diff --git a/public/app/plugins/panel/alertlist/editor.html b/public/app/plugins/panel/alertlist/editor.html index 344af7408fc..b8cee3b3317 100644 --- a/public/app/plugins/panel/alertlist/editor.html +++ b/public/app/plugins/panel/alertlist/editor.html @@ -11,6 +11,12 @@ Max items +
+ Sort order +
+ +
+
diff --git a/public/app/plugins/panel/alertlist/module.ts b/public/app/plugins/panel/alertlist/module.ts index d03fad49073..9749394d205 100644 --- a/public/app/plugins/panel/alertlist/module.ts +++ b/public/app/plugins/panel/alertlist/module.ts @@ -17,6 +17,12 @@ class AlertListPanel extends PanelCtrl { {text: 'Recent state changes', value: 'changes'} ]; + sortOrderOptions = [ + {text: 'Alphabetical (asc)', value: 1}, + {text: 'Alphabetical (desc)', value: 2}, + {text: 'Importance', value: 3}, + ]; + contentHeight: string; stateFilter: any = {}; currentAlerts: any = []; @@ -26,10 +32,10 @@ class AlertListPanel extends PanelCtrl { show: 'current', limit: 10, stateFilter: [], - onlyAlertsOnDashboard: false + onlyAlertsOnDashboard: false, + sortOrder: 1 }; - /** @ngInject */ constructor($scope, $injector, private $location, private backendSrv, private timeSrv, private templateSrv) { super($scope, $injector); @@ -44,6 +50,19 @@ class AlertListPanel extends PanelCtrl { } } + sortResult(alerts) { + if (this.panel.sortOrder === 3) { + return _.sortBy(alerts, a => { return alertDef.alertStateSortScore[a.state]; }); + } + + var result = _.sortBy(alerts, a => { return a.name.toLowerCase();}); + if (this.panel.sortOrder === 2) { + result.reverse(); + } + + return result; + } + updateStateFilter() { var result = []; @@ -104,11 +123,11 @@ class AlertListPanel extends PanelCtrl { this.backendSrv.get(`/api/alerts`, params) .then(res => { - this.currentAlerts = _.map(res, al => { + this.currentAlerts = this.sortResult(_.map(res, al => { al.stateModel = alertDef.getStateDisplayModel(al.state); al.newStateDateAgo = moment(al.newStateDate).fromNow().replace(" ago", ""); return al; - }); + })); }); }