Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate fetch in Visualize #41204

Merged
merged 11 commits into from
Aug 8, 2019
32 changes: 21 additions & 11 deletions src/legacy/core_plugins/kibana/public/visualize/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,30 +402,33 @@ function VisEditor(
$scope.$listenAndDigestAsync(timefilter, 'timeUpdate', updateTimeRange);
$scope.$listenAndDigestAsync(timefilter, 'refreshIntervalUpdate', updateRefreshInterval);

// update the searchSource when filters update
const filterUpdateSubscription = subscribeWithScope($scope, queryFilter.getUpdates$(), {
next: () => {
$scope.filters = queryFilter.getFilters();
$scope.fetch();
}
});

// update the searchSource when query updates
$scope.fetch = function () {
$state.save();
savedVis.searchSource.setField('query', $state.query);
savedVis.searchSource.setField('filter', $state.filters);
$scope.globalFilters = queryFilter.getGlobalFilters();
$scope.vis.forceReload();
};

// update the searchSource when filters update
const filterUpdateSubscription = subscribeWithScope($scope, queryFilter.getUpdates$(), {
next: () => {
$scope.filters = queryFilter.getFilters();
lizozom marked this conversation as resolved.
Show resolved Hide resolved
$scope.globalFilters = queryFilter.getGlobalFilters();
}
});
const filterFetchSubscription = subscribeWithScope($scope, queryFilter.getFetches$(), {
next: $scope.fetch
});

$scope.$on('$destroy', function () {
if ($scope._handler) {
$scope._handler.destroy();
}
savedVis.destroy();
stateMonitor.destroy();
filterUpdateSubscription.unsubscribe();
filterFetchSubscription.unsubscribe();
});

if (!$scope.chrome.getVisible()) {
Expand All @@ -441,9 +444,16 @@ function VisEditor(
}

$scope.updateQueryAndFetch = function ({ query, dateRange }) {
timefilter.setTime(dateRange);
const isUpdate = (
(query && !_.isEqual(query, $state.query)) ||
(dateRange && !_.isEqual(dateRange, $scope.timeRange))
);

$state.query = query;
$scope.fetch();
timefilter.setTime(dateRange);

// If nothing has changed, trigger the fetch manually, otherwise it will happen as a result of the changes
if (!isUpdate) $scope.fetch();
};

$scope.onRefreshChange = function ({ isPaused, refreshInterval }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe('EmbeddedVisualizeHandler', () => {
});

it('should call dataLoader.render with updated filters', () => {
const params = { filters: [{ foo: 'bar' }] };
const params = { filters: [{ meta: { disabled: false } }] };
handler.update(params);
jest.runAllTimers();
expect(mockDataLoaderFetch).toHaveBeenCalledWith({ ...dataLoaderParams, ...params });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { EventEmitter } from 'events';
import { debounce, forEach, get } from 'lodash';
import { debounce, forEach, get, isEqual } from 'lodash';
import * as Rx from 'rxjs';
import { share } from 'rxjs/operators';
import { i18n } from '@kbn/i18n';
Expand All @@ -38,6 +38,7 @@ import { VisFiltersProvider } from '../../vis/vis_filters';
import { PipelineDataLoader } from './pipeline_data_loader';
import { visualizationLoader } from './visualization_loader';
import { VisualizeDataLoader } from './visualize_data_loader';
import { onlyDisabledFiltersChanged } from '../../../../core_plugins/data/public';

import { DataAdapter, RequestAdapter } from '../../inspector/adapters';

Expand Down Expand Up @@ -236,15 +237,21 @@ export class EmbeddedVisualizeHandler {
}

let fetchRequired = false;
if (params.hasOwnProperty('timeRange')) {
if (
params.hasOwnProperty('timeRange') &&
!isEqual(this.dataLoaderParams.timeRange, params.timeRange)
) {
fetchRequired = true;
this.dataLoaderParams.timeRange = params.timeRange;
}
if (params.hasOwnProperty('filters')) {
if (
params.hasOwnProperty('filters') &&
!onlyDisabledFiltersChanged(this.dataLoaderParams.filters, params.filters)
) {
fetchRequired = true;
this.dataLoaderParams.filters = params.filters;
}
if (params.hasOwnProperty('query')) {
if (params.hasOwnProperty('query') && !isEqual(this.dataLoaderParams.query, params.query)) {
fetchRequired = true;
this.dataLoaderParams.query = params.query;
}
Expand Down