Skip to content

Commit

Permalink
[Dashboard] Export appropriate references from byValue panels (#91567) (
Browse files Browse the repository at this point in the history
#92714)

* Adds references from byValue panels when saving dashboard

* Remove extra spaces

* Rework a type check

* Fix type check

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Corey Robertson <corey.robertson@elastic.co>
  • Loading branch information
kibanamachine and Corey Robertson committed Feb 24, 2021
1 parent fee942d commit 14b9432
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<string, string>, 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 };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -105,4 +106,15 @@ export class EmbeddableFactory implements EmbeddableFactoryDefinition {
parent
);
}

extract(state: EmbeddableStateWithType) {
let references: SavedObjectReference[] = [];
const typedState = (state as unknown) as LensEmbeddableInput;

if ('attributes' in typedState && typedState.attributes !== undefined) {
references = typedState.attributes.references;
}

return { state, references };
}
}
17 changes: 16 additions & 1 deletion x-pack/plugins/maps/public/embeddable/map_embeddable_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
*/

import { i18n } from '@kbn/i18n';
import { EmbeddableStateWithType } from 'src/plugins/embeddable/common';
import {
EmbeddableFactoryDefinition,
IContainer,
} from '../../../../../src/plugins/embeddable/public';
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;
Expand Down Expand Up @@ -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: [] };
}
}

0 comments on commit 14b9432

Please sign in to comment.