Ensure originally selected job is retained on switch to singleMetricViewer (#36385) (#36398)

This commit is contained in:
Melissa Alvarez 2019-05-09 17:10:21 -04:00 committed by GitHub
parent 000102877c
commit c6bc1bec55
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 18 deletions

View file

@ -34,13 +34,6 @@ function getInvalidJobIds(ids) {
});
}
function checkGlobalState(globalState) {
if (globalState.ml === undefined) {
globalState.ml = {};
globalState.save();
}
}
function loadJobIdsFromGlobalState(globalState) { // jobIds, groups
const jobIds = [];
let groups = [];
@ -81,14 +74,15 @@ function loadJobIdsFromGlobalState(globalState) { // jobIds, groups
}
export function setGlobalState(globalState, { selectedIds, selectedGroups }) {
checkGlobalState(globalState);
if (globalState.ml === undefined) {
globalState.ml = {};
}
globalState.ml.jobIds = selectedIds;
globalState.ml.groups = selectedGroups;
globalState.ml.groups = selectedGroups || [];
globalState.save();
}
// called externally to retrieve the selected jobs ids.
// passing in `true` will load the jobs ids from the URL first
// called externally to retrieve the selected jobs ids
export function getSelectedJobIds(globalState) {
return loadJobIdsFromGlobalState(globalState);
}

View file

@ -196,7 +196,7 @@ module.controller('MlExplorerController', function (
// Listen for changes to job selection.
$scope.jobSelectionUpdateInProgress = false;
mlJobSelectService.subscribe(({ selection }) => {
const jobSelectServiceSub = mlJobSelectService.subscribe(({ selection }) => {
if (selection !== undefined) {
$scope.jobSelectionUpdateInProgress = true;
jobSelectionUpdate(EXPLORER_ACTION.JOB_SELECTION_CHANGE, { fullJobs: mlJobService.jobs, selectedJobIds: selection });
@ -286,6 +286,7 @@ module.controller('MlExplorerController', function (
$scope.$on('$destroy', () => {
explorerSubscriber.unsubscribe();
jobSelectServiceSub.unsubscribe();
refreshWatcher.cancel();
$(window).off('resize', jqueryRedrawOnResize);
// Cancel listening for updates to the global nav state.

View file

@ -87,11 +87,11 @@ module.controller('MlTimeSeriesExplorerController', function (
Private,
AppState,
config,
globalState,
i18n) {
$injector.get('mlSelectIntervalService');
$injector.get('mlSelectSeverityService');
const globalState = $injector.get('globalState');
const mlJobSelectService = $injector.get('mlJobSelectService');
$scope.timeFieldName = 'timestamp';
@ -175,7 +175,8 @@ module.controller('MlTimeSeriesExplorerController', function (
defaultMessage: 'You can only view one job at a time in this dashboard'
})
);
setGlobalState(globalState, [selectedJobIds[0]]);
setGlobalState(globalState, { selectedIds: [selectedJobIds[0]] });
mlJobSelectService.next({ selection: [selectedJobIds[0]], resetSelection: true });
} else {
// if a group has been loaded
@ -186,12 +187,13 @@ module.controller('MlTimeSeriesExplorerController', function (
defaultMessage: 'You can only view one job at a time in this dashboard'
})
);
setGlobalState(globalState, [selectedJobIds[0]]);
setGlobalState(globalState, { selectedIds: [selectedJobIds[0]] });
mlJobSelectService.next({ selection: [selectedJobIds[0]], resetSelection: true });
} else if ($scope.jobs.length > 0) {
// if there are no valid jobs in the group but there are valid jobs
// in the list of all jobs, select the first
setGlobalState(globalState, [$scope.jobs[0].id]);
setGlobalState(globalState, { selectedIds: [$scope.jobs[0].id] });
mlJobSelectService.next({ selection: [$scope.jobs[0].id], resetSelection: true });
} else {
// if there are no valid jobs left.
@ -201,7 +203,7 @@ module.controller('MlTimeSeriesExplorerController', function (
} else if (invalidIds.length > 0 && selectedJobIds.length > 0) {
// if some ids have been filtered out because they were invalid.
// refresh the URL with the first valid id
setGlobalState(globalState, [selectedJobIds[0]]);
setGlobalState(globalState, { selectedIds: [selectedJobIds[0]] });
mlJobSelectService.next({ selection: [selectedJobIds[0]], resetSelection: true });
} else if (selectedJobIds.length > 0) {
// normal behavior. a job ID has been loaded from the URL
@ -210,7 +212,7 @@ module.controller('MlTimeSeriesExplorerController', function (
if (selectedJobIds.length === 0 && $scope.jobs.length > 0) {
// no jobs were loaded from the URL, so add the first job
// from the full jobs list.
setGlobalState(globalState, [$scope.jobs[0].id]);
setGlobalState(globalState, { selectedIds: [$scope.jobs[0].id] });
mlJobSelectService.next({ selection: [$scope.jobs[0].id], resetSelection: true });
} else {
// Jobs exist, but no time series jobs.