Skip to content

Commit

Permalink
do not add embeddable prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
nreese committed Feb 15, 2022
1 parent 00171f7 commit 557234e
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test('Should handle missing layerListJSON attribute', () => {
const attributes = {
title: 'my map',
};
expect(extractReferences({ attributes, embeddableId: 'panel1' })).toEqual({
expect(extractReferences({ attributes })).toEqual({
attributes: {
title: 'my map',
},
Expand All @@ -26,16 +26,16 @@ test('Should extract index-pattern reference from source with indexPatternId', (
layerListJSON:
'[{"sourceDescriptor":{"indexPatternId":"c698b940-e149-11e8-a35a-370a8516603a"}}]',
};
expect(extractReferences({ attributes, embeddableId: 'panel1' })).toEqual({
expect(extractReferences({ attributes })).toEqual({
attributes: {
title: 'my map',
layerListJSON:
'[{"sourceDescriptor":{"indexPatternRefName":"panel1_layer_0_source_index_pattern"}}]',
'[{"sourceDescriptor":{"indexPatternRefName":"layer_0_source_index_pattern"}}]',
},
references: [
{
id: 'c698b940-e149-11e8-a35a-370a8516603a',
name: 'panel1_layer_0_source_index_pattern',
name: 'layer_0_source_index_pattern',
type: 'index-pattern',
},
],
Expand All @@ -48,16 +48,16 @@ test('Should extract index-pattern reference from join with indexPatternId', ()
layerListJSON:
'[{"joins":[{"right":{"indexPatternId":"e20b2a30-f735-11e8-8ce0-9723965e01e3"}}]}]',
};
expect(extractReferences({ attributes, embeddableId: 'panel1' })).toEqual({
expect(extractReferences({ attributes })).toEqual({
attributes: {
title: 'my map',
layerListJSON:
'[{"joins":[{"right":{"indexPatternRefName":"panel1_layer_0_join_0_index_pattern"}}]}]',
'[{"joins":[{"right":{"indexPatternRefName":"layer_0_join_0_index_pattern"}}]}]',
},
references: [
{
id: 'e20b2a30-f735-11e8-8ce0-9723965e01e3',
name: 'panel1_layer_0_join_0_index_pattern',
name: 'layer_0_join_0_index_pattern',
type: 'index-pattern',
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,9 @@ type VectorLayerDescriptor = LayerDescriptor & {
joins?: JoinDescriptor[];
};

export function extractReferences({
attributes,
references = [],
embeddableId,
}: {
attributes: Record<string, string>;
references?: SavedObjectReference[];
embeddableId: string;
}) {
export function extractReferences({ attributes }: { attributes: Record<string, string> }) {
if (!attributes.layerListJSON) {
return { attributes, references };
return { attributes, references: [] };
}

const extractedReferences: SavedObjectReference[] = [];
Expand All @@ -48,15 +40,14 @@ export function extractReferences({
try {
layerList = JSON.parse(attributes.layerListJSON);
} catch (e) {
return { attributes, references };
return { attributes, references: [] };
}

layerList.forEach((layer, layerIndex) => {
// Extract index-pattern references from source descriptor
if (layer.sourceDescriptor && 'indexPatternId' in layer.sourceDescriptor) {
const sourceDescriptor = layer.sourceDescriptor as IndexPatternReferenceDescriptor;
const baseRefName = `layer_${layerIndex}_source_index_pattern`;
const refName = embeddableId ? `${embeddableId}_${baseRefName}` : baseRefName;
const refName = `layer_${layerIndex}_source_index_pattern`;
extractedReferences.push({
name: refName,
type: 'index-pattern',
Expand All @@ -73,8 +64,7 @@ export function extractReferences({
joins.forEach((join, joinIndex) => {
if ('indexPatternId' in join.right) {
const sourceDescriptor = join.right as IndexPatternReferenceDescriptor;
const baseRefName = `layer_${layerIndex}_join_${joinIndex}_index_pattern`;
const refName = embeddableId ? `${embeddableId}_${baseRefName}` : baseRefName;
const refName = `layer_${layerIndex}_join_${joinIndex}_index_pattern`;
extractedReferences.push({
name: refName,
type: 'index-pattern',
Expand All @@ -92,6 +82,6 @@ export function extractReferences({
...attributes,
layerListJSON: JSON.stringify(layerList),
},
references: references.concat(extractedReferences),
references: extractedReferences,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { savedObjectsServiceMock } from '../../../../../core/server/mocks';
import { migrateMapEmbeddable } from './migrate_map_embeddable';

test('Should extract references from by-value map panels', () => {
test('Should update by-value map panels with reference names', () => {
const dashboard = {
id: 'cf56e3f0-8de8-11ec-975f-f7e09cf7ebaf',
type: 'dashboard',
Expand All @@ -33,7 +33,7 @@ test('Should extract references from by-value map panels', () => {
references: [
{
id: '90943e30-9a47-11e8-b64d-95841ca0b247',
name: '6c75ee1d-28d9-4eea-8816-4110f3d8c154:layer_1_source_index_pattern',
name: '6c75ee1d-28d9-4eea-8816-4110f3d8c154:layer_0_source_index_pattern',
type: 'index-pattern',
},
],
Expand All @@ -44,20 +44,13 @@ test('Should extract references from by-value map panels', () => {
const updated = migrateMapEmbeddable(dashboard, contextMock);
expect(updated.references).toEqual([
{
id: '90943e30-9a47-11e8-b64d-95841ca0b247',
name: '6c75ee1d-28d9-4eea-8816-4110f3d8c154:layer_1_source_index_pattern',
type: 'index-pattern',
},
{
name: '6c75ee1d-28d9-4eea-8816-4110f3d8c154_layer_0_source_index_pattern',
name: '6c75ee1d-28d9-4eea-8816-4110f3d8c154:layer_0_source_index_pattern',
type: 'index-pattern',
id: '90943e30-9a47-11e8-b64d-95841ca0b247',
},
]);

const panels = JSON.parse(updated.attributes.panelsJSON);
const layerList = JSON.parse(panels[0].embeddableConfig.attributes.layerListJSON);
expect(layerList[0].sourceDescriptor.indexPatternRefName).toEqual(
'6c75ee1d-28d9-4eea-8816-4110f3d8c154_layer_0_source_index_pattern'
);
expect(layerList[0].sourceDescriptor.indexPatternRefName).toEqual('layer_0_source_index_pattern');
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ export const migrateMapEmbeddable: SavedObjectMigrationFn<any, any> = (doc) => {
return doc;
}

const dashboardReferences = doc.references ? [...doc.references] : [];

let panels: SavedDashboardPanel[] = [];
try {
panels = JSON.parse(doc.attributes.panelsJSON);
Expand All @@ -32,11 +30,9 @@ export const migrateMapEmbeddable: SavedObjectMigrationFn<any, any> = (doc) => {
return panel;
}

const { attributes, references } = extractReferences({
const { attributes } = extractReferences({
attributes: panel.embeddableConfig.attributes as Record<string, string>,
embeddableId: panel.panelIndex,
});
dashboardReferences.push(...references);
return {
...panel,
embeddableConfig: {
Expand All @@ -52,6 +48,5 @@ export const migrateMapEmbeddable: SavedObjectMigrationFn<any, any> = (doc) => {
...doc.attributes,
panelsJSON: JSON.stringify(migratedPanels),
},
references: dashboardReferences,
};
};
4 changes: 2 additions & 2 deletions x-pack/plugins/maps/common/embeddable/extract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ test('Should update state with refNames with by-value embeddable state', () => {
references: [
{
id: '90943e30-9a47-11e8-b64d-95841ca0b247',
name: '8d62c3f0-c61f-4c09-ac24-9b8ee4320e20_layer_0_source_index_pattern',
name: 'layer_0_source_index_pattern',
type: 'index-pattern',
},
],
state: {
id: '8d62c3f0-c61f-4c09-ac24-9b8ee4320e20',
attributes: {
layerListJSON:
'[{"sourceDescriptor":{"indexPatternRefName":"8d62c3f0-c61f-4c09-ac24-9b8ee4320e20_layer_0_source_index_pattern"}}]',
'[{"sourceDescriptor":{"indexPatternRefName":"layer_0_source_index_pattern"}}]',
},
type: 'map',
},
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/maps/common/embeddable/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export const extract: EmbeddableRegistryDefinition['extract'] = (state) => {
// by-value embeddable
const { attributes, references } = extractReferences({
attributes: typedState.attributes as MapSavedObjectAttributes,
embeddableId: typedState.id,
});

return {
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/maps/common/embeddable/inject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ test('Should inject refNames with by-value embeddable state', () => {
id: '8d62c3f0-c61f-4c09-ac24-9b8ee4320e20',
attributes: {
layerListJSON:
'[{"sourceDescriptor":{"indexPatternRefName":"8d62c3f0-c61f-4c09-ac24-9b8ee4320e20_layer_0_source_index_pattern"}}]',
'[{"sourceDescriptor":{"indexPatternRefName":"layer_0_source_index_pattern"}}]',
},
type: 'map',
};
const refernces = [
{
name: '8d62c3f0-c61f-4c09-ac24-9b8ee4320e20_layer_0_source_index_pattern',
name: 'layer_0_source_index_pattern',
type: 'index-pattern',
id: 'changed_index_pattern_id',
},
Expand Down
Loading

0 comments on commit 557234e

Please sign in to comment.