Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dashboard] Export appropriate references from byValue panels #91567

Merged
merged 9 commits into from
Feb 24, 2021
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 All @@ -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';
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 ((typedState as LensByValueInput).attributes !== undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: You can get around these two type casts by checking for by value input like this:
'attributes' in typedState && typedState.attributes

It's turned into a type guard automatically by typescript, so you won't need to case again in the branch

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh nice. That's very handy to know.

references = (typedState as LensByValueInput).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: [] };
}
}