Skip to content

Commit

Permalink
[8.9] [Lens] Automatically unlink cloned library annotation group lay…
Browse files Browse the repository at this point in the history
…ers (#161130) (#161148)

# Backport

This will backport the following commits from `main` to `8.9`:
- [[Lens] Automatically unlink cloned library annotation group layers
(#161130)](#161130)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Drew
Tate","email":"drew.tate@elastic.co"},"sourceCommit":{"committedDate":"2023-07-04T07:43:59Z","message":"[Lens]
Automatically unlink cloned library annotation group layers
(#161130)","sha":"5d444945f5acfa20b4f6c90daa1ce9f8e7cf16ff","branchLabelMapping":{"^v8.10.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Visualizations","release_note:skip","Feature:Lens","backport:prev-minor","v8.10.0"],"number":161130,"url":"https://github.com/elastic/kibana/pull/161130","mergeCommit":{"message":"[Lens]
Automatically unlink cloned library annotation group layers
(#161130)","sha":"5d444945f5acfa20b4f6c90daa1ce9f8e7cf16ff"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.10.0","labelRegex":"^v8.10.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/161130","number":161130,"mergeCommit":{"message":"[Lens]
Automatically unlink cloned library annotation group layers
(#161130)","sha":"5d444945f5acfa20b4f6c90daa1ce9f8e7cf16ff"}}]}]
BACKPORT-->

Co-authored-by: Drew Tate <drew.tate@elastic.co>
  • Loading branch information
kibanamachine and drewdaemon authored Jul 5, 2023
1 parent bca89ad commit 7cec9ff
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ import { unifiedSearchPluginMock } from '@kbn/unified-search-plugin/public/mocks
import { layerTypes, Visualization } from '../..';
import { set } from '@kbn/safer-lodash-set';
import { SavedObjectReference } from '@kbn/core-saved-objects-api-server';
import { getAnnotationsLayers } from './visualization_helpers';
import {
getAnnotationsLayers,
isAnnotationsLayer,
isByReferenceAnnotationsLayer,
} from './visualization_helpers';
import { cloneDeep } from 'lodash';
import { DataViewsServicePublic } from '@kbn/data-views-plugin/public';

Expand Down Expand Up @@ -3240,6 +3244,59 @@ describe('xy_visualization', () => {
});
});

describe('#cloneLayer', () => {
it('should turned cloned by-reference annotation groups into by-value', () => {
const state = exampleState();
const layer: XYByValueAnnotationLayerConfig = {
layerId: 'layer-id',
layerType: 'annotations',
indexPatternId: 'some-index-pattern',
ignoreGlobalFilters: false,
annotations: [
{
id: 'some-annotation-id',
type: 'manual',
key: {
type: 'point_in_time',
timestamp: 'timestamp',
},
} as PointInTimeEventAnnotationConfig,
],
};

state.layers = [
{
...layer,
annotationGroupId: 'some-group-id',
__lastSaved: {
...layer,
title: '',
description: '',
tags: [],
},
},
];

const newLayerId = 'new-layer-id';

const stateWithClonedLayer = xyVisualization.cloneLayer!(
state,
layer.layerId,
newLayerId,
new Map()
);

expect(
isAnnotationsLayer(stateWithClonedLayer.layers[0]) &&
isByReferenceAnnotationsLayer(stateWithClonedLayer.layers[0])
).toBe(true);
expect(
isAnnotationsLayer(stateWithClonedLayer.layers[1]) &&
isByReferenceAnnotationsLayer(stateWithClonedLayer.layers[1])
).toBe(false);
});
});

describe('#getUniqueLabels', () => {
it('creates unique labels for single annotations layer with repeating labels', async () => {
const annotationLayer: XYAnnotationLayerConfig = {
Expand Down
19 changes: 17 additions & 2 deletions x-pack/plugins/lens/public/visualizations/xy/visualization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ import {
validateLayersForDimension,
} from './visualization_helpers';
import { groupAxesByType } from './axes_configuration';
import type { XYState } from './types';
import type { XYByValueAnnotationLayerConfig, XYState } from './types';
import { ReferenceLinePanel } from './xy_config_panel/reference_line_config_panel';
import { AnnotationsPanel } from './xy_config_panel/annotations_config_panel';
import { defaultAnnotationLabel } from './annotations/helpers';
Expand Down Expand Up @@ -174,10 +174,25 @@ export const getXyVisualization = ({
if (isAnnotationsLayer(toCopyLayer)) {
toCopyLayer.annotations.forEach((i) => clonedIDsMap.set(i.id, generateId()));
}
const newLayer = renewIDs(toCopyLayer, [...clonedIDsMap.keys()], (id: string) =>

let newLayer = renewIDs(toCopyLayer, [...clonedIDsMap.keys()], (id: string) =>
clonedIDsMap.get(id)
);

newLayer.layerId = newLayerId;

if (isAnnotationsLayer(newLayer) && isByReferenceAnnotationsLayer(newLayer)) {
const byValueVersion: XYByValueAnnotationLayerConfig = {
annotations: newLayer.annotations,
ignoreGlobalFilters: newLayer.ignoreGlobalFilters,
layerId: newLayer.layerId,
layerType: newLayer.layerType,
indexPatternId: newLayer.indexPatternId,
};

newLayer = byValueVersion;
}

return {
...state,
layers: [...state.layers, newLayer],
Expand Down

0 comments on commit 7cec9ff

Please sign in to comment.