Skip to content

Commit 42166d0

Browse files
authored
[Discover] Data selector enhancement (#6571)
* 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]>
1 parent c5b618c commit 42166d0

24 files changed

+891
-282
lines changed

Diff for: changelogs/fragments/6571.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
refactor:
2+
- discover data selector enhancement and refactoring ([#6571](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6571))

Diff for: src/plugins/data/public/data_sources/constants.tsx

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import { i18n } from '@osd/i18n';
7+
import { DataSourceUIGroupType } from './datasource/types';
8+
9+
export const S3_GLUE_DATA_SOURCE_DISPLAY_NAME = 'Amazon S3';
10+
export const S3_GLUE_DATA_SOURCE_TYPE = 's3glue';
11+
export const DEFAULT_DATA_SOURCE_TYPE = 'DEFAULT_INDEX_PATTERNS';
12+
export const DEFAULT_DATA_SOURCE_NAME = i18n.translate('data.datasource.type.openSearchDefault', {
13+
defaultMessage: 'OpenSearch Default',
14+
});
15+
export const DEFAULT_DATA_SOURCE_DISPLAY_NAME = i18n.translate(
16+
'data.datasource.type.openSearchDefaultDisplayName',
17+
{
18+
defaultMessage: 'Index patterns',
19+
}
20+
);
21+
22+
export const defaultDataSourceMetadata = {
23+
ui: {
24+
label: DEFAULT_DATA_SOURCE_DISPLAY_NAME,
25+
typeLabel: DEFAULT_DATA_SOURCE_DISPLAY_NAME,
26+
groupType: DataSourceUIGroupType.defaultOpenSearchDataSource,
27+
selector: {
28+
displayDatasetsAsSource: true,
29+
},
30+
},
31+
};
32+
33+
export const s3DataSourceMetadata = {
34+
ui: {
35+
label: S3_GLUE_DATA_SOURCE_DISPLAY_NAME,
36+
typeLabel: S3_GLUE_DATA_SOURCE_TYPE,
37+
groupType: DataSourceUIGroupType.s3glue,
38+
selector: {
39+
displayDatasetsAsSource: false,
40+
},
41+
},
42+
};
43+
44+
export const DATA_SELECTOR_REFRESHER_POPOVER_TEXT = 'Refresh data selector';
45+
export const DATA_SELECTOR_DEFAULT_PLACEHOLDER = 'Select a data source';
46+
export const DATA_SELECTOR_S3_DATA_SOURCE_GROUP_HINT_LABEL = ' - Opens in Log Explorer';

Diff for: src/plugins/data/public/data_sources/datasource/datasource.ts

+35-17
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,41 @@
1313
* DataSourceQueryResult: Represents the result from querying the data source.
1414
*/
1515

16-
import { ConnectionStatus } from './types';
16+
import {
17+
DataSourceConnectionStatus,
18+
IDataSetParams,
19+
IDataSourceDataSet,
20+
IDataSourceMetadata,
21+
IDataSourceQueryParams,
22+
IDataSourceQueryResponse,
23+
IDataSourceSettings,
24+
} from './types';
1725

1826
/**
1927
* @experimental this class is experimental and might change in future releases.
2028
*/
2129
export abstract class DataSource<
22-
DataSourceMetaData,
23-
DataSetParams,
24-
SourceDataSet,
25-
DataSourceQueryParams,
26-
DataSourceQueryResult
30+
TMetadata extends IDataSourceMetadata = IDataSourceMetadata,
31+
TDataSetParams extends IDataSetParams = IDataSetParams,
32+
TDataSet extends IDataSourceDataSet = IDataSourceDataSet,
33+
TQueryParams extends IDataSourceQueryParams = IDataSourceQueryParams,
34+
TQueryResult extends IDataSourceQueryResponse = IDataSourceQueryResponse
2735
> {
28-
constructor(
29-
private readonly name: string,
30-
private readonly type: string,
31-
private readonly metadata: DataSourceMetaData
32-
) {}
36+
private readonly id: string;
37+
private readonly name: string;
38+
private readonly type: string;
39+
private readonly metadata: TMetadata;
40+
41+
constructor(settings: IDataSourceSettings<TMetadata>) {
42+
this.id = settings.id;
43+
this.name = settings.name;
44+
this.type = settings.type;
45+
this.metadata = settings.metadata;
46+
}
47+
48+
getId() {
49+
return this.id;
50+
}
3351

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

6078
/**
6179
* Abstract method to run a query against the data source.
6280
* Implementing classes need to provide the specific implementation.
6381
*
6482
* @experimental This API is experimental and might change in future releases.
65-
* @returns {DataSourceQueryResult} Result from querying the data source.
83+
* @returns {Promise<TQueryResult>} Result from querying the data source.
6684
*/
67-
abstract runQuery(queryParams: DataSourceQueryParams): DataSourceQueryResult;
85+
abstract runQuery(queryParams?: TQueryParams): Promise<TQueryResult>;
6886

6987
/**
7088
* Abstract method to test the connection to the data source.
7189
* Implementing classes should provide the specific logic to determine
7290
* the connection status, typically indicating success or failure.
7391
*
7492
* @experimental This API is experimental and might change in future releases.
75-
* @returns {ConnectionStatus | Promise<void>} Status of the connection test.
93+
* @returns {Promise<DataSourceConnectionStatus | boolean>} Status of the connection test.
7694
* @experimental
7795
*/
78-
abstract testConnection(): ConnectionStatus | Promise<boolean>;
96+
abstract testConnection(): Promise<DataSourceConnectionStatus | boolean>;
7997
}

Diff for: src/plugins/data/public/data_sources/datasource/factory.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@ class MockDataSource extends DataSource<any, any, any, any, any> {
1111
private readonly indexPatterns;
1212

1313
constructor({
14+
id,
1415
name,
1516
type,
1617
metadata,
1718
indexPatterns,
1819
}: {
20+
id: string;
1921
name: string;
2022
type: string;
2123
metadata: any;
2224
indexPatterns: IndexPatternsService;
2325
}) {
24-
super(name, type, metadata);
26+
super({ id, name, type, metadata });
2527
this.indexPatterns = indexPatterns;
2628
}
2729

Diff for: src/plugins/data/public/data_sources/datasource/factory.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
* It serves as a registry for different data source types and provides a way to instantiate them.
99
*/
1010

11-
import { DataSourceType } from '../datasource_services';
1211
import { DataSource } from '../datasource';
1312

1413
type DataSourceClass<
@@ -66,11 +65,11 @@ export class DataSourceFactory {
6665
*
6766
* @experimental This API is experimental and might change in future releases.
6867
* @param {string} type - The identifier for the data source type.
69-
* @param {any} config - The configuration for the data source instance.
68+
* @param {unknown} config - The configuration for the data source instance.
7069
* @returns {DataSourceType} An instance of the specified data source type.
7170
* @throws {Error} Throws an error if the data source type is not supported.
7271
*/
73-
getDataSourceInstance(type: string, config: any): DataSourceType {
72+
getDataSourceInstance(type: string, config: unknown): DataSource {
7473
const DataSourceClass = this.dataSourceClasses[type];
7574
if (!DataSourceClass) {
7675
throw new Error('Unsupported data source type');

Diff for: src/plugins/data/public/data_sources/datasource/index.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55

66
export { DataSource } from './datasource';
77
export {
8-
IDataSourceMetaData,
9-
ISourceDataSet,
8+
IDataSourceMetadata,
9+
DataSetWithDataSource,
1010
IDataSetParams,
1111
IDataSourceQueryParams,
1212
IDataSourceQueryResult,
13-
ConnectionStatus,
14-
DataSourceConfig,
13+
DataSourceConnectionStatus,
1514
IndexPatternOption,
1615
} from './types';
1716
export { DataSourceFactory } from './factory';

Diff for: src/plugins/data/public/data_sources/datasource/types.ts

+86-23
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,112 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6+
import { DataSource } from './datasource';
7+
68
/**
79
* @experimental These interfaces are experimental and might change in future releases.
810
*/
911

10-
import { IndexPatternsService } from '../../index_patterns';
11-
import { DataSourceType } from '../datasource_services';
12-
1312
export interface IndexPatternOption {
1413
title: string;
1514
id: string;
1615
}
1716

18-
export interface IDataSourceMetaData {
17+
export interface IDataSourceGroup {
1918
name: string;
2019
}
2120

22-
export interface IDataSourceGroup {
23-
name: string;
21+
export interface DataSetWithDataSource<T = unknown> {
22+
ds: DataSource;
23+
list: T[];
2424
}
2525

26-
export interface ISourceDataSet {
27-
ds: DataSourceType;
28-
data_sets: Array<string | IndexPatternOption>;
26+
export interface IDataSetParams<T = {}> {
27+
query: T;
2928
}
3029

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

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

38-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
39-
export interface IDataSourceQueryResult {}
38+
export enum ConnectionStatus {
39+
Connected = 'connected',
40+
Disconnected = 'disconnected',
41+
Error = 'error',
42+
}
4043

41-
export interface ConnectionStatus {
42-
success: boolean;
43-
info: string;
44+
export interface DataSourceConnectionStatus {
45+
status: ConnectionStatus;
46+
message: string;
47+
error?: Error;
4448
}
4549

46-
export interface DataSourceConfig {
47-
name: string;
50+
export interface IDataSourceSettings<T extends IDataSourceMetadata = IDataSourceMetadata> {
51+
id: string;
4852
type: string;
49-
metadata: any;
50-
indexPatterns: IndexPatternsService;
53+
name: string;
54+
metadata: T;
55+
}
56+
57+
export interface IDataSourceMetadata {
58+
ui: IDataSourceUISettings;
59+
}
60+
61+
export interface IDataSourceUISelector {
62+
displayDatasetsAsSource: boolean;
63+
}
64+
65+
/**
66+
* Represents the UI settings for a data source.
67+
*/
68+
export interface IDataSourceUISettings {
69+
/**
70+
* Controls UI elements related to data source selector.
71+
*/
72+
selector: IDataSourceUISelector;
73+
74+
/**
75+
* The display name of the data source.
76+
*/
77+
label: string;
78+
79+
/**
80+
* The group to which the data source belongs. This is used to group data sources in the selector.
81+
*/
82+
groupType: DataSourceUIGroupType;
83+
84+
/**
85+
* The display name of the data source type.
86+
*/
87+
typeLabel: string;
88+
89+
/**
90+
* A short description of the data source.
91+
* @optional
92+
*/
93+
description?: string;
94+
95+
/**
96+
* URI of the icon representing the data source.
97+
* @optional
98+
*/
99+
icon?: string;
100+
}
101+
102+
export interface IDataSourceDataSet<T = {}> {
103+
dataSets: T;
104+
}
105+
106+
export interface IDataSourceQueryResponse<T = {}> {
107+
data: T;
108+
}
109+
110+
export enum DataSourceUIGroupType {
111+
defaultOpenSearchDataSource = 'DEFAULT_INDEX_PATTERNS',
112+
s3glue = 's3glue',
113+
spark = 'spark',
51114
}

0 commit comments

Comments
 (0)