[ML] Fixes getting the currently selected jobs from globalState. (#41255) (#41364)

Fixes a race condition where the anomaly explorer wouldn't pick up the most recently selected jobs.
This commit is contained in:
Walter Rafelsberger 2019-07-17 17:53:46 +02:00 committed by GitHub
parent 0018ec19c6
commit bf56629c1c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 14 deletions

View file

@ -35,6 +35,9 @@ function getInvalidJobIds(ids) {
}
function loadJobIdsFromGlobalState(globalState) { // jobIds, groups
// fetch to get the latest state
globalState.fetch();
const jobIds = [];
let groups = [];

View file

@ -198,8 +198,6 @@ export function JobSelector({
setSelectedIds(newSelection);
setNewSelection([]);
applyTimeRangeFromSelection(allNewSelectionUnique);
jobSelectService.next({ selection: allNewSelectionUnique });
setGlobalState(globalState, { selectedIds: allNewSelectionUnique, selectedGroups: groupSelection });
}

View file

@ -55,7 +55,7 @@ module
// Subscribe to changes to globalState and trigger
// a mlJobSelectService update if the job selection changed.
globalState.on('save_with_changes', () => {
const { newJobIds, newSelectedGroups } = getSelectedJobIds(globalState);
const { jobIds: newJobIds, selectedGroups: newSelectedGroups } = getSelectedJobIds(globalState);
const oldSelectedJobIds = mlJobSelectService.getValue().selection;
if (newJobIds && !(_.isEqual(oldSelectedJobIds, newJobIds))) {

View file

@ -13,6 +13,7 @@
import $ from 'jquery';
import moment from 'moment-timezone';
import { Subscription } from 'rxjs';
import '../components/annotations/annotations_table';
import '../components/anomalies_table';
@ -33,6 +34,7 @@ import { explorer$ } from './explorer_dashboard_service';
import { mlFieldFormatService } from 'plugins/ml/services/field_format_service';
import { mlJobService } from '../services/job_service';
import { refreshIntervalWatcher } from '../util/refresh_interval_watcher';
import { getSelectedJobIds } from '../components/job_selector/job_select_service_utils';
import { timefilter } from 'ui/timefilter';
import { APP_STATE_ACTION, EXPLORER_ACTION } from './explorer_constants';
@ -60,6 +62,7 @@ module.controller('MlExplorerController', function (
AppState,
Private,
config,
globalState,
) {
// Even if they are not used directly anymore in this controller but via imports
@ -71,6 +74,7 @@ module.controller('MlExplorerController', function (
$injector.get('mlSelectSeverityService');
const mlJobSelectService = $injector.get('mlJobSelectService');
const subscriptions = new Subscription();
// $scope should only contain what's actually still necessary for the angular part.
// For the moment that's the job selector and the (hidden) filter bar.
@ -147,7 +151,7 @@ module.controller('MlExplorerController', function (
// Jobs load via route resolver
if (mlJobService.jobs.length > 0) {
// Select any jobs set in the global state (i.e. passed in the URL).
const selectedJobIds = mlJobSelectService.getValue().selection;
const { jobIds: selectedJobIds } = getSelectedJobIds(globalState);
let selectedCells;
let filterData = {};
@ -179,6 +183,14 @@ module.controller('MlExplorerController', function (
selectedJobIds,
swimlaneViewByFieldName: $scope.appState.mlExplorerSwimlane.viewByFieldName,
});
subscriptions.add(mlJobSelectService.subscribe(({ selection }) => {
if (selection !== undefined) {
$scope.jobSelectionUpdateInProgress = true;
jobSelectionUpdate(EXPLORER_ACTION.JOB_SELECTION_CHANGE, { fullJobs: mlJobService.jobs, selectedJobIds: selection });
}
}));
} else {
explorer$.next({
action: EXPLORER_ACTION.RELOAD,
@ -191,18 +203,11 @@ module.controller('MlExplorerController', function (
}
}
const explorerSubscriber = explorer$.subscribe(loadJobsListener);
subscriptions.add(explorer$.subscribe(loadJobsListener));
// Listen for changes to job selection.
$scope.jobSelectionUpdateInProgress = false;
const jobSelectServiceSub = mlJobSelectService.subscribe(({ selection }) => {
if (selection !== undefined) {
$scope.jobSelectionUpdateInProgress = true;
jobSelectionUpdate(EXPLORER_ACTION.JOB_SELECTION_CHANGE, { fullJobs: mlJobService.jobs, selectedJobIds: selection });
}
});
// Refresh all the data when the time range is altered.
$scope.$listenAndDigestAsync(timefilter, 'fetch', () => {
if ($scope.jobSelectionUpdateInProgress === false) {
@ -285,8 +290,7 @@ module.controller('MlExplorerController', function (
});
$scope.$on('$destroy', () => {
explorerSubscriber.unsubscribe();
jobSelectServiceSub.unsubscribe();
subscriptions.unsubscribe();
refreshWatcher.cancel();
$(window).off('resize', jqueryRedrawOnResize);
// Cancel listening for updates to the global nav state.