Skip to content

Commit d33b58e

Browse files
committed
Discover: Add handling for source column (#91815)
1 parent dcf6336 commit d33b58e

10 files changed

Lines changed: 104 additions & 15 deletions

File tree

src/plugins/discover/public/application/angular/context.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ function ContextAppRouteController($routeParams, $scope, $route) {
6565
storeInSessionStorage: getServices().uiSettings.get('state:storeInSessionStorage'),
6666
history: getServices().history(),
6767
toasts: getServices().core.notifications.toasts,
68+
uiSettings: getServices().core.uiSettings,
6869
});
6970
this.state = { ...appState.getState() };
7071
this.anchorId = $routeParams.id;

src/plugins/discover/public/application/angular/context_state.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
* Side Public License, v 1.
77
*/
88

9+
import { IUiSettingsClient } from 'kibana/public';
910
import { getState } from './context_state';
1011
import { createBrowserHistory, History } from 'history';
1112
import { FilterManager, Filter } from '../../../../data/public';
1213
import { coreMock } from '../../../../../core/public/mocks';
14+
import { SEARCH_FIELDS_FROM_SOURCE } from '../../../common';
1315
const setupMock = coreMock.createSetup();
1416

1517
describe('Test Discover Context State', () => {
@@ -23,6 +25,10 @@ describe('Test Discover Context State', () => {
2325
defaultStepSize: '4',
2426
timeFieldName: 'time',
2527
history,
28+
uiSettings: {
29+
get: <T>(key: string) =>
30+
((key === SEARCH_FIELDS_FROM_SOURCE ? true : ['_source']) as unknown) as T,
31+
} as IUiSettingsClient,
2632
});
2733
state.startSync();
2834
});

src/plugins/discover/public/application/angular/context_state.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import _ from 'lodash';
1010
import { History } from 'history';
11-
import { NotificationsStart } from 'kibana/public';
11+
import { NotificationsStart, IUiSettingsClient } from 'kibana/public';
1212
import {
1313
createStateContainer,
1414
createKbnUrlStateStorage,
@@ -17,6 +17,7 @@ import {
1717
withNotifyOnErrors,
1818
} from '../../../../kibana_utils/public';
1919
import { esFilters, FilterManager, Filter, Query } from '../../../../data/public';
20+
import { handleSourceColumnState } from './helpers';
2021

2122
export interface AppState {
2223
/**
@@ -73,6 +74,11 @@ interface GetStateParams {
7374
* kbnUrlStateStorage will use it notifying about inner errors
7475
*/
7576
toasts?: NotificationsStart['toasts'];
77+
78+
/**
79+
* core ui settings service
80+
*/
81+
uiSettings: IUiSettingsClient;
7682
}
7783

7884
interface GetStateReturn {
@@ -123,6 +129,7 @@ export function getState({
123129
storeInSessionStorage = false,
124130
history,
125131
toasts,
132+
uiSettings,
126133
}: GetStateParams): GetStateReturn {
127134
const stateStorage = createKbnUrlStateStorage({
128135
useHash: storeInSessionStorage,
@@ -134,7 +141,12 @@ export function getState({
134141
const globalStateContainer = createStateContainer<GlobalState>(globalStateInitial);
135142

136143
const appStateFromUrl = stateStorage.get(APP_STATE_URL_KEY) as AppState;
137-
const appStateInitial = createInitialAppState(defaultStepSize, timeFieldName, appStateFromUrl);
144+
const appStateInitial = createInitialAppState(
145+
defaultStepSize,
146+
timeFieldName,
147+
appStateFromUrl,
148+
uiSettings
149+
);
138150
const appStateContainer = createStateContainer<AppState>(appStateInitial);
139151

140152
const { start, stop } = syncStates([
@@ -257,7 +269,8 @@ function getFilters(state: AppState | GlobalState): Filter[] {
257269
function createInitialAppState(
258270
defaultSize: string,
259271
timeFieldName: string,
260-
urlState: AppState
272+
urlState: AppState,
273+
uiSettings: IUiSettingsClient
261274
): AppState {
262275
const defaultState = {
263276
columns: ['_source'],
@@ -270,8 +283,11 @@ function createInitialAppState(
270283
return defaultState;
271284
}
272285

273-
return {
274-
...defaultState,
275-
...urlState,
276-
};
286+
return handleSourceColumnState(
287+
{
288+
...defaultState,
289+
...urlState,
290+
},
291+
uiSettings
292+
);
277293
}

src/plugins/discover/public/application/angular/discover.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ app.config(($routeProvider) => {
110110
const history = getHistory();
111111
const savedSearchId = $route.current.params.id;
112112
return data.indexPatterns.ensureDefaultIndexPattern(history).then(() => {
113-
const { appStateContainer } = getState({ history });
113+
const { appStateContainer } = getState({ history, uiSettings: config });
114114
const { index } = appStateContainer.getState();
115115
return Promise.props({
116116
ip: loadIndexPattern(index, data.indexPatterns, config),
@@ -195,6 +195,7 @@ function discoverController($route, $scope, Promise) {
195195
storeInSessionStorage: config.get('state:storeInSessionStorage'),
196196
history,
197197
toasts: core.notifications.toasts,
198+
uiSettings: config,
198199
});
199200

200201
const {

src/plugins/discover/public/application/angular/discover_state.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Side Public License, v 1.
77
*/
88

9+
import { IUiSettingsClient } from 'kibana/public';
910
import {
1011
getState,
1112
GetStateReturn,
@@ -14,18 +15,25 @@ import {
1415
import { createBrowserHistory, History } from 'history';
1516
import { dataPluginMock } from '../../../../data/public/mocks';
1617
import { SavedSearch } from '../../saved_searches';
18+
import { SEARCH_FIELDS_FROM_SOURCE } from '../../../common';
1719

1820
let history: History;
1921
let state: GetStateReturn;
2022
const getCurrentUrl = () => history.createHref(history.location);
2123

24+
const uiSettingsMock = {
25+
get: <T>(key: string) =>
26+
((key === SEARCH_FIELDS_FROM_SOURCE ? true : ['_source']) as unknown) as T,
27+
} as IUiSettingsClient;
28+
2229
describe('Test discover state', () => {
2330
beforeEach(async () => {
2431
history = createBrowserHistory();
2532
history.push('/');
2633
state = getState({
2734
getStateDefaults: () => ({ index: 'test' }),
2835
history,
36+
uiSettings: uiSettingsMock,
2937
});
3038
await state.replaceUrlAppState({});
3139
await state.startSync();
@@ -81,6 +89,7 @@ describe('Test discover state with legacy migration', () => {
8189
state = getState({
8290
getStateDefaults: () => ({ index: 'test' }),
8391
history,
92+
uiSettings: uiSettingsMock,
8493
});
8594
expect(state.appStateContainer.getState()).toMatchInlineSnapshot(`
8695
Object {
@@ -106,6 +115,7 @@ describe('createSearchSessionRestorationDataProvider', () => {
106115
data: mockDataPlugin,
107116
appStateContainer: getState({
108117
history: createBrowserHistory(),
118+
uiSettings: uiSettingsMock,
109119
}).appStateContainer,
110120
getSavedSearch: () => mockSavedSearch,
111121
});

src/plugins/discover/public/application/angular/discover_state.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import { isEqual } from 'lodash';
1010
import { i18n } from '@kbn/i18n';
1111
import { History } from 'history';
12-
import { NotificationsStart } from 'kibana/public';
12+
import { NotificationsStart, IUiSettingsClient } from 'kibana/public';
1313
import {
1414
createKbnUrlStateStorage,
1515
createStateContainer,
@@ -30,6 +30,7 @@ import { migrateLegacyQuery } from '../helpers/migrate_legacy_query';
3030
import { DiscoverGridSettings } from '../components/discover_grid/types';
3131
import { DISCOVER_APP_URL_GENERATOR, DiscoverUrlGeneratorState } from '../../url_generator';
3232
import { SavedSearch } from '../../saved_searches';
33+
import { handleSourceColumnState } from './helpers';
3334

3435
export interface AppState {
3536
/**
@@ -90,6 +91,11 @@ interface GetStateParams {
9091
* kbnUrlStateStorage will use it notifying about inner errors
9192
*/
9293
toasts?: NotificationsStart['toasts'];
94+
95+
/**
96+
* core ui settings service
97+
*/
98+
uiSettings: IUiSettingsClient;
9399
}
94100

95101
export interface GetStateReturn {
@@ -149,6 +155,7 @@ export function getState({
149155
storeInSessionStorage = false,
150156
history,
151157
toasts,
158+
uiSettings,
152159
}: GetStateParams): GetStateReturn {
153160
const defaultAppState = getStateDefaults ? getStateDefaults() : {};
154161
const stateStorage = createKbnUrlStateStorage({
@@ -163,10 +170,14 @@ export function getState({
163170
appStateFromUrl.query = migrateLegacyQuery(appStateFromUrl.query);
164171
}
165172

166-
let initialAppState = {
167-
...defaultAppState,
168-
...appStateFromUrl,
169-
};
173+
let initialAppState = handleSourceColumnState(
174+
{
175+
...defaultAppState,
176+
...appStateFromUrl,
177+
},
178+
uiSettings
179+
);
180+
// todo filter source depending on fields fetchinbg flag (if no columns remain and source fetching is enabled, use default columns)
170181
let previousAppState: AppState;
171182
const appStateContainer = createStateContainer<AppState>(initialAppState);
172183

src/plugins/discover/public/application/angular/helpers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88

99
export { buildPointSeriesData } from './point_series';
1010
export { formatRow } from './row_formatter';
11+
export { handleSourceColumnState } from './state_helpers';
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
import { IUiSettingsClient } from 'src/core/public';
10+
import { SEARCH_FIELDS_FROM_SOURCE, DEFAULT_COLUMNS_SETTING } from '../../../../common';
11+
12+
/**
13+
* Makes sure the current state is not referencing the source column when using the fields api
14+
* @param state
15+
* @param uiSettings
16+
*/
17+
export function handleSourceColumnState<TState extends { columns?: string[] }>(
18+
state: TState,
19+
uiSettings: IUiSettingsClient
20+
): TState {
21+
if (!state.columns) {
22+
return state;
23+
}
24+
const useNewFieldsApi = !uiSettings.get(SEARCH_FIELDS_FROM_SOURCE);
25+
const defaultColumns = uiSettings.get(DEFAULT_COLUMNS_SETTING);
26+
if (useNewFieldsApi) {
27+
// if fields API is used, filter out the source column
28+
return {
29+
...state,
30+
columns: state.columns.filter((column) => column !== '_source'),
31+
};
32+
} else if (state.columns.length === 0) {
33+
// if _source fetching is used and there are no column, switch back to default columns
34+
// this can happen if the fields API was previously used
35+
return {
36+
...state,
37+
columns: [...defaultColumns],
38+
};
39+
}
40+
41+
return state;
42+
}

src/plugins/discover/public/application/components/discover_grid/discover_grid.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@ export const DiscoverGrid = ({
315315
<DiscoverGridFlyout
316316
indexPattern={indexPattern}
317317
hit={expandedDoc}
318-
columns={columns}
318+
// if default columns are used, dont make them part of the URL - the context state handling will take care to restore them
319+
columns={defaultColumns ? [] : columns}
319320
onFilter={onFilter}
320321
onRemoveColumn={onRemoveColumn}
321322
onAddColumn={onAddColumn}

test/functional/apps/discover/_shared_links.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
7777
'/app/discover?_t=1453775307251#' +
7878
'/?_g=(filters:!(),refreshInterval:(pause:!t,value:0),time' +
7979
":(from:'2015-09-19T06:31:44.000Z',to:'2015-09" +
80-
"-23T18:31:44.000Z'))&_a=(columns:!(_source),filters:!(),index:'logstash-" +
80+
"-23T18:31:44.000Z'))&_a=(columns:!(),filters:!(),index:'logstash-" +
8181
"*',interval:auto,query:(language:kuery,query:'')" +
8282
",sort:!(!('@timestamp',desc)))";
8383
const actualUrl = await PageObjects.share.getSharedUrl();

0 commit comments

Comments
 (0)