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

Discover Flint datasource selector implementation #6833

Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@
const searchParams = getSearchParams(getConfig);
const dataFrame = getDataFrame();
const indexTitle = searchRequest.index.title || searchRequest.index;
const dataSource = searchRequest.dataSource;

Check warning on line 63 in src/plugins/data/common/search/search_source/fetch/get_search_params.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/search/search_source/fetch/get_search_params.ts#L63

Added line #L63 was not covered by tests

return {
index: indexTitle,
dataSource,
paulstn marked this conversation as resolved.
Show resolved Hide resolved
body: {
...searchRequest.body,
...(dataFrame && dataFrame?.name === indexTitle ? { df: dataFrame } : {}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ export class SearchSource {
const fields = uniq((data[key] || []).concat(val));
return addToRoot(key, fields);
case 'index':
case 'dataSource':
case 'type':
case 'highlightAll':
return key && data[key] == null && addToRoot(key, val);
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/data/common/search/search_source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/

import { NameList } from 'elasticsearch';
import { DataSource } from 'src/plugins/data/public';
paulstn marked this conversation as resolved.
Show resolved Hide resolved
import { Filter, IDataFrame, IndexPattern, Query } from '../..';
import { SearchSource } from './search_source';

Expand Down Expand Up @@ -104,6 +105,7 @@ export interface SearchSourceFields {
timeout?: string;
terminate_after?: number;
df?: IDataFrame;
dataSource: DataSource;
}

export interface SearchSourceOptions {
Expand Down
16 changes: 1 addition & 15 deletions src/plugins/data/public/data_sources/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import { i18n } from '@osd/i18n';
import { DataSourceUIGroupType } from './datasource/types';

export const S3_GLUE_DATA_SOURCE_DISPLAY_NAME = 'Amazon S3';
export const S3_GLUE_DATA_SOURCE_TYPE = 's3glue';
export const DEFAULT_DATA_SOURCE_TYPE = 'DEFAULT_INDEX_PATTERNS';
export const DEFAULT_DATA_SOURCE_TYPE = 'default';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this have functional purposes outside of redirecting?

export const DEFAULT_DATA_SOURCE_NAME = i18n.translate('data.datasource.type.openSearchDefault', {
defaultMessage: 'OpenSearch Default',
});
Expand All @@ -30,17 +28,5 @@ export const defaultDataSourceMetadata = {
},
};

export const s3DataSourceMetadata = {
ui: {
label: S3_GLUE_DATA_SOURCE_DISPLAY_NAME,
typeLabel: S3_GLUE_DATA_SOURCE_TYPE,
groupType: DataSourceUIGroupType.s3glue,
selector: {
displayDatasetsAsSource: false,
},
},
};

export const DATA_SELECTOR_REFRESHER_POPOVER_TEXT = 'Refresh data selector';
export const DATA_SELECTOR_DEFAULT_PLACEHOLDER = 'Select a data source';
export const DATA_SELECTOR_S3_DATA_SOURCE_GROUP_HINT_LABEL = ' - Opens in Log Explorer';
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { DataSelectorRefresher } from './data_selector_refresher';
import {
DATA_SELECTOR_DEFAULT_PLACEHOLDER,
DATA_SELECTOR_REFRESHER_POPOVER_TEXT,
DATA_SELECTOR_S3_DATA_SOURCE_GROUP_HINT_LABEL,
} from '../constants';

// Asynchronously retrieves and formats dataset from a given data source.
Expand Down Expand Up @@ -68,18 +67,12 @@ const addOrUpdateGroup = (
) => {
const metadata = dataSource.getMetadata();
const groupType = metadata.ui.groupType;
let groupName =
const groupName =
metadata.ui.typeLabel ||
i18n.translate('dataExplorer.dataSourceSelector.defaultGroupTitle', {
defaultMessage: 'Default Group',
});

if (dataSource.getType() !== 'DEFAULT_INDEX_PATTERNS') {
groupName += i18n.translate('dataExplorer.dataSourceSelector.redirectionHint', {
defaultMessage: DATA_SELECTOR_S3_DATA_SOURCE_GROUP_HINT_LABEL,
});
}

const group = existingGroups.find((g: DataSourceGroup) => g.id === groupType);
if (group && !group.options.some((opt) => opt.key === option.key)) {
group.options.push(option);
Expand Down
26 changes: 7 additions & 19 deletions src/plugins/data_explorer/public/components/sidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import { DataExplorerServices } from '../../types';
import { setIndexPattern, useTypedDispatch, useTypedSelector } from '../../utils/state_management';
import './index.scss';
import { setDataSource } from '../../utils/state_management/metadata_slice';

export const Sidebar: FC = ({ children }) => {
const { indexPattern: indexPatternId } = useTypedSelector((state) => state.metadata);
Expand All @@ -25,7 +26,6 @@
services: {
data: { indexPatterns, dataSources, ui },
notifications: { toasts },
application,
},
} = useOpenSearchDashboards<DataExplorerServices>();

Expand Down Expand Up @@ -77,31 +77,19 @@
}
}, [indexPatternId, activeDataSources, dataSourceOptionList]);

const redirectToLogExplorer = useCallback(
(dsName: string, dsType: string) => {
return application.navigateToUrl(
`../observability-logs#/explorer?datasourceName=${dsName}&datasourceType=${dsType}`
);
},
[application]
);

const handleSourceSelection = useCallback(
(selectedDataSources: DataSourceOption[]) => {
setSelectedSources(selectedDataSources);

Check warning on line 82 in src/plugins/data_explorer/public/components/sidebar/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_explorer/public/components/sidebar/index.tsx#L82

Added line #L82 was not covered by tests
if (selectedDataSources.length === 0) {
setSelectedSources(selectedDataSources);
return;
}
// Temporary redirection solution for 2.11, where clicking non-index-pattern data sources
// will prompt users with modal explaining they are being redirected to Observability log explorer
if (selectedDataSources[0]?.ds?.getType() !== 'DEFAULT_INDEX_PATTERNS') {
paulstn marked this conversation as resolved.
Show resolved Hide resolved
redirectToLogExplorer(selectedDataSources[0].label, selectedDataSources[0].type);
return;

if (selectedDataSources[0].type === 'default') {
dispatch(setIndexPattern(selectedDataSources[0].value));

Check warning on line 88 in src/plugins/data_explorer/public/components/sidebar/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_explorer/public/components/sidebar/index.tsx#L88

Added line #L88 was not covered by tests
}
setSelectedSources(selectedDataSources);
dispatch(setIndexPattern(selectedDataSources[0].value));
dispatch(setDataSource(selectedDataSources[0].ds.getId()));

Check warning on line 90 in src/plugins/data_explorer/public/components/sidebar/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_explorer/public/components/sidebar/index.tsx#L90

Added line #L90 was not covered by tests
},
[dispatch, redirectToLogExplorer, setSelectedSources]
[dispatch, setSelectedSources]
);

const handleGetDataSetError = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

export interface MetadataState {
indexPattern?: string;
dataSource?: string;
originatingApp?: string;
view?: string;
}
Expand Down Expand Up @@ -40,6 +41,9 @@
setIndexPattern: (state, action: PayloadAction<string>) => {
state.indexPattern = action.payload;
},
setDataSource: (state, action: PayloadAction<string>) => {
state.dataSource = action.payload;

Check warning on line 45 in src/plugins/data_explorer/public/utils/state_management/metadata_slice.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_explorer/public/utils/state_management/metadata_slice.ts#L45

Added line #L45 was not covered by tests
},
setOriginatingApp: (state, action: PayloadAction<string | undefined>) => {
state.originatingApp = action.payload;
},
Expand All @@ -53,4 +57,10 @@
});

export const { reducer } = slice;
export const { setIndexPattern, setOriginatingApp, setView, setState } = slice.actions;
export const {
setIndexPattern,
setDataSource,
setOriginatingApp,
setView,
setState,
} = slice.actions;
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import {
ISearchSource,
indexPatterns as indexPatternUtils,
AggConfigs,
DataSource,
} from '../../../../../data/public';
import { DiscoverServices } from '../../../build_services';
import { SortOrder } from '../../../saved_searches/types';
import { getSortForSearchSource } from './get_sort_for_search_source';
import { SORT_DEFAULT_ORDER_SETTING, SAMPLE_SIZE_SETTING } from '../../../../common';

interface Props {
dataSource: DataSource | undefined;
indexPattern: IndexPattern;
services: DiscoverServices;
sort: SortOrder[] | undefined;
Expand All @@ -23,6 +25,7 @@ interface Props {
}

export const updateSearchSource = async ({
dataSource,
indexPattern,
services,
searchSource,
Expand Down Expand Up @@ -67,6 +70,7 @@ export const updateSearchSource = async ({
searchSourceInstance.setParent(timeRangeSearchSource);

searchSourceInstance.setFields({
dataSource,
index: dataSet,
sort: sortForSearchSource,
size,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { useEffect, useState } from 'react';
import { DataSource, IDataSourceFilter } from '../../../../../data/public';
import { useSelector } from '../../utils/state_management';
import { DiscoverViewServices } from '../../../build_services';

export const useDataSource = (services: DiscoverViewServices) => {
const dataSourceId = useSelector((state) => state.metadata.dataSource);
const [dataSource, setDataSource] = useState<DataSource | undefined>(undefined);
const { data } = services;

useEffect(() => {
let isMounted = true;
let subscription;

if (dataSourceId) {
// get the data source object from the id
subscription = data.dataSources.dataSourceService
.getDataSources$([dataSourceId] as IDataSourceFilter)
.subscribe((ds) => {
if (isMounted) {
setDataSource(ds[dataSourceId]);
}
});
} else {
subscription = data.dataSources.dataSourceService.getDataSources$().subscribe(() => {});
}

return () => {
subscription.unsubscribe();
isMounted = false;
};
}, [dataSourceId, data.dataSources]);

return dataSource;
};
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
getResponseInspectorStats,
} from '../../../opensearch_dashboards_services';
import { SEARCH_ON_PAGE_LOAD_SETTING } from '../../../../common';
import { useDataSource } from './use_datasource';

export enum ResultStatus {
UNINITIALIZED = 'uninitialized',
Expand Down Expand Up @@ -73,6 +74,7 @@ export const useSearch = (services: DiscoverViewServices) => {
const { savedSearch: savedSearchId, sort, interval } = useSelector((state) => state.discover);
const { data, filterManager, getSavedSearchById, core, toastNotifications, chrome } = services;
const indexPattern = useIndexPattern(services);
const dataSource = useDataSource(services);
const timefilter = data.query.timefilter.timefilter;
const fetchStateRef = useRef<{
abortController: AbortController | undefined;
Expand Down Expand Up @@ -128,6 +130,7 @@ export const useSearch = (services: DiscoverViewServices) => {
? createHistogramConfigs(dataSet, interval || 'auto', data)
: undefined;
const searchSource = await updateSearchSource({
dataSource,
indexPattern: dataSet,
services,
sort,
Expand Down Expand Up @@ -220,6 +223,7 @@ export const useSearch = (services: DiscoverViewServices) => {
}
}, [
indexPattern,
dataSource,
interval,
timefilter,
toastNotifications,
Expand Down
Loading