From 7ec4f0b321b19f64e42d54cc4e4b692d7ec8cd26 Mon Sep 17 00:00:00 2001 From: Corey Robertson Date: Fri, 19 Feb 2021 11:36:37 -0500 Subject: [PATCH 1/4] Adds references from byValue panels when saving dashboard --- .../embeddable/embeddable_references.ts | 2 + .../visualize_embeddable_factory.tsx | 40 +++++++++++++++++++ .../embeddable/embeddable_factory.ts | 16 +++++++- .../embeddable/map_embeddable_factory.ts | 17 +++++++- 4 files changed, 72 insertions(+), 3 deletions(-) diff --git a/src/plugins/dashboard/common/embeddable/embeddable_references.ts b/src/plugins/dashboard/common/embeddable/embeddable_references.ts index 59ea238434fcf3..352a39310e60e4 100644 --- a/src/plugins/dashboard/common/embeddable/embeddable_references.ts +++ b/src/plugins/dashboard/common/embeddable/embeddable_references.ts @@ -51,6 +51,7 @@ export function extractPanelsReferences( for (const panel of panels) { const embeddable = convertSavedDashboardPanelToPanelState(panel); + const { state: embeddableInputWithExtractedReferences, references, @@ -58,6 +59,7 @@ export function extractPanelsReferences( ...embeddable.explicitInput, type: embeddable.type, }); + embeddable.explicitInput = omit(embeddableInputWithExtractedReferences, 'type'); const newPanel = convertPanelStateToSavedDashboardPanel(embeddable, panel.version); diff --git a/src/plugins/visualizations/public/embeddable/visualize_embeddable_factory.tsx b/src/plugins/visualizations/public/embeddable/visualize_embeddable_factory.tsx index a7a5b8626914af..349e024f31c315 100644 --- a/src/plugins/visualizations/public/embeddable/visualize_embeddable_factory.tsx +++ b/src/plugins/visualizations/public/embeddable/visualize_embeddable_factory.tsx @@ -9,7 +9,9 @@ import { i18n } from '@kbn/i18n'; import { SavedObjectMetaData, OnSaveProps } from 'src/plugins/saved_objects/public'; import { first } from 'rxjs/operators'; +import { EmbeddableStateWithType } from 'src/plugins/embeddable/common'; import { SavedObjectAttributes } from '../../../../core/public'; +import { extractSearchSourceReferences } from '../../../data/public'; import { EmbeddableFactoryDefinition, EmbeddableOutput, @@ -236,4 +238,42 @@ export class VisualizeEmbeddableFactory } ); } + + public extract(_state: EmbeddableStateWithType) { + const state = (_state as unknown) as VisualizeInput; + const references = []; + + if (state.savedVis?.data.searchSource) { + const [, searchSourceReferences] = extractSearchSourceReferences( + state.savedVis.data.searchSource + ); + + references.push(...searchSourceReferences); + } + + if (state.savedVis?.data.savedSearchId) { + references.push({ + name: 'search_0', + type: 'search', + id: String(state.savedVis.data.savedSearchId), + }); + } + + if (state.savedVis?.params.controls) { + const controls = state.savedVis.params.controls; + controls.forEach((control: Record, i: number) => { + if (!control.indexPattern) { + return; + } + control.indexPatternRefName = `control_${i}_index_pattern`; + references.push({ + name: control.indexPatternRefName, + type: 'index-pattern', + id: control.indexPattern, + }); + }); + } + + return { state: _state, references }; + } } diff --git a/x-pack/plugins/lens/public/editor_frame_service/embeddable/embeddable_factory.ts b/x-pack/plugins/lens/public/editor_frame_service/embeddable/embeddable_factory.ts index 4c40282012d6db..be3daaf92b9de7 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/embeddable/embeddable_factory.ts +++ b/x-pack/plugins/lens/public/editor_frame_service/embeddable/embeddable_factory.ts @@ -5,10 +5,11 @@ * 2.0. */ -import { Capabilities, HttpSetup } from 'kibana/public'; +import { Capabilities, HttpSetup, SavedObjectReference } from 'kibana/public'; import { i18n } from '@kbn/i18n'; import { RecursiveReadonly } from '@kbn/utility-types'; import { Ast } from '@kbn/interpreter/target/common'; +import { EmbeddableStateWithType } from 'src/plugins/embeddable/common'; import { IndexPatternsContract, TimefilterContract, @@ -18,7 +19,7 @@ import { EmbeddableFactoryDefinition, IContainer, } from '../../../../../../src/plugins/embeddable/public'; -import { LensByReferenceInput, LensEmbeddableInput } from './embeddable'; +import { LensByReferenceInput, LensByValueInput, LensEmbeddableInput } from './embeddable'; import { UiActionsStart } from '../../../../../../src/plugins/ui_actions/public'; import { Document } from '../../persistence/saved_object_store'; import { LensAttributeService } from '../../lens_attribute_service'; @@ -105,4 +106,15 @@ export class EmbeddableFactory implements EmbeddableFactoryDefinition { parent ); } + + extract(state: EmbeddableStateWithType) { + let references: SavedObjectReference[] = []; + const typedState = (state as unknown) as LensEmbeddableInput; + + if ((typedState as LensByValueInput).attributes !== undefined) { + references = (typedState as LensByValueInput).attributes.references; + } + + return { state, references }; + } } diff --git a/x-pack/plugins/maps/public/embeddable/map_embeddable_factory.ts b/x-pack/plugins/maps/public/embeddable/map_embeddable_factory.ts index b0390763054982..7e15bfa9a340e9 100644 --- a/x-pack/plugins/maps/public/embeddable/map_embeddable_factory.ts +++ b/x-pack/plugins/maps/public/embeddable/map_embeddable_factory.ts @@ -6,6 +6,7 @@ */ import { i18n } from '@kbn/i18n'; +import { EmbeddableStateWithType } from 'src/plugins/embeddable/common'; import { EmbeddableFactoryDefinition, IContainer, @@ -13,8 +14,10 @@ import { import '../index.scss'; import { MAP_SAVED_OBJECT_TYPE, APP_ICON } from '../../common/constants'; import { getMapEmbeddableDisplayName } from '../../common/i18n_getters'; -import { MapByReferenceInput, MapEmbeddableInput } from './types'; +import { MapByReferenceInput, MapEmbeddableInput, MapByValueInput } from './types'; import { lazyLoadMapModules } from '../lazy_load_bundle'; +// @ts-expect-error +import { extractReferences } from '../../common/migrations/references'; export class MapEmbeddableFactory implements EmbeddableFactoryDefinition { type = MAP_SAVED_OBJECT_TYPE; @@ -61,4 +64,16 @@ export class MapEmbeddableFactory implements EmbeddableFactoryDefinition { parent ); }; + + extract(state: EmbeddableStateWithType) { + const maybeMapByValueInput = state as EmbeddableStateWithType | MapByValueInput; + + if ((maybeMapByValueInput as MapByValueInput).attributes !== undefined) { + const { references } = extractReferences(maybeMapByValueInput); + + return { state, references }; + } + + return { state, references: [] }; + } } From 7cc6a79a68c5b0e4f85f1a0be2262bceb8c98de4 Mon Sep 17 00:00:00 2001 From: Corey Robertson Date: Fri, 19 Feb 2021 13:48:49 -0500 Subject: [PATCH 2/4] Remove extra spaces --- .../dashboard/common/embeddable/embeddable_references.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/plugins/dashboard/common/embeddable/embeddable_references.ts b/src/plugins/dashboard/common/embeddable/embeddable_references.ts index 352a39310e60e4..59ea238434fcf3 100644 --- a/src/plugins/dashboard/common/embeddable/embeddable_references.ts +++ b/src/plugins/dashboard/common/embeddable/embeddable_references.ts @@ -51,7 +51,6 @@ export function extractPanelsReferences( for (const panel of panels) { const embeddable = convertSavedDashboardPanelToPanelState(panel); - const { state: embeddableInputWithExtractedReferences, references, @@ -59,7 +58,6 @@ export function extractPanelsReferences( ...embeddable.explicitInput, type: embeddable.type, }); - embeddable.explicitInput = omit(embeddableInputWithExtractedReferences, 'type'); const newPanel = convertPanelStateToSavedDashboardPanel(embeddable, panel.version); From 8c9f3a3dd04dbd4f2cd6004d429f3101bf9a1e0a Mon Sep 17 00:00:00 2001 From: Corey Robertson Date: Mon, 22 Feb 2021 15:28:32 -0500 Subject: [PATCH 3/4] Rework a type check --- .../editor_frame_service/embeddable/embeddable_factory.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/lens/public/editor_frame_service/embeddable/embeddable_factory.ts b/x-pack/plugins/lens/public/editor_frame_service/embeddable/embeddable_factory.ts index be3daaf92b9de7..8884f6a2612b50 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/embeddable/embeddable_factory.ts +++ b/x-pack/plugins/lens/public/editor_frame_service/embeddable/embeddable_factory.ts @@ -111,8 +111,8 @@ export class EmbeddableFactory implements EmbeddableFactoryDefinition { let references: SavedObjectReference[] = []; const typedState = (state as unknown) as LensEmbeddableInput; - if ((typedState as LensByValueInput).attributes !== undefined) { - references = (typedState as LensByValueInput).attributes.references; + if ('attributes' in typedState && typedState.attributes !== undefined) { + references = typedState.attributes.references; } return { state, references }; From c0ad690fde8289ae92126ab2e90d2136e3f587fe Mon Sep 17 00:00:00 2001 From: Corey Robertson Date: Mon, 22 Feb 2021 20:25:16 -0500 Subject: [PATCH 4/4] Fix type check --- .../editor_frame_service/embeddable/embeddable_factory.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/lens/public/editor_frame_service/embeddable/embeddable_factory.ts b/x-pack/plugins/lens/public/editor_frame_service/embeddable/embeddable_factory.ts index 8884f6a2612b50..a676b7283671ca 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/embeddable/embeddable_factory.ts +++ b/x-pack/plugins/lens/public/editor_frame_service/embeddable/embeddable_factory.ts @@ -19,7 +19,7 @@ import { EmbeddableFactoryDefinition, IContainer, } from '../../../../../../src/plugins/embeddable/public'; -import { LensByReferenceInput, LensByValueInput, LensEmbeddableInput } from './embeddable'; +import { LensByReferenceInput, LensEmbeddableInput } from './embeddable'; import { UiActionsStart } from '../../../../../../src/plugins/ui_actions/public'; import { Document } from '../../persistence/saved_object_store'; import { LensAttributeService } from '../../lens_attribute_service';