Skip to content

Commit

Permalink
Metadata slice
Browse files Browse the repository at this point in the history
Signed-off-by: Kawika Avilla <[email protected]>
  • Loading branch information
kavilla committed Aug 26, 2024
1 parent 18751e4 commit 182792d
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 52 deletions.
9 changes: 2 additions & 7 deletions src/plugins/data/common/datasets/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ export interface BaseDataset {
type: string;
/** Optional reference to the data source */
dataSource?: DataSource;
fields?: DatasetField[];
}

/**
Expand All @@ -213,16 +212,12 @@ export interface BaseDataset {
*
* @example
* Example of a Dataset for an OpenSearch index pattern
* Index patterns have a reference to the data source and their title is pre appended with the data source they belong to so we dont need to append the data source to the dataset object
* const logsIndexDataset: Dataset = {
* id: "2e1b1b80-9c4d-11ee-8c90-0242ac120001",
* title: "logs-*",
* title: "Cluster1::logs-*",
* type: "INDEX_PATTERN",
* timeFieldName: "@timestamp",
* dataSource: {
* id: "main-cluster",
* title: "Main OpenSearch Cluster",
* type: "OPENSEARCH"
* },
* };
*
* @example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export class IndexPatternsService {

this.savedObjectsCache = await Promise.all(
this.savedObjectsCache.map(async (obj) => {
// TODO: This behaviour will cause the index pattern title to be resolved differently depending on how its fetched since the get method in this service will not append the datasource title
if (obj.type === 'index-pattern') {
const result = { ...obj };
result.attributes.title = await getIndexPatternTitle(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,11 @@ export const indexPatternTypeConfig: DatasetTypeConfig = {
};

const fetchIndexPatterns = async (client: SavedObjectsClientContract): Promise<DataStructure[]> => {
const resp = await client.find<any>({
type: 'index-pattern',
fields: ['title'],
perPage: 10000,
});
return resp.savedObjects.map((savedObject) => ({
id: savedObject.id,
title: savedObject.attributes.title,
const indexPatterns = await getIndexPatterns().getIdsWithTitle();

return indexPatterns.map((indexPattern) => ({
id: indexPattern.id,
title: indexPattern.title,
type: DEFAULT_DATA.SET_TYPES.INDEX_PATTERN,
}));
};
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,10 @@ export class QueryStringManager {
* Updates the query.
* @param {Query} query
*/
public setQuery = (query: Query) => {
public setQuery = (query: Partial<Query>) => {
const curQuery = this.query$.getValue();
if (
query?.language !== curQuery.language ||
query?.query !== curQuery.query ||
query?.dataset !== curQuery.dataset
) {
this.query$.next(query);
}
const newQuery = { ...curQuery, ...query };
this.query$.next(newQuery);
};

/**
Expand Down
6 changes: 2 additions & 4 deletions src/plugins/data/public/ui/dataset_selector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ interface ConnectedDatasetSelectorProps {
}

const ConnectedDatasetSelector = ({ onSubmit }: ConnectedDatasetSelectorProps) => {
const [selectedDataset, setSelectedDataset] = useState<Dataset | undefined>();
const { services } = useOpenSearchDashboards<IDataPluginServices>();
const queryString = services.data.query.queryString;
const initialDataset = queryString.getQuery().dataset || queryString.getDefaultQuery().dataset;
const [selectedDataset, setSelectedDataset] = useState<Dataset | undefined>(initialDataset);

useEffect(() => {
const initialDataset = queryString.getQuery().dataset || queryString.getDefaultQuery().dataset;
setSelectedDataset(initialDataset);

const subscription = queryString.getUpdates$().subscribe((query) => {
setSelectedDataset(query.dataset);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,16 @@ export const useQueryStringManager = (props: UseQueryStringProps) => {
return () => {
subscriptions.unsubscribe();
};
}, [props.queryString]); // Remove query from dependencies
}, [props.queryString]);

// Use callback to memoize the function
const updateQuery = useCallback(
(newQuery) => {
props.queryString.setQuery(newQuery);
setQuery(newQuery);
(newQueryPartial: Partial<Query>) => {
const updatedQuery = { ...query, ...newQueryPartial };
props.queryString.setQuery(updatedQuery);
setQuery(updatedQuery);
},
[props.queryString]
[query, props.queryString]
);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@

import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { DataExplorerServices } from '../../types';
import { Dataset } from '../../../../data/common';

export interface MetadataState {
indexPattern?: string;
originatingApp?: string;
view?: string;
dataSet?: Omit<Dataset, 'id'>;
}

const initialState: MetadataState = {};
Expand Down Expand Up @@ -39,12 +37,9 @@ export const slice = createSlice({
name: 'metadata',
initialState,
reducers: {
setIndexPattern: (state, action: PayloadAction<string>) => {
setIndexPattern: (state, action: PayloadAction<string | undefined>) => {
state.indexPattern = action.payload;
},
setDataSet: (state, action: PayloadAction<Omit<Dataset, 'id'>>) => {
state.dataSet = action.payload;
},
setOriginatingApp: (state, action: PayloadAction<string | undefined>) => {
state.originatingApp = action.payload;
},
Expand All @@ -58,4 +53,4 @@ export const slice = createSlice({
});

export const { reducer } = slice;
export const { setIndexPattern, setDataSet, setOriginatingApp, setView, setState } = slice.actions;
export const { setIndexPattern, setOriginatingApp, setView, setState } = slice.actions;
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,49 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { DEFAULT_DATA } from '../../../../data/common';
import { QUERY_ENHANCEMENT_ENABLED_SETTING } from '../../components/constants';
import { DataExplorerServices } from '../../types';
import { getPreloadedState } from './preload';
import { RootState } from './store';

export const loadReduxState = async (services: DataExplorerServices) => {
try {
const serializedState = services.osdUrlStateStorage.get<RootState>('_a');
if (serializedState !== null) return serializedState;
if (serializedState !== null) {
const isQueryEnhancementEnabled = services.uiSettings.get(QUERY_ENHANCEMENT_ENABLED_SETTING);

// Migrate index pattern to query state
if (isQueryEnhancementEnabled && serializedState.metadata.indexPattern) {
const indexPattern = await services.data.indexPatterns.get(
serializedState.metadata.indexPattern
);

const dataset = {
id: serializedState.metadata.indexPattern,
title: indexPattern.title,
type: DEFAULT_DATA.SET_TYPES.INDEX_PATTERN,
};

// TODO: This is a temporary fix since indexpattern.get does not modify the title like the other list based methods. This should be handeled in a better way: https://github.com/opensearch-project/OpenSearch-Dashboards/issues/7837
if (indexPattern.dataSourceRef) {
const dataSource = await services.data.indexPatterns.getDataSource(
indexPattern.dataSourceRef.id
);

if (dataSource) {
dataset.title = `${dataSource.attributes.title}::${dataset.title}`;
}
}
services.data.query.queryString.setQuery({
dataset,
});

delete serializedState.metadata.indexPattern;
}

return serializedState;
}
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,4 @@ export type RenderState = Omit<RootState, 'metadata'>; // Remaining state after
export type Store = ReturnType<typeof configurePreloadedStore>;
export type AppDispatch = Store['dispatch'];

export { MetadataState, setIndexPattern, setDataSet, setOriginatingApp } from './metadata_slice';
export { MetadataState, setIndexPattern, setOriginatingApp } from './metadata_slice';
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { TypedUseSelectorHook } from 'react-redux';
import {
RootState,
setIndexPattern as updateIndexPattern,
setDataSet as updateDataSet,
useTypedDispatch,
useTypedSelector,
} from '../../../../../data_explorer/public';
Expand All @@ -21,4 +20,4 @@ export interface DiscoverRootState extends RootState {

export const useSelector: TypedUseSelectorHook<DiscoverRootState> = useTypedSelector;
export const useDispatch = useTypedDispatch;
export { updateIndexPattern, updateDataSet };
export { updateIndexPattern };
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { i18n } from '@osd/i18n';
import React, { useEffect, useState, useRef, useCallback } from 'react';
import {
EuiButtonIcon,
EuiContextMenu,
EuiPanel,
EuiPopover,
EuiCompressedSwitch,
} from '@elastic/eui';
import { EuiPanel } from '@elastic/eui';
import { TopNav } from './top_nav';
import { ViewProps } from '../../../../../data_explorer/public';
import { DiscoverTable } from './discover_table';
Expand All @@ -33,7 +26,6 @@ import {
import { OpenSearchSearchHit } from '../../../application/doc_views/doc_views_types';
import { buildColumns } from '../../utils/columns';
import './discover_canvas.scss';
import { getNewDiscoverSetting, setNewDiscoverSetting } from '../../components/utils/local_storage';
import { HeaderVariant } from '../../../../../../core/public';

// eslint-disable-next-line import/no-default-export
Expand Down

0 comments on commit 182792d

Please sign in to comment.