From 568e6317372b72ffa45c899d378be9c252273259 Mon Sep 17 00:00:00 2001 From: Yaroslav Kuznietsov Date: Tue, 19 Jul 2022 09:50:06 +0300 Subject: [PATCH] [Dashboard] Fixes Copy to dashboard includes filters. (#136275) * Fixed applying of the pinned filters. * Filtered unrelated filters to dashboard with saving the filters state. * Added handling filters from bwc urls. * Changed the code, according to the nit. Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- .../hooks/use_dashboard_app_state.test.tsx | 1 + .../lib/convert_dashboard_state.ts | 21 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/plugins/dashboard/public/application/hooks/use_dashboard_app_state.test.tsx b/src/plugins/dashboard/public/application/hooks/use_dashboard_app_state.test.tsx index 99016cbe2fe46..2f906127b9229 100644 --- a/src/plugins/dashboard/public/application/hooks/use_dashboard_app_state.test.tsx +++ b/src/plugins/dashboard/public/application/hooks/use_dashboard_app_state.test.tsx @@ -70,6 +70,7 @@ const createDashboardAppStateServices = () => { const data = dataPluginMock.createStartContract(); data.query.filterManager.getUpdates$ = jest.fn().mockImplementation(() => of(void 0)); + data.query.filterManager.getFilters = jest.fn().mockImplementation(() => []); data.query.queryString.getUpdates$ = jest.fn().mockImplementation(() => of({})); data.query.timefilter.timefilter.getTimeUpdate$ = jest.fn().mockImplementation(() => of(void 0)); data.query.timefilter.timefilter.getRefreshIntervalUpdate$ = jest diff --git a/src/plugins/dashboard/public/application/lib/convert_dashboard_state.ts b/src/plugins/dashboard/public/application/lib/convert_dashboard_state.ts index 24d891c658056..22af4f5d3a022 100644 --- a/src/plugins/dashboard/public/application/lib/convert_dashboard_state.ts +++ b/src/plugins/dashboard/public/application/lib/convert_dashboard_state.ts @@ -9,6 +9,13 @@ import _ from 'lodash'; import type { KibanaExecutionContext } from '@kbn/core/public'; import { ControlGroupInput } from '@kbn/controls-plugin/public'; +import { + compareFilters, + isFilterPinned, + migrateFilter, + COMPARE_ALL_OPTIONS, + type Filter, +} from '@kbn/es-query'; import { DashboardSavedObject } from '../../saved_dashboards'; import { getTagsFromSavedDashboard, migrateAppState } from '.'; import { EmbeddablePackageState, ViewMode } from '../../services/embeddable'; @@ -110,11 +117,20 @@ export const stateToDashboardContainerInput = ({ query, title, timeRestore, + filters: dashboardFilters, } = dashboardState; return { refreshConfig: timefilter.getRefreshInterval(), - filters: filterManager.getFilters(), + filters: filterManager + .getFilters() + .filter( + (filter) => + isFilterPinned(filter) || + dashboardFilters.some((dashboardFilter) => + filtersAreEqual(migrateFilter(_.cloneDeep(dashboardFilter)), filter) + ) + ), isFullScreenMode: fullScreenMode, id: savedDashboard.id || '', dashboardCapabilities, @@ -136,6 +152,9 @@ export const stateToDashboardContainerInput = ({ }; }; +const filtersAreEqual = (first: Filter, second: Filter) => + compareFilters(first, second, { ...COMPARE_ALL_OPTIONS, state: false }); + /** * Converts a given dashboard state object to raw dashboard state. This is useful for sharing, and session restoration, as * they require panels to be formatted as an array.