Skip to content

Commit

Permalink
[Discover] Data selector enhancement (opensearch-project#6571)
Browse files Browse the repository at this point in the history
* datasource and service refactoring

Signed-off-by: Eric <[email protected]>

* datasource enhancement and refactoring

Signed-off-by: Eric <[email protected]>

* datasource selectable consolidation and refactoring

Signed-off-by: Eric <[email protected]>

* add in memory cache with refresh

Signed-off-by: Eric <[email protected]>

* move refresh to right side

Signed-off-by: Eric <[email protected]>

* renaming

Signed-off-by: Eric <[email protected]>

* update default datasource tests

Signed-off-by: Eric <[email protected]>

* added more tests for default datasource

Signed-off-by: Eric <[email protected]>

* update selector tests

Signed-off-by: Eric <[email protected]>

* update changelog

Signed-off-by: Eric <[email protected]>

* fix data source service tests

Signed-off-by: Eric <[email protected]>

* add and update tests for datasource service

Signed-off-by: Eric <[email protected]>

* add more data source service tests

Signed-off-by: Eric <[email protected]>

* fix sidebar tests

Signed-off-by: Eric <[email protected]>

* add to change log yml

Signed-off-by: Eric <[email protected]>

* address comments along with more tests

Signed-off-by: Eric <[email protected]>

* add test subject

Signed-off-by: Eric <[email protected]>

* reference from correct type path

Signed-off-by: Eric <[email protected]>

* correct text

Signed-off-by: Eric <[email protected]>

* minor change - remove yet used displayOrder

Signed-off-by: Eric <[email protected]>

* remove from changelog as having fragments already

Signed-off-by: Eric <[email protected]>

* use expanded name

Signed-off-by: Eric <[email protected]>

* fix one test

Signed-off-by: Eric <[email protected]>

---------

Signed-off-by: Eric <[email protected]>
Signed-off-by: Eric Wei <[email protected]>
  • Loading branch information
mengweieric authored Apr 30, 2024
1 parent c5b618c commit 42166d0
Show file tree
Hide file tree
Showing 24 changed files with 891 additions and 282 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/6571.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
refactor:
- discover data selector enhancement and refactoring ([#6571](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6571))
46 changes: 46 additions & 0 deletions src/plugins/data/public/data_sources/constants.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { i18n } from '@osd/i18n';
import { DataSourceUIGroupType } from './datasource/types';

export const S3_GLUE_DATA_SOURCE_DISPLAY_NAME = 'Amazon S3';
export const S3_GLUE_DATA_SOURCE_TYPE = 's3glue';
export const DEFAULT_DATA_SOURCE_TYPE = 'DEFAULT_INDEX_PATTERNS';
export const DEFAULT_DATA_SOURCE_NAME = i18n.translate('data.datasource.type.openSearchDefault', {
defaultMessage: 'OpenSearch Default',
});
export const DEFAULT_DATA_SOURCE_DISPLAY_NAME = i18n.translate(
'data.datasource.type.openSearchDefaultDisplayName',
{
defaultMessage: 'Index patterns',
}
);

export const defaultDataSourceMetadata = {
ui: {
label: DEFAULT_DATA_SOURCE_DISPLAY_NAME,
typeLabel: DEFAULT_DATA_SOURCE_DISPLAY_NAME,
groupType: DataSourceUIGroupType.defaultOpenSearchDataSource,
selector: {
displayDatasetsAsSource: true,
},
},
};

export const s3DataSourceMetadata = {
ui: {
label: S3_GLUE_DATA_SOURCE_DISPLAY_NAME,
typeLabel: S3_GLUE_DATA_SOURCE_TYPE,
groupType: DataSourceUIGroupType.s3glue,
selector: {
displayDatasetsAsSource: false,
},
},
};

export const DATA_SELECTOR_REFRESHER_POPOVER_TEXT = 'Refresh data selector';
export const DATA_SELECTOR_DEFAULT_PLACEHOLDER = 'Select a data source';
export const DATA_SELECTOR_S3_DATA_SOURCE_GROUP_HINT_LABEL = ' - Opens in Log Explorer';
52 changes: 35 additions & 17 deletions src/plugins/data/public/data_sources/datasource/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,41 @@
* DataSourceQueryResult: Represents the result from querying the data source.
*/

import { ConnectionStatus } from './types';
import {
DataSourceConnectionStatus,
IDataSetParams,
IDataSourceDataSet,
IDataSourceMetadata,
IDataSourceQueryParams,
IDataSourceQueryResponse,
IDataSourceSettings,
} from './types';

/**
* @experimental this class is experimental and might change in future releases.
*/
export abstract class DataSource<
DataSourceMetaData,
DataSetParams,
SourceDataSet,
DataSourceQueryParams,
DataSourceQueryResult
TMetadata extends IDataSourceMetadata = IDataSourceMetadata,
TDataSetParams extends IDataSetParams = IDataSetParams,
TDataSet extends IDataSourceDataSet = IDataSourceDataSet,
TQueryParams extends IDataSourceQueryParams = IDataSourceQueryParams,
TQueryResult extends IDataSourceQueryResponse = IDataSourceQueryResponse
> {
constructor(
private readonly name: string,
private readonly type: string,
private readonly metadata: DataSourceMetaData
) {}
private readonly id: string;
private readonly name: string;
private readonly type: string;
private readonly metadata: TMetadata;

constructor(settings: IDataSourceSettings<TMetadata>) {
this.id = settings.id;
this.name = settings.name;
this.type = settings.type;
this.metadata = settings.metadata;
}

getId() {
return this.id;
}

getName() {
return this.name;
Expand All @@ -53,27 +71,27 @@ export abstract class DataSource<
* patterns for OpenSearch data source
*
* @experimental This API is experimental and might change in future releases.
* @returns {SourceDataSet} Dataset associated with the data source.
* @returns {Promise<TDataSet>} Dataset associated with the data source.
*/
abstract getDataSet(dataSetParams?: DataSetParams): SourceDataSet;
abstract getDataSet(dataSetParams?: TDataSetParams): Promise<TDataSet>;

/**
* Abstract method to run a query against the data source.
* Implementing classes need to provide the specific implementation.
*
* @experimental This API is experimental and might change in future releases.
* @returns {DataSourceQueryResult} Result from querying the data source.
* @returns {Promise<TQueryResult>} Result from querying the data source.
*/
abstract runQuery(queryParams: DataSourceQueryParams): DataSourceQueryResult;
abstract runQuery(queryParams?: TQueryParams): Promise<TQueryResult>;

/**
* Abstract method to test the connection to the data source.
* Implementing classes should provide the specific logic to determine
* the connection status, typically indicating success or failure.
*
* @experimental This API is experimental and might change in future releases.
* @returns {ConnectionStatus | Promise<void>} Status of the connection test.
* @returns {Promise<DataSourceConnectionStatus | boolean>} Status of the connection test.
* @experimental
*/
abstract testConnection(): ConnectionStatus | Promise<boolean>;
abstract testConnection(): Promise<DataSourceConnectionStatus | boolean>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ class MockDataSource extends DataSource<any, any, any, any, any> {
private readonly indexPatterns;

constructor({
id,
name,
type,
metadata,
indexPatterns,
}: {
id: string;
name: string;
type: string;
metadata: any;
indexPatterns: IndexPatternsService;
}) {
super(name, type, metadata);
super({ id, name, type, metadata });
this.indexPatterns = indexPatterns;
}

Expand Down
5 changes: 2 additions & 3 deletions src/plugins/data/public/data_sources/datasource/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* It serves as a registry for different data source types and provides a way to instantiate them.
*/

import { DataSourceType } from '../datasource_services';
import { DataSource } from '../datasource';

type DataSourceClass<
Expand Down Expand Up @@ -66,11 +65,11 @@ export class DataSourceFactory {
*
* @experimental This API is experimental and might change in future releases.
* @param {string} type - The identifier for the data source type.
* @param {any} config - The configuration for the data source instance.
* @param {unknown} config - The configuration for the data source instance.
* @returns {DataSourceType} An instance of the specified data source type.
* @throws {Error} Throws an error if the data source type is not supported.
*/
getDataSourceInstance(type: string, config: any): DataSourceType {
getDataSourceInstance(type: string, config: unknown): DataSource {
const DataSourceClass = this.dataSourceClasses[type];
if (!DataSourceClass) {
throw new Error('Unsupported data source type');
Expand Down
7 changes: 3 additions & 4 deletions src/plugins/data/public/data_sources/datasource/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@

export { DataSource } from './datasource';
export {
IDataSourceMetaData,
ISourceDataSet,
IDataSourceMetadata,
DataSetWithDataSource,
IDataSetParams,
IDataSourceQueryParams,
IDataSourceQueryResult,
ConnectionStatus,
DataSourceConfig,
DataSourceConnectionStatus,
IndexPatternOption,
} from './types';
export { DataSourceFactory } from './factory';
109 changes: 86 additions & 23 deletions src/plugins/data/public/data_sources/datasource/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,112 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { DataSource } from './datasource';

/**
* @experimental These interfaces are experimental and might change in future releases.
*/

import { IndexPatternsService } from '../../index_patterns';
import { DataSourceType } from '../datasource_services';

export interface IndexPatternOption {
title: string;
id: string;
}

export interface IDataSourceMetaData {
export interface IDataSourceGroup {
name: string;
}

export interface IDataSourceGroup {
name: string;
export interface DataSetWithDataSource<T = unknown> {
ds: DataSource;
list: T[];
}

export interface ISourceDataSet {
ds: DataSourceType;
data_sets: Array<string | IndexPatternOption>;
export interface IDataSetParams<T = {}> {
query: T;
}

// to-dos: add common interfaces for datasource
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface IDataSetParams {}
export interface IDataSourceQueryParams<T = {}> {
query: T;
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface IDataSourceQueryParams {}
export interface IDataSourceQueryResult<T = {}> {
data: T;
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface IDataSourceQueryResult {}
export enum ConnectionStatus {
Connected = 'connected',
Disconnected = 'disconnected',
Error = 'error',
}

export interface ConnectionStatus {
success: boolean;
info: string;
export interface DataSourceConnectionStatus {
status: ConnectionStatus;
message: string;
error?: Error;
}

export interface DataSourceConfig {
name: string;
export interface IDataSourceSettings<T extends IDataSourceMetadata = IDataSourceMetadata> {
id: string;
type: string;
metadata: any;
indexPatterns: IndexPatternsService;
name: string;
metadata: T;
}

export interface IDataSourceMetadata {
ui: IDataSourceUISettings;
}

export interface IDataSourceUISelector {
displayDatasetsAsSource: boolean;
}

/**
* Represents the UI settings for a data source.
*/
export interface IDataSourceUISettings {
/**
* Controls UI elements related to data source selector.
*/
selector: IDataSourceUISelector;

/**
* The display name of the data source.
*/
label: string;

/**
* The group to which the data source belongs. This is used to group data sources in the selector.
*/
groupType: DataSourceUIGroupType;

/**
* The display name of the data source type.
*/
typeLabel: string;

/**
* A short description of the data source.
* @optional
*/
description?: string;

/**
* URI of the icon representing the data source.
* @optional
*/
icon?: string;
}

export interface IDataSourceDataSet<T = {}> {
dataSets: T;
}

export interface IDataSourceQueryResponse<T = {}> {
data: T;
}

export enum DataSourceUIGroupType {
defaultOpenSearchDataSource = 'DEFAULT_INDEX_PATTERNS',
s3glue = 's3glue',
spark = 'spark',
}
Loading

0 comments on commit 42166d0

Please sign in to comment.