forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Data Explorer][Discover 2.0] Fix issues when change index pattern
* allow side nav show only selected index pattern fields when switch * allow reset column state when switch index pattern Issue Resolve: opensearch-project#4840 opensearch-project#4846 Signed-off-by: ananzh <[email protected]>
- Loading branch information
Showing
4 changed files
with
72 additions
and
15 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
33 changes: 33 additions & 0 deletions
33
src/plugins/discover/public/application/view_components/utils/filter_columns.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,33 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { filterColumns } from './filter_columns'; | ||
import { IndexPattern } from '../../../opensearch_dashboards_services'; | ||
|
||
describe('filterColumns', () => { | ||
const indexPatternMock = { | ||
fields: { | ||
getAll: () => [{ name: 'a' }, { name: 'c' }, { name: 'd' }], | ||
}, | ||
} as IndexPattern; | ||
|
||
it('should return columns that exist in the index pattern fields', () => { | ||
const columns = ['a', 'b']; | ||
const result = filterColumns(columns, indexPatternMock); | ||
expect(result).toEqual(['a']); | ||
}); | ||
|
||
it('should return _source if no columns exist in the index pattern fields', () => { | ||
const columns = ['b', 'e']; | ||
const result = filterColumns(columns, indexPatternMock); | ||
expect(result).toEqual(['_source']); | ||
}); | ||
|
||
it('should return empty array if no columns and indexPattern is null', () => { | ||
const columns = ['b', 'e']; | ||
const result = filterColumns(columns, null); | ||
expect(result).toEqual(['_source']); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
src/plugins/discover/public/application/view_components/utils/filter_columns.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,20 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { IndexPattern } from '../../../opensearch_dashboards_services'; | ||
|
||
/** | ||
* Helper function to filter columns based on the fields of the index pattern. | ||
* This function is used when we switch between index patterns. We want to keep the columns that are | ||
* still available in the new index pattern and remove the ones that are not. | ||
* If the resulting array is empty, it provides a fallback to a single '_source' column. | ||
* @param columns Array of column names | ||
* @param indexPattern Index pattern object | ||
*/ | ||
export function filterColumns(columns: string[], indexPattern: IndexPattern) { | ||
const fieldsName = indexPattern?.fields.getAll().map((fld) => fld.name) || []; | ||
const filteredColumns = columns.filter((column) => fieldsName.includes(column)); | ||
return filteredColumns.length > 0 ? filteredColumns : ['_source']; | ||
} |