Skip to content

Commit

Permalink
[Multiple Datasource] Fix data source filter bug and add tests (#6152) (
Browse files Browse the repository at this point in the history
#6159)

* fix data source filter

Signed-off-by: Lu Yu <[email protected]>

* add change log

Signed-off-by: Lu Yu <[email protected]>

---------

Signed-off-by: Lu Yu <[email protected]>
(cherry picked from commit 7e316d1)
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 7596e39 commit eab3cf1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ describe('DataSourceSelector: check dataSource options', () => {
disabled={false}
hideLocalCluster={false}
fullWidth={false}
filterFn={(ds) => ds.attributes.auth.type !== AuthType.NoAuth}
dataSourceFilter={(ds) => ds.attributes.auth.type !== AuthType.NoAuth}
/>
);

Expand All @@ -152,4 +152,22 @@ describe('DataSourceSelector: check dataSource options', () => {
expect(component).toMatchSnapshot();
expect(toasts.addWarning).toBeCalledTimes(0);
});

it('should return empty options if filter out all options and hide local cluster', async () => {
component = shallow(
<DataSourceSelector
savedObjectsClient={client}
notifications={toasts}
onSelectedDataSource={jest.fn()}
disabled={false}
hideLocalCluster={true}
fullWidth={false}
dataSourceFilter={(ds) => ds.attributes.auth.type === 'random'}
/>
);
component.instance().componentDidMount!();
await nextTick();
expect(component).toMatchSnapshot();
expect(toasts.addWarning).toBeCalledTimes(0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import React from 'react';
import { i18n } from '@osd/i18n';
import { EuiComboBox } from '@elastic/eui';
import { SavedObjectsClientContract, ToastsStart } from 'opensearch-dashboards/public';
import { SavedObjectsClientContract, ToastsStart, SavedObject } from 'opensearch-dashboards/public';
import { getDataSourcesWithFields } from '../utils';
import { DataSourceAttributes } from '../../types';

export const LocalCluster: DataSourceOption = {
label: i18n.translate('dataSource.localCluster', {
Expand All @@ -26,7 +27,7 @@ export interface DataSourceSelectorProps {
defaultOption?: DataSourceOption[];
placeholderText?: string;
removePrepend?: boolean;
filterFn?: (dataSource: any) => boolean;
dataSourceFilter?: (dataSource: SavedObject<DataSourceAttributes>) => boolean;
compressed?: boolean;
}

Expand Down Expand Up @@ -72,14 +73,13 @@ export class DataSourceSelector extends React.Component<
getDataSourcesWithFields(this.props.savedObjectsClient, ['id', 'title', 'auth.type'])
.then((fetchedDataSources) => {
if (fetchedDataSources?.length) {
let filteredDataSources = [];
if (this.props.filterFn) {
filteredDataSources = fetchedDataSources.filter((ds) => this.props.filterFn!(ds));
let filteredDataSources = fetchedDataSources;
if (this.props.dataSourceFilter) {
filteredDataSources = fetchedDataSources.filter((ds) =>
this.props.dataSourceFilter!(ds)
);
}

if (filteredDataSources.length === 0) {
filteredDataSources = fetchedDataSources;
}
const dataSourceOptions = filteredDataSources.map((dataSource) => ({
id: dataSource.id,
label: dataSource.attributes?.title || '',
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/data_source_management/public/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { HttpStart, SavedObjectsClientContract } from 'src/core/public';
import { HttpStart, SavedObjectsClientContract, SavedObject } from 'src/core/public';
import {
DataSourceAttributes,
DataSourceTableItem,
Expand Down Expand Up @@ -39,8 +39,8 @@ export async function getDataSources(savedObjectsClient: SavedObjectsClientContr
export async function getDataSourcesWithFields(
savedObjectsClient: SavedObjectsClientContract,
fields: string[]
) {
const response = await savedObjectsClient.find({
): Promise<Array<SavedObject<DataSourceAttributes>>> {
const response = await savedObjectsClient.find<DataSourceAttributes>({
type: 'data-source',
fields,
perPage: 10000,
Expand Down

0 comments on commit eab3cf1

Please sign in to comment.