-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Discover] Modify columns and sort when switching index pattern (#74336)
* Modify columns and sort when switching index pattern * Add unit tests * Add uiSetting to allow switch to legacy behavior
- Loading branch information
Showing
5 changed files
with
168 additions
and
3 deletions.
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