Skip to content

Commit

Permalink
initial implementation for url state
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Li <[email protected]>
  • Loading branch information
sejli committed Jul 23, 2024
1 parent f2ca5b0 commit 7f1a07d
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 49 deletions.
3 changes: 3 additions & 0 deletions src/plugins/data/common/search/search_source/search_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
40 changes: 25 additions & 15 deletions src/plugins/data/public/ui/dataset_navigator/dataset_navigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -49,14 +49,16 @@ import {
} from './lib';

export interface DataSetNavigatorProps {
dataSetId: string | undefined;
dataSet: Omit<SimpleDataSet, 'id'> | undefined;
indexPatternId: string | undefined;
savedObjectsClient?: SavedObjectsClientContract;
http?: HttpStart;
onSelectDataSet: (dataSet: SimpleDataSet) => void;
}

export const DataSetNavigator = ({
dataSetId,
dataSet,
indexPatternId,
savedObjectsClient,
http,
onSelectDataSet,
Expand All @@ -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<SimpleDataSet | undefined>();
// const [selectedDataSet, setSelectedDataSet] = useState<SimpleDataSet | undefined>({ id: dataSet?.id, title: dataSet?.datasource?.name, });
const [selectedObject, setSelectedObject] = useState<SimpleDataSet | undefined>();
const [selectedDataSource, setSelectedDataSource] = useState<SimpleDataSource | undefined>();
const [selectedDataSourceObjects, setSelectedDataSourceObjects] = useState<SimpleObject[]>([]);
Expand Down Expand Up @@ -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,
Expand All @@ -137,7 +145,7 @@ export const DataSetNavigator = ({
setIsLoading(false);
});
}
}, [indexPatternsService, dataSetId, savedObjectsClient, selectedDataSet, isMounted, http]);
}, [indexPatternsService, savedObjectsClient, isMounted, http]);

Check failure on line 148 in src/plugins/data/public/ui/dataset_navigator/dataset_navigator.tsx

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

React Hook useEffect has missing dependencies: 'dataSet', 'indexPatternId', and 'onSelectDataSet'. Either include them or remove the dependency array. If 'onSelectDataSet' changes too often, find the parent component that defines it and wrap that definition in useCallback

useEffect(() => {
const status = dataSourcesLoadStatus.toLowerCase();
Expand Down Expand Up @@ -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);
}
},
[
Expand Down Expand Up @@ -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
}`}
</EuiButtonEmpty>
}
Expand Down
18 changes: 18 additions & 0 deletions src/plugins/data/public/ui/dataset_navigator/lib/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
15 changes: 11 additions & 4 deletions src/plugins/data_explorer/public/components/sidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<DataSourceOption[]>([]);
const [dataSourceOptionList, setDataSourceOptionList] = useState<DataSourceGroup[]>([]);
Expand Down Expand Up @@ -141,8 +143,9 @@ export const Sidebar: FC = ({ children }) => {
const handleDataSetSelection = useCallback(
(dataSet: any) => {

Check failure on line 144 in src/plugins/data_explorer/public/components/sidebar/index.tsx

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

'dataSet' is already declared in the upper scope
batch(() => {
dispatch(setIndexPattern(dataSet!.id));
dispatch(setDataSet(dataSet));
const { id, ...ds } = dataSet;
dispatch(setIndexPattern(id));
dispatch(setDataSet(ds));
});
},
[dispatch]
Expand All @@ -166,7 +169,11 @@ export const Sidebar: FC = ({ children }) => {
containerRef.current = node;
}}
>
<DataSetNavigator dataSetId={indexPatternId} onSelectDataSet={handleDataSetSelection} />
<DataSetNavigator
dataSet={dataSet}
indexPatternId={indexPatternId}
onSelectDataSet={handleDataSetSelection}
/>
</EuiPortal>
)}
{!isEnhancementsEnabled && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SimpleDataSet, 'id'>;
}

const initialState: MetadataState = {};
Expand All @@ -44,9 +49,9 @@ export const getPreloadedState = async ({
...initialState,
originatingApp,
indexPattern: defaultIndexPattern?.id,
dataSet: {
id: defaultIndexPattern?.id,
},
// dataSet: {
// id: defaultIndexPattern?.id,
// },
};

return preloadedState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IndexPattern | undefined>(undefined);
const { data, toastNotifications, uiSettings: config, store } = services;

useEffect(() => {
const checkDataSet = async (dataset: any, indexPatternIdFromState: string) => {

Check failure on line 34 in src/plugins/discover/public/application/view_components/utils/use_index_pattern.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

'indexPatternIdFromState' is already declared in the upper scope
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) => {
Expand Down Expand Up @@ -65,7 +87,7 @@ export const useIndexPattern = (services: DiscoverViewServices) => {
fetchIndexPatternDetails(newId);
});
} else {
fetchIndexPatternDetails(indexPatternIdFromState);
checkDataSet(dataSetFromState, indexPatternIdFromState);
}

return () => {
Expand Down

0 comments on commit 7f1a07d

Please sign in to comment.