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
* 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]>
  • Loading branch information
BionIT authored Mar 15, 2024
1 parent c2c4f53 commit 7e316d1
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [BUG][Multiple Datasource] Fix missing customApiRegistryPromise param for test connection ([#5944](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5944))
- [BUG][Multiple Datasource] Add a migration function for datasource to add migrationVersion field ([#6025](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6025))
- [BUG][MD]Expose picker using function in data source management plugin setup([#6030](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6030))
- [BUG][Multiple Datasource] Fix data source filter bug and add tests ([#6152](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6152))

### 🚞 Infrastructure

Expand Down

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 @@ -73,14 +74,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 7e316d1

Please sign in to comment.