Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7040354
[Lens] move references to server
nickofthyme Oct 14, 2025
5e0138b
Merge branch 'main' into lens-server-refs
nickofthyme Oct 19, 2025
c1f0633
chore: cleanup transforms, handle dynamic actions, stub out api confi…
nickofthyme Oct 21, 2025
61822ed
Merge branch 'main' into lens-server-refs
nickofthyme Oct 21, 2025
c91a92f
fix type errors
nickofthyme Oct 22, 2025
b72c7e6
Merge branch 'main' into lens-server-refs
nickofthyme Oct 22, 2025
642db89
Merge branch 'main' into lens-server-refs
dej611 Oct 23, 2025
da71217
Merge remote-tracking branch 'upstream/main' into lens-server-refs
dej611 Oct 24, 2025
3f21b73
:label: first type pass
dej611 Oct 24, 2025
d7aa151
:label: More types fixed
dej611 Oct 24, 2025
c023bcc
:wrench: Flag package for treeshake
dej611 Oct 24, 2025
7319d75
:white_check_mark: Align test to lack of references
dej611 Oct 24, 2025
19a76e7
chore: remove `schema` on transform definition
nickofthyme Oct 28, 2025
5c507da
chore: move `LensParentApi` types to `@kbn/lens-common`
nickofthyme Oct 28, 2025
3385eb1
chore: revert code cleanup
nickofthyme Oct 28, 2025
de8412e
Merge branch 'main' into lens-server-refs
nickofthyme Oct 28, 2025
4ad0c86
fix: lens api integration tests
nickofthyme Oct 28, 2025
8b2a35e
[CI] Auto-commit changed files from 'node scripts/eslint_all_files --…
kibanamachine Oct 28, 2025
e8fc9db
fix add panel from library
nickofthyme Oct 28, 2025
7e65eee
Merge branch 'main' into lens-server-refs
nickofthyme Oct 28, 2025
7ee482f
Merge remote-tracking branch 'origin/lens-server-refs' into lens-serv…
nickofthyme Oct 28, 2025
05215fe
fix: background session issue
nickofthyme Oct 29, 2025
7b2483a
Merge branch 'main' into lens-server-refs
nickofthyme Oct 29, 2025
897b867
Merge branch 'main' into lens-server-refs
nickofthyme Oct 29, 2025
35c3697
Merge branch 'main' into lens-server-refs
nickofthyme Oct 30, 2025
b2b01af
chore: update cloud limits
nickofthyme Oct 30, 2025
46b7b69
Merge remote-tracking branch 'origin/lens-server-refs' into lens-serv…
nickofthyme Oct 30, 2025
c364724
Merge branch 'main' into lens-server-refs
nickofthyme Oct 30, 2025
46ecfb3
Merge branch 'main' into lens-server-refs
dej611 Oct 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

52 changes: 52 additions & 0 deletions x-pack/platform/plugins/shared/lens/common/references/index.ts
Comment thread
nickofthyme marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.
*/

import { cloneDeep } from 'lodash';

import type { Reference } from '@kbn/content-management-utils';

import type { LensSerializedState } from '../../public';

export const injectLensReferences = (
state: LensSerializedState,
references: Reference[] = []
): LensSerializedState => {
const clonedState = cloneDeep(state);

if (clonedState.savedObjectId || !clonedState.attributes) {
return clonedState;
}

// match references based on name, so only references associated with this lens panel are injected.
const matchedReferences: Reference[] = [];

if (Array.isArray(clonedState.attributes.references)) {
clonedState.attributes.references.forEach((serializableRef) => {
const internalReference = serializableRef;
const matchedReference = references.find(
(reference) => reference.name === internalReference.name
);
if (matchedReference) matchedReferences.push(matchedReference);
});
}

clonedState.attributes.references = matchedReferences;

return clonedState;
};

export const extractLensReferences = (
state: LensSerializedState
): {
state: LensSerializedState;
references: Reference[];
} => {
return {
state,
references: state.attributes?.references ?? state.references ?? [],
};
};

This file was deleted.

20 changes: 19 additions & 1 deletion x-pack/platform/plugins/shared/lens/common/transforms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,22 @@
* 2.0.
*/

export { ConfigBuilderStub } from './config_builder_stub';
import { schema } from '@kbn/config-schema';
import type { EnhancementsRegistry } from '@kbn/embeddable-plugin/common/enhancements/registry';

import type { LensTransforms } from './types';
import { getTransformIn } from './transform_in';
import { getTransformOut } from './transform_out';

export interface LensTransformDependencies {
transformEnhancementsIn?: EnhancementsRegistry['transformIn'];
transformEnhancementsOut?: EnhancementsRegistry['transformOut'];
}

export function getLensTransforms(deps: LensTransformDependencies): LensTransforms {
return {
transformIn: getTransformIn(deps),
transformOut: getTransformOut(deps),
schema: schema.any(),
Comment thread
nickofthyme marked this conversation as resolved.
Outdated
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.
*/

import type { LensTransformDependencies } from '.';
import { DOC_TYPE } from '../constants';
import { extractLensReferences } from '../references';
import type {
LensByRefTransformInResult,
LensByValueTransformInResult,
LensTransformIn,
} from './types';
import { LENS_SAVED_OBJECT_REF_NAME, isByRefLensState } from './utils';

/**
* Transform from Lens API format to Lens Serialized State
*/
export const getTransformIn = ({
transformEnhancementsIn,
}: LensTransformDependencies): LensTransformIn => {
return function transformIn(state) {
const { enhancementsState: enhancements = null, enhancementsReferences = [] } =
state.enhancements ? transformEnhancementsIn?.(state.enhancements) ?? {} : {};
const enhancementsState = enhancements ? { enhancements } : {};

if (isByRefLensState(state)) {
const { savedObjectId: id, ...rest } = state;
return {
state: rest,
...enhancementsState,
references: [
{
name: LENS_SAVED_OBJECT_REF_NAME,
type: DOC_TYPE,
id: id!,
},
...enhancementsReferences,
],
} satisfies LensByRefTransformInResult;
}

const { state: lensState, references: lensReferences } = extractLensReferences(state);

return {
state: lensState,
...enhancementsState,
references: [...lensReferences, ...enhancementsReferences],
} satisfies LensByValueTransformInResult;
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.
*/

import type { DynamicActionsSerializedState } from '@kbn/embeddable-enhanced-plugin/public';
import type { LensTransformDependencies } from '.';
import type { LensByValueSerializedState } from '../../public/react_embeddable/types';
import { LENS_ITEM_VERSION_V1, transformToV1LensItemAttributes } from '../content_management/v1';
import { injectLensReferences } from '../references';
import type {
LensByRefTransformOutResult,
LensByValueTransformOutResult,
LensTransformOut,
} from './types';
import { findLensReference, isByRefLensState } from './utils';

/**
* Transform from Lens Serialized State to Lens API format
*/
export const getTransformOut = ({
transformEnhancementsOut,
}: LensTransformDependencies): LensTransformOut => {
return function transformOut(state, references) {
const enhancements = state.enhancements
? transformEnhancementsOut?.(state.enhancements, references ?? [])
: undefined;
const enhancementsState = (
enhancements ? { enhancements } : {}
) as DynamicActionsSerializedState;

const savedObjectRef = findLensReference(references);

if (savedObjectRef && isByRefLensState(state)) {
return {
...state,
...enhancementsState,
savedObjectId: savedObjectRef.id,
} satisfies LensByRefTransformOutResult;
}

const migratedAttributes = migrateAttributes(state.attributes);
const injectedState = injectLensReferences(
{
...state,
...enhancementsState,
attributes: migratedAttributes,
},
references
);

return injectedState satisfies LensByValueTransformOutResult;
};
};

/**
* Handles transforming old lens SO in dashboard to v1 Lens SO
*/
function migrateAttributes(attributes: LensByValueSerializedState['attributes']) {
if (!attributes) {
throw new Error('Why are attributes undefined?');
}

const { visualizationType } = attributes;

if (!visualizationType) {
throw new Error('Missing visualizationType');
}

const version = attributes.version ?? 0;

let newAttributes = { ...attributes };
if (version < LENS_ITEM_VERSION_V1) {
newAttributes = {
...newAttributes,
...transformToV1LensItemAttributes({ ...attributes, visualizationType }),
};
}

return newAttributes;
}
40 changes: 40 additions & 0 deletions x-pack/platform/plugins/shared/lens/common/transforms/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.
*/

import type { EmbeddableTransforms } from '@kbn/embeddable-plugin/common';

import type {
LensSerializedState,
LensByRefSerializedState,
LensByValueSerializedState,
} from '../../public/react_embeddable/types';

export type LensTransforms = Required<
EmbeddableTransforms<LensSerializedState, LensSerializedState>
>;

/**
* Transform from Lens API format to Lens Serialized State
*/
export type LensTransformIn = LensTransforms['transformIn'];

/**
* Transform from to Lens Serialized State to Lens API format
*/
export type LensTransformOut = LensTransforms['transformOut'];

type LensByRefTransforms = Required<
EmbeddableTransforms<LensByRefSerializedState, LensByRefSerializedState>
>;
export type LensByRefTransformInResult = ReturnType<LensByRefTransforms['transformIn']>;
export type LensByRefTransformOutResult = ReturnType<LensByRefTransforms['transformOut']>;

type LensByValueTransforms = Required<
EmbeddableTransforms<LensByValueSerializedState, LensByValueSerializedState>
>;
export type LensByValueTransformInResult = ReturnType<LensByValueTransforms['transformIn']>;
export type LensByValueTransformOutResult = ReturnType<LensByValueTransforms['transformOut']>;
24 changes: 24 additions & 0 deletions x-pack/platform/plugins/shared/lens/common/transforms/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.
*/

import type { Reference } from '@kbn/content-management-utils';

import type { LensSerializedState } from '../../public';
import type { LensByRefSerializedState } from '../../public/react_embeddable/types';
import { DOC_TYPE } from '../constants';

export const LENS_SAVED_OBJECT_REF_NAME = 'savedObjectRef';

export function findLensReference(references?: Reference[]) {
return references
? references.find((ref) => ref.type === DOC_TYPE && ref.name === LENS_SAVED_OBJECT_REF_NAME)
: undefined;
}

export function isByRefLensState(state: LensSerializedState): state is LensByRefSerializedState {
return !state.attributes;
}
Loading