From 7f1a07d3162bcaff56c41ad59fba89632b760f76 Mon Sep 17 00:00:00 2001 From: Sean Li Date: Mon, 22 Jul 2024 23:28:48 -0700 Subject: [PATCH] initial implementation for url state Signed-off-by: Sean Li --- .../search/search_source/search_source.ts | 3 ++ .../dataset_navigator/dataset_navigator.tsx | 40 ++++++++++++------- .../public/ui/dataset_navigator/lib/types.tsx | 18 +++++++++ .../public/components/sidebar/index.tsx | 15 +++++-- .../utils/state_management/metadata_slice.ts | 39 ++++++++++-------- .../utils/update_search_source.ts | 24 +++++------ .../utils/use_index_pattern.ts | 24 ++++++++++- 7 files changed, 114 insertions(+), 49 deletions(-) diff --git a/src/plugins/data/common/search/search_source/search_source.ts b/src/plugins/data/common/search/search_source/search_source.ts index 90d7b22e6c96..3236da2e572c 100644 --- a/src/plugins/data/common/search/search_source/search_source.ts +++ b/src/plugins/data/common/search/search_source/search_source.ts @@ -358,6 +358,9 @@ export class SearchSource { if (getConfig(UI_SETTINGS.COURIER_BATCH_SEARCHES)) { response = await this.legacyFetch(searchRequest, options); } else if (this.isUnsupportedRequest(searchRequest)) { + const indexPattern = this.getField('index'); + searchRequest.dataSourceId = indexPattern?.dataSourceRef?.id; + options = { ...options, isAsync: this.getField('type')?.includes('async') }; response = await this.fetchExternalSearch(searchRequest, options); } else { diff --git a/src/plugins/data/public/ui/dataset_navigator/dataset_navigator.tsx b/src/plugins/data/public/ui/dataset_navigator/dataset_navigator.tsx index a08fb627bf32..c94e956710b9 100644 --- a/src/plugins/data/public/ui/dataset_navigator/dataset_navigator.tsx +++ b/src/plugins/data/public/ui/dataset_navigator/dataset_navigator.tsx @@ -32,7 +32,7 @@ import { useLoadTablesToCache, } from './lib/catalog_cache/cache_loader'; import { CatalogCacheManager } from './lib/catalog_cache/cache_manager'; -import { CachedDataSourceStatus, DirectQueryLoadingStatus } from './lib/types'; +import { CachedDataSourceStatus, DataSet, DirectQueryLoadingStatus } from './lib/types'; import { getIndexPatterns, getNotifications, @@ -49,14 +49,16 @@ import { } from './lib'; export interface DataSetNavigatorProps { - dataSetId: string | undefined; + dataSet: Omit | undefined; + indexPatternId: string | undefined; savedObjectsClient?: SavedObjectsClientContract; http?: HttpStart; onSelectDataSet: (dataSet: SimpleDataSet) => void; } export const DataSetNavigator = ({ - dataSetId, + dataSet, + indexPatternId, savedObjectsClient, http, onSelectDataSet, @@ -71,7 +73,7 @@ export const DataSetNavigator = ({ const [isLoading, setIsLoading] = useState(false); const [isMounted, setIsMounted] = useState(false); const [isExternalDataSourcesEnabled, setIsExternalDataSourcesEnabled] = useState(false); - const [selectedDataSet, setSelectedDataSet] = useState(); + // const [selectedDataSet, setSelectedDataSet] = useState({ id: dataSet?.id, title: dataSet?.datasource?.name, }); const [selectedObject, setSelectedObject] = useState(); const [selectedDataSource, setSelectedDataSource] = useState(); const [selectedDataSourceObjects, setSelectedDataSourceObjects] = useState([]); @@ -119,12 +121,18 @@ export const DataSetNavigator = ({ setIsExternalDataSourcesEnabled(isExternalDSEnabled); setIndexPatterns(defaultIndexPatterns); setDataSources(defaultDataSources); - if (!selectedDataSet && dataSetId) { + // If there's no dataset, then should be an index pattern + if (!dataSet && indexPatternId) { const selectedPattern = defaultIndexPatterns.find( - (pattern) => pattern.id === dataSetId + (pattern) => pattern.id === indexPatternId ); if (selectedPattern) { - setSelectedDataSet({ + // setSelectedDataSet({ + // id: selectedPattern.id ?? selectedPattern.title, + // title: selectedPattern.title, + // type: SIMPLE_DATA_SET_TYPES.INDEX_PATTERN, + // }); + onSelectDataSet({ id: selectedPattern.id ?? selectedPattern.title, title: selectedPattern.title, type: SIMPLE_DATA_SET_TYPES.INDEX_PATTERN, @@ -137,7 +145,7 @@ export const DataSetNavigator = ({ setIsLoading(false); }); } - }, [indexPatternsService, dataSetId, savedObjectsClient, selectedDataSet, isMounted, http]); + }, [indexPatternsService, savedObjectsClient, isMounted, http]); useEffect(() => { const status = dataSourcesLoadStatus.toLowerCase(); @@ -366,14 +374,19 @@ export const DataSetNavigator = ({ dataSourceRef: dataSet.dataSourceRef?.id, }); searchService.df.clear(); - onSelectDataSet(dataSet); + onSelectDataSet({ + id: dataSet.id, + title: dataSet.title, + dataSourceRef: dataSet.dataSourceRef, + type: dataSet.type, + }); queryService.queryString.setQuery(getInitialQuery(dataSet)); closePopover(); }; if (ds) { await onDataSetSelected(ds); - setSelectedDataSet(ds); + // setSelectedDataSet(ds); } }, [ @@ -419,11 +432,8 @@ export const DataSetNavigator = ({ iconSide="right" onClick={() => setIsOpen(!isOpen)} > - {`${selectedDataSet?.dataSourceRef ? `${selectedDataSet.dataSourceRef.name}::` : ''}${ - selectedDataSet?.title ?? - i18n.translate('data.query.dataSetNavigator.selectDataSet', { - defaultMessage: 'Select data set', - }) + {`${dataSet?.dataSourceRef?.name ? `${dataSet.dataSourceRef?.name}::` : ''}${ + dataSet?.title }`} } diff --git a/src/plugins/data/public/ui/dataset_navigator/lib/types.tsx b/src/plugins/data/public/ui/dataset_navigator/lib/types.tsx index a51870b4e74f..8d90e84cb716 100644 --- a/src/plugins/data/public/ui/dataset_navigator/lib/types.tsx +++ b/src/plugins/data/public/ui/dataset_navigator/lib/types.tsx @@ -314,3 +314,21 @@ export interface ExternalDataSourcesCacheData { lastUpdated: string; status: CachedDataSourceStatus; } + +interface DataSourceMeta { + // ref: string; // MDS ID + // dsName?: string; // flint datasource + id: string; + name: string; + type?: string; +} + +export interface DataSet { + id: string | undefined; // index pattern ID, index name, or flintdatasource.database.table + datasource?: DataSourceMeta; + meta?: { + timestampField: string; + mapping?: any; + }; + type?: 'dataSet' | 'temporary'; +} diff --git a/src/plugins/data_explorer/public/components/sidebar/index.tsx b/src/plugins/data_explorer/public/components/sidebar/index.tsx index eb0850f8f56d..16bd8f72bcb7 100644 --- a/src/plugins/data_explorer/public/components/sidebar/index.tsx +++ b/src/plugins/data_explorer/public/components/sidebar/index.tsx @@ -20,7 +20,9 @@ import { import './index.scss'; export const Sidebar: FC = ({ children }) => { - const { indexPattern: indexPatternId } = useTypedSelector((state) => state.metadata); + const { indexPattern: indexPatternId, dataSet: dataSet } = useTypedSelector( + (state) => state.metadata + ); const dispatch = useTypedDispatch(); const [selectedSources, setSelectedSources] = useState([]); const [dataSourceOptionList, setDataSourceOptionList] = useState([]); @@ -141,8 +143,9 @@ export const Sidebar: FC = ({ children }) => { const handleDataSetSelection = useCallback( (dataSet: any) => { batch(() => { - dispatch(setIndexPattern(dataSet!.id)); - dispatch(setDataSet(dataSet)); + const { id, ...ds } = dataSet; + dispatch(setIndexPattern(id)); + dispatch(setDataSet(ds)); }); }, [dispatch] @@ -166,7 +169,11 @@ export const Sidebar: FC = ({ children }) => { containerRef.current = node; }} > - + )} {!isEnhancementsEnabled && ( diff --git a/src/plugins/data_explorer/public/utils/state_management/metadata_slice.ts b/src/plugins/data_explorer/public/utils/state_management/metadata_slice.ts index a8bc30e89b0d..c9700a086398 100644 --- a/src/plugins/data_explorer/public/utils/state_management/metadata_slice.ts +++ b/src/plugins/data_explorer/public/utils/state_management/metadata_slice.ts @@ -5,27 +5,32 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit'; import { DataExplorerServices } from '../../types'; +import { SimpleDataSet } from '../../../../data/common'; -interface DataSourceMeta { - ref: string; // MDS ID - dsName?: string; // flint datasource -} +// interface DataSourceMeta { +// // ref: string; // MDS ID +// // dsName?: string; // flint datasource +// id: string; +// name: string; +// type?: string; +// } -export interface DataSet { - id: string | undefined; // index pattern ID, index name, or flintdatasource.database.table - datasource?: DataSourceMeta; - meta?: { - timestampField: string; - mapping?: any; - }; - type?: 'dataSet' | 'temporary'; -} +// export interface DataSet { +// // id: string | undefined; // index pattern ID, index name, or flintdatasource.database.table +// // TODO: treating index pattern ID as DataSet ID +// datasource?: DataSourceMeta; +// meta?: { +// timestampField: string; +// mapping?: any; +// }; +// type?: 'dataSet' | 'temporary'; +// } export interface MetadataState { indexPattern?: string; originatingApp?: string; view?: string; - dataSet?: DataSet; + dataSet?: Omit; } const initialState: MetadataState = {}; @@ -44,9 +49,9 @@ export const getPreloadedState = async ({ ...initialState, originatingApp, indexPattern: defaultIndexPattern?.id, - dataSet: { - id: defaultIndexPattern?.id, - }, + // dataSet: { + // id: defaultIndexPattern?.id, + // }, }; return preloadedState; diff --git a/src/plugins/discover/public/application/view_components/utils/update_search_source.ts b/src/plugins/discover/public/application/view_components/utils/update_search_source.ts index a8480fdad18a..aa35dd37c229 100644 --- a/src/plugins/discover/public/application/view_components/utils/update_search_source.ts +++ b/src/plugins/discover/public/application/view_components/utils/update_search_source.ts @@ -30,18 +30,18 @@ export const updateSearchSource = async ({ histogramConfigs, }: Props) => { const { uiSettings, data } = services; - let dataSet = indexPattern; - const dataFrame = searchSource?.getDataFrame(); - if ( - searchSource && - dataFrame && - dataFrame.name && - dataFrame.name !== '' && - dataSet.title !== dataFrame.name - ) { - dataSet = data.indexPatterns.getByTitle(dataFrame.name, true) ?? dataSet; - searchSource.setField('index', dataSet); - } + const dataSet = indexPattern; + // const dataFrame = searchSource?.getDataFrame(); + // if ( + // searchSource && + // dataFrame && + // dataFrame.name && + // dataFrame.name !== '' && + // dataSet.title !== dataFrame.name + // ) { + // dataSet = data.indexPatterns.getByTitle(dataFrame.name, true) ?? dataSet; + // searchSource.setField('index', dataSet); + // } const sortForSearchSource = getSortForSearchSource( sort, diff --git a/src/plugins/discover/public/application/view_components/utils/use_index_pattern.ts b/src/plugins/discover/public/application/view_components/utils/use_index_pattern.ts index e8a81234278e..58c83969bc7a 100644 --- a/src/plugins/discover/public/application/view_components/utils/use_index_pattern.ts +++ b/src/plugins/discover/public/application/view_components/utils/use_index_pattern.ts @@ -26,10 +26,32 @@ import { getIndexPatternId } from '../../helpers/get_index_pattern_id'; */ export const useIndexPattern = (services: DiscoverViewServices) => { const indexPatternIdFromState = useSelector((state) => state.metadata.indexPattern); + const dataSetFromState = useSelector((state) => state.metadata.dataSet); const [indexPattern, setIndexPattern] = useState(undefined); const { data, toastNotifications, uiSettings: config, store } = services; useEffect(() => { + const checkDataSet = async (dataset: any, indexPatternIdFromState: string) => { + if (dataset) { + const temporaryIndexPattern = await data.indexPatterns.create( + { + id: dataset.id, + title: dataset.id, + type: dataset.type, + dataSourceRef: { + id: dataset.datasource?.ref!, + name: dataset.datasource?.dsName!, + type: dataset.type!, + }, + timeFieldName: dataset.meta?.timestampField, + }, + true + ); + data.indexPatterns.saveToCache(temporaryIndexPattern.title, temporaryIndexPattern); + } + fetchIndexPatternDetails(indexPatternIdFromState); + }; + let isMounted = true; const fetchIndexPatternDetails = (id: string) => { @@ -65,7 +87,7 @@ export const useIndexPattern = (services: DiscoverViewServices) => { fetchIndexPatternDetails(newId); }); } else { - fetchIndexPatternDetails(indexPatternIdFromState); + checkDataSet(dataSetFromState, indexPatternIdFromState); } return () => {