Skip to content

Commit

Permalink
add dataset service
Browse files Browse the repository at this point in the history
Signed-off-by: Kawika Avilla <[email protected]>
  • Loading branch information
kavilla committed Aug 24, 2024
1 parent e5f22a6 commit 42dab80
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from '../../../../common';
import { IndexPatternsContract } from '../../../index_patterns';
import { indexPatternHandlerConfig, indexHandlerConfig } from './lib';
import { DatasetHandlerConfig } from './types';
import { DatasetTypeConfig } from './types';

/**
* Manages datasets and their associated handlers.
Expand All @@ -28,7 +28,7 @@ export class DatasetManager {
private indexPatterns?: IndexPatternsContract;
private defaultDataset?: Dataset;
private dataStructureCache: DataStructureCache;
private datasetHandlers: Map<string, DatasetHandlerConfig>;
private datasetHandlers: Map<string, DatasetTypeConfig>;
private dataStructuresMap: Map<string, DataStructure>;

/**
Expand Down Expand Up @@ -56,15 +56,15 @@ export class DatasetManager {
* Registers a dataset handler.
* @param handler - The dataset handler configuration.
*/
public registerDatasetHandler(handler: DatasetHandlerConfig) {
public registerDatasetHandler(handler: DatasetTypeConfig) {
this.datasetHandlers.set(handler.id, handler);
}

/**
* Gets all registered dataset handlers.
* @returns An array of dataset handler configurations.
*/
public getDatasetHandlers(): DatasetHandlerConfig[] {
public getDatasetHandlers(): DatasetTypeConfig[] {
return Array.from(this.datasetHandlers.values());
}

Expand All @@ -73,7 +73,7 @@ export class DatasetManager {
* @param id - The ID of the dataset handler.
* @returns The dataset handler configuration, or undefined if not found.
*/
public getDatasetHandlerById(id: string): DatasetHandlerConfig | undefined {
public getDatasetHandlerById(id: string): DatasetTypeConfig | undefined {
return this.datasetHandlers.get(id);
}

Expand Down Expand Up @@ -118,7 +118,7 @@ export class DatasetManager {
* @returns The dataset handler configuration.
* @throws Error if no handler is found.
*/
private getHandlerForDataStructure(dataStructure: DataStructure): DatasetHandlerConfig {
private getHandlerForDataStructure(dataStructure: DataStructure): DatasetTypeConfig {
const handler =
this.datasetHandlers.get(dataStructure.type) ||
this.datasetHandlers.get(DEFAULT_DATA.SET_TYPES.INDEX_PATTERN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
DatasetField,
Dataset,
} from '../../../../../common';
import { DatasetHandlerConfig } from '../types';
import { DatasetTypeConfig } from '../types';
import { getSearchService, getIndexPatterns } from '../../../../services';

const INDEX_INFO = {
Expand All @@ -33,7 +33,7 @@ const meta = {
tooltip: INDEX_INFO.TITLE,
} as DataStructureFeatureMeta;

export const indexHandlerConfig: DatasetHandlerConfig = {
export const indexHandlerConfig: DatasetTypeConfig = {
id: DEFAULT_DATA.SET_TYPES.INDEX,
title: 'Indexes',
meta: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import { SavedObjectsClientContract } from 'opensearch-dashboards/public';
import { DEFAULT_DATA, DataStructure, DatasetField, Dataset } from '../../../../../common';
import { DatasetHandlerConfig } from '../types';
import { DatasetTypeConfig } from '../types';
import { getIndexPatterns } from '../../../../services';

export const indexPatternHandlerConfig: DatasetHandlerConfig = {
export const indexPatternHandlerConfig: DatasetTypeConfig = {
id: DEFAULT_DATA.SET_TYPES.INDEX_PATTERN,
title: 'Index Patterns',
meta: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { BaseDataset, Dataset, DatasetField, DataStructure } from '../../../../c
/**
* Configuration for handling dataset operations.
*/
export interface DatasetHandlerConfig {
export interface DatasetTypeConfig {
/** Unique identifier for the dataset handler */
id: string;
/** Human-readable title for the dataset type */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { CoreStart, SavedObjectsClientContract } from 'opensearch-dashboards/public';
import { Dataset, UI_SETTINGS, DataStructure, IndexPatternSpec } from 'src/plugins/data/common';
import { DatasetTypeConfig } from '../dataset_manager';
import { getIndexPatterns } from '../../../services';

export class DatasetService {
private defaultDataset?: Dataset;
private typesRegistry: Map<string, DatasetTypeConfig> = new Map();

constructor(private readonly uiSettings: CoreStart['uiSettings']) {}

public async init(): Promise<void> {
if (!this.uiSettings.get(UI_SETTINGS.QUERY_ENHANCEMENTS_ENABLED)) return;
this.defaultDataset = await this.fetchDefaultDataset();
}

public registerType(handlerConfig: DatasetTypeConfig): void {
this.typesRegistry.set(handlerConfig.id, handlerConfig);
}

public getType(type: string): DatasetTypeConfig | undefined {
return this.typesRegistry.get(type);
}

public getDefault(): Dataset | undefined {
return this.defaultDataset;
}

public async cacheDataset(dataset: Dataset): Promise<void> {
const type = this.getType(dataset.type);
if (dataset) {
const spec = {
...dataset,
fields: await type?.fetchFields(dataset),
dataSourceRef: dataset.dataSource
? {
id: dataset.dataSource.id!,
name: dataset.dataSource.title,
type: dataset.dataSource.type,
}
: undefined,
} as IndexPatternSpec;
const temporaryIndexPattern = await getIndexPatterns().create(spec, true);
getIndexPatterns().saveToCache(dataset.id, temporaryIndexPattern);
}
}

public fetchOptions(
savedObjects: SavedObjectsClientContract,
path: DataStructure[],
dataType: string
): Promise<DataStructure> {
// ROOT is the zero index
const type = this.typesRegistry.get(dataType);
if (!type) {
throw new Error(`No handler found for type: ${path[0]}`);
}
return type.fetch(savedObjects, path);
}

public getTypes(): string[] {
return Array.from(this.typesRegistry.keys());
}

private async fetchDefaultDataset(): Promise<Dataset | undefined> {
const defaultIndexPatternId = this.uiSettings.get('defaultIndex');
if (!defaultIndexPatternId) {
return undefined;
}

const indexPattern = await getIndexPatterns().get(defaultIndexPatternId);
if (!indexPattern || !indexPattern.id) {
return undefined;
}

const handler = this.typesRegistry.get('INDEX_PATTERNS');
if (handler) {
const dataset = handler.toDataset([
{
id: indexPattern.id,
title: indexPattern.title,
type: 'INDEX_PATTERNS',
},
]);
return { ...dataset, timeFieldName: indexPattern.timeFieldName };
}

return undefined;
}
}

export type DatasetServiceContract = PublicMethodsOf<DatasetService>;

0 comments on commit 42dab80

Please sign in to comment.