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) (#5746)

* 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 <[email protected]>

* fix deps

Signed-off-by: qiwen li <[email protected]>

* Update CHANGELOG.md

Signed-off-by: Qiwen Li <[email protected]>

* modify to match discover legacy behavior, columns from previous column are only shown in canvas area

Signed-off-by: qiwen li <[email protected]>

* Update CHANGELOG.md

Co-authored-by: Anan Zhuang <[email protected]>
Signed-off-by: Qiwen Li <[email protected]>

* removed unused variables, added comments and @param

Signed-off-by: Qiwen Li <[email protected]>

---------

Signed-off-by: qiwen li <[email protected]>
Signed-off-by: Qiwen Li <[email protected]>
Signed-off-by: Anan Zhuang <[email protected]>
Signed-off-by: Ashwin P Chandran <[email protected]>
Co-authored-by: Anan Zhuang <[email protected]>
Co-authored-by: Ashwin P Chandran <[email protected]>
(cherry picked from commit 34994f8)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

# Conflicts:
#	CHANGELOG.md

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent dae1ef3 commit 02c51f3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
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 02c51f3

Please sign in to comment.