-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Canvas] Generic embeddable function (#104499)
* Created generic embeddable function Fixed telemetry Updates expression on input change Fixed ts errors Store embeddable input to expression Added lib functions Added comments Fixed type errors Fixed ts errors Clean up Removed extraneous import Added context type to embeddable function def Fix import Update encode/decode fns Moved embeddable data url lib file Added embeddable test Updated comment * Fix reference extract/inject in embeddable fn * Simplify embeddable toExpression * Moved labsService to flyout.tsx * Added comment
- Loading branch information
Showing
17 changed files
with
322 additions
and
38 deletions.
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) { | ||
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.