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

[Backport 2.x] [Data Explorer][Discover 2.0] Fix issues when change index pattern #4885

Merged
merged 1 commit into from
Sep 1, 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
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,5 @@ export function getIndexPatternFieldList(
fieldCounts?: Record<string, number>
) {
if (!indexPattern || !fieldCounts) return [];

const fieldNamesInDocs = Object.keys(fieldCounts);
const fieldNamesInIndexPattern = indexPattern.fields.getAll().map((fld) => fld.name);
const unknownTypes: IndexPatternField[] = [];

difference(fieldNamesInDocs, fieldNamesInIndexPattern).forEach((unknownFieldName) => {
unknownTypes.push({
displayName: String(unknownFieldName),
name: String(unknownFieldName),
type: 'unknown',
} as IndexPatternField);
});

return [...indexPattern.fields.getAll(), ...unknownTypes];
return [...indexPattern.fields.getAll()];
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { useEffect, useState } from 'react';
import React, { useEffect, useState, useRef } from 'react';
import { EuiFlexGroup, EuiFlexItem, EuiPanel, EuiCallOut, EuiLink } from '@elastic/eui';
import { TopNav } from './top_nav';
import { ViewProps } from '../../../../../data_explorer/public';
Expand All @@ -14,10 +14,26 @@ import { ResultStatus, SearchData } from '../utils/use_search';
import { DiscoverNoResults } from '../../components/no_results/no_results';
import { DiscoverUninitialized } from '../../components/uninitialized/uninitialized';
import { LoadingSpinner } from '../../components/loading_spinner/loading_spinner';
import { setColumns, useDispatch, useSelector } from '../../utils/state_management';
import { DiscoverViewServices } from '../../../build_services';
import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public';
import { filterColumns } from '../utils/filter_columns';
import { DEFAULT_COLUMNS_SETTING } from '../../../../common';

// eslint-disable-next-line import/no-default-export
export default function DiscoverCanvas({ setHeaderActionMenu, history }: ViewProps) {
const { data$, refetch$, indexPattern } = useDiscoverContext();
const {
services: { uiSettings },
} = useOpenSearchDashboards<DiscoverViewServices>();
const { columns } = useSelector((state) => state.discover);
const filteredColumns = filterColumns(
columns,
indexPattern,
uiSettings.get(DEFAULT_COLUMNS_SETTING)
);
const dispatch = useDispatch();
const prevIndexPattern = useRef(indexPattern);

const [fetchState, setFetchState] = useState<SearchData>({
status: data$.getValue().status,
Expand Down Expand Up @@ -69,6 +85,13 @@ export default function DiscoverCanvas({ setHeaderActionMenu, history }: ViewPro
};
}, [data$, fetchState]);

useEffect(() => {
if (indexPattern !== prevIndexPattern.current) {
dispatch(setColumns({ columns: filteredColumns }));
prevIndexPattern.current = indexPattern;
}
}, [dispatch, filteredColumns, indexPattern]);

const timeField = indexPattern?.timeFieldName ? indexPattern.timeFieldName : undefined;

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { filterColumns } from './filter_columns';
import { IndexPattern } from '../../../opensearch_dashboards_services';

describe('filterColumns', () => {
const indexPatternMock = {
fields: {
getAll: () => [{ name: 'a' }, { name: 'c' }, { name: 'd' }],
},
} as IndexPattern;
const defaultColumns = ['_defaultColumn'];

it('should return columns that exist in the index pattern fields', () => {
const columns = ['a', 'b'];
const result = filterColumns(columns, indexPatternMock, defaultColumns);
expect(result).toEqual(['a']);
});

it('should return defaultColumns if no columns exist in the index pattern fields', () => {
const columns = ['b', 'e'];
const result = filterColumns(columns, indexPatternMock, defaultColumns);
expect(result).toEqual(defaultColumns);
});

it('should return defaultColumns if no columns and indexPattern is null', () => {
const columns = ['b', 'e'];
const result = filterColumns(columns, null, defaultColumns);
expect(result).toEqual(defaultColumns);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { IndexPattern } from '../../../opensearch_dashboards_services';

/**
* Helper function to filter columns based on the fields of the index pattern.
* This function is used when we switch between index patterns. We want to keep the columns that are
* still available in the new index pattern and remove the ones that are not.
* If the resulting array is empty, it provides a fallback to the default column.
* @param columns Array of column names
* @param indexPattern Index pattern object
* @param defaultColumns Array of default columns
*/
export function filterColumns(
columns: string[],
indexPattern: IndexPattern,
defaultColumns: string[]
) {
const fieldsName = indexPattern?.fields.getAll().map((fld) => fld.name) || [];
const filteredColumns = columns.filter((column) => fieldsName.includes(column));
return filteredColumns.length > 0 ? filteredColumns : defaultColumns;
}