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

[Backport 2.x] Fix Queries of Unsupported types appearing on Visualize & Vis Builder Search Bar #8656

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions src/plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
UiSettingsPublicToCommon,
} from './index_patterns';
import {
setApplication,
setFieldFormats,
setIndexPatterns,
setNotifications,
Expand Down Expand Up @@ -151,6 +152,7 @@
sessionStorage: this.sessionStorage,
defaultSearchInterceptor: searchService.getDefaultSearchInterceptor(),
application: core.application,
notifications: core.notifications,
});

uiActions.registerAction(
Expand Down Expand Up @@ -195,6 +197,7 @@
setNotifications(notifications);
setOverlays(overlays);
setUiSettings(uiSettings);
setApplication(application);

Check warning on line 200 in src/plugins/data/public/plugin.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/plugin.ts#L200

Added line #L200 was not covered by tests

const fieldFormats = this.fieldFormatsService.start();
setFieldFormats(fieldFormats);
Expand Down Expand Up @@ -226,6 +229,7 @@
uiSettings,
indexPatterns,
application: core.application,
notifications,
});
setQueryService(query);

Expand Down
4 changes: 3 additions & 1 deletion src/plugins/data/public/query/query_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export class QueryService {
storage,
sessionStorage,
defaultSearchInterceptor,
notifications,
}: QueryServiceSetupDependencies): IQuerySetup {
this.filterManager = new FilterManager(uiSettings);

Expand All @@ -75,7 +76,8 @@ export class QueryService {
storage,
sessionStorage,
uiSettings,
defaultSearchInterceptor
defaultSearchInterceptor,
notifications
);

this.state$ = createQueryStateObservable({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ export const getDQLLanguageConfig = (
},
showDocLinks: true,
editorSupportedAppNames: ['discover'],
supportedAppNames: ['discover', 'dashboards', 'visualize', 'data-explorer'],
supportedAppNames: ['discover', 'dashboards', 'visualize', 'data-explorer', 'vis-builder', '*'],
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ export const getLuceneLanguageConfig = (
},
showDocLinks: true,
editorSupportedAppNames: ['discover'],
supportedAppNames: ['discover', 'dashboards', 'visualize', 'data-explorer'],
supportedAppNames: ['discover', 'dashboards', 'visualize', 'data-explorer', 'vis-builder', '*'],
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@

import { BehaviorSubject } from 'rxjs';
import { skip } from 'rxjs/operators';
import { CoreStart } from 'opensearch-dashboards/public';
import { isEqual } from 'lodash';
import { CoreStart, NotificationsSetup } from 'opensearch-dashboards/public';
import { debounce, isEqual } from 'lodash';
import { i18n } from '@osd/i18n';
import { Dataset, DataStorage, Query, TimeRange, UI_SETTINGS } from '../../../common';
import { createHistory, QueryHistory } from './query_history';
import { DatasetService, DatasetServiceContract } from './dataset_service';
import { LanguageService, LanguageServiceContract } from './language_service';
import { ISearchInterceptor } from '../../search';
import { getApplication } from '../../services';

export class QueryStringManager {
private query$: BehaviorSubject<Query>;
Expand All @@ -48,7 +50,8 @@
private readonly storage: DataStorage,
private readonly sessionStorage: DataStorage,
private readonly uiSettings: CoreStart['uiSettings'],
private readonly defaultSearchInterceptor: ISearchInterceptor
private readonly defaultSearchInterceptor: ISearchInterceptor,
private readonly notifications: NotificationsSetup
) {
this.query$ = new BehaviorSubject<Query>(this.getDefaultQuery());
this.queryHistory = createHistory({ storage });
Expand Down Expand Up @@ -108,6 +111,40 @@
};

public getQuery = (): Query => {
const currentAppId = this.getCurrentAppId();
const query = this.query$.getValue();

if (currentAppId) {
const currentLanguage = query.language;

Check warning on line 118 in src/plugins/data/public/query/query_string/query_string_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/query_string_manager.ts#L118

Added line #L118 was not covered by tests
if (
containsWildcardOrValue(
this.languageService.getLanguage(currentLanguage)?.supportedAppNames,
currentAppId
)
) {
return this.query$.getValue();

Check warning on line 125 in src/plugins/data/public/query/query_string/query_string_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/query_string_manager.ts#L125

Added line #L125 was not covered by tests
}

const defaultLanguage = this.uiSettings.get('search:queryLanguage');
const defaultLanguageTitle = this.languageService.getLanguage(defaultLanguage)?.title;

Check warning on line 129 in src/plugins/data/public/query/query_string/query_string_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/query_string_manager.ts#L128-L129

Added lines #L128 - L129 were not covered by tests

showWarning(this.notifications, {

Check warning on line 131 in src/plugins/data/public/query/query_string/query_string_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/query_string_manager.ts#L131

Added line #L131 was not covered by tests
title: i18n.translate('data.unSupportedLanguageTitle', {
defaultMessage: 'Unsupported Language Selected',
}),
text: i18n.translate('data.unSupportedLanguageBody', {
defaultMessage:
'Selected language {currentLanguage} is not supported. Defaulting to {defaultLanguage}.',
values: {
currentLanguage,
defaultLanguage: defaultLanguageTitle,
},
}),
});

const updatedQuery = this.getInitialQueryByLanguage(defaultLanguage);
this.setQuery(updatedQuery);

Check warning on line 146 in src/plugins/data/public/query/query_string/query_string_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/query_string_manager.ts#L145-L146

Added lines #L145 - L146 were not covered by tests
}
return this.query$.getValue();
};

Expand Down Expand Up @@ -194,6 +231,29 @@
this.uiSettings.get(UI_SETTINGS.SEARCH_QUERY_LANGUAGE)
);
}

private getCurrentAppId = () => {
let appId;
try {
const application = getApplication();
if (application) {
application.currentAppId$.subscribe((val) => (appId = val)).unsubscribe();

Check warning on line 240 in src/plugins/data/public/query/query_string/query_string_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/query_string_manager.ts#L240

Added line #L240 was not covered by tests
}
} catch (err) {
// eslint-disable-next-line no-console
console.log('Application Not available.');
}

return appId;
};
}

const showWarning = (notifications: NotificationsSetup, { title, text }) => {
notifications.toasts.addWarning({ title, text, id: 'unsupported_language_selected' });

Check warning on line 252 in src/plugins/data/public/query/query_string/query_string_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/query_string_manager.ts#L252

Added line #L252 was not covered by tests
};

const containsWildcardOrValue = (arr: string[] | undefined, value: string) => {
return arr ? arr.includes('*') || arr.includes(value) : true;
};

export type QueryStringContract = PublicMethodsOf<QueryStringManager>;
4 changes: 4 additions & 0 deletions src/plugins/data/public/query/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
ApplicationSetup,
ApplicationStart,
IUiSettingsClient,
NotificationsSetup,
NotificationsStart,
SavedObjectsClientContract,
} from 'opensearch-dashboards/public';
import { Observable } from 'rxjs';
Expand Down Expand Up @@ -44,6 +46,7 @@ export interface QueryServiceSetupDependencies {
sessionStorage: DataStorage;
defaultSearchInterceptor: ISearchInterceptor;
application: ApplicationSetup;
notifications: NotificationsSetup;
}

/** @internal */
Expand All @@ -53,4 +56,5 @@ export interface QueryServiceStartDependencies {
uiSettings: IUiSettingsClient;
indexPatterns: IndexPatternsService;
application: ApplicationStart;
notifications: NotificationsStart;
}
4 changes: 3 additions & 1 deletion src/plugins/data/public/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* under the License.
*/

import { NotificationsStart, CoreStart } from 'src/core/public';
import { NotificationsStart, CoreStart, ApplicationStart } from 'src/core/public';
import { FieldFormatsStart } from './field_formats';
import { createGetterSetter } from '../../opensearch_dashboards_utils/public';
import { IndexPatternsContract } from './index_patterns';
Expand Down Expand Up @@ -61,3 +61,5 @@ export const [getSearchService, setSearchService] = createGetterSetter<
>('Search');

export const [getUiService, setUiService] = createGetterSetter<DataPublicPluginStart['ui']>('Ui');

export const [getApplication, setApplication] = createGetterSetter<ApplicationStart>('Application');
Loading