Skip to content

Commit

Permalink
fix import/export refs for dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwizp committed Mar 26, 2021
1 parent 9d7c237 commit 9ddf39e
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { first } from 'rxjs/operators';
import { EmbeddableStateWithType } from 'src/plugins/embeddable/common';
import { SavedObjectAttributes } from '../../../../core/public';
import { extractSearchSourceReferences } from '../../../data/public';
import { SavedObjectReference } from '../../../../core/public';

import {
EmbeddableFactoryDefinition,
EmbeddableOutput,
Expand All @@ -38,6 +40,12 @@ import {
} from '../services';
import { showNewVisModal } from '../wizard';
import { convertToSerializedVis } from '../saved_visualizations/_saved_vis';
import {
extractControlsReferences,
extractTimeSeriesReferences,
injectTimeSeriesReferences,
injectControlsReferences,
} from '../saved_visualizations/saved_visualization_references';
import { createVisEmbeddableFromObject } from './create_vis_embeddable_from_object';
import { StartServicesGetter } from '../../../kibana_utils/public';
import { VisualizationsStartDeps } from '../plugin';
Expand Down Expand Up @@ -239,6 +247,19 @@ export class VisualizeEmbeddableFactory
);
}

public inject(_state: EmbeddableStateWithType, references: SavedObjectReference[]) {
const state = (_state as unknown) as VisualizeInput;

const { type, params } = state.savedVis ?? {};

if (type && params) {
injectControlsReferences(type, params, references);
injectTimeSeriesReferences(type, params, references);
}

return _state;
}

public extract(_state: EmbeddableStateWithType) {
const state = (_state as unknown) as VisualizeInput;
const references = [];
Expand All @@ -259,19 +280,11 @@ export class VisualizeEmbeddableFactory
});
}

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,
});
});
const { type, params } = state.savedVis ?? {};

if (type && params) {
extractControlsReferences(type, params, references, `control_${state.id}`);
extractTimeSeriesReferences(type, params, references, `metrics_${state.id}`);
}

return { state: _state, references };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,46 @@
import { SavedObjectReference } from '../../../../../core/types';
import { VisParams } from '../../../common';

const isControlsVis = (visType: string) => visType === 'input_control_vis';

export const extractControlsReferences = (
visType: string,
visParams: VisParams,
references: SavedObjectReference[] = []
references: SavedObjectReference[] = [],
prefix: string = 'control'
) => {
const controls = visParams?.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,
if (isControlsVis(visType)) {
(visParams?.controls ?? []).forEach((control: Record<string, string>, i: number) => {
if (!control.indexPattern) {
return;
}
control.indexPatternRefName = `${prefix}_${i}_index_pattern`;
references.push({
name: control.indexPatternRefName,
type: 'index-pattern',
id: control.indexPattern,
});
delete control.indexPattern;
});
delete control.indexPattern;
});
}
};

export const injectControlsReferences = (
visType: string,
visParams: VisParams,
references: SavedObjectReference[]
) => {
const controls = visParams.controls ?? [];

controls.forEach((control: Record<string, string>) => {
if (!control.indexPatternRefName) {
return;
}
const reference = references.find((ref) => ref.name === control.indexPatternRefName);
if (!reference) {
throw new Error(`Could not find index pattern reference "${control.indexPatternRefName}"`);
}
control.indexPattern = reference.id;
delete control.indexPatternRefName;
});
if (isControlsVis(visType)) {
(visParams.controls ?? []).forEach((control: Record<string, string>) => {
if (!control.indexPatternRefName) {
return;
}
const reference = references.find((ref) => ref.name === control.indexPatternRefName);
if (!reference) {
throw new Error(`Could not find index pattern reference "${control.indexPatternRefName}"`);
}
control.indexPattern = reference.id;
delete control.indexPatternRefName;
});
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ describe('extractReferences', () => {
};
const updatedDoc = extractReferences(doc);
expect(updatedDoc).toMatchInlineSnapshot(`
Object {
"attributes": Object {
"foo": true,
},
"references": Array [],
}
`);
Object {
"attributes": Object {
"foo": true,
},
"references": Array [],
}
`);
});

test('extracts references from savedSearchId', () => {
Expand All @@ -41,20 +41,20 @@ Object {
};
const updatedDoc = extractReferences(doc);
expect(updatedDoc).toMatchInlineSnapshot(`
Object {
"attributes": Object {
"foo": true,
"savedSearchRefName": "search_0",
},
"references": Array [
Object {
"id": "123",
"name": "search_0",
"type": "search",
},
],
}
`);
Object {
"attributes": Object {
"foo": true,
"savedSearchRefName": "search_0",
},
"references": Array [
Object {
"id": "123",
"name": "search_0",
"type": "search",
},
],
}
`);
});

test('extracts references from controls', () => {
Expand All @@ -63,6 +63,7 @@ Object {
attributes: {
foo: true,
visState: JSON.stringify({
type: 'input_control_vis',
params: {
controls: [
{
Expand All @@ -81,20 +82,20 @@ Object {
const updatedDoc = extractReferences(doc);

expect(updatedDoc).toMatchInlineSnapshot(`
Object {
"attributes": Object {
"foo": true,
"visState": "{\\"params\\":{\\"controls\\":[{\\"bar\\":true,\\"indexPatternRefName\\":\\"control_0_index_pattern\\"},{\\"bar\\":false}]}}",
},
"references": Array [
Object {
"id": "pattern*",
"name": "control_0_index_pattern",
"type": "index-pattern",
},
],
}
`);
Object {
"attributes": Object {
"foo": true,
"visState": "{\\"type\\":\\"input_control_vis\\",\\"params\\":{\\"controls\\":[{\\"bar\\":true,\\"indexPatternRefName\\":\\"control_0_index_pattern\\"},{\\"bar\\":false}]}}",
},
"references": Array [
Object {
"id": "pattern*",
"name": "control_0_index_pattern",
"type": "index-pattern",
},
],
}
`);
});
});

Expand All @@ -106,11 +107,11 @@ describe('injectReferences', () => {
} as VisSavedObject;
injectReferences(context, []);
expect(context).toMatchInlineSnapshot(`
Object {
"id": "1",
"title": "test",
}
`);
Object {
"id": "1",
"title": "test",
}
`);
});

test('injects references into context', () => {
Expand All @@ -119,6 +120,7 @@ Object {
title: 'test',
savedSearchRefName: 'search_0',
visState: ({
type: 'input_control_vis',
params: {
controls: [
{
Expand Down Expand Up @@ -146,25 +148,26 @@ Object {
];
injectReferences(context, references);
expect(context).toMatchInlineSnapshot(`
Object {
"id": "1",
"savedSearchId": "123",
"title": "test",
"visState": Object {
"params": Object {
"controls": Array [
Object {
"foo": true,
"indexPattern": "pattern*",
},
Object {
"foo": false,
Object {
"id": "1",
"savedSearchId": "123",
"title": "test",
"visState": Object {
"params": Object {
"controls": Array [
Object {
"foo": true,
"indexPattern": "pattern*",
},
Object {
"foo": false,
},
],
},
"type": "input_control_vis",
},
],
},
},
}
`);
}
`);
});

test(`fails when it can't find the saved search reference in the array`, () => {
Expand All @@ -183,6 +186,7 @@ Object {
id: '1',
title: 'test',
visState: ({
type: 'input_control_vis',
params: {
controls: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ export function extractReferences({
if (updatedAttributes.visState) {
const visState = JSON.parse(String(updatedAttributes.visState)) as SavedVisState;

extractControlsReferences(visState.params, updatedReferences);

if (visState.type === 'metrics') {
extractTimeSeriesReferences(visState.params, updatedReferences);
if (visState.type && visState.params) {
extractControlsReferences(visState.type, visState.params, updatedReferences);
extractTimeSeriesReferences(visState.type, visState.params, updatedReferences);
}

updatedAttributes.visState = JSON.stringify(visState);
Expand Down Expand Up @@ -87,11 +86,10 @@ export function injectReferences(savedObject: VisSavedObject, references: SavedO
delete savedObject.savedSearchRefName;
}

if (savedObject.visState?.params) {
injectControlsReferences(savedObject.visState.params, references);
const { type, params } = savedObject.visState ?? {};

if (savedObject.visState.type === 'metrics') {
injectTimeSeriesReferences(savedObject.visState.params, references);
}
if (type && params) {
injectControlsReferences(type, params, references);
injectTimeSeriesReferences(type, params, references);
}
}
Loading

0 comments on commit 9ddf39e

Please sign in to comment.