Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support injecting DataStructureMeta from QueryEditorExtensions for Query Assist #7871

Merged
merged 5 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/7871.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feat:
- Support injecting `DataStructureMeta` from `QueryEditorExtensions` for Query Assist ([#7871](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7871))
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
- [Opensearch dashboards.release notes 1.3.14](../release-notes/opensearch-dashboards.release-notes-1.3.14.md)
- [Opensearch dashboards.release notes 1.3.15](../release-notes/opensearch-dashboards.release-notes-1.3.15.md)
- [Opensearch dashboards.release notes 1.3.17](../release-notes/opensearch-dashboards.release-notes-1.3.17.md)
- [Opensearch dashboards.release notes 1.3.19](../release-notes/opensearch-dashboards.release-notes-1.3.19.md)
- [Opensearch dashboards.release notes 1.3.2](../release-notes/opensearch-dashboards.release-notes-1.3.2.md)
- [Opensearch dashboards.release notes 1.3.3](../release-notes/opensearch-dashboards.release-notes-1.3.3.md)
- [Opensearch dashboards.release notes 1.3.5](../release-notes/opensearch-dashboards.release-notes-1.3.5.md)
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/data/common/datasets/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export enum DATA_STRUCTURE_META_TYPES {
*/
export interface DataStructureFeatureMeta {
type: DATA_STRUCTURE_META_TYPES.FEATURE;
icon?: string;
icon?: EuiIconProps;
tooltip?: string;
}

Expand All @@ -157,6 +157,8 @@ export interface DataStructureDataTypeMeta {
*/
export interface DataStructureCustomMeta {
type: DATA_STRUCTURE_META_TYPES.CUSTOM;
icon?: EuiIconProps;
tooltip?: string;
[key: string]: any;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
} from '../../../../../common';
import { DatasetTypeConfig } from '../types';
import { getIndexPatterns } from '../../../../services';
import { injectMetaToDataStructures } from './utils';

export const indexPatternTypeConfig: DatasetTypeConfig = {
id: DEFAULT_DATA.SET_TYPES.INDEX_PATTERN,
Expand Down Expand Up @@ -97,7 +98,7 @@
});
}

return resp.savedObjects.map(
const dataStructures = resp.savedObjects.map(

Check warning on line 101 in src/plugins/data/public/query/query_string/dataset_service/lib/index_pattern_type.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/dataset_service/lib/index_pattern_type.ts#L101

Added line #L101 was not covered by tests
(savedObject): DataStructure => {
const dataSourceId = savedObject.references.find((ref) => ref.type === 'data-source')?.id;
const dataSource = dataSourceId ? dataSourceMap[dataSourceId] : undefined;
Expand All @@ -122,4 +123,6 @@
return indexPatternDataStructure;
}
);

return injectMetaToDataStructures(dataStructures, (dataStructure) => dataStructure.parent?.id);

Check warning on line 127 in src/plugins/data/public/query/query_string/dataset_service/lib/index_pattern_type.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/dataset_service/lib/index_pattern_type.ts#L127

Added line #L127 was not covered by tests
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { DEFAULT_DATA, DataStructure, Dataset } from '../../../../../common';
import { DatasetTypeConfig } from '../types';
import { getSearchService, getIndexPatterns } from '../../../../services';
import { injectMetaToDataStructures } from './utils';

const INDEX_INFO = {
LOCAL_DATASOURCE: {
Expand Down Expand Up @@ -93,14 +94,15 @@
type: 'data-source',
perPage: 10000,
});
const dataSources: DataStructure[] = [INDEX_INFO.LOCAL_DATASOURCE];
return dataSources.concat(
const dataSources: DataStructure[] = [INDEX_INFO.LOCAL_DATASOURCE].concat(

Check warning on line 97 in src/plugins/data/public/query/query_string/dataset_service/lib/index_type.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/dataset_service/lib/index_type.ts#L97

Added line #L97 was not covered by tests
resp.savedObjects.map((savedObject) => ({
id: savedObject.id,
title: savedObject.attributes.title,
type: 'DATA_SOURCE',
}))
);

return injectMetaToDataStructures(dataSources);

Check warning on line 105 in src/plugins/data/public/query/query_string/dataset_service/lib/index_type.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/dataset_service/lib/index_type.ts#L105

Added line #L105 was not covered by tests
};

const fetchIndices = async (dataStructure: DataStructure): Promise<string[]> => {
Expand Down
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker but can we add some tests for this function?

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { DataStructure, DataStructureMeta } from '../../../../../common';
import { getQueryService } from '../../../../services';

/**
* Inject {@link DataStructureMeta} to DataStructures based on
* {@link QueryEditorExtensions}.
*
* This function combines the meta fields from QueryEditorExtensions and
* provided data structures. Lower extension order is higher priority, and
* existing meta fields have highest priority.
*
* @param dataStructures - {@link DataStructure}
* @param selectDataSourceId - function to get data source id given a data structure
* @returns data structures with meta
*/
export const injectMetaToDataStructures = async (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ashwin-pc are we sure we don't want to do convert the dataset config to a class.

I was thinking it would be similar to the the interceptor class:

https://github.com/opensearch-project/OpenSearch-Dashboards/blob/main/src/plugins/data/public/search/search_interceptor.ts#L57

This way we don't have to ensure developers import this in their configs.

@joshuali925 could implement this in the base class and then it would be available for classes that extend the base class

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree. will think about it later, right now just trying to add instead of refactor..

dataStructures: DataStructure[],
selectDataSourceId: (dataStructure: DataStructure) => string | undefined = (
dataStructure: DataStructure
) => dataStructure.id

Check warning on line 25 in src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts#L25

Added line #L25 was not covered by tests
) => {
const queryEditorExtensions = Object.values(

Check warning on line 27 in src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts#L27

Added line #L27 was not covered by tests
getQueryService().queryString.getLanguageService().getQueryEditorExtensionMap()
);
queryEditorExtensions.sort((a, b) => b.order - a.order);

Check warning on line 30 in src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts#L30

Added line #L30 was not covered by tests

return Promise.all(

Check warning on line 32 in src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts#L32

Added line #L32 was not covered by tests
dataStructures.map(async (dataStructure) => {
const metaArray = await Promise.allSettled(

Check warning on line 34 in src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts#L34

Added line #L34 was not covered by tests
queryEditorExtensions.map((curr) =>
curr.getDataStructureMeta?.(selectDataSourceId(dataStructure))

Check warning on line 36 in src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts#L36

Added line #L36 was not covered by tests
)
).then((settledResults) =>
settledResults

Check warning on line 39 in src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts#L39

Added line #L39 was not covered by tests
.filter(
<T>(result: PromiseSettledResult<T>): result is PromiseFulfilledResult<T> =>
result.status === 'fulfilled'

Check warning on line 42 in src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts#L42

Added line #L42 was not covered by tests
)
.map((result) => result.value)

Check warning on line 44 in src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts#L44

Added line #L44 was not covered by tests
);
const meta = metaArray.reduce(

Check warning on line 46 in src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts#L46

Added line #L46 was not covered by tests
(acc, curr) => (acc || curr ? ({ ...acc, ...curr } as DataStructureMeta) : undefined),
undefined
);
if (meta || dataStructure.meta) {
dataStructure.meta = { ...meta, ...dataStructure.meta } as DataStructureMeta;

Check warning on line 51 in src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts#L51

Added line #L51 was not covered by tests
}
return dataStructure;

Check warning on line 53 in src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/dataset_service/lib/utils.ts#L53

Added line #L53 was not covered by tests
})
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ export const AdvancedSelector = ({
.getTypes()
.map((type) => {
return {
id: type!.id,
title: type!.title,
type: type!.id,
id: type.id,
title: type.title,
type: type.id,
meta: {
...type!.meta,
...type.meta,
type: DATA_STRUCTURE_META_TYPES.TYPE,
},
} as DataStructure;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,26 +190,24 @@ const LoadingEmptyColumn = ({ isLoading }: { isLoading: boolean }) =>
<EmptyColumn />
);
const appendIcon = (item: DataStructure) => {
if (item.meta?.type === DATA_STRUCTURE_META_TYPES.FEATURE) {
if (item.meta?.type === DATA_STRUCTURE_META_TYPES.TYPE) {
return (
<EuiToolTip content={item.meta.tooltip}>
<EuiIcon type="iInCircle" />
</EuiToolTip>
);
} else {
if (item.meta?.icon && item.meta?.tooltip) {
return (
<EuiToolTip content={item.meta.tooltip}>
<EuiIcon type={item.meta.icon} />
<EuiIcon {...item.meta.icon} />
</EuiToolTip>
);
} else if (item.meta?.icon) {
return <EuiIcon type={item.meta.icon} />;
return <EuiIcon {...item.meta.icon} />;
}
}

if (item.meta?.type === DATA_STRUCTURE_META_TYPES.TYPE) {
return (
<EuiToolTip content={item.meta.tooltip}>
<EuiIcon type="iInCircle" />
</EuiToolTip>
);
}

return null;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { EuiErrorBoundary } from '@elastic/eui';
import React, { useEffect, useMemo, useRef, useState } from 'react';
import ReactDOM from 'react-dom';
import { Observable } from 'rxjs';
import { DataStructureMeta } from '../../../../common';

interface QueryEditorExtensionProps {
config: QueryEditorExtensionConfig;
Expand Down Expand Up @@ -48,6 +49,12 @@ export interface QueryEditorExtensionConfig {
* @returns whether the extension is enabled.
*/
isEnabled$: (dependencies: QueryEditorExtensionDependencies) => Observable<boolean>;
/**
* @returns DataStructureMeta for a given data source id.
*/
getDataStructureMeta?: (
dataSourceId: string | undefined
) => Promise<DataStructureMeta | undefined>;
/**
* A function that returns the query editor extension component. The component
* will be displayed on top of the query editor in the search bar.
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/query_enhancements/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class QueryEnhancementsPlugin
queryString.getLanguageService().registerLanguage(sqlLanguageConfig);

data.__enhance({
ui: {
editor: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry about that. Thanks for fixing.

queryEditorExtension: createQueryAssistExtension(core.http, data, this.config.queryAssist),
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { HttpSetup } from 'opensearch-dashboards/public';
import React, { useEffect, useState } from 'react';
import { distinctUntilChanged, map, startWith, switchMap } from 'rxjs/operators';
import { DEFAULT_DATA } from '../../../../data/common';
import { DATA_STRUCTURE_META_TYPES, DEFAULT_DATA } from '../../../../data/common';
import {
DataPublicPluginSetup,
QueryEditorExtensionConfig,
Expand All @@ -15,16 +15,32 @@
import { API } from '../../../common';
import { ConfigSchema } from '../../../common/config';
import { QueryAssistBanner, QueryAssistBar } from '../components';
import assistantMark from '../../assets/query_assist_mark.svg';

/**
* @returns list of query assist supported languages for the given data source.
*/
const getAvailableLanguagesForDataSource = (() => {
const availableLanguagesByDataSource: Map<string | undefined, string[]> = new Map();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice way to cache the data

return async (http: HttpSetup, dataSourceId: string | undefined) => {
const cached = availableLanguagesByDataSource.get(dataSourceId);

Check warning on line 26 in src/plugins/query_enhancements/public/query_assist/utils/create_extension.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/query_enhancements/public/query_assist/utils/create_extension.tsx#L26

Added line #L26 was not covered by tests
if (cached !== undefined) return cached;
const languages = await http

Check warning on line 28 in src/plugins/query_enhancements/public/query_assist/utils/create_extension.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/query_enhancements/public/query_assist/utils/create_extension.tsx#L28

Added line #L28 was not covered by tests
.get<{ configuredLanguages: string[] }>(API.QUERY_ASSIST.LANGUAGES, {
query: { dataSourceId },
})
.then((response) => response.configuredLanguages)
.catch(() => []);
availableLanguagesByDataSource.set(dataSourceId, languages);
return languages;

Check warning on line 35 in src/plugins/query_enhancements/public/query_assist/utils/create_extension.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/query_enhancements/public/query_assist/utils/create_extension.tsx#L32-L35

Added lines #L32 - L35 were not covered by tests
};
})();

/**
* @returns observable list of query assist agent configured languages in the
* selected data source.
*/
const getAvailableLanguages$ = (
availableLanguagesByDataSource: Map<string | undefined, string[]>,
http: HttpSetup,
data: DataPublicPluginSetup
) =>
const getAvailableLanguages$ = (http: HttpSetup, data: DataPublicPluginSetup) =>
data.query.queryString.getUpdates$().pipe(
startWith(data.query.queryString.getQuery()),
distinctUntilChanged(),
Expand All @@ -34,16 +50,7 @@
if (query.dataset?.dataSource?.type !== DEFAULT_DATA.SOURCE_TYPES.OPENSEARCH) return [];

const dataSourceId = query.dataset?.dataSource?.id;
const cached = availableLanguagesByDataSource.get(dataSourceId);
if (cached !== undefined) return cached;
const languages = await http
.get<{ configuredLanguages: string[] }>(API.QUERY_ASSIST.LANGUAGES, {
query: { dataSourceId },
})
.then((response) => response.configuredLanguages)
.catch(() => []);
availableLanguagesByDataSource.set(dataSourceId, languages);
return languages;
return getAvailableLanguagesForDataSource(http, dataSourceId);

Check warning on line 53 in src/plugins/query_enhancements/public/query_assist/utils/create_extension.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/query_enhancements/public/query_assist/utils/create_extension.tsx#L53

Added line #L53 was not covered by tests
})
);

Expand All @@ -52,38 +59,35 @@
data: DataPublicPluginSetup,
config: ConfigSchema['queryAssist']
): QueryEditorExtensionConfig => {
const availableLanguagesByDataSource: Map<string | undefined, string[]> = new Map();

return {
id: 'query-assist',
order: 1000,
getDataStructureMeta: async (dataSourceId) => {
const isEnabled = await getAvailableLanguagesForDataSource(http, dataSourceId).then(
(languages) => languages.length > 0

Check warning on line 67 in src/plugins/query_enhancements/public/query_assist/utils/create_extension.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/query_enhancements/public/query_assist/utils/create_extension.tsx#L66-L67

Added lines #L66 - L67 were not covered by tests
);
if (isEnabled) {
return {

Check warning on line 70 in src/plugins/query_enhancements/public/query_assist/utils/create_extension.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/query_enhancements/public/query_assist/utils/create_extension.tsx#L70

Added line #L70 was not covered by tests
type: DATA_STRUCTURE_META_TYPES.FEATURE,
icon: { type: assistantMark },
tooltip: 'Query assist is available',
};
}
},
isEnabled$: () =>
getAvailableLanguages$(availableLanguagesByDataSource, http, data).pipe(
map((languages) => languages.length > 0)
),
getAvailableLanguages$(http, data).pipe(map((languages) => languages.length > 0)),

Check warning on line 78 in src/plugins/query_enhancements/public/query_assist/utils/create_extension.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/query_enhancements/public/query_assist/utils/create_extension.tsx#L78

Added line #L78 was not covered by tests
getComponent: (dependencies) => {
// only show the component if user is on a supported language.
return (
<QueryAssistWrapper
availableLanguagesByDataSource={availableLanguagesByDataSource}
dependencies={dependencies}
http={http}
data={data}
>
<QueryAssistWrapper dependencies={dependencies} http={http} data={data}>
<QueryAssistBar dependencies={dependencies} />
</QueryAssistWrapper>
);
},
getBanner: (dependencies) => {
// advertise query assist if user is not on a supported language.
return (
<QueryAssistWrapper
availableLanguagesByDataSource={availableLanguagesByDataSource}
dependencies={dependencies}
http={http}
data={data}
invert
>
<QueryAssistWrapper dependencies={dependencies} http={http} data={data} invert>
<QueryAssistBanner
dependencies={dependencies}
languages={config.supportedLanguages.map((conf) => conf.language)}
Expand All @@ -95,7 +99,6 @@
};

interface QueryAssistWrapperProps {
availableLanguagesByDataSource: Map<string | undefined, string[]>;
dependencies: QueryEditorExtensionDependencies;
http: HttpSetup;
data: DataPublicPluginSetup;
Expand All @@ -108,11 +111,7 @@
useEffect(() => {
let mounted = true;

const subscription = getAvailableLanguages$(
props.availableLanguagesByDataSource,
props.http,
props.data
).subscribe((languages) => {
const subscription = getAvailableLanguages$(props.http, props.data).subscribe((languages) => {

Check warning on line 114 in src/plugins/query_enhancements/public/query_assist/utils/create_extension.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/query_enhancements/public/query_assist/utils/create_extension.tsx#L114

Added line #L114 was not covered by tests
const available = languages.includes(props.dependencies.language);
if (mounted) setVisible(props.invert ? !available : available);
});
Expand Down
Loading