From 8cd5a9963fa66da26813b2545d7ba4dbd921547b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 27 May 2014 19:21:21 +0200 Subject: [PATCH] Fixes for filter option loading and nested filters. Fixes #447, Fixes #412 --- CHANGELOG.md | 6 +++ src/app/panels/filtering/module.js | 75 ++++++++++++++++-------------- src/app/services/filterSrv.js | 13 ++---- 3 files changed, 49 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d085aa989d..1f1b4b15c55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,12 @@ vNext - Allow special characters in serie names (influxdb datasource), PR #390 - thx @majst01 - Refactoring of filterSrv (Issue #428), thx @Tetha +# Fixes +- Filter option loading when having muliple nested filters now works better. + Options are now reloaded correctly and there are no multiple renders/refresh inbetween (#447), + After an option is changed and a nested template param is also reloaded, if the current value + exists after the options are reloaded the current selected value is kept (Closes #447, Closes #412) + # 1.5.4 (2014-05-13) ### New features and improvements - InfluxDB enhancement: support for multiple hosts (with retries) and raw queries (Issue #318, thx @toddboom) diff --git a/src/app/panels/filtering/module.js b/src/app/panels/filtering/module.js index 4630608d9af..46a88fa0565 100644 --- a/src/app/panels/filtering/module.js +++ b/src/app/panels/filtering/module.js @@ -14,7 +14,7 @@ function (angular, app, _) { var module = angular.module('kibana.panels.filtering', []); app.useModule(module); - module.controller('filtering', function($scope, datasourceSrv, $rootScope, $timeout) { + module.controller('filtering', function($scope, datasourceSrv, $rootScope, $timeout, $q) { $scope.panelMeta = { status : "Stable", @@ -27,56 +27,67 @@ function (angular, app, _) { _.defaults($scope.panel,_d); $scope.init = function() { - // empty. Don't know if I need the function then. + // empty. Don't know if I need the function then. }; $scope.remove = function(templateParameter) { - $scope.filter.removeTemplateParameter(templateParameter); + $scope.filter.removeTemplateParameter(templateParameter); + }; - // TODO hkraemer: check if this makes sense like this - if(!$rootScope.$$phase) { - $rootScope.$apply(); - } - $timeout(function(){ + $scope.filterOptionSelected = function(templateParameter, option, recursive) { + templateParameter.current = option; + + $scope.filter.updateTemplateData(); + + return $scope.applyFilterToOtherFilters(templateParameter) + .then(function() { + // only refresh in the outermost call + if (!recursive) { $scope.dashboard.refresh(); - },0); + } + }); }; - $scope.filterOptionSelected = function(templateParameter, option) { - $scope.filter.templateOptionSelected(templateParameter, option); - $scope.applyFilterToOtherFilters(templateParameter); - }; - - $scope.applyFilterToOtherFilters = function(updatedFilter) { - _.each($scope.filter.templateParameters, function(templateParameter) { - if (templateParameter === updatedFilter) { + $scope.applyFilterToOtherFilters = function(updatedTemplatedParam) { + var promises = _.map($scope.filter.templateParameters, function(templateParam) { + if (templateParam === updatedTemplatedParam) { return; } - if (templateParameter.query.indexOf(updatedFilter.name) !== -1) { - $scope.applyFilter(templateParameter); + if (templateParam.query.indexOf(updatedTemplatedParam.name) !== -1) { + return $scope.applyFilter(templateParam); } }); + + return $q.all(promises); }; - $scope.applyFilter = function(filter) { - - datasourceSrv.default.metricFindQuery($scope.filter, filter.query) + $scope.applyFilter = function(templateParam) { + return datasourceSrv.default.metricFindQuery($scope.filter, templateParam.query) .then(function (results) { - filter.editing=undefined; - filter.options = _.map(results, function(node) { + templateParam.editing = undefined; + templateParam.options = _.map(results, function(node) { return { text: node.text, value: node.text }; }); - if (filter.includeAll) { + if (templateParam.includeAll) { var allExpr = '{'; - _.each(filter.options, function(option) { + _.each(templateParam.options, function(option) { allExpr += option.text + ','; }); allExpr = allExpr.substring(0, allExpr.length - 1) + '}'; - filter.options.unshift({text: 'All', value: allExpr}); + templateParam.options.unshift({text: 'All', value: allExpr}); } - $scope.filter.templateOptionSelected(filter, filter.options[0]); + // if parameter has current value + // if it exists in options array keep value + if (templateParam.current) { + var currentExists = _.findWhere(templateParam.options, { value: templateParam.current.value }); + if (currentExists) { + return $scope.filterOptionSelected(templateParam, templateParam.current, true); + } + } + + return $scope.filterOptionSelected(templateParam, templateParam.options[0], true); }); }; @@ -89,13 +100,5 @@ function (angular, app, _) { }); }; - $scope.refresh = function() { - $scope.dashboard.refresh(); - }; - - $scope.render = function() { - $rootScope.$broadcast('render'); - }; - }); }); diff --git a/src/app/services/filterSrv.js b/src/app/services/filterSrv.js index 4a82cff80f2..95e69fcf226 100644 --- a/src/app/services/filterSrv.js +++ b/src/app/services/filterSrv.js @@ -16,7 +16,8 @@ define([ }; var result = { - _updateTemplateData: function(initial) { + + updateTemplateData: function(initial) { var _templateData = {}; _.each(this.templateParameters, function(templateParameter) { if (initial) { @@ -34,15 +35,9 @@ define([ this._templateData = _templateData; }, - templateOptionSelected: function(templateParameter, option) { - templateParameter.current = option; - this._updateTemplateData(); - dashboard.refresh(); - }, - addTemplateParameter: function(templateParameter) { this.templateParameters.push(templateParameter); - this._updateTemplateData(); + this.updateTemplateData(); }, applyTemplateToTarget: function(target) { @@ -105,7 +100,7 @@ define([ if(dashboard.services && dashboard.services.filter) { this.time = dashboard.services.filter.time; this.templateParameters = dashboard.services.filter.list || []; - this._updateTemplateData(true); + this.updateTemplateData(true); } }