Skip to content

Commit

Permalink
[osci23] implmented advance setting in Dicover: MODIFY_COLUMN_ON_SWIT…
Browse files Browse the repository at this point in the history
…CH (#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>
  • Loading branch information
3 people committed Dec 21, 2023
1 parent 45e867f commit 34994f8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [BUG][Data] Support for custom filters with heterogeneous data fields ([5577](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5577))
- [BUG][Data] Fix empty suggestion history when querying in search bar [#5349](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5349)
- [BUG][Discover] Fix what is displayed in `selected fields` when removing columns from canvas [#5537](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5537)
- [BUG][Discover] Fix advanced setting `discover:modifyColumnsOnSwitch` ([#5508](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5508))
- [Discover] Fix missing index pattern field from breaking Discover [#5626](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5626)

### 🚞 Infrastructure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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'];
}

0 comments on commit 34994f8

Please sign in to comment.