From ce26f7bc2d8847d715843d60090a41bd06846c56 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 30 Apr 2024 16:40:29 +0000 Subject: [PATCH] [VisBuilder ] Fix filter and query bugs in the saved objects (#6460) * [VisBuilder] Fix filter and query bugs In this PR: add filter and query in vb clean filterManager when start vb add opensearch_dashboards_context to embeddable. This can add filter and query in the expression. Issue Resolve https://github.com/opensearch-project/OpenSearch-Dashboards/issues/5643 https://github.com/opensearch-project/OpenSearch-Dashboards/issues/5644 https://github.com/opensearch-project/OpenSearch-Dashboards/issues/5646 https://github.com/opensearch-project/OpenSearch-Dashboards/issues/6512 Signed-off-by: Anan Zhuang --------- Signed-off-by: Anan Zhuang Co-authored-by: Miki (cherry picked from commit 0ac9db37b4e94973a819e3d38159d0f8baa81344) Signed-off-by: github-actions[bot] # Conflicts: # CHANGELOG.md --- .../utils/get_top_nav_config.test.tsx | 5 ++- .../application/utils/get_top_nav_config.tsx | 5 ++- .../utils/use/use_saved_vis_builder_vis.ts | 12 +++++++ .../embeddable/vis_builder_embeddable.tsx | 34 +++++++++++++++---- src/plugins/vis_builder/public/plugin.ts | 4 +++ .../public/saved_visualizations/transforms.ts | 8 +++-- src/plugins/visualizations/public/index.ts | 1 + 7 files changed, 58 insertions(+), 11 deletions(-) diff --git a/src/plugins/vis_builder/public/application/utils/get_top_nav_config.test.tsx b/src/plugins/vis_builder/public/application/utils/get_top_nav_config.test.tsx index 353b9d90e1f..536596cc9e1 100644 --- a/src/plugins/vis_builder/public/application/utils/get_top_nav_config.test.tsx +++ b/src/plugins/vis_builder/public/application/utils/get_top_nav_config.test.tsx @@ -95,7 +95,10 @@ describe('getOnSave', () => { }, ], }, - "searchSourceFields": Object {}, + "searchSourceFields": Object { + "filter": null, + "query": null, + }, "styleState": "", "title": "new title", "version": 0, diff --git a/src/plugins/vis_builder/public/application/utils/get_top_nav_config.tsx b/src/plugins/vis_builder/public/application/utils/get_top_nav_config.tsx index 2a30e1700b4..945e09a0973 100644 --- a/src/plugins/vis_builder/public/application/utils/get_top_nav_config.tsx +++ b/src/plugins/vis_builder/public/application/utils/get_top_nav_config.tsx @@ -172,7 +172,7 @@ export const getOnSave = ( returnToOrigin: boolean; newDescription?: string; }) => { - const { embeddable, toastNotifications, application, history } = services; + const { data, embeddable, toastNotifications, application, history } = services; const stateTransfer = embeddable.getStateTransfer(); if (!savedVisBuilderVis) { @@ -183,6 +183,9 @@ export const getOnSave = ( savedVisBuilderVis.title = newTitle; savedVisBuilderVis.description = newDescription; savedVisBuilderVis.copyOnSave = newCopyOnSave; + const searchSourceInstance = savedVisBuilderVis.searchSourceFields; + searchSourceInstance.query = data.query.queryString.getQuery() || null; + searchSourceInstance.filter = data.query.filterManager.getFilters() || null; const newlyCreated = !savedVisBuilderVis.id || savedVisBuilderVis.copyOnSave; try { diff --git a/src/plugins/vis_builder/public/application/utils/use/use_saved_vis_builder_vis.ts b/src/plugins/vis_builder/public/application/utils/use/use_saved_vis_builder_vis.ts index 44ffbaf7595..3ec69902c9c 100644 --- a/src/plugins/vis_builder/public/application/utils/use/use_saved_vis_builder_vis.ts +++ b/src/plugins/vis_builder/public/application/utils/use/use_saved_vis_builder_vis.ts @@ -35,6 +35,7 @@ export const useSavedVisBuilderVis = (visualizationIdFromUrl: string | undefined const { application: { navigateToApp }, chrome, + data, history, http: { basePath }, toastNotifications, @@ -61,6 +62,17 @@ export const useSavedVisBuilderVis = (visualizationIdFromUrl: string | undefined const { title, state } = getStateFromSavedObject(savedVisBuilderVis); chrome.setBreadcrumbs(getEditBreadcrumbs(title, navigateToApp)); chrome.docTitle.change(title); + // sync initial app filters from savedObject to filterManager + const filters = savedVisBuilderVis.searchSourceFields.filter; + const query = + savedVisBuilderVis.searchSourceFields.query || data.query.queryString.getDefaultQuery(); + const actualFilters = []; + const tempFilters = typeof filters === 'function' ? filters() : filters; + (Array.isArray(tempFilters) ? tempFilters : [tempFilters]).forEach((filter) => { + if (filter) actualFilters.push(filter); + }); + data.query.filterManager.setAppFilters(actualFilters); + data.query.queryString.setQuery(query); dispatch(setUIStateState(state.ui)); dispatch(setStyleState(state.style)); diff --git a/src/plugins/vis_builder/public/embeddable/vis_builder_embeddable.tsx b/src/plugins/vis_builder/public/embeddable/vis_builder_embeddable.tsx index a931877ffe6..38b84452ff4 100644 --- a/src/plugins/vis_builder/public/embeddable/vis_builder_embeddable.tsx +++ b/src/plugins/vis_builder/public/embeddable/vis_builder_embeddable.tsx @@ -35,7 +35,7 @@ import { getTypeService, getUIActions, } from '../plugin_services'; -import { PersistedState } from '../../../visualizations/public'; +import { PersistedState, prepareJson } from '../../../visualizations/public'; import { VisBuilderSavedVis } from '../saved_visualizations/transforms'; import { handleVisEvent } from '../application/utils/handle_vis_event'; import { VisBuilderEmbeddableFactoryDeps } from './vis_builder_embeddable_factory'; @@ -246,6 +246,28 @@ export class VisBuilderEmbeddable extends Embeddable { + extends Pick< + VisBuilderSavedObjectAttributes, + 'id' | 'title' | 'description' | 'searchSourceFields' + > { state: RenderState; } export const getStateFromSavedObject = ( obj: VisBuilderSavedObjectAttributes ): VisBuilderSavedVis => { - const { id, title, description } = obj; + const { id, title, description, searchSourceFields } = obj; const styleState = JSON.parse(obj.styleState || '{}'); const uiState = JSON.parse(obj.uiState || '{}'); const vizStateWithoutIndex = JSON.parse(obj.visualizationState || ''); @@ -74,6 +77,7 @@ export const getStateFromSavedObject = ( id, title, description, + searchSourceFields, state: { visualization: visualizationState, style: styleState, diff --git a/src/plugins/visualizations/public/index.ts b/src/plugins/visualizations/public/index.ts index b36c6087ec3..98fe5a5463b 100644 --- a/src/plugins/visualizations/public/index.ts +++ b/src/plugins/visualizations/public/index.ts @@ -77,3 +77,4 @@ export { ExprVisAPIEvents } from './expressions/vis'; export { VisualizationListItem } from './vis_types/vis_type_alias_registry'; export { VISUALIZE_ENABLE_LABS_SETTING } from '../common/constants'; export { createSavedVisLoader } from './saved_visualizations'; +export { prepareJson } from './legacy/build_pipeline';