Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Discover] Modify columns and sort when switching index pattern #74336

Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/plugins/discover/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ export const FIELDS_LIMIT_SETTING = 'fields:popularLimit';
export const CONTEXT_DEFAULT_SIZE_SETTING = 'context:defaultSize';
export const CONTEXT_STEP_SETTING = 'context:step';
export const CONTEXT_TIE_BREAKER_FIELDS_SETTING = 'context:tieBreakerFields';
export const MODIFY_COLUMNS_ON_SWITCH = 'discover:modifyColumnsOnSwitch';
22 changes: 19 additions & 3 deletions src/plugins/discover/public/application/angular/discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const {
import { getRootBreadcrumbs, getSavedSearchBreadcrumbs } from '../helpers/breadcrumbs';
import { validateTimeRange } from '../helpers/validate_time_range';
import { popularizeField } from '../helpers/popularize_field';

import { getSwitchIndexPatternAppState } from '../helpers/get_switch_index_pattern_app_state';
import { getIndexPatternId } from '../helpers/get_index_pattern_id';
import { addFatalError } from '../../../../kibana_legacy/public';
import {
Expand All @@ -80,6 +80,7 @@ import {
SORT_DEFAULT_ORDER_SETTING,
SEARCH_ON_PAGE_LOAD_SETTING,
DOC_HIDE_TIME_COLUMN_SETTING,
MODIFY_COLUMNS_ON_SWITCH,
} from '../../../common';

const fetchStatuses = {
Expand Down Expand Up @@ -253,6 +254,11 @@ function discoverController($element, $route, $scope, $timeout, $window, Promise

if (!_.isEqual(newStatePartial, oldStatePartial)) {
$scope.$evalAsync(async () => {
if (oldStatePartial.index !== newStatePartial.index) {
//in case of index switch the route has currently to be reloaded, legacy
return;
}

$scope.state = { ...newState };

// detect changes that should trigger fetching of new data
Expand All @@ -277,8 +283,18 @@ function discoverController($element, $route, $scope, $timeout, $window, Promise
});

$scope.setIndexPattern = async (id) => {
await replaceUrlAppState({ index: id });
$route.reload();
const nextIndexPattern = await indexPatterns.get(id);
if (nextIndexPattern) {
const nextAppState = getSwitchIndexPatternAppState(
$scope.indexPattern,
nextIndexPattern,
$scope.state.columns,
$scope.state.sort,
config.get(MODIFY_COLUMNS_ON_SWITCH)
);
await replaceUrlAppState(nextAppState);
$route.reload();
}
};

// update data source when filters update
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { getSwitchIndexPatternAppState } from './get_switch_index_pattern_app_state';
import { IIndexPatternFieldList, IndexPattern } from '../../../../data/common/index_patterns';

const currentIndexPattern: IndexPattern = {
id: 'prev',
getFieldByName(name) {
return this.fields.getByName(name);
},
fields: {
getByName: (name: string) => {
const fields = [
{ name: 'category', sortable: true },
{ name: 'name', sortable: true },
] as IIndexPatternFieldList;
return fields.find((field) => field.name === name);
},
},
} as IndexPattern;

const nextIndexPattern = {
id: 'next',
getFieldByName(name) {
return this.fields.getByName(name);
},
fields: {
getByName: (name: string) => {
const fields = [{ name: 'category', sortable: true }] as IIndexPatternFieldList;
return fields.find((field) => field.name === name);
},
},
} as IndexPattern;

describe('Discover getSwitchIndexPatternAppState', () => {
test('removing fields that are not part of the next index pattern, keeping unknown fields ', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the reasoning behind this? How would a user run into this case and why would they want to keep the unknown fields?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, imagine you have 2 ecommerce index patterns, and you've selected products, then switching index patterns wouldn't remove them.
Bildschirmfoto 2020-10-05 um 17 56 22

const result = getSwitchIndexPatternAppState(
currentIndexPattern,
nextIndexPattern,
['category', 'name', 'unknown'],
[['category', 'desc']]
);
expect(result.columns).toEqual(['category', 'unknown']);
expect(result.sort).toEqual([['category', 'desc']]);
});
test('removing sorted by fields that are not part of the next index pattern', async () => {
const result = getSwitchIndexPatternAppState(
currentIndexPattern,
nextIndexPattern,
['name'],
[
['category', 'desc'],
['name', 'asc'],
]
);
expect(result.columns).toEqual(['_source']);
expect(result.sort).toEqual([['category', 'desc']]);
});
test('removing sorted by fields that without modifying columns', async () => {
const result = getSwitchIndexPatternAppState(
currentIndexPattern,
nextIndexPattern,
['name'],
[
['category', 'desc'],
['name', 'asc'],
],
false
);
expect(result.columns).toEqual(['name']);
expect(result.sort).toEqual([['category', 'desc']]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { getSortArray } from '../angular/doc_table';
import { SortPairArr } from '../angular/doc_table/lib/get_sort';
import { IndexPattern } from '../../kibana_services';

/**
* Helper function to remove or adapt the currently selected columns/sort to be valid with the next
* index pattern, returns a new state object
*/
export function getSwitchIndexPatternAppState(
currentIndexPattern: IndexPattern,
nextIndexPattern: IndexPattern,
currentColumns: string[],
currentSort: SortPairArr[],
modifyColumns: boolean = true
) {
const nextColumns = modifyColumns
? currentColumns.filter(
(column) =>
nextIndexPattern.fields.getByName(column) || !currentIndexPattern.fields.getByName(column)
)
: currentColumns;
const nextSort = getSortArray(currentSort, nextIndexPattern);
return {
index: nextIndexPattern.id,
columns: nextColumns.length ? nextColumns : ['_source'],
sort: nextSort,
};
}
14 changes: 14 additions & 0 deletions src/plugins/discover/server/ui_settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
CONTEXT_DEFAULT_SIZE_SETTING,
CONTEXT_STEP_SETTING,
CONTEXT_TIE_BREAKER_FIELDS_SETTING,
MODIFY_COLUMNS_ON_SWITCH,
} from '../common';

export const uiSettings: Record<string, UiSettingsParams> = {
Expand Down Expand Up @@ -163,4 +164,17 @@ export const uiSettings: Record<string, UiSettingsParams> = {
category: ['discover'],
schema: schema.arrayOf(schema.string()),
},
[MODIFY_COLUMNS_ON_SWITCH]: {
name: i18n.translate('discover.advancedSettings.discover.modifyColumnsOnSwitchTitle', {
defaultMessage: 'Modify columns when index pattern is switched',
}),
value: true,
description: i18n.translate('discover.advancedSettings.discover.modifyColumnsOnSwitchText', {
defaultMessage:
'When switching index patterns, selected columns, that are not available in the ' +
'next index pattern, are removed.',
}),
category: ['discover'],
schema: schema.boolean(),
},
kertal marked this conversation as resolved.
Show resolved Hide resolved
};