From ce7908cdac87af1e3b02ac4038fc3985602cf95a Mon Sep 17 00:00:00 2001 From: jasper Date: Wed, 22 Mar 2017 15:00:45 -0400 Subject: [PATCH] Update existing filters when negated via a visualization (#10850) Backports PR #10778 **Commit 1:** Handle single filter scenario * Original sha: ccd24cb9bddb4f6d1f6d85e5ad80bf04b62989e4 * Authored by Matthew Bargar on 2017-03-16T21:03:42Z **Commit 2:** Handle the multi filter scenario * Original sha: c90535d28d3f91de43b5c3687ea4628656e21881 * Authored by Matthew Bargar on 2017-03-16T21:26:28Z --- src/ui/public/filter_bar/filter_bar.js | 23 ++++++++++++++++--- .../filter_bar/filter_bar_click_handler.js | 2 +- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/ui/public/filter_bar/filter_bar.js b/src/ui/public/filter_bar/filter_bar.js index 4be87c2a70c8d..25561fbdc5594 100644 --- a/src/ui/public/filter_bar/filter_bar.js +++ b/src/ui/public/filter_bar/filter_bar.js @@ -8,6 +8,7 @@ import FilterBarLibExtractTimeFilterProvider from 'ui/filter_bar/lib/extract_tim import FilterBarLibFilterOutTimeBasedFilterProvider from 'ui/filter_bar/lib/filter_out_time_based_filter'; import FilterBarLibChangeTimeFilterProvider from 'ui/filter_bar/lib/change_time_filter'; import FilterBarQueryFilterProvider from 'ui/filter_bar/query_filter'; +import compareFilters from './lib/compare_filters'; import uiModules from 'ui/modules'; const module = uiModules.get('kibana'); @@ -52,8 +53,7 @@ module.directive('filterBar', function (Private, Promise, getAppState) { }; $scope.applyFilters = function (filters) { - // add new filters - $scope.addFilters(filterAppliedAndUnwrap(filters)); + addAndInvertFilters(filterAppliedAndUnwrap(filters)); $scope.newFilters = []; // change time filter @@ -128,10 +128,27 @@ module.directive('filterBar', function (Private, Promise, getAppState) { return filters; }) .then(filterOutTimeBasedFilter) - .then($scope.addFilters); + .then(addAndInvertFilters); } }); + function addAndInvertFilters(filters) { + const existingFilters = queryFilter.getFilters(); + const inversionFilters = _.filter(existingFilters, (existingFilter) => { + const newMatchingFilter = _.find(filters, _.partial(compareFilters, existingFilter)); + return newMatchingFilter + && newMatchingFilter.meta + && existingFilter.meta + && existingFilter.meta.negate !== newMatchingFilter.meta.negate; + }); + const newFilters = _.reject(filters, (filter) => { + return _.find(inversionFilters, _.partial(compareFilters, filter)); + }); + + _.forEach(inversionFilters, $scope.invertFilter); + $scope.addFilters(newFilters); + } + function convertToEditableFilter(filter) { return _.omit(_.cloneDeep(filter), function (val, key) { return key.match(privateFilterFieldRegex); diff --git a/src/ui/public/filter_bar/filter_bar_click_handler.js b/src/ui/public/filter_bar/filter_bar_click_handler.js index c4b9f942c230e..db634bbf89365 100644 --- a/src/ui/public/filter_bar/filter_bar_click_handler.js +++ b/src/ui/public/filter_bar/filter_bar_click_handler.js @@ -56,7 +56,7 @@ export default function (Notifier) { }); } - filters = dedupFilters($state.filters, uniqFilters(filters)); + filters = dedupFilters($state.filters, uniqFilters(filters), { negate: true }); // We need to add a bunch of filter deduping here. if (!simulate) { $state.$newFilters = filters;