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 7 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 @@ -31,6 +31,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [BUG] Add platform "darwin-arm64" to unit test ([#5290](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5290))
- [BUG][Dev Tool] Add dev tool documentation link to dev tool's help menu [#5166](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5166)
- Fix missing border for header navigation control on right ([#5450](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5450))
- [Discover] Fix advanced setting discover:modifyColumnsOnSwitch ([#5508](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5508))
MadaniKK marked this conversation as resolved.
Show resolved Hide resolved

### 🚞 Infrastructure

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import {
import { FormattedMessage, I18nProvider } from '@osd/i18n/react';
import { DiscoverField } from './discover_field';
import { DiscoverFieldSearch } from './discover_field_search';
import { FIELDS_LIMIT_SETTING } from '../../../../common';
import { FIELDS_LIMIT_SETTING, MODIFY_COLUMNS_ON_SWITCH } from '../../../../common';
MadaniKK marked this conversation as resolved.
Show resolved Hide resolved
import { groupFields } from './lib/group_fields';
import { IndexPatternField, IndexPattern, UI_SETTINGS } from '../../../../../data/public';
import { getDetails } from './lib/get_details';
Expand Down Expand Up @@ -100,6 +100,7 @@ export function DiscoverSidebar({
const [fields, setFields] = useState<IndexPatternField[] | null>(null);
const [fieldFilterState, setFieldFilterState] = useState(getDefaultFieldFilter());
const services = useMemo(() => getServices(), []);
const uiSettings = services.uiSettings;
MadaniKK marked this conversation as resolved.
Show resolved Hide resolved

useEffect(() => {
const newFields = getIndexPatternFieldList(selectedIndexPattern, fieldCounts);
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 @@ -17,11 +18,16 @@ import { IndexPattern } from '../../../opensearch_dashboards_services';
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 (!modifyColumn) {
MadaniKK marked this conversation as resolved.
Show resolved Hide resolved
return columns.length > 0 ? columns : ['_source'];
}
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