Skip to content

Commit

Permalink
Move dataset location
Browse files Browse the repository at this point in the history
Signed-off-by: Kawika Avilla <[email protected]>
  • Loading branch information
kavilla committed Aug 18, 2024
1 parent 4e3c441 commit 4c94b68
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/plugins/data/common/datasets/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface DataSourceMeta {
* const openSearchCluster: DataStructure = {
* id: "b18e5f58-cf71-11ee-ad92-2468ce360004",
* title: "Production Cluster",
* type: "OPENSEARCH",
* type: "DATA-SOURCE",
* children: [
* {
* id: "b18e5f58-cf71-11ee-ad92-2468ce360004::logs-2023.05",
Expand All @@ -58,7 +58,7 @@ export interface DataSourceMeta {
* const s3DataSource: DataStructure = {
* id: "7d5c3e1c-ae5f-11ee-9c91-1357bd240003",
* title: "mys3",
* type: "S3,
* type: "DATA-SOURCE",
* children: [
* {
* id: "mys3.defaultDb",
Expand Down
2 changes: 0 additions & 2 deletions src/plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,6 @@ export {
QueryState,
getDefaultQuery,
FilterManager,
DataSetManager,
DataSetContract,
SavedQuery,
SavedQueryService,
SavedQueryTimeFilter,
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/public/query/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export * from './lib';

export * from './query_service';
export * from './filter_manager';
export * from './dataset_manager';
export * from './query_string/dataset_manager';
export * from './timefilter';
export * from './saved_query';
export * from './persisted_log';
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/public/query/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { QueryService, QuerySetup, QueryStart } from '.';
import { timefilterServiceMock } from './timefilter/timefilter_service.mock';
import { createFilterManagerMock } from './filter_manager/filter_manager.mock';
import { queryStringManagerMock } from './query_string/query_string_manager.mock';
import { dataSetManagerMock } from './dataset_manager/dataset_manager.mock';
import { dataSetManagerMock } from './query_string/dataset_manager/dataset_manager.mock';

type QueryServiceClientContract = PublicMethodsOf<QueryService>;

Expand Down
11 changes: 4 additions & 7 deletions src/plugins/data/public/query/query_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import { TimefilterService, TimefilterSetup } from './timefilter';
import { createSavedQueryService } from './saved_query/saved_query_service';
import { createQueryStateObservable } from './state_sync/create_global_query_observable';
import { QueryStringManager, QueryStringContract } from './query_string';
import { DataSetContract, DataSetManager } from './dataset_manager';
import { buildOpenSearchQuery, getOpenSearchQueryConfig, IndexPatternsService } from '../../common';
import { getUiSettings } from '../services';
import { IndexPattern } from '..';
Expand All @@ -63,7 +62,6 @@ export class QueryService {
filterManager!: FilterManager;
timefilter!: TimefilterSetup;
queryStringManager!: QueryStringContract;
dataSetManager!: DataSetContract;

state$!: ReturnType<typeof createQueryStateObservable>;

Expand All @@ -77,20 +75,19 @@ export class QueryService {
});

this.queryStringManager = new QueryStringManager(storage, uiSettings);
this.dataSetManager = new DataSetManager(uiSettings);

this.state$ = createQueryStateObservable({
filterManager: this.filterManager,
timefilter: this.timefilter,
queryString: this.queryStringManager,
dataSetManager: this.dataSetManager,
datasetManager: this.queryStringManager.getDatasetManager(),
}).pipe(share());

return {
filterManager: this.filterManager,
timefilter: this.timefilter,
queryString: this.queryStringManager,
dataSetManager: this.dataSetManager,
datasetManager: this.queryStringManager.getDatasetManager(),
state$: this.state$,
};
}
Expand All @@ -101,15 +98,15 @@ export class QueryService {
uiSettings,
indexPatterns,
}: QueryServiceStartDependencies) {
this.dataSetManager.init(indexPatterns);
this.queryStringManager.getDatasetManager().init(indexPatterns);
return {
addToQueryLog: createAddToQueryLog({
storage,
uiSettings,
}),
filterManager: this.filterManager,
queryString: this.queryStringManager,
dataSetManager: this.dataSetManager,
dataSetManager: this.queryStringManager.getDatasetManager(),
savedQueries: createSavedQueryService(savedObjectsClient),
state$: this.state$,
timefilter: this.timefilter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { DataSetContract } from '.';
import { DatasetContract } from '.';

const createSetupContractMock = () => {
const dataSetManagerMock: jest.Mocked<DataSetContract> = {
const datasetManagerMock: jest.Mocked<DatasetContract> = {
init: jest.fn(),
getDataSet: jest.fn(),
setDataSet: jest.fn(),
getDataset: jest.fn(),
setDataset: jest.fn(),
getUpdates$: jest.fn(),
getDefaultDataSet: jest.fn(),
fetchDefaultDataSet: jest.fn(),
getDefaultDataset: jest.fn(),
fetchDefaultDataset: jest.fn(),
initWithIndexPattern: jest.fn(),
};
return dataSetManagerMock;
return datasetManagerMock;
};

export const dataSetManagerMock = {
export const datasetManagerMock = {
createSetupContract: createSetupContractMock,
createStartContract: createSetupContractMock,
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { DataSetManager } from './dataset_manager';
import { coreMock } from '../../../../../core/public/mocks';
import { Dataset } from '../../../common';
import { DatasetManager } from './dataset_manager';
import { coreMock } from '../../../../../../core/public/mocks';
import { Dataset } from '../../../../common';

describe('DatasetManager', () => {
let service: DataSetManager;
let service: DatasetManager;

beforeEach(() => {
const uiSettingsMock = coreMock.createSetup().uiSettings;
uiSettingsMock.get.mockReturnValue(true);
service = new DataSetManager(uiSettingsMock);
service = new DatasetManager(uiSettingsMock);
});

test('getUpdates$ is a cold emits only after dataset changes', () => {
Expand All @@ -25,16 +25,16 @@ describe('DatasetManager', () => {
expect(emittedValues).toHaveLength(0);
expect(emittedValues[0]).toEqual(undefined);

const newDataSet: Dataset = {
const newDataset: Dataset = {
id: 'test_dataset',
title: 'Test Dataset',
type: 'INDEX_PATTERN',
};
service.setDataSet(newDataSet);
service.setDataset(newDataset);
expect(emittedValues).toHaveLength(1);
expect(emittedValues[0]).toEqual(newDataSet);
expect(emittedValues[0]).toEqual(newDataset);

service.setDataSet({ ...newDataSet });
service.setDataset({ ...newDataset });
expect(emittedValues).toHaveLength(2);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
import { BehaviorSubject } from 'rxjs';
import { CoreStart } from 'opensearch-dashboards/public';
import { skip } from 'rxjs/operators';
import { DEFAULT_QUERY, Dataset, DataSource, IndexPattern, UI_SETTINGS } from '../../../common';
import { IndexPatternsContract } from '../../index_patterns';
import { DEFAULT_QUERY, Dataset, DataSource, IndexPattern, UI_SETTINGS } from '../../../../common';
import { IndexPatternsContract } from '../../../index_patterns';

export class DataSetManager {
private dataSet$: BehaviorSubject<Dataset | undefined>;
export class DatasetManager {
private dataset$: BehaviorSubject<Dataset | undefined>;
private indexPatterns?: IndexPatternsContract;
private defaultDataSet?: Dataset;
private defaultDataset?: Dataset;

constructor(private readonly uiSettings: CoreStart['uiSettings']) {
this.dataSet$ = new BehaviorSubject<Dataset | undefined>(undefined);
this.dataset$ = new BehaviorSubject<Dataset | undefined>(undefined);
}

public init = async (indexPatterns: IndexPatternsContract) => {
if (!this.uiSettings.get(UI_SETTINGS.QUERY_ENHANCEMENTS_ENABLED)) return;
this.indexPatterns = indexPatterns;
this.defaultDataSet = await this.fetchDefaultDataSet();
this.defaultDataset = await this.fetchDefaultDataset();
};

public initWithIndexPattern = (indexPattern: IndexPattern | null) => {
Expand All @@ -30,7 +30,7 @@ export class DataSetManager {
return undefined;
}

this.defaultDataSet = {
this.defaultDataset = {
id: indexPattern.id,
title: indexPattern.title,
type: DEFAULT_QUERY.DATASET_TYPE,
Expand All @@ -48,27 +48,27 @@ export class DataSetManager {
};

public getUpdates$ = () => {
return this.dataSet$.asObservable().pipe(skip(1));
return this.dataset$.asObservable().pipe(skip(1));
};

public getDataSet = () => {
return this.dataSet$.getValue();
public getDataset = () => {
return this.dataset$.getValue();
};

/**
* Updates the query.
* @param {Query} query
*/
public setDataSet = (dataSet: Dataset | undefined) => {
public setDataset = (dataSet: Dataset | undefined) => {
if (!this.uiSettings.get(UI_SETTINGS.QUERY_ENHANCEMENTS_ENABLED)) return;
this.dataSet$.next(dataSet);
this.dataset$.next(dataSet);
};

public getDefaultDataSet = () => {
return this.defaultDataSet;
public getDefaultDataset = () => {
return this.defaultDataset;
};

public fetchDefaultDataSet = async (): Promise<Dataset | undefined> => {
public fetchDefaultDataset = async (): Promise<Dataset | undefined> => {
const defaultIndexPatternId = this.uiSettings.get('defaultIndex');
if (!defaultIndexPatternId) {
return undefined;
Expand Down Expand Up @@ -97,4 +97,4 @@ export class DataSetManager {
};
}

export type DataSetContract = PublicMethodsOf<DataSetManager>;
export type DatasetContract = PublicMethodsOf<DatasetManager>;
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
* SPDX-License-Identifier: Apache-2.0
*/

export { DataSetContract, DataSetManager } from './dataset_manager';
export { DatasetContract, DatasetManager } from './dataset_manager';
39 changes: 39 additions & 0 deletions src/plugins/data/public/query/query_string/query_string_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@ import { skip } from 'rxjs/operators';
import { CoreStart } from 'opensearch-dashboards/public';
import { IStorageWrapper } from 'src/plugins/opensearch_dashboards_utils/public';
import { Query, UI_SETTINGS } from '../../../common';
import { DatasetContract, DatasetManager } from './dataset_manager';

export class QueryStringManager {
private query$: BehaviorSubject<Query>;
private datasetManager!: DatasetContract;

constructor(
private readonly storage: IStorageWrapper,
private readonly uiSettings: CoreStart['uiSettings']
) {
this.query$ = new BehaviorSubject<Query>(this.getDefaultQuery());
this.datasetManager = new DatasetManager(uiSettings);
}

private getDefaultQueryString() {
Expand Down Expand Up @@ -100,6 +103,42 @@ export class QueryStringManager {
public clearQuery = () => {
this.setQuery(this.getDefaultQuery());
};

/**
* TODO: verify if we want to just access the dataset manager directly or access each function
*/
public getDatasetManager = () => {
return this.datasetManager;
};

// TODO: uncomment or use based on decision above
// public initDataset = async (indexPatterns: IndexPatternsContract) => {
// return this.datasetManager.init(indexPatterns);
// };

// public initDatasetWithIndexPattern = (indexPattern: IndexPattern | null) => {
// return this.datasetManager.initWithIndexPattern(indexPattern);
// };

// public getDatasetUpdates$ = () => {
// return this.datasetManager.getUpdates$();
// };

// public getDataset = () => {
// return this.datasetManager.getDataset();
// };

// public setDataset = (dataset: Dataset | undefined) => {
// return this.datasetManager.setDataset(dataset);
// };

// public getDefaultDataset = () => {
// return this.datasetManager.getDefaultDataset();
// };

// public fetchDefaultDataset = async (): Promise<Dataset | undefined> => {
// return this.datasetManager.fetchDefaultDataset();
// };
}

export type QueryStringContract = PublicMethodsOf<QueryStringManager>;
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,26 @@ import { QueryState, QueryStateChange } from './index';
import { createStateContainer } from '../../../../opensearch_dashboards_utils/public';
import { isFilterPinned, compareFilters, COMPARE_ALL_OPTIONS } from '../../../common';
import { QueryStringContract } from '../query_string';
import { DataSetContract } from '../dataset_manager';
import { DatasetContract } from '../query_string/dataset_manager';

export function createQueryStateObservable({
timefilter: { timefilter },
filterManager,
queryString,
dataSetManager,
datasetManager,
}: {
timefilter: TimefilterSetup;
filterManager: FilterManager;
queryString: QueryStringContract;
dataSetManager: DataSetContract;
datasetManager: DatasetContract;
}): Observable<{ changes: QueryStateChange; state: QueryState }> {
return new Observable((subscriber) => {
const state = createStateContainer<QueryState>({
time: timefilter.getTime(),
refreshInterval: timefilter.getRefreshInterval(),
filters: filterManager.getFilters(),
query: queryString.getQuery(),
dataSet: dataSetManager.getDataSet(),
dataset: queryString.getDatasetManager().getDataset(),
});

let currentChange: QueryStateChange = {};
Expand All @@ -64,10 +64,13 @@ export function createQueryStateObservable({
currentChange.query = true;
state.set({ ...state.get(), query: queryString.getQuery() });
}),
dataSetManager.getUpdates$().subscribe(() => {
currentChange.dataSet = true;
state.set({ ...state.get(), dataSet: dataSetManager.getDataSet() });
}),
queryString
.getDatasetManager()
.getUpdates$()
.subscribe(() => {
currentChange.dataset = true;
state.set({ ...state.get(), dataset: queryString.getDatasetManager().getDataset() });
}),
timefilter.getTimeUpdate$().subscribe(() => {
currentChange.time = true;
state.set({ ...state.get(), time: timefilter.getTime() });
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/data/public/query/state_sync/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export interface QueryState {
refreshInterval?: RefreshInterval;
filters?: Filter[];
query?: Query;
// TODO: remove once query object correctly has dataset
dataset?: Dataset;
}

type QueryStateChangePartial = {
Expand Down

0 comments on commit 4c94b68

Please sign in to comment.