From a0469c291630d998cc8983af1cdd3a195f00b1c6 Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Mon, 5 Feb 2024 14:37:15 -0800 Subject: [PATCH] [osci23] implmented advance setting in Dicover: MODIFY_COLUMN_ON_SWITCH (#5508) (#5638) * Revert "[Data Explorer][Discover 2.0] Fix issues when change index pattern (#4875) (#4885)" This reverts commit 0102a32dd007f90a85a140b80be8d1fb312f30cb. try revert: * [osci23] implmented advance setting in Dicover: MODIFY_COLUMN_ON_SWITCH Signed-off-by: qiwen li * fix deps Signed-off-by: qiwen li * Update CHANGELOG.md Signed-off-by: Qiwen Li * modify to match discover legacy behavior, columns from previous column are only shown in canvas area Signed-off-by: qiwen li * Update CHANGELOG.md Co-authored-by: Anan Zhuang Signed-off-by: Qiwen Li * removed unused variables, added comments and @param Signed-off-by: Qiwen Li --------- Signed-off-by: qiwen li Signed-off-by: Qiwen Li Signed-off-by: Anan Zhuang Signed-off-by: Ashwin P Chandran Co-authored-by: Anan Zhuang Co-authored-by: Ashwin P Chandran (cherry picked from commit 34994f816c79854efb26c91df045a79915d89383) Signed-off-by: github-actions[bot] # Conflicts: # CHANGELOG.md Co-authored-by: github-actions[bot] --- .../view_components/canvas/index.tsx | 5 +++-- .../utils/filter_columns.test.ts | 21 ++++++++++++++----- .../view_components/utils/filter_columns.ts | 13 ++++++++++-- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/plugins/discover/public/application/view_components/canvas/index.tsx b/src/plugins/discover/public/application/view_components/canvas/index.tsx index fabe2373bfea..d5c54158e997 100644 --- a/src/plugins/discover/public/application/view_components/canvas/index.tsx +++ b/src/plugins/discover/public/application/view_components/canvas/index.tsx @@ -18,7 +18,7 @@ import { setColumns, useDispatch, useSelector } from '../../utils/state_manageme import { DiscoverViewServices } from '../../../build_services'; import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public'; import { filterColumns } from '../utils/filter_columns'; -import { DEFAULT_COLUMNS_SETTING } from '../../../../common'; +import { DEFAULT_COLUMNS_SETTING, MODIFY_COLUMNS_ON_SWITCH } from '../../../../common'; import { OpenSearchSearchHit } from '../../../application/doc_views/doc_views_types'; import './discover_canvas.scss'; @@ -32,7 +32,8 @@ export default function DiscoverCanvas({ setHeaderActionMenu, history }: ViewPro const filteredColumns = filterColumns( columns, indexPattern, - uiSettings.get(DEFAULT_COLUMNS_SETTING) + uiSettings.get(DEFAULT_COLUMNS_SETTING), + uiSettings.get(MODIFY_COLUMNS_ON_SWITCH) ); const dispatch = useDispatch(); const prevIndexPattern = useRef(indexPattern); diff --git a/src/plugins/discover/public/application/view_components/utils/filter_columns.test.ts b/src/plugins/discover/public/application/view_components/utils/filter_columns.test.ts index c6694e141007..fe9f143cb030 100644 --- a/src/plugins/discover/public/application/view_components/utils/filter_columns.test.ts +++ b/src/plugins/discover/public/application/view_components/utils/filter_columns.test.ts @@ -13,21 +13,32 @@ describe('filterColumns', () => { }, } as IndexPattern; - it('should return columns that exist in the index pattern fields', () => { + it('should return columns that exist in the index pattern fields when MODIFY_COLUMN_ON_SWITCH is true', () => { const columns = ['a', 'b']; - const result = filterColumns(columns, indexPatternMock, ['a']); + const result = filterColumns(columns, indexPatternMock, ['a'], true); expect(result).toEqual(['a']); }); - it('should return defaultColumns if no columns exist in the index pattern fields', () => { + it('should return all of the columns when MODIFY_COLUMN_ON_SWITCH is false', () => { + const columns = ['a', 'b']; + const result = filterColumns(columns, indexPatternMock, ['a'], false); + expect(result).toEqual(['a', 'b']); + }); + + it('should return defualt columns if columns are empty', () => { + const result = filterColumns([], indexPatternMock, ['a'], false); + expect(result).toEqual(['_source']); + }); + + it('should return defaultColumns if no columns exist in the index pattern fields when MODIFY_COLUMN_ON_SWITCH is true', () => { const columns = ['b', 'e']; - const result = filterColumns(columns, indexPatternMock, ['e']); + const result = filterColumns(columns, indexPatternMock, ['e'], true); expect(result).toEqual(['_source']); }); it('should return defaultColumns if no columns and indexPattern is undefined', () => { const columns = ['b', 'e']; - const result = filterColumns(columns, undefined, ['a']); + const result = filterColumns(columns, undefined, ['a'], true); expect(result).toEqual(['_source']); }); }); diff --git a/src/plugins/discover/public/application/view_components/utils/filter_columns.ts b/src/plugins/discover/public/application/view_components/utils/filter_columns.ts index f9776eb0151e..604a1bfdce3b 100644 --- a/src/plugins/discover/public/application/view_components/utils/filter_columns.ts +++ b/src/plugins/discover/public/application/view_components/utils/filter_columns.ts @@ -4,6 +4,7 @@ */ import { IndexPattern } from '../../../opensearch_dashboards_services'; +import { buildColumns } from '../../utils/columns'; /** * Helper function to filter columns based on the fields of the index pattern. @@ -13,15 +14,23 @@ import { IndexPattern } from '../../../opensearch_dashboards_services'; * @param columns Array of column names * @param indexPattern Index pattern object * @param defaultColumns Array of default columns + * @param modifyColumn Booelan of 'discover:modifyColumnsOnSwitch' */ export function filterColumns( columns: string[], indexPattern: IndexPattern | undefined, - defaultColumns: string[] + defaultColumns: string[], + modifyColumn: boolean ) { + // if false, we keep all the chosen columns + if (!modifyColumn) { + return columns.length > 0 ? columns : ['_source']; + } + // if true, we keep columns that exist in the new index pattern const fieldsName = indexPattern?.fields.getAll().map((fld) => fld.name) || []; // combine columns and defaultColumns without duplicates const combinedColumns = [...new Set([...columns, ...defaultColumns])]; const filteredColumns = combinedColumns.filter((column) => fieldsName.includes(column)); - return filteredColumns.length > 0 ? filteredColumns : ['_source']; + const adjustedColumns = buildColumns(filteredColumns); + return adjustedColumns.length > 0 ? adjustedColumns : ['_source']; }