Skip to content

Commit 5d44494

Browse files
authored
[Lens] Automatically unlink cloned library annotation group layers (#161130)
1 parent ff0f121 commit 5d44494

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

x-pack/plugins/lens/public/visualizations/xy/visualization.test.tsx

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ import { unifiedSearchPluginMock } from '@kbn/unified-search-plugin/public/mocks
5050
import { layerTypes, Visualization } from '../..';
5151
import { set } from '@kbn/safer-lodash-set';
5252
import { SavedObjectReference } from '@kbn/core-saved-objects-api-server';
53-
import { getAnnotationsLayers } from './visualization_helpers';
53+
import {
54+
getAnnotationsLayers,
55+
isAnnotationsLayer,
56+
isByReferenceAnnotationsLayer,
57+
} from './visualization_helpers';
5458
import { cloneDeep } from 'lodash';
5559
import { DataViewsServicePublic } from '@kbn/data-views-plugin/public';
5660

@@ -3240,6 +3244,59 @@ describe('xy_visualization', () => {
32403244
});
32413245
});
32423246

3247+
describe('#cloneLayer', () => {
3248+
it('should turned cloned by-reference annotation groups into by-value', () => {
3249+
const state = exampleState();
3250+
const layer: XYByValueAnnotationLayerConfig = {
3251+
layerId: 'layer-id',
3252+
layerType: 'annotations',
3253+
indexPatternId: 'some-index-pattern',
3254+
ignoreGlobalFilters: false,
3255+
annotations: [
3256+
{
3257+
id: 'some-annotation-id',
3258+
type: 'manual',
3259+
key: {
3260+
type: 'point_in_time',
3261+
timestamp: 'timestamp',
3262+
},
3263+
} as PointInTimeEventAnnotationConfig,
3264+
],
3265+
};
3266+
3267+
state.layers = [
3268+
{
3269+
...layer,
3270+
annotationGroupId: 'some-group-id',
3271+
__lastSaved: {
3272+
...layer,
3273+
title: '',
3274+
description: '',
3275+
tags: [],
3276+
},
3277+
},
3278+
];
3279+
3280+
const newLayerId = 'new-layer-id';
3281+
3282+
const stateWithClonedLayer = xyVisualization.cloneLayer!(
3283+
state,
3284+
layer.layerId,
3285+
newLayerId,
3286+
new Map()
3287+
);
3288+
3289+
expect(
3290+
isAnnotationsLayer(stateWithClonedLayer.layers[0]) &&
3291+
isByReferenceAnnotationsLayer(stateWithClonedLayer.layers[0])
3292+
).toBe(true);
3293+
expect(
3294+
isAnnotationsLayer(stateWithClonedLayer.layers[1]) &&
3295+
isByReferenceAnnotationsLayer(stateWithClonedLayer.layers[1])
3296+
).toBe(false);
3297+
});
3298+
});
3299+
32433300
describe('#getUniqueLabels', () => {
32443301
it('creates unique labels for single annotations layer with repeating labels', async () => {
32453302
const annotationLayer: XYAnnotationLayerConfig = {

x-pack/plugins/lens/public/visualizations/xy/visualization.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ import {
106106
validateLayersForDimension,
107107
} from './visualization_helpers';
108108
import { groupAxesByType } from './axes_configuration';
109-
import type { XYState } from './types';
109+
import type { XYByValueAnnotationLayerConfig, XYState } from './types';
110110
import { ReferenceLinePanel } from './xy_config_panel/reference_line_config_panel';
111111
import { AnnotationsPanel } from './xy_config_panel/annotations_config_panel';
112112
import { defaultAnnotationLabel } from './annotations/helpers';
@@ -174,10 +174,25 @@ export const getXyVisualization = ({
174174
if (isAnnotationsLayer(toCopyLayer)) {
175175
toCopyLayer.annotations.forEach((i) => clonedIDsMap.set(i.id, generateId()));
176176
}
177-
const newLayer = renewIDs(toCopyLayer, [...clonedIDsMap.keys()], (id: string) =>
177+
178+
let newLayer = renewIDs(toCopyLayer, [...clonedIDsMap.keys()], (id: string) =>
178179
clonedIDsMap.get(id)
179180
);
181+
180182
newLayer.layerId = newLayerId;
183+
184+
if (isAnnotationsLayer(newLayer) && isByReferenceAnnotationsLayer(newLayer)) {
185+
const byValueVersion: XYByValueAnnotationLayerConfig = {
186+
annotations: newLayer.annotations,
187+
ignoreGlobalFilters: newLayer.ignoreGlobalFilters,
188+
layerId: newLayer.layerId,
189+
layerType: newLayer.layerType,
190+
indexPatternId: newLayer.indexPatternId,
191+
};
192+
193+
newLayer = byValueVersion;
194+
}
195+
181196
return {
182197
...state,
183198
layers: [...state.layers, newLayer],

0 commit comments

Comments
 (0)