-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Canvas] Extract and inject references for by-value embeddables #115124
Merged
cqliu1
merged 4 commits into
elastic:canvas/by-value-embeddables
from
cqliu1:canvas/by-value-references
Oct 26, 2021
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -12,8 +12,9 @@ import { getFunctionHelp } from '../../../i18n'; | |
import { SavedObjectReference } from '../../../../../../src/core/types'; | ||
import { getQueryFilters } from '../../../common/lib/build_embeddable_filters'; | ||
import { decode, encode } from '../../../common/lib/embeddable_dataurl'; | ||
import { InitializeArguments } from '.'; | ||
|
||
interface Arguments { | ||
export interface Arguments { | ||
config: string; | ||
type: string; | ||
} | ||
|
@@ -31,77 +32,114 @@ const baseEmbeddableInput = { | |
|
||
type Return = EmbeddableExpression<EmbeddableInput>; | ||
|
||
export function embeddable(): ExpressionFunctionDefinition< | ||
type EmbeddableFunction = ExpressionFunctionDefinition< | ||
'embeddable', | ||
ExpressionValueFilter | null, | ||
Arguments, | ||
Return | ||
> { | ||
const { help, args: argHelp } = getFunctionHelp().embeddable; | ||
|
||
return { | ||
name: 'embeddable', | ||
help, | ||
args: { | ||
config: { | ||
aliases: ['_'], | ||
types: ['string'], | ||
required: true, | ||
help: argHelp.config, | ||
}, | ||
type: { | ||
types: ['string'], | ||
required: true, | ||
help: argHelp.type, | ||
}, | ||
}, | ||
context: { | ||
types: ['filter'], | ||
}, | ||
type: EmbeddableExpressionType, | ||
fn: (input, args) => { | ||
const filters = input ? input.and : []; | ||
|
||
const embeddableInput = decode(args.config) as EmbeddableInput; | ||
|
||
return { | ||
type: EmbeddableExpressionType, | ||
input: { | ||
...baseEmbeddableInput, | ||
...embeddableInput, | ||
filters: getQueryFilters(filters), | ||
>; | ||
|
||
export function embeddableFunctionFactory({ | ||
embeddablePersistableStateService, | ||
}: InitializeArguments): () => EmbeddableFunction { | ||
return function embeddable(): EmbeddableFunction { | ||
const { help, args: argHelp } = getFunctionHelp().embeddable; | ||
|
||
return { | ||
name: 'embeddable', | ||
help, | ||
args: { | ||
config: { | ||
aliases: ['_'], | ||
types: ['string'], | ||
required: true, | ||
help: argHelp.config, | ||
}, | ||
generatedAt: Date.now(), | ||
embeddableType: args.type, | ||
}; | ||
}, | ||
|
||
extract(state) { | ||
const input = decode(state.config[0] as string); | ||
const refName = 'embeddable.id'; | ||
|
||
const references: SavedObjectReference[] = [ | ||
{ | ||
name: refName, | ||
type: state.type[0] as string, | ||
id: input.savedObjectId as string, | ||
type: { | ||
types: ['string'], | ||
required: true, | ||
help: argHelp.type, | ||
}, | ||
]; | ||
}, | ||
context: { | ||
types: ['filter'], | ||
}, | ||
type: EmbeddableExpressionType, | ||
fn: (input, args) => { | ||
const filters = input ? input.and : []; | ||
|
||
const embeddableInput = decode(args.config) as EmbeddableInput; | ||
|
||
return { | ||
state, | ||
references, | ||
}; | ||
}, | ||
return { | ||
type: EmbeddableExpressionType, | ||
input: { | ||
...baseEmbeddableInput, | ||
...embeddableInput, | ||
filters: getQueryFilters(filters), | ||
}, | ||
generatedAt: Date.now(), | ||
embeddableType: args.type, | ||
}; | ||
}, | ||
|
||
inject(state, references) { | ||
const reference = references.find((ref) => ref.name === 'embeddable.id'); | ||
if (reference) { | ||
extract(state) { | ||
const input = decode(state.config[0] as string); | ||
input.savedObjectId = reference.id; | ||
state.config[0] = encode(input); | ||
} | ||
return state; | ||
}, | ||
|
||
// extracts references for by-reference embeddables | ||
if (input.savedObjectId) { | ||
const refName = 'embeddable.savedObjectId'; | ||
|
||
const references: SavedObjectReference[] = [ | ||
{ | ||
name: refName, | ||
type: state.type[0] as string, | ||
id: input.savedObjectId as string, | ||
}, | ||
]; | ||
|
||
return { | ||
state, | ||
references, | ||
}; | ||
} | ||
|
||
// extracts references for by-value embeddables | ||
const { state: extractedState, references: extractedReferences } = | ||
embeddablePersistableStateService.extract({ | ||
...input, | ||
type: state.type[0], | ||
}); | ||
|
||
const { type, ...extractedInput } = extractedState; | ||
|
||
return { | ||
state: { ...state, config: [encode(extractedInput)], type: [type] }, | ||
references: extractedReferences, | ||
}; | ||
}, | ||
|
||
inject(state, references) { | ||
const input = decode(state.config[0] as string); | ||
const savedObjectReference = references.find( | ||
(ref) => ref.name === 'embeddable.savedObjectId' | ||
); | ||
|
||
// injects saved object id for by-references embeddable | ||
if (savedObjectReference) { | ||
input.savedObjectId = savedObjectReference.id; | ||
state.config[0] = encode(input); | ||
state.type[0] = savedObjectReference.type; | ||
} else { | ||
// injects references for by-value embeddables | ||
const { type, ...injectedInput } = embeddablePersistableStateService.inject( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cqliu1 I think this is the source of the issue I mentioned to you. |
||
{ ...input, type: state.type[0] }, | ||
references | ||
); | ||
state.config[0] = encode(injectedInput); | ||
state.type[0] = type; | ||
} | ||
return state; | ||
}, | ||
}; | ||
}; | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These references must get de-duped somewhere in the workpad extract / inject references, but I'm not sure where this happens. Just to make sure: If you look at the workpad in the saved objects explorer, each by reference embeddable has a unique ID?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update: Looks like the de-duping is conducted properly inside the workpad, each reference is prepended with the element ID.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each reference name includes the unique element ID as a prefix. Here's an example with two elements backed by the same saved object id.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again for double checking this for me!