-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Lens] move reference handling to server
#239029
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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 5e0138b
Merge branch 'main' into lens-server-refs
nickofthyme c1f0633
chore: cleanup transforms, handle dynamic actions, stub out api confi…
nickofthyme 61822ed
Merge branch 'main' into lens-server-refs
nickofthyme c91a92f
fix type errors
nickofthyme b72c7e6
Merge branch 'main' into lens-server-refs
nickofthyme 642db89
Merge branch 'main' into lens-server-refs
dej611 da71217
Merge remote-tracking branch 'upstream/main' into lens-server-refs
dej611 3f21b73
:label: first type pass
dej611 d7aa151
:label: More types fixed
dej611 c023bcc
:wrench: Flag package for treeshake
dej611 7319d75
:white_check_mark: Align test to lack of references
dej611 19a76e7
chore: remove `schema` on transform definition
nickofthyme 5c507da
chore: move `LensParentApi` types to `@kbn/lens-common`
nickofthyme 3385eb1
chore: revert code cleanup
nickofthyme de8412e
Merge branch 'main' into lens-server-refs
nickofthyme 4ad0c86
fix: lens api integration tests
nickofthyme 8b2a35e
[CI] Auto-commit changed files from 'node scripts/eslint_all_files --…
kibanamachine e8fc9db
fix add panel from library
nickofthyme 7e65eee
Merge branch 'main' into lens-server-refs
nickofthyme 7ee482f
Merge remote-tracking branch 'origin/lens-server-refs' into lens-serv…
nickofthyme 05215fe
fix: background session issue
nickofthyme 7b2483a
Merge branch 'main' into lens-server-refs
nickofthyme 897b867
Merge branch 'main' into lens-server-refs
nickofthyme 35c3697
Merge branch 'main' into lens-server-refs
nickofthyme b2b01af
chore: update cloud limits
nickofthyme 46b7b69
Merge remote-tracking branch 'origin/lens-server-refs' into lens-serv…
nickofthyme c364724
Merge branch 'main' into lens-server-refs
nickofthyme 46ecfb3
Merge branch 'main' into lens-server-refs
dej611 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 0 additions & 58 deletions
58
x-pack/platform/plugins/shared/lens/common/embeddable_factory/index.ts
This file was deleted.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
x-pack/platform/plugins/shared/lens/common/references/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 ?? [], | ||
| }; | ||
| }; |
38 changes: 0 additions & 38 deletions
38
x-pack/platform/plugins/shared/lens/common/transforms/config_builder_stub.ts
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
x-pack/platform/plugins/shared/lens/common/transforms/transform_in.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| }; | ||
| }; |
83 changes: 83 additions & 0 deletions
83
x-pack/platform/plugins/shared/lens/common/transforms/transform_out.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
40
x-pack/platform/plugins/shared/lens/common/transforms/types.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
24
x-pack/platform/plugins/shared/lens/common/transforms/utils.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.