-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
kertal
merged 13 commits into
elastic:master
from
kertal:kertal-pr-2020-08-05-discover-modify-columns-on-index-pattern-switch
Oct 6, 2020
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fb052cf
Modify columns and sort when switching index pattern
kertal 4602ea2
Omit setting local state on index pattern switch
kertal c6935eb
Merge branch 'master' into kertal-pr-2020-08-05-discover-modify-colum…
elasticmachine bce18c9
Merge remote-tracking branch 'upstream/master' into kertal-pr-2020-08…
kertal 92f8773
Add unit tests
kertal 15f9b75
Merge branch 'kertal-pr-2020-08-05-discover-modify-columns-on-index-p…
kertal 089fc85
Merge master / fix conflicts
kertal 10a4df7
Merge remote-tracking branch 'upstream/master' into kertal-pr-2020-08…
kertal 36d9b38
Add uiSetting to allow switch to legacy behavior
kertal df08d57
Change title of ui settings
kertal 445f050
Update ui_settings.ts
kertal fbc3f9e
Change linting error
kertal b8827e6
Merge branch 'master' into kertal-pr-2020-08-05-discover-modify-colum…
kibanamachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/plugins/discover/public/application/helpers/get_switch_index_pattern_app_state.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () => { | ||
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']]); | ||
}); | ||
}); |
46 changes: 46 additions & 0 deletions
46
src/plugins/discover/public/application/helpers/get_switch_index_pattern_app_state.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.