Skip to content

Commit 6a78486

Browse files
committed
change how time range is restored and improve back button for drilldowns
1 parent 0deb6b4 commit 6a78486

File tree

4 files changed

+29
-35
lines changed

4 files changed

+29
-35
lines changed

src/legacy/core_plugins/kibana/public/dashboard/np_ready/dashboard_app_controller.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
IndexPattern,
3737
IndexPatternsContract,
3838
Query,
39+
QueryState,
3940
SavedQuery,
4041
syncQueryStateWithUrl,
4142
} from '../../../../../../plugins/data/public';
@@ -132,13 +133,6 @@ export class DashboardAppController {
132133
const queryFilter = filterManager;
133134
const timefilter = queryService.timefilter.timefilter;
134135

135-
// starts syncing `_g` portion of url with query services
136-
// note: dashboard_state_manager.ts syncs `_a` portion of url
137-
const {
138-
stop: stopSyncingQueryServiceStateWithUrl,
139-
hasInheritedQueryFromUrl: hasInheritedGlobalStateFromUrl,
140-
} = syncQueryStateWithUrl(queryService, kbnUrlStateStorage);
141-
142136
let lastReloadRequestTime = 0;
143137
const dash = ($scope.dash = $route.current.locals.dash);
144138
if (dash.id) {
@@ -170,9 +164,20 @@ export class DashboardAppController {
170164

171165
// The hash check is so we only update the time filter on dashboard open, not during
172166
// normal cross app navigation.
173-
if (dashboardStateManager.getIsTimeSavedWithDashboard() && !hasInheritedGlobalStateFromUrl) {
167+
const initialGlobalStateInUrl = kbnUrlStateStorage.get<QueryState>('_g');
168+
const hasInheritedTimeRange = Boolean(initialGlobalStateInUrl?.time);
169+
if (dashboardStateManager.getIsTimeSavedWithDashboard() && !hasInheritedTimeRange) {
174170
dashboardStateManager.syncTimefilterWithDashboard(timefilter);
175171
}
172+
// starts syncing `_g` portion of url with query services
173+
// note: dashboard_state_manager.ts syncs `_a` portion of url
174+
// it is important to start this syncing after `dashboardStateManager.syncTimefilterWithDashboard(timefilter);` above is run,
175+
// otherwise it will case redundant browser history record
176+
const { stop: stopSyncingQueryServiceStateWithUrl } = syncQueryStateWithUrl(
177+
queryService,
178+
kbnUrlStateStorage
179+
);
180+
176181
$scope.showSaveQuery = dashboardCapabilities.saveQuery as boolean;
177182

178183
const getShouldShowEditHelp = () =>

test/functional/apps/management/_kibana_settings.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ export default function({ getService, getPageObjects }) {
4646
});
4747

4848
describe('state:storeInSessionStorage', () => {
49+
async function getStateFromUrl() {
50+
const currentUrl = await browser.getCurrentUrl();
51+
let match = currentUrl.match(/(.*)?_g=(.*)&_a=(.*)/);
52+
if (match) return [match[2], match[3]];
53+
match = currentUrl.match(/(.*)?_a=(.*)&_g=(.*)/);
54+
if (match) return [match[3], match[2]];
55+
56+
if (!match) {
57+
throw new Error('State in url is missing or malformed');
58+
}
59+
}
60+
4961
it('defaults to null', async () => {
5062
await PageObjects.settings.clickKibanaSettings();
5163
const storeInSessionStorage = await PageObjects.settings.getAdvancedSettingCheckbox(
@@ -58,10 +70,7 @@ export default function({ getService, getPageObjects }) {
5870
await PageObjects.common.navigateToApp('dashboard');
5971
await PageObjects.dashboard.clickNewDashboard();
6072
await PageObjects.timePicker.setDefaultAbsoluteRange();
61-
const currentUrl = await browser.getCurrentUrl();
62-
const urlPieces = currentUrl.match(/(.*)?_g=(.*)&_a=(.*)/);
63-
const globalState = urlPieces[2];
64-
const appState = urlPieces[3];
73+
const [globalState, appState] = await getStateFromUrl();
6574

6675
// We don't have to be exact, just need to ensure it's greater than when the hashed variation is being used,
6776
// which is less than 20 characters.
@@ -83,10 +92,7 @@ export default function({ getService, getPageObjects }) {
8392
await PageObjects.common.navigateToApp('dashboard');
8493
await PageObjects.dashboard.clickNewDashboard();
8594
await PageObjects.timePicker.setDefaultAbsoluteRange();
86-
const currentUrl = await browser.getCurrentUrl();
87-
const urlPieces = currentUrl.match(/(.*)?_g=(.*)&_a=(.*)/);
88-
const globalState = urlPieces[2];
89-
const appState = urlPieces[3];
95+
const [globalState, appState] = await getStateFromUrl();
9096

9197
// We don't have to be exact, just need to ensure it's less than the unhashed version, which will be
9298
// greater than 20 characters with the default state plus a time.
@@ -100,10 +106,7 @@ export default function({ getService, getPageObjects }) {
100106
await PageObjects.settings.clickKibanaSettings();
101107
await PageObjects.settings.toggleAdvancedSettingCheckbox('state:storeInSessionStorage');
102108
await PageObjects.header.clickDashboard();
103-
const currentUrl = await browser.getCurrentUrl();
104-
const urlPieces = currentUrl.match(/(.*)?_g=(.*)&_a=(.*)/);
105-
const globalState = urlPieces[2];
106-
const appState = urlPieces[3];
109+
const [globalState, appState] = await getStateFromUrl();
107110
// We don't have to be exact, just need to ensure it's greater than when the hashed variation is being used,
108111
// which is less than 20 characters.
109112
expect(globalState.length).to.be.greaterThan(20);

x-pack/plugins/dashboard_enhanced/public/services/drilldowns/dashboard_to_dashboard_drilldown/drilldown.test.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ describe('.execute()', () => {
6868
const navigateToApp = jest.fn();
6969
const dataPluginActions = dataPluginMock.createStartContract().actions;
7070
const savedObjectsClient = savedObjectsServiceMock.createStartContract().client;
71-
savedObjectsClient.get = jest
72-
.fn()
73-
.mockReturnValue({ attributes: { from: 'now-15m', to: 'now' } });
7471

7572
const drilldown = new DashboardToDashboardDrilldown({
7673
getNavigateToApp: () => Promise.resolve(navigateToApp),

x-pack/plugins/dashboard_enhanced/public/services/drilldowns/dashboard_to_dashboard_drilldown/drilldown.tsx

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ export class DashboardToDashboardDrilldown
6060
) => {
6161
const getUrlGenerator = await this.params.getGetUrlGenerator();
6262
const navigateToApp = await this.params.getNavigateToApp();
63-
const savedObjectsClient = await this.params.getSavedObjectsClient();
6463

6564
const {
6665
selectRangeActionGetFilters,
@@ -72,16 +71,6 @@ export class DashboardToDashboardDrilldown
7271
filters: currentFilters,
7372
} = context.embeddable.getInput();
7473

75-
const savedDashboard = await savedObjectsClient.get<{ timeTo: string; timeFrom: string }>(
76-
'dashboard',
77-
config.dashboardId as string
78-
);
79-
80-
const defaultTimeRange = {
81-
to: savedDashboard.attributes.timeTo,
82-
from: savedDashboard.attributes.timeFrom,
83-
};
84-
8574
// if useCurrentDashboardFilters enabled, then preserve all the filters (pinned and unpinned)
8675
// otherwise preserve only pinned
8776
const filters =
@@ -92,7 +81,7 @@ export class DashboardToDashboardDrilldown
9281
// if useCurrentDashboardDataRange is enabled, then preserve current time range
9382
// if undefined is passed, then destination dashboard will figure out time range itself
9483
// for brush event this time range would be overwritten
95-
let timeRange = config.useCurrentDateRange ? currentTimeRange : defaultTimeRange;
84+
let timeRange = config.useCurrentDateRange ? currentTimeRange : undefined;
9685

9786
if (context.data.range) {
9887
// look up by range

0 commit comments

Comments
 (0)