Skip to content

Commit

Permalink
[Search Sessions] omit searchSessionId from the initialState, explici…
Browse files Browse the repository at this point in the history
…tly pause refreshInterval in restoreState (#88650)
  • Loading branch information
Dosant authored Jan 25, 2021
1 parent 9d7cac7 commit 403021f
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* and the Server Side Public License, v 1; you may not use this file except in
* compliance with, at your election, the Elastic License or the Server Side
* Public License, v 1.
*/

import { dataPluginMock } from '../../../../data/public/mocks';
import { createSessionRestorationDataProvider } from './session_restoration';
import { getAppStateDefaults } from './get_app_state_defaults';
import { getSavedDashboardMock } from '../test_helpers';
import { SavedObjectTagDecoratorTypeGuard } from '../../../../saved_objects_tagging_oss/public';

describe('createSessionRestorationDataProvider', () => {
const mockDataPlugin = dataPluginMock.createStartContract();
const searchSessionInfoProvider = createSessionRestorationDataProvider({
data: mockDataPlugin,
getAppState: () =>
getAppStateDefaults(
getSavedDashboardMock(),
false,
((() => false) as unknown) as SavedObjectTagDecoratorTypeGuard
),
getDashboardTitle: () => 'Dashboard',
getDashboardId: () => 'Id',
});

describe('session state', () => {
test('restoreState has sessionId and initialState has not', async () => {
const searchSessionId = 'id';
(mockDataPlugin.search.session.getSessionId as jest.Mock).mockImplementation(
() => searchSessionId
);
const { initialState, restoreState } = await searchSessionInfoProvider.getUrlGeneratorData();
expect(initialState.searchSessionId).toBeUndefined();
expect(restoreState.searchSessionId).toBe(searchSessionId);
});

test('restoreState has absoluteTimeRange', async () => {
const relativeTime = 'relativeTime';
const absoluteTime = 'absoluteTime';
(mockDataPlugin.query.timefilter.timefilter.getTime as jest.Mock).mockImplementation(
() => relativeTime
);
(mockDataPlugin.query.timefilter.timefilter.getAbsoluteTime as jest.Mock).mockImplementation(
() => absoluteTime
);
const { initialState, restoreState } = await searchSessionInfoProvider.getUrlGeneratorData();
expect(initialState.timeRange).toBe(relativeTime);
expect(restoreState.timeRange).toBe(absoluteTime);
});

test('restoreState has refreshInterval paused', async () => {
const { initialState, restoreState } = await searchSessionInfoProvider.getUrlGeneratorData();
expect(initialState.refreshInterval).toBeUndefined();
expect(restoreState.refreshInterval?.pause).toBe(true);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export function createSessionRestorationDataProvider(deps: {
getUrlGeneratorData: async () => {
return {
urlGeneratorId: DASHBOARD_APP_URL_GENERATOR,
initialState: getUrlGeneratorState({ ...deps, forceAbsoluteTime: false }),
restoreState: getUrlGeneratorState({ ...deps, forceAbsoluteTime: true }),
initialState: getUrlGeneratorState({ ...deps, shouldRestoreSearchSession: false }),
restoreState: getUrlGeneratorState({ ...deps, shouldRestoreSearchSession: true }),
};
},
};
Expand All @@ -32,20 +32,17 @@ function getUrlGeneratorState({
data,
getAppState,
getDashboardId,
forceAbsoluteTime,
shouldRestoreSearchSession,
}: {
data: DataPublicPluginStart;
getAppState: () => DashboardAppState;
getDashboardId: () => string;
/**
* Can force time range from time filter to convert from relative to absolute time range
*/
forceAbsoluteTime: boolean;
shouldRestoreSearchSession: boolean;
}): DashboardUrlGeneratorState {
const appState = getAppState();
return {
dashboardId: getDashboardId(),
timeRange: forceAbsoluteTime
timeRange: shouldRestoreSearchSession
? data.query.timefilter.timefilter.getAbsoluteTime()
: data.query.timefilter.timefilter.getTime(),
filters: data.query.filterManager.getFilters(),
Expand All @@ -55,6 +52,12 @@ function getUrlGeneratorState({
preserveSavedFilters: false,
viewMode: appState.viewMode,
panels: getDashboardId() ? undefined : appState.panels,
searchSessionId: data.search.session.getSessionId(),
searchSessionId: shouldRestoreSearchSession ? data.search.session.getSessionId() : undefined,
refreshInterval: shouldRestoreSearchSession
? {
pause: true, // force pause refresh interval when restoring a session
value: 0,
}
: undefined,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ describe('Test discover state with legacy migration', () => {

describe('createSearchSessionRestorationDataProvider', () => {
let mockSavedSearch: SavedSearch = ({} as unknown) as SavedSearch;
const mockDataPlugin = dataPluginMock.createStartContract();
const searchSessionInfoProvider = createSearchSessionRestorationDataProvider({
data: dataPluginMock.createStartContract(),
data: mockDataPlugin,
appStateContainer: getState({
history: createBrowserHistory(),
}).appStateContainer,
Expand All @@ -124,4 +125,30 @@ describe('createSearchSessionRestorationDataProvider', () => {
expect(await searchSessionInfoProvider.getName()).toBe('Discover');
});
});

describe('session state', () => {
test('restoreState has sessionId and initialState has not', async () => {
const searchSessionId = 'id';
(mockDataPlugin.search.session.getSessionId as jest.Mock).mockImplementation(
() => searchSessionId
);
const { initialState, restoreState } = await searchSessionInfoProvider.getUrlGeneratorData();
expect(initialState.searchSessionId).toBeUndefined();
expect(restoreState.searchSessionId).toBe(searchSessionId);
});

test('restoreState has absoluteTimeRange', async () => {
const relativeTime = 'relativeTime';
const absoluteTime = 'absoluteTime';
(mockDataPlugin.query.timefilter.timefilter.getTime as jest.Mock).mockImplementation(
() => relativeTime
);
(mockDataPlugin.query.timefilter.timefilter.getAbsoluteTime as jest.Mock).mockImplementation(
() => absoluteTime
);
const { initialState, restoreState } = await searchSessionInfoProvider.getUrlGeneratorData();
expect(initialState.timeRange).toBe(relativeTime);
expect(restoreState.timeRange).toBe(absoluteTime);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,12 @@ export function createSearchSessionRestorationDataProvider(deps: {
initialState: createUrlGeneratorState({
...deps,
getSavedSearchId,
forceAbsoluteTime: false,
shouldRestoreSearchSession: false,
}),
restoreState: createUrlGeneratorState({
...deps,
getSavedSearchId,
forceAbsoluteTime: true,
shouldRestoreSearchSession: true,
}),
};
},
Expand All @@ -291,26 +291,23 @@ function createUrlGeneratorState({
appStateContainer,
data,
getSavedSearchId,
forceAbsoluteTime,
shouldRestoreSearchSession,
}: {
appStateContainer: StateContainer<AppState>;
data: DataPublicPluginStart;
getSavedSearchId: () => string | undefined;
/**
* Can force time range from time filter to convert from relative to absolute time range
*/
forceAbsoluteTime: boolean;
shouldRestoreSearchSession: boolean;
}): DiscoverUrlGeneratorState {
const appState = appStateContainer.get();
return {
filters: data.query.filterManager.getFilters(),
indexPatternId: appState.index,
query: appState.query,
savedSearchId: getSavedSearchId(),
timeRange: forceAbsoluteTime
timeRange: shouldRestoreSearchSession
? data.query.timefilter.timefilter.getAbsoluteTime()
: data.query.timefilter.timefilter.getTime(),
searchSessionId: data.search.session.getSessionId(),
searchSessionId: shouldRestoreSearchSession ? data.search.session.getSessionId() : undefined,
columns: appState.columns,
sort: appState.sort,
savedQuery: appState.savedQuery,
Expand Down

0 comments on commit 403021f

Please sign in to comment.