Skip to content

Commit 34994f8

Browse files
MadaniKKananzhashwin-pc
authored
[osci23] implmented advance setting in Dicover: MODIFY_COLUMN_ON_SWITCH (#5508)
* Revert "[Data Explorer][Discover 2.0] Fix issues when change index pattern (#4875) (#4885)" This reverts commit 0102a32. try revert: * [osci23] implmented advance setting in Dicover: MODIFY_COLUMN_ON_SWITCH Signed-off-by: qiwen li <qiwen_li@brown.edu> * fix deps Signed-off-by: qiwen li <qiwen_li@brown.edu> * Update CHANGELOG.md Signed-off-by: Qiwen Li <qiwen_li@brown.edu> * modify to match discover legacy behavior, columns from previous column are only shown in canvas area Signed-off-by: qiwen li <qiwen_li@brown.edu> * Update CHANGELOG.md Co-authored-by: Anan Zhuang <ananzh@amazon.com> Signed-off-by: Qiwen Li <qiwen_li@brown.edu> * removed unused variables, added comments and @param Signed-off-by: Qiwen Li <qiwen_li@brown.edu> --------- Signed-off-by: qiwen li <qiwen_li@brown.edu> Signed-off-by: Qiwen Li <qiwen_li@brown.edu> Signed-off-by: Anan Zhuang <ananzh@amazon.com> Signed-off-by: Ashwin P Chandran <ashwinpc@amazon.com> Co-authored-by: Anan Zhuang <ananzh@amazon.com> Co-authored-by: Ashwin P Chandran <ashwinpc@amazon.com>
1 parent 45e867f commit 34994f8

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
3939
- [BUG][Data] Support for custom filters with heterogeneous data fields ([5577](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5577))
4040
- [BUG][Data] Fix empty suggestion history when querying in search bar [#5349](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5349)
4141
- [BUG][Discover] Fix what is displayed in `selected fields` when removing columns from canvas [#5537](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5537)
42+
- [BUG][Discover] Fix advanced setting `discover:modifyColumnsOnSwitch` ([#5508](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5508))
4243
- [Discover] Fix missing index pattern field from breaking Discover [#5626](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5626)
4344

4445
### 🚞 Infrastructure

src/plugins/discover/public/application/view_components/canvas/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { setColumns, useDispatch, useSelector } from '../../utils/state_manageme
1818
import { DiscoverViewServices } from '../../../build_services';
1919
import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public';
2020
import { filterColumns } from '../utils/filter_columns';
21-
import { DEFAULT_COLUMNS_SETTING } from '../../../../common';
21+
import { DEFAULT_COLUMNS_SETTING, MODIFY_COLUMNS_ON_SWITCH } from '../../../../common';
2222
import { OpenSearchSearchHit } from '../../../application/doc_views/doc_views_types';
2323
import './discover_canvas.scss';
2424

@@ -32,7 +32,8 @@ export default function DiscoverCanvas({ setHeaderActionMenu, history }: ViewPro
3232
const filteredColumns = filterColumns(
3333
columns,
3434
indexPattern,
35-
uiSettings.get(DEFAULT_COLUMNS_SETTING)
35+
uiSettings.get(DEFAULT_COLUMNS_SETTING),
36+
uiSettings.get(MODIFY_COLUMNS_ON_SWITCH)
3637
);
3738
const dispatch = useDispatch();
3839
const prevIndexPattern = useRef(indexPattern);

src/plugins/discover/public/application/view_components/utils/filter_columns.test.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,32 @@ describe('filterColumns', () => {
1313
},
1414
} as IndexPattern;
1515

16-
it('should return columns that exist in the index pattern fields', () => {
16+
it('should return columns that exist in the index pattern fields when MODIFY_COLUMN_ON_SWITCH is true', () => {
1717
const columns = ['a', 'b'];
18-
const result = filterColumns(columns, indexPatternMock, ['a']);
18+
const result = filterColumns(columns, indexPatternMock, ['a'], true);
1919
expect(result).toEqual(['a']);
2020
});
2121

22-
it('should return defaultColumns if no columns exist in the index pattern fields', () => {
22+
it('should return all of the columns when MODIFY_COLUMN_ON_SWITCH is false', () => {
23+
const columns = ['a', 'b'];
24+
const result = filterColumns(columns, indexPatternMock, ['a'], false);
25+
expect(result).toEqual(['a', 'b']);
26+
});
27+
28+
it('should return defualt columns if columns are empty', () => {
29+
const result = filterColumns([], indexPatternMock, ['a'], false);
30+
expect(result).toEqual(['_source']);
31+
});
32+
33+
it('should return defaultColumns if no columns exist in the index pattern fields when MODIFY_COLUMN_ON_SWITCH is true', () => {
2334
const columns = ['b', 'e'];
24-
const result = filterColumns(columns, indexPatternMock, ['e']);
35+
const result = filterColumns(columns, indexPatternMock, ['e'], true);
2536
expect(result).toEqual(['_source']);
2637
});
2738

2839
it('should return defaultColumns if no columns and indexPattern is undefined', () => {
2940
const columns = ['b', 'e'];
30-
const result = filterColumns(columns, undefined, ['a']);
41+
const result = filterColumns(columns, undefined, ['a'], true);
3142
expect(result).toEqual(['_source']);
3243
});
3344
});

src/plugins/discover/public/application/view_components/utils/filter_columns.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import { IndexPattern } from '../../../opensearch_dashboards_services';
7+
import { buildColumns } from '../../utils/columns';
78

89
/**
910
* Helper function to filter columns based on the fields of the index pattern.
@@ -13,15 +14,23 @@ import { IndexPattern } from '../../../opensearch_dashboards_services';
1314
* @param columns Array of column names
1415
* @param indexPattern Index pattern object
1516
* @param defaultColumns Array of default columns
17+
* @param modifyColumn Booelan of 'discover:modifyColumnsOnSwitch'
1618
*/
1719
export function filterColumns(
1820
columns: string[],
1921
indexPattern: IndexPattern | undefined,
20-
defaultColumns: string[]
22+
defaultColumns: string[],
23+
modifyColumn: boolean
2124
) {
25+
// if false, we keep all the chosen columns
26+
if (!modifyColumn) {
27+
return columns.length > 0 ? columns : ['_source'];
28+
}
29+
// if true, we keep columns that exist in the new index pattern
2230
const fieldsName = indexPattern?.fields.getAll().map((fld) => fld.name) || [];
2331
// combine columns and defaultColumns without duplicates
2432
const combinedColumns = [...new Set([...columns, ...defaultColumns])];
2533
const filteredColumns = combinedColumns.filter((column) => fieldsName.includes(column));
26-
return filteredColumns.length > 0 ? filteredColumns : ['_source'];
34+
const adjustedColumns = buildColumns(filteredColumns);
35+
return adjustedColumns.length > 0 ? adjustedColumns : ['_source'];
2736
}

0 commit comments

Comments
 (0)