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
34 changes: 22 additions & 12 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);
$state.query = query;
$scope.fetch();
const isUpdate = (
(query && !_.isEqual(query, $state.query)) ||
(dateRange && !_.isEqual(dateRange, $scope.timeRange))
);

if (query && !_.isEqual(query, $state.query)) $state.query = query;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we still need this checks here if w have the checks in embedded_visualize_handler ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need to check if nothing has changed in order to manually trigger a fetch, but you're right, we don't need to check before we set them. Fixed in cc10e25.

if (dateRange && !_.isEqual(dateRange, $scope.timeRange)) 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 @@ -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