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
84 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
34 changes: 34 additions & 0 deletions
34
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,34 @@ | ||
/* | ||
* 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; | ||
const defaultColumns = ['_defaultColumn']; | ||
|
||
it('should return columns that exist in the index pattern fields', () => { | ||
const columns = ['a', 'b']; | ||
const result = filterColumns(columns, indexPatternMock, defaultColumns); | ||
expect(result).toEqual(['a']); | ||
}); | ||
|
||
it('should return defaultColumns if no columns exist in the index pattern fields', () => { | ||
const columns = ['b', 'e']; | ||
const result = filterColumns(columns, indexPatternMock, defaultColumns); | ||
expect(result).toEqual(defaultColumns); | ||
}); | ||
|
||
it('should return defaultColumns if no columns and indexPattern is null', () => { | ||
const columns = ['b', 'e']; | ||
const result = filterColumns(columns, null, defaultColumns); | ||
expect(result).toEqual(defaultColumns); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
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,25 @@ | ||
/* | ||
* 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 the default column. | ||
* @param columns Array of column names | ||
* @param indexPattern Index pattern object | ||
* @param defaultColumns Array of default columns | ||
*/ | ||
export function filterColumns( | ||
columns: string[], | ||
indexPattern: IndexPattern, | ||
defaultColumns: string[] | ||
) { | ||
const fieldsName = indexPattern?.fields.getAll().map((fld) => fld.name) || []; | ||
const filteredColumns = columns.filter((column) => fieldsName.includes(column)); | ||
return filteredColumns.length > 0 ? filteredColumns : defaultColumns; | ||
} |