Skip to content

Commit

Permalink
types and datasource prepend
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 b6f55d1 commit 2ed09f7
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 405 deletions.
6 changes: 3 additions & 3 deletions src/core/public/saved_objects/saved_objects_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,14 +457,14 @@ export class SavedObjectsClient {
* { id: 'foo', type: 'index-pattern' }
* ])
*/
public bulkGet = (objects: Array<{ id: string; type: string }> = []) => {
public bulkGet = <T = unknown>(objects: Array<{ id: string; type: string }> = []) => {
const filteredObjects = objects.map((obj) => pick(obj, ['id', 'type']));
return this.performBulkGet(filteredObjects).then((resp) => {
resp.saved_objects = resp.saved_objects.map((d) => this.createSavedObject(d));
return renameKeys<
PromiseType<ReturnType<SavedObjectsApi['bulkGet']>>,
SavedObjectsBatchResponse
>({ saved_objects: 'savedObjects' }, resp) as SavedObjectsBatchResponse;
SavedObjectsBatchResponse<T>
>({ saved_objects: 'savedObjects' }, resp) as SavedObjectsBatchResponse<T>;
});
};

Expand Down
10 changes: 6 additions & 4 deletions src/plugins/data/common/datasets/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ export interface DataStructure {
parent?: DataStructure;
/** Optional array of child data structures */
children?: DataStructure[];
/** Optional array of data structures of ancestors */
path?: DataStructure[];
hasNext?: boolean;
columnHeader?: string;
/** Optional metadata for the data structure */
Expand Down Expand Up @@ -212,12 +210,16 @@ 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: "Cluster1::logs-*",
* title: "logs-*",
* type: "INDEX_PATTERN",
* timeFieldName: "@timestamp",
* dataSource: {
* id: "2e1b1b80-9c4d-11ee-8c90-0242ac120001",
* title: "Cluster1",
* type: "OpenSearch"
* }
* };
*
* @example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ export class DatasetService {
return undefined;
}

const handler = this.typesRegistry.get(DEFAULT_DATA.SET_TYPES.INDEX_PATTERN);
if (handler) {
const dataset = handler.toDataset([
const dataType = this.typesRegistry.get(DEFAULT_DATA.SET_TYPES.INDEX_PATTERN);
if (dataType) {
const dataset = dataType.toDataset([
{
id: indexPattern.id,
title: indexPattern.title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
*/

import { SavedObjectsClientContract } from 'opensearch-dashboards/public';
import { DataSourceAttributes } from '../../../../../../data_source/common/data_sources';
import {
DEFAULT_DATA,
DataStructure,
DatasetField,
Dataset,
IIndexPattern,
DATA_STRUCTURE_META_TYPES,
} from '../../../../../common';
import { DatasetTypeConfig } from '../types';
import { getIndexPatterns } from '../../../../services';
Expand All @@ -22,7 +24,7 @@ export const indexPatternTypeConfig: DatasetTypeConfig = {
tooltip: 'OpenSearch Index Patterns',
},

toDataset: (path: DataStructure[]): Dataset => {
toDataset: (path) => {
const pattern = path[path.length - 1];
return {
id: pattern.id,
Expand All @@ -35,13 +37,10 @@ export const indexPatternTypeConfig: DatasetTypeConfig = {
type: pattern.parent.type,
}
: undefined,
};
} as Dataset;
},

fetch: async (
savedObjects: SavedObjectsClientContract,
path: DataStructure[]
): Promise<DataStructure> => {
fetch: async (savedObjects, path) => {
const dataStructure = path[path.length - 1];
const indexPatterns = await fetchIndexPatterns(savedObjects);
return {
Expand All @@ -60,8 +59,11 @@ export const indexPatternTypeConfig: DatasetTypeConfig = {
}));
},

supportedLanguages: (): string[] => {
return ['SQL', 'PPL', 'DQL', 'Lucene'];
supportedLanguages: (dataset): string[] => {
if (dataset.dataSource?.type === 'OpenSearch Serverless') {
return ['DQL', 'Lucene'];
}
return ['DQL', 'Lucene', 'PPL', 'SQL'];
},
};

Expand All @@ -73,19 +75,51 @@ const fetchIndexPatterns = async (client: SavedObjectsClientContract): Promise<D
searchFields: ['title'],
perPage: 100,
});
return resp.savedObjects.map((savedObject) => ({
id: savedObject.id,
title: savedObject.attributes.title,
timeFieldName: savedObject.attributes.timeFieldName,
type: DEFAULT_DATA.SET_TYPES.INDEX_PATTERN,
...(savedObject.references[0]
? {
dataSource: {
id: savedObject.references[0]?.id,
name: savedObject.references[0]?.name,
type: 'data-source',
},
}
: {}),
}));

// Get all unique data source ids
const datasourceIds = Array.from(
new Set(
resp.savedObjects
.filter((savedObject) => savedObject.references.length > 0)
.map((savedObject) => savedObject.references.find((ref) => ref.type === 'data-source')?.id)
.filter(Boolean)
)
) as string[];

const dataSourceMap: Record<string, DataSourceAttributes> = {};
if (datasourceIds.length > 0) {
const dataSourceResp = await client.bulkGet<DataSourceAttributes>(
datasourceIds.map((id) => ({ id, type: 'data-source' }))
);

dataSourceResp.savedObjects.forEach((savedObject) => {
dataSourceMap[savedObject.id] = savedObject.attributes;
});
}

return resp.savedObjects.map(
(savedObject): DataStructure => {
const dataSourceId = savedObject.references.find((ref) => ref.type === 'data-source')?.id;
const dataSource = dataSourceId ? dataSourceMap[dataSourceId] : undefined;

const indexPatternDataStructure: DataStructure = {
id: savedObject.id,
title: savedObject.attributes.title,
type: DEFAULT_DATA.SET_TYPES.INDEX_PATTERN,
meta: {
type: DATA_STRUCTURE_META_TYPES.CUSTOM,
timeFieldName: savedObject.attributes.timeFieldName,
},
};

if (dataSource) {
indexPatternDataStructure.parent = {
id: dataSourceId!, // Since we know it exists
title: dataSource.title,
type: dataSource.dataSourceEngineType ?? 'OpenSearch',
};
}
return indexPatternDataStructure;
}
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

Expand All @@ -25,7 +25,7 @@ export const indexTypeConfig: DatasetTypeConfig = {
tooltip: 'OpenSearch Indexes',
},

toDataset: (path: DataStructure[]): Dataset => {
toDataset: (path) => {
const index = path[path.length - 1];
const dataSource = path.find((ds) => ds.type === 'DATA_SOURCE');

Expand All @@ -43,10 +43,7 @@ export const indexTypeConfig: DatasetTypeConfig = {
};
},

fetch: async (
savedObjects: SavedObjectsClientContract,
path: DataStructure[]
): Promise<DataStructure> => {
fetch: async (savedObjects, path) => {
const dataStructure = path[path.length - 1];
switch (dataStructure.type) {
case 'DATA_SOURCE': {
Expand Down Expand Up @@ -75,7 +72,7 @@ export const indexTypeConfig: DatasetTypeConfig = {
}
},

fetchFields: async (dataset: Dataset): Promise<DatasetField[]> => {
fetchFields: async (dataset) => {
const fields = await getIndexPatterns().getFieldsForWildcard({
pattern: dataset.title,
dataSourceId: dataset.dataSource?.id,
Expand All @@ -86,7 +83,7 @@ export const indexTypeConfig: DatasetTypeConfig = {
}));
},

supportedLanguages: (): string[] => {
supportedLanguages: (dataset: Dataset): string[] => {
return ['SQL', 'PPL', 'DQL', 'Lucene'];
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ export interface DatasetTypeConfig {
* Retrieves the supported query languages for this dataset type.
* @returns {Promise<string[]>} A promise that resolves to an array of supported language names.
*/
supportedLanguages: () => string[];
supportedLanguages: (dataset: Dataset) => string[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const createSetupContractMock = () => {
getInitialQueryByLanguage: jest.fn(),
getDatasetService: jest.fn(),
getLanguageService: jest.fn(),
getInitialQueryByDataset: jest.fn(),
};
return queryStringManagerMock;
};
Expand Down
Loading

0 comments on commit 2ed09f7

Please sign in to comment.