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

initial implementation for url state #67

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
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 @@
} 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 @@
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 @@
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 @@
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 @@ -312,7 +320,7 @@

const handleSelectedDataSet = useCallback(
async (ds: any) => {
const createTemporaryIndexPattern = async (dataSet: SimpleDataSet) => {

Check failure on line 323 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)

'dataSet' is already declared in the upper scope
const fieldsMap = dataSet.fields?.reduce((acc: any, field: any) => {
acc[field.name] = field;
return acc;
Expand All @@ -335,7 +343,7 @@
indexPatternsService.saveToCache(temporaryIndexPattern.title, temporaryIndexPattern);
};

const getInitialQuery = (dataSet: SimpleDataSet) => {

Check failure on line 346 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)

'dataSet' is already declared in the upper scope
const language = uiService.Settings.getUserQueryLanguage();
const input = uiService.Settings.getQueryEnhancements(language)?.searchBar?.queryStringInput
?.initialValue;
Expand All @@ -352,7 +360,7 @@
};
};

const onDataSetSelected = async (dataSet: SimpleDataSet) => {

Check failure on line 363 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)

'dataSet' is already declared in the upper scope
if (
dataSet.type === SIMPLE_DATA_SET_TYPES.TEMPORARY ||
dataSet.type === SIMPLE_DATA_SET_TYPES.TEMPORARY_ASYNC
Expand All @@ -366,14 +374,19 @@
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 @@
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 './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 @@ -139,10 +141,11 @@
);

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 @@
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 @@
*/
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,13 +87,13 @@
fetchIndexPatternDetails(newId);
});
} else {
fetchIndexPatternDetails(indexPatternIdFromState);
checkDataSet(dataSetFromState, indexPatternIdFromState);
}

return () => {
isMounted = false;
};
}, [indexPatternIdFromState, data.indexPatterns, toastNotifications, config, store]);

Check failure on line 96 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)

React Hook useEffect has a missing dependency: 'dataSetFromState'. Either include it or remove the dependency array

return indexPattern;
};
Loading