Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[osci23] implmented advance setting in Dicover: MODIFY_COLUMN_ON_SWITCH #5508

Merged
merged 13 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
MadaniKK marked this conversation as resolved.
Show resolved Hide resolved
columns: string[],
indexPattern: IndexPattern | undefined,
defaultColumns: string[]
defaultColumns: string[],
modifyColumn: boolean
) {
// if false, we keep all the chosen columns
if (!modifyColumn) {
MadaniKK marked this conversation as resolved.
Show resolved Hide resolved
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'];
}
Loading