Skip to content
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] Canvas embeddable #39839

Merged
merged 10 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
1 change: 1 addition & 0 deletions x-pack/dev-tools/jest/create_jest_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export function createJestConfig({
],
transform: {
'^.+\\.(js|tsx?)$': `${kibanaDirectory}/src/dev/jest/babel_transform.js`,
'^.+\\.html?$': 'jest-raw-loader',
},
transformIgnorePatterns: [
// ignore all node_modules except @elastic/eui which requires babel transforms to handle dynamic import()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { ExpressionType } from '../../../../../../src/plugins/data/common/expressions';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Webpack creates an alias for these that allow you to remove the leading ../. For example:

https://github.com/elastic/kibana/blob/master/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/browser/location.ts#L7

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did changing this to src/plugins/data/common/expressions not work for you?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the alias works here, but there are some other files where it does not appear to work, the embeddable_types file that is in the same directory as this file for instance gives module not found errors when using the src/legacy/... path instead of the full relative path. So I'm just unsure when and when to not do relative paths. I've fixed all the paths that don't cause errors when changing them to use the alias.

// @ts-ignore Untyped Local
import { MAP_SAVED_OBJECT_TYPE } from '../../../maps/common/constants';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's create a single file within Canvas, perhaps embeddable_types.ts, that imports and exports these. It will make the code easier to read, and allow us to add embed types as they arrive.

import { SEARCH_EMBEDDABLE_TYPE } from '../../../../../../src/legacy/core_plugins/kibana/public/discover/embeddable/search_embeddable';
// TODO: Doing this visualize import makes type_check fail
import { VISUALIZE_EMBEDDABLE_TYPE } from '../../../../../../src/legacy/core_plugins/kibana/public/visualize/embeddable';
import { EmbeddableInput } from '../../../../../../src/legacy/core_plugins/embeddable_api/public';

export const EmbeddableExpressionType = 'embeddable';

export const EmbeddableTypes = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep... as I said above: let's put this in a file of its own.

map: MAP_SAVED_OBJECT_TYPE,
search: SEARCH_EMBEDDABLE_TYPE,
visualization: VISUALIZE_EMBEDDABLE_TYPE,
};

export interface EmbeddableExpression<Input extends EmbeddableInput> {
type: typeof EmbeddableExpressionType;
input: Input;
embeddableType: string;
}

export const embeddableType = (): ExpressionType<
typeof EmbeddableExpressionType,
EmbeddableExpression<any>
> => ({
name: EmbeddableExpressionType,
to: {
render: (embeddableExpression: EmbeddableExpression<any>) => {
return {
type: 'render',
as: EmbeddableExpressionType,
value: embeddableExpression,
};
},
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { embeddableType } from './embeddable';

export * from './embeddable';

export const typeFunctions = [embeddableType];
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ import { rounddate } from './rounddate';
import { rowCount } from './rowCount';
import { repeatImage } from './repeatImage';
import { revealImage } from './revealImage';
import { savedMap } from './savedMap';
import { savedSearch } from './savedSearch';
import { savedVisualization } from './savedVisualization';
import { seriesStyle } from './seriesStyle';
import { shape } from './shape';
import { sort } from './sort';
Expand Down Expand Up @@ -103,6 +106,9 @@ export const functions = [
revealImage,
rounddate,
rowCount,
savedMap,
savedSearch,
savedVisualization,
seriesStyle,
shape,
sort,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { savedMap } from './savedMap';
import { buildEmbeddableFilters } from '../../../server/lib/build_embeddable_filters';

const filterContext = {
and: [
{ and: [], value: 'filter-value', column: 'filter-column', type: 'exactly' },
{
and: [],
column: 'time-column',
type: 'time',
from: '2019-06-04T04:00:00.000Z',
to: '2019-06-05T04:00:00.000Z',
},
],
};

describe('savedMap', () => {
const fn = savedMap().fn;
const args = {
id: 'some-id',
};

it('accepts null context', () => {
const expression = fn(null, args, {});

expect(expression.input.filters).toEqual([]);
expect(expression.input.timeRange).toBeUndefined();
});

it('accepts filter context', () => {
const expression = fn(filterContext, args, {});
const embeddableFilters = buildEmbeddableFilters(filterContext.and);

expect(expression.input.filters).toEqual(embeddableFilters.filters);
expect(expression.input.timeRange).toEqual(embeddableFilters.timeRange);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd name all these files with snake case to be consistent with the rest of the code base (looks like a mix in this folder).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { Filter as ESFilterType } from '@kbn/es-query';
import { ExpressionFunction } from 'src/legacy/core_plugins/interpreter/public';
import { TimeRange } from 'ui/timefilter/time_history';
import { EmbeddableInput } from '../../../../../../../src/legacy/core_plugins/embeddable_api/public';
import { buildEmbeddableFilters } from '../../../server/lib/build_embeddable_filters';
import { Filter } from '../../../types';
import {
EmbeddableTypes,
EmbeddableExpressionType,
EmbeddableExpression,
} from '../../expression_types';

interface Arguments {
id: string;
}

// Map embeddable is missing proper typings, so type is just to document what we
// are expecting to pass to the embeddable
interface SavedMapInput extends EmbeddableInput {
id: string;
timeRange?: TimeRange;
refreshConfig: {
isPaused: boolean;
interval: number;
};
filters: ESFilterType[];
}

type Return = EmbeddableExpression<SavedMapInput>;

export function savedMap(): ExpressionFunction<'savedMap', Filter | null, Arguments, Return> {
return {
name: 'savedMap',
help: 'Render a Saved Map',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😠 i18n ...!!

args: {
id: {
types: ['string'],
required: false,
help: 'Id of the saved map',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same. You should be getting TS errors about these not being present in the help dictionary. Let me know if you're not.

},
},
type: EmbeddableExpressionType,
fn: (context, { id }) => {
const filters = context ? context.and : [];

return {
type: EmbeddableExpressionType,
input: {
id,
...buildEmbeddableFilters(filters),

refreshConfig: {
isPaused: false,
interval: 0,
},
},
embeddableType: EmbeddableTypes.map,
};
},
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { savedSearch } from './savedSearch';
import { buildEmbeddableFilters } from '../../../server/lib/build_embeddable_filters';

const filterContext = {
and: [
{ and: [], value: 'filter-value', column: 'filter-column', type: 'exactly' },
{
and: [],
column: 'time-column',
type: 'time',
from: '2019-06-04T04:00:00.000Z',
to: '2019-06-05T04:00:00.000Z',
},
],
};

describe('savedSearch', () => {
const fn = savedSearch().fn;
const args = {
id: 'some-id',
};

it('accepts null context', () => {
const expression = fn(null, args, {});

expect(expression.input.filters).toEqual([]);
expect(expression.input.timeRange).toBeUndefined();
});

it('accepts filter context', () => {
const expression = fn(filterContext, args, {});
const embeddableFilters = buildEmbeddableFilters(filterContext.and);

expect(expression.input.filters).toEqual(embeddableFilters.filters);
expect(expression.input.timeRange).toEqual(embeddableFilters.timeRange);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { ExpressionFunction } from 'src/legacy/core_plugins/interpreter/public';
import {
EmbeddableTypes,
EmbeddableExpressionType,
EmbeddableExpression,
} from '../../expression_types';

import { SearchInput } from '../../../../../../../src/legacy/core_plugins/kibana/public/discover/embeddable';
import { buildEmbeddableFilters } from '../../../server/lib/build_embeddable_filters';
import { Filter } from '../../../types';

interface Arguments {
id: string;
}

type Return = EmbeddableExpression<Partial<SearchInput> & { id: SearchInput['id'] }>;

export function savedSearch(): ExpressionFunction<'savedSearch', Filter | null, Arguments, Return> {
return {
name: 'savedSearch',
help: 'Render a Saved Search Query',
args: {
id: {
types: ['string'],
required: false,
help: 'Id of the saved search',
},
},
type: EmbeddableExpressionType,
fn: (context, { id }) => {
const filters = context ? context.and : [];
return {
type: EmbeddableExpressionType,
input: {
id,
...buildEmbeddableFilters(filters),
},
embeddableType: EmbeddableTypes.search,
};
},
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { savedVisualization } from './savedVisualization';
import { buildEmbeddableFilters } from '../../../server/lib/build_embeddable_filters';

const filterContext = {
and: [
{ and: [], value: 'filter-value', column: 'filter-column', type: 'exactly' },
{
and: [],
column: 'time-column',
type: 'time',
from: '2019-06-04T04:00:00.000Z',
to: '2019-06-05T04:00:00.000Z',
},
],
};

describe('savedVisualization', () => {
const fn = savedVisualization().fn;
const args = {
id: 'some-id',
};

it('accepts null context', () => {
const expression = fn(null, args, {});

expect(expression.input.filters).toEqual([]);
expect(expression.input.timeRange).toBeUndefined();
});

it('accepts filter context', () => {
const expression = fn(filterContext, args, {});
const embeddableFilters = buildEmbeddableFilters(filterContext.and);

expect(expression.input.filters).toEqual(embeddableFilters.filters);
expect(expression.input.timeRange).toEqual(embeddableFilters.timeRange);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { ExpressionFunction } from 'src/legacy/core_plugins/interpreter/public';
import {
EmbeddableTypes,
EmbeddableExpressionType,
EmbeddableExpression,
} from '../../expression_types';
// import { VisualizeInput } from '../../../../../../../src/legacy/core_plugins/kibana/public/visualize/embeddable';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove if not relevant.

import { buildEmbeddableFilters } from '../../../server/lib/build_embeddable_filters';
import { Filter } from '../../../types';

interface Arguments {
id: string;
}

// TODO: Importing from visualize/embeddable chokes type_check script
// Using an any here now until we can get that resolved
type Return = EmbeddableExpression</* VisualizeInput */ any & { id: string }>;

export function savedVisualization(): ExpressionFunction<
'savedVisualization',
Filter | null,
Arguments,
Return
> {
return {
name: 'savedVisualization',
help: 'Render a Saved Search Query',
args: {
id: {
types: ['string'],
required: false,
help: 'Id of the saved search',
},
},
type: EmbeddableExpressionType,
fn: (context, { id }) => {
const filters = context ? context.and : [];

return {
type: EmbeddableExpressionType,
input: {
id,
...buildEmbeddableFilters(filters),
},
embeddableType: EmbeddableTypes.visualization,
};
},
};
}
Loading