Skip to content

Commit

Permalink
Filter Saved Query by supported language (opensearch-project#8325)
Browse files Browse the repository at this point in the history
Signed-off-by: Suchit Sahoo <suchsah@amazon.com>
  • Loading branch information
LDrago27 authored Sep 26, 2024
1 parent 1db585d commit 33f4f64
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export class DataPublicPlugin
storage: this.storage,
sessionStorage: this.sessionStorage,
defaultSearchInterceptor: searchService.getDefaultSearchInterceptor(),
applicaton: core.application,
});

uiActions.registerAction(
Expand Down Expand Up @@ -224,6 +225,7 @@ export class DataPublicPlugin
savedObjectsClient: savedObjects.client,
uiSettings,
indexPatterns,
applicaton: core.application,
});
setQueryService(query);

Expand Down
7 changes: 6 additions & 1 deletion src/plugins/data/public/query/query_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class QueryService {
storage,
uiSettings,
indexPatterns,
applicaton,
}: QueryServiceStartDependencies): IQueryStart {
this.queryStringManager.getDatasetService().init(indexPatterns);
return {
Expand All @@ -106,7 +107,11 @@ export class QueryService {
}),
filterManager: this.filterManager,
queryString: this.queryStringManager,
savedQueries: createSavedQueryService(savedObjectsClient),
savedQueries: createSavedQueryService(
savedObjectsClient,
this.queryStringManager,
applicaton
),
state$: this.state$,
timefilter: this.timefilter,
getOpenSearchQuery: (indexPattern: IndexPattern) => {
Expand Down
33 changes: 27 additions & 6 deletions src/plugins/data/public/query/saved_query/saved_query_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
*/

import { isObject } from 'lodash';
import { SavedObjectsClientContract, SavedObjectAttributes } from 'src/core/public';
import { SavedObjectsClientContract, SavedObjectAttributes, CoreStart } from 'src/core/public';
import { first } from 'rxjs/operators';
import { SavedQueryAttributes, SavedQuery, SavedQueryService } from './types';
import { QueryStringContract } from '../query_string';

type SerializedSavedQueryAttributes = SavedObjectAttributes &
SavedQueryAttributes & {
Expand All @@ -41,7 +43,9 @@ type SerializedSavedQueryAttributes = SavedObjectAttributes &
};

export const createSavedQueryService = (
savedObjectsClient: SavedObjectsClientContract
savedObjectsClient: SavedObjectsClientContract,
queryStringManager?: QueryStringContract,
application?: CoreStart['application']
): SavedQueryService => {
const saveQuery = async (attributes: SavedQueryAttributes, { overwrite = false } = {}) => {
if (!attributes.title.length) {
Expand Down Expand Up @@ -117,12 +121,29 @@ export const createSavedQueryService = (
page: activePage,
});

let queries = response.savedObjects.map(
(savedObject: { id: string; attributes: SerializedSavedQueryAttributes }) =>
parseSavedQueryObject(savedObject)
);

const currentAppId =
(await application?.currentAppId$?.pipe(first()).toPromise()) ?? Promise.resolve(undefined);
const languageService = queryStringManager?.getLanguageService();

// Filtering saved queries based on language supported by cirrent application
if (currentAppId && languageService) {
queries = queries.filter((query) => {
const languageId = query.attributes.query.language;
return (
languageService?.getLanguage(languageId)?.supportedAppNames?.includes(currentAppId) ??
true
);
});
}

return {
total: response.total,
queries: response.savedObjects.map(
(savedObject: { id: string; attributes: SerializedSavedQueryAttributes }) =>
parseSavedQueryObject(savedObject)
),
queries,
};
};

Expand Down
9 changes: 8 additions & 1 deletion src/plugins/data/public/query/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { IUiSettingsClient, SavedObjectsClientContract } from 'opensearch-dashboards/public';
import {
ApplicationSetup,
ApplicationStart,
IUiSettingsClient,
SavedObjectsClientContract,
} from 'opensearch-dashboards/public';
import { Observable } from 'rxjs';
import { DataStorage } from '../../common';
import { IndexPattern, IndexPatternsService } from '../index_patterns';
Expand Down Expand Up @@ -38,6 +43,7 @@ export interface QueryServiceSetupDependencies {
storage: DataStorage;
sessionStorage: DataStorage;
defaultSearchInterceptor: ISearchInterceptor;
applicaton: ApplicationSetup;
}

/** @internal */
Expand All @@ -46,4 +52,5 @@ export interface QueryServiceStartDependencies {
storage: DataStorage;
uiSettings: IUiSettingsClient;
indexPatterns: IndexPatternsService;
applicaton: ApplicationStart;
}

0 comments on commit 33f4f64

Please sign in to comment.