-
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] Generic embeddable function #104499
Merged
cqliu1
merged 6 commits into
elastic:canvas/by-value-embeddables
from
cqliu1:canvas/generic-embeddable-fn
Oct 1, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
180f47e
Created generic embeddable function
cqliu1 81a15dc
Fix reference extract/inject in embeddable fn
cqliu1 fd80ede
Simplify embeddable toExpression
cqliu1 5163ffd
Moved labsService to flyout.tsx
cqliu1 2b3fe3d
Added comment
cqliu1 db4a95a
Merge branch 'canvas/by-value-embeddables' into canvas/generic-embedd…
cqliu1 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
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
59 changes: 59 additions & 0 deletions
59
x-pack/plugins/canvas/canvas_plugin_src/functions/external/embeddable.test.ts
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* 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 { embeddable } from './embeddable'; | ||
import { getQueryFilters } from '../../../common/lib/build_embeddable_filters'; | ||
import { ExpressionValueFilter } from '../../../types'; | ||
import { encode } from '../../../common/lib/embeddable_dataurl'; | ||
|
||
const filterContext: ExpressionValueFilter = { | ||
type: 'filter', | ||
and: [ | ||
{ | ||
type: 'filter', | ||
and: [], | ||
value: 'filter-value', | ||
column: 'filter-column', | ||
filterType: 'exactly', | ||
}, | ||
{ | ||
type: 'filter', | ||
and: [], | ||
column: 'time-column', | ||
filterType: 'time', | ||
from: '2019-06-04T04:00:00.000Z', | ||
to: '2019-06-05T04:00:00.000Z', | ||
}, | ||
], | ||
}; | ||
|
||
describe('embeddable', () => { | ||
const fn = embeddable().fn; | ||
const config = { | ||
id: 'some-id', | ||
timerange: { from: '15m', to: 'now' }, | ||
title: 'test embeddable', | ||
}; | ||
|
||
const args = { | ||
config: encode(config), | ||
type: 'visualization', | ||
}; | ||
|
||
it('accepts null context', () => { | ||
const expression = fn(null, args, {} as any); | ||
|
||
expect(expression.input.filters).toEqual([]); | ||
}); | ||
|
||
it('accepts filter context', () => { | ||
const expression = fn(filterContext, args, {} as any); | ||
const embeddableFilters = getQueryFilters(filterContext.and); | ||
|
||
expect(expression.input.filters).toEqual(embeddableFilters); | ||
}); | ||
}); |
119 changes: 119 additions & 0 deletions
119
x-pack/plugins/canvas/canvas_plugin_src/functions/external/embeddable.ts
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 |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* 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 { ExpressionFunctionDefinition } from 'src/plugins/expressions/common'; | ||
import { TimeRange } from 'src/plugins/data/public'; | ||
import { Filter } from '@kbn/es-query'; | ||
import { ExpressionValueFilter } from '../../../types'; | ||
import { | ||
EmbeddableExpressionType, | ||
EmbeddableExpression, | ||
EmbeddableInput as Input, | ||
} from '../../expression_types'; | ||
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'; | ||
|
||
interface Arguments { | ||
config: string; | ||
type: string; | ||
} | ||
|
||
const defaultTimeRange = { | ||
from: 'now-15m', | ||
to: 'now', | ||
}; | ||
|
||
export type EmbeddableInput = Input & { | ||
timeRange?: TimeRange; | ||
filters?: Filter[]; | ||
savedObjectId: string; | ||
}; | ||
|
||
const baseEmbeddableInput = { | ||
timeRange: defaultTimeRange, | ||
disableTriggers: true, | ||
renderMode: 'noInteractivity', | ||
}; | ||
|
||
type Return = EmbeddableExpression<EmbeddableInput>; | ||
|
||
export function embeddable(): 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), | ||
}, | ||
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, | ||
}, | ||
]; | ||
|
||
return { | ||
state, | ||
references, | ||
}; | ||
}, | ||
|
||
inject(state, references) { | ||
const reference = references.find((ref) => ref.name === 'embeddable.id'); | ||
if (reference) { | ||
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. And here should also be passed on to EmbeddablePersistableStateService for inject |
||
const input = decode(state.config[0] as string); | ||
input.savedObjectId = reference.id; | ||
state.config[0] = encode(input); | ||
} | ||
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
13 changes: 13 additions & 0 deletions
13
...gins/canvas/canvas_plugin_src/renderers/embeddable/input_type_to_expression/embeddable.ts
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* 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 { encode } from '../../../../common/lib/embeddable_dataurl'; | ||
import { EmbeddableInput } from '../../../expression_types'; | ||
|
||
export function toExpression(input: EmbeddableInput, embeddableType: string): string { | ||
return `embeddable config="${encode(input)}" type="${embeddableType}"`; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* 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 { EmbeddableInput } from '../../canvas_plugin_src/expression_types'; | ||
|
||
export const encode = (input: Partial<EmbeddableInput>) => | ||
Buffer.from(JSON.stringify(input)).toString('base64'); | ||
export const decode = (serializedInput: string) => | ||
JSON.parse(Buffer.from(serializedInput, 'base64').toString()); |
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.
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.
If this is a "by value" embeddable, then the state should be passed on to the EmbeddablePersistableStateService for extract.
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.
I'll tackle this in a separate PR to address extracting/injecting references for by-value embeddables.