Skip to content

Commit

Permalink
[Maps] fix index pattern references not extracted for tracks source (#…
Browse files Browse the repository at this point in the history
…92226)

* [Maps] fix index pattern references not extracted for tracks source

* merge with master

* tslint

* tslint

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
nreese and kibanamachine authored Feb 25, 2021
1 parent 2d011e2 commit 37d7ea2
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 113 deletions.
107 changes: 0 additions & 107 deletions x-pack/plugins/maps/common/migrations/references.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe('injectReferences', () => {
const attributes = {
title: 'my map',
};
expect(injectReferences({ attributes })).toEqual({
expect(injectReferences({ attributes, references: [] })).toEqual({
attributes: {
title: 'my map',
},
Expand Down
120 changes: 120 additions & 0 deletions x-pack/plugins/maps/common/migrations/references.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

// Can not use public Layer classes to extract references since this logic must run in both client and server.

import { SavedObjectReference } from '../../../../../src/core/types';
import { MapSavedObjectAttributes } from '../map_saved_object_type';
import { LayerDescriptor } from '../descriptor_types';

interface IndexPatternReferenceDescriptor {
indexPatternId?: string;
indexPatternRefName?: string;
}

export function extractReferences({
attributes,
references = [],
}: {
attributes: MapSavedObjectAttributes;
references?: SavedObjectReference[];
}) {
if (!attributes.layerListJSON) {
return { attributes, references };
}

const extractedReferences: SavedObjectReference[] = [];

const layerList: LayerDescriptor[] = JSON.parse(attributes.layerListJSON);
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 refName = `layer_${layerIndex}_source_index_pattern`;
extractedReferences.push({
name: refName,
type: 'index-pattern',
id: sourceDescriptor.indexPatternId!,
});
delete sourceDescriptor.indexPatternId;
sourceDescriptor.indexPatternRefName = refName;
}

// Extract index-pattern references from join
const joins = layer.joins ? layer.joins : [];
joins.forEach((join, joinIndex) => {
if ('indexPatternId' in join.right) {
const sourceDescriptor = join.right as IndexPatternReferenceDescriptor;
const refName = `layer_${layerIndex}_join_${joinIndex}_index_pattern`;
extractedReferences.push({
name: refName,
type: 'index-pattern',
id: sourceDescriptor.indexPatternId!,
});
delete sourceDescriptor.indexPatternId;
sourceDescriptor.indexPatternRefName = refName;
}
});
});

return {
attributes: {
...attributes,
layerListJSON: JSON.stringify(layerList),
},
references: references.concat(extractedReferences),
};
}

function findReference(targetName: string, references: SavedObjectReference[]) {
const reference = references.find(({ name }) => name === targetName);
if (!reference) {
throw new Error(`Could not find reference "${targetName}"`);
}
return reference;
}

export function injectReferences({
attributes,
references,
}: {
attributes: MapSavedObjectAttributes;
references: SavedObjectReference[];
}) {
if (!attributes.layerListJSON) {
return { attributes };
}

const layerList: LayerDescriptor[] = JSON.parse(attributes.layerListJSON);
layerList.forEach((layer) => {
// Inject index-pattern references into source descriptor
if (layer.sourceDescriptor && 'indexPatternRefName' in layer.sourceDescriptor) {
const sourceDescriptor = layer.sourceDescriptor as IndexPatternReferenceDescriptor;
const reference = findReference(sourceDescriptor.indexPatternRefName!, references);
sourceDescriptor.indexPatternId = reference.id;
delete sourceDescriptor.indexPatternRefName;
}

// Inject index-pattern references into join
const joins = layer.joins ? layer.joins : [];
joins.forEach((join) => {
if ('indexPatternRefName' in join.right) {
const sourceDescriptor = join.right as IndexPatternReferenceDescriptor;
const reference = findReference(sourceDescriptor.indexPatternRefName!, references);
sourceDescriptor.indexPatternId = reference.id;
delete sourceDescriptor.indexPatternRefName;
}
});
});

return {
attributes: {
...attributes,
layerListJSON: JSON.stringify(layerList),
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { MAP_SAVED_OBJECT_TYPE, APP_ICON } from '../../common/constants';
import { getMapEmbeddableDisplayName } from '../../common/i18n_getters';
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 {
Expand Down Expand Up @@ -69,7 +68,9 @@ export class MapEmbeddableFactory implements EmbeddableFactoryDefinition {
const maybeMapByValueInput = state as EmbeddableStateWithType | MapByValueInput;

if ((maybeMapByValueInput as MapByValueInput).attributes !== undefined) {
const { references } = extractReferences(maybeMapByValueInput);
const { references } = extractReferences({
attributes: (maybeMapByValueInput as MapByValueInput).attributes,
});

return { state, references };
}
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/maps/public/map_attribute_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { MAP_SAVED_OBJECT_TYPE } from '../common/constants';
import { getMapEmbeddableDisplayName } from '../common/i18n_getters';
import { checkForDuplicateTitle, OnSaveProps } from '../../../../src/plugins/saved_objects/public';
import { getCoreOverlays, getEmbeddableService, getSavedObjectsClient } from './kibana_services';
// @ts-expect-error
import { extractReferences, injectReferences } from '../common/migrations/references';
import { MapByValueInput, MapByReferenceInput } from './embeddable/types';

Expand Down
6 changes: 4 additions & 2 deletions x-pack/plugins/maps/server/maps_telemetry/maps_telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
import { MapSavedObject, MapSavedObjectAttributes } from '../../common/map_saved_object_type';
import { getIndexPatternsService, getInternalRepository } from '../kibana_server_services';
import { MapsConfigType } from '../../config';
// @ts-expect-error
import { injectReferences } from '././../../common/migrations/references';

interface Settings {
Expand Down Expand Up @@ -314,7 +313,10 @@ export async function getMapsTelemetry(config: MapsConfigType): Promise<MapsUsag
MAP_SAVED_OBJECT_TYPE,
(savedObjects) => {
const savedObjectsWithIndexPatternIds = savedObjects.map((savedObject) => {
return injectReferences(savedObject);
return {
...savedObject,
...injectReferences(savedObject),
};
});
return layerLists.push(...getLayerLists(savedObjectsWithIndexPatternIds));
}
Expand Down

0 comments on commit 37d7ea2

Please sign in to comment.