diff --git a/.eslintignore b/.eslintignore index a9c0861b093d0..0a030a64f7f3c 100644 --- a/.eslintignore +++ b/.eslintignore @@ -24,6 +24,7 @@ bower_components /packages/kbn-ui-framework/dist /packages/kbn-ui-framework/doc_site/build /packages/kbn-ui-framework/generator-kui/*/templates/ +/packages/kbn-interpreter/plugin /x-pack/coverage /x-pack/build /x-pack/plugins/**/__tests__/fixtures/** diff --git a/package.json b/package.json index bb81e4908cfe6..a7acba5253a1b 100644 --- a/package.json +++ b/package.json @@ -73,6 +73,7 @@ "@kbn/pm": "link:packages/kbn-pm", "@kbn/test-subj-selector": "link:packages/kbn-test-subj-selector", "@kbn/ui-framework": "link:packages/kbn-ui-framework", + "@kbn/interpreter": "link:packages/kbn-interpreter", "abortcontroller-polyfill": "^1.1.9", "angular": "1.6.9", "angular-aria": "1.6.6", @@ -81,10 +82,15 @@ "angular-route": "1.4.7", "angular-sanitize": "1.5.7", "angular-sortable-view": "0.0.15", + "axios": "^0.18.0", "autoprefixer": "^9.1.0", "babel-core": "6.21.0", "babel-loader": "7.1.2", "babel-polyfill": "6.20.0", + "babel-plugin-inline-react-svg": "^0.5.4", + "babel-plugin-mock-imports": "^0.0.5", + "babel-plugin-pegjs-inline-precompile": "^0.1.0", + "babel-plugin-transform-react-remove-prop-types": "^0.4.14", "babel-register": "6.18.0", "bluebird": "2.9.34", "boom": "5.2.0", @@ -177,6 +183,7 @@ "rxjs": "^6.2.1", "script-loader": "0.7.2", "semver": "^5.5.0", + "squel": "^5.12.2", "style-loader": "0.19.0", "tar": "2.2.0", "tinygradient": "0.3.0", diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/fixtures/test_styles.js b/packages/kbn-interpreter/common/__tests__/fixtures/test_styles.js similarity index 58% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/fixtures/test_styles.js rename to packages/kbn-interpreter/common/__tests__/fixtures/test_styles.js index 82d911e72772d..094f4e4aaf12d 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/fixtures/test_styles.js +++ b/packages/kbn-interpreter/common/__tests__/fixtures/test_styles.js @@ -1,10 +1,23 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ -import { elasticLogo } from '../../../../lib/elastic_logo'; +import { elasticLogo } from '../../elastic_logo'; export const fontStyle = { type: 'style', @@ -18,8 +31,8 @@ export const fontStyle = { fontSize: '14px', lineHeight: '21px', }, - css: - 'font-family:Chalkboard, serif;font-weight:bolder;font-style:normal;text-decoration:underline;color:pink;text-align:center;font-size:14px;line-height:21px', + css: 'font-family:Chalkboard, serif;font-weight:bolder;font-style:normal;' + + 'text-decoration:underline;color:pink;text-align:center;font-size:14px;line-height:21px', }; export const containerStyle = { diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/fixtures/test_tables.js b/packages/kbn-interpreter/common/__tests__/fixtures/test_tables.js similarity index 81% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/fixtures/test_tables.js rename to packages/kbn-interpreter/common/__tests__/fixtures/test_tables.js index 7c58bb53bc367..50d7b11cf280a 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/fixtures/test_tables.js +++ b/packages/kbn-interpreter/common/__tests__/fixtures/test_tables.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ const emptyTable = { diff --git a/packages/kbn-interpreter/common/__tests__/helpers/function_wrapper.js b/packages/kbn-interpreter/common/__tests__/helpers/function_wrapper.js new file mode 100644 index 0000000000000..94b981616b028 --- /dev/null +++ b/packages/kbn-interpreter/common/__tests__/helpers/function_wrapper.js @@ -0,0 +1,31 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { mapValues } from 'lodash'; + +// It takes a function spec and passes in default args into the spec fn +export const functionWrapper = (fnSpec, mockReduxStore) => { + const spec = fnSpec(); + const defaultArgs = mapValues(spec.args, argSpec => { + return argSpec.default; + }); + + return (context, args, handlers) => + spec.fn(context, { ...defaultArgs, ...args }, handlers, mockReduxStore); +}; diff --git a/packages/kbn-interpreter/common/elastic_logo.js b/packages/kbn-interpreter/common/elastic_logo.js new file mode 100644 index 0000000000000..1ade7f1f269c0 --- /dev/null +++ b/packages/kbn-interpreter/common/elastic_logo.js @@ -0,0 +1,2 @@ +/* eslint-disable */ +export const elasticLogo = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjxzdmcKICAgeG1sbnM6ZGM9Imh0dHA6Ly9wdXJsLm9yZy9kYy9lbGVtZW50cy8xLjEvIgogICB4bWxuczpjYz0iaHR0cDovL2NyZWF0aXZlY29tbW9ucy5vcmcvbnMjIgogICB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiCiAgIHhtbG5zOnN2Zz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciCiAgIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIKICAgdmlld0JveD0iMCAwIDI3MC42MDAwMSAyNjkuNTQ2NjYiCiAgIGhlaWdodD0iMjY5LjU0NjY2IgogICB3aWR0aD0iMjcwLjYwMDAxIgogICB4bWw6c3BhY2U9InByZXNlcnZlIgogICBpZD0ic3ZnMiIKICAgdmVyc2lvbj0iMS4xIj48bWV0YWRhdGEKICAgICBpZD0ibWV0YWRhdGE4Ij48cmRmOlJERj48Y2M6V29yawogICAgICAgICByZGY6YWJvdXQ9IiI+PGRjOmZvcm1hdD5pbWFnZS9zdmcreG1sPC9kYzpmb3JtYXQ+PGRjOnR5cGUKICAgICAgICAgICByZGY6cmVzb3VyY2U9Imh0dHA6Ly9wdXJsLm9yZy9kYy9kY21pdHlwZS9TdGlsbEltYWdlIiAvPjwvY2M6V29yaz48L3JkZjpSREY+PC9tZXRhZGF0YT48ZGVmcwogICAgIGlkPSJkZWZzNiIgLz48ZwogICAgIHRyYW5zZm9ybT0ibWF0cml4KDEuMzMzMzMzMywwLDAsLTEuMzMzMzMzMywwLDI2OS41NDY2NykiCiAgICAgaWQ9ImcxMCI+PGcKICAgICAgIHRyYW5zZm9ybT0ic2NhbGUoMC4xKSIKICAgICAgIGlkPSJnMTIiPjxwYXRoCiAgICAgICAgIGlkPSJwYXRoMTQiCiAgICAgICAgIHN0eWxlPSJmaWxsOiNmZmZmZmY7ZmlsbC1vcGFjaXR5OjE7ZmlsbC1ydWxlOm5vbnplcm87c3Ryb2tlOm5vbmUiCiAgICAgICAgIGQ9Im0gMjAyOS40OCw5NjIuNDQxIGMgMCwxNzAuMDk5IC0xMDUuNDYsMzE4Ljc5OSAtMjY0LjE3LDM3Ni42NTkgNi45OCwzNS44NiAxMC42Miw3MS43MSAxMC42MiwxMDkuMDUgMCwzMTYuMTkgLTI1Ny4yNCw1NzMuNDMgLTU3My40Nyw1NzMuNDMgLTE4NC43MiwwIC0zNTYuNTU4LC04OC41OSAtNDY0LjUzLC0yMzcuODUgLTUzLjA5LDQxLjE4IC0xMTguMjg1LDYzLjc1IC0xODYuMzA1LDYzLjc1IC0xNjcuODM2LDAgLTMwNC4zODMsLTEzNi41NCAtMzA0LjM4MywtMzA0LjM4IDAsLTM3LjA4IDYuNjE3LC03Mi41OCAxOS4wMzEsLTEwNi4wOCBDIDEwOC40ODgsMTM4MC4wOSAwLDEyMjcuODkgMCwxMDU4Ljg4IDAsODg3LjkxIDEwNS45NzcsNzM4LjUzOSAyNjUuMzk4LDY4MS4wOSBjIC02Ljc2OSwtMzUuNDQyIC0xMC40NiwtNzIuMDIgLTEwLjQ2LC0xMDkgQyAyNTQuOTM4LDI1Ni42MjEgNTExLjU2NiwwIDgyNy4wMjcsMCAxMDEyLjIsMCAxMTgzLjk0LDg4Ljk0MTQgMTI5MS4zLDIzOC44MzIgYyA1My40NSwtNDEuOTYxIDExOC44LC02NC45OTIgMTg2LjU2LC02NC45OTIgMTY3LjgzLDAgMzA0LjM4LDEzNi40OTIgMzA0LjM4LDMwNC4zMzIgMCwzNy4wNzggLTYuNjIsNzIuNjI5IC0xOS4wMywxMDYuMTI5IDE1Ny43OCw1Ni44NzkgMjY2LjI3LDIwOS4xMjkgMjY2LjI3LDM3OC4xNCIgLz48cGF0aAogICAgICAgICBpZD0icGF0aDE2IgogICAgICAgICBzdHlsZT0iZmlsbDojZmFjZjA5O2ZpbGwtb3BhY2l0eToxO2ZpbGwtcnVsZTpub256ZXJvO3N0cm9rZTpub25lIgogICAgICAgICBkPSJtIDc5Ny44OTgsMTE1MC45MyA0NDQuMDcyLC0yMDIuNDUgNDQ4LjA1LDM5Mi41OCBjIDYuNDksMzIuMzkgOS42Niw2NC42NyA5LjY2LDk4LjQ2IDAsMjc2LjIzIC0yMjQuNjgsNTAwLjk1IC01MDAuOSw1MDAuOTUgLTE2NS4yNCwwIC0zMTkuMzcsLTgxLjM2IC00MTMuMDUzLC0yMTcuNzkgbCAtNzQuNTI0LC0zODYuNjQgODYuNjk1LC0xODUuMTEiIC8+PHBhdGgKICAgICAgICAgaWQ9InBhdGgxOCIKICAgICAgICAgc3R5bGU9ImZpbGw6IzQ5YzFhZTtmaWxsLW9wYWNpdHk6MTtmaWxsLXJ1bGU6bm9uemVybztzdHJva2U6bm9uZSIKICAgICAgICAgZD0ibSAzMzguMjIzLDY4MC42NzIgYyAtNi40ODksLTMyLjM4MyAtOS44MDksLTY1Ljk4MSAtOS44MDksLTk5Ljk3MyAwLC0yNzYuOTI5IDIyNS4zMzYsLTUwMi4yNTc2IDUwMi4zMTMsLTUwMi4yNTc2IDE2Ni41OTMsMCAzMjEuNDczLDgyLjExNzYgNDE1LjAxMywyMTkuOTQ5NiBsIDczLjk3LDM4NS4zNDcgLTk4LjcyLDE4OC42MjEgTCA3NzUuMTU2LDEwNzUuNTcgMzM4LjIyMyw2ODAuNjcyIiAvPjxwYXRoCiAgICAgICAgIGlkPSJwYXRoMjAiCiAgICAgICAgIHN0eWxlPSJmaWxsOiNlZjI5OWI7ZmlsbC1vcGFjaXR5OjE7ZmlsbC1ydWxlOm5vbnplcm87c3Ryb2tlOm5vbmUiCiAgICAgICAgIGQ9Im0gMzM1LjQxLDE0NDkuMTggMzA0LjMzMiwtNzEuODYgNjYuNjgsMzQ2LjAyIGMgLTQxLjU4NiwzMS43OCAtOTIuOTMsNDkuMTggLTE0NS43MzEsNDkuMTggLTEzMi4yNSwwIC0yMzkuODEyLC0xMDcuNjEgLTIzOS44MTIsLTIzOS44NyAwLC0yOS4yMSA0Ljg3OSwtNTcuMjIgMTQuNTMxLC04My40NyIgLz48cGF0aAogICAgICAgICBpZD0icGF0aDIyIgogICAgICAgICBzdHlsZT0iZmlsbDojNGNhYmU0O2ZpbGwtb3BhY2l0eToxO2ZpbGwtcnVsZTpub256ZXJvO3N0cm9rZTpub25lIgogICAgICAgICBkPSJNIDMwOC45OTIsMTM3Ni43IEMgMTczLjAyLDEzMzEuNjQgNzguNDgwNSwxMjAxLjMgNzguNDgwNSwxMDU3LjkzIDc4LjQ4MDUsOTE4LjM0IDE2NC44Miw3OTMuNjggMjk0LjQwNiw3NDQuMzUyIGwgNDI2Ljk4MSwzODUuOTM4IC03OC4zOTUsMTY3LjUxIC0zMzQsNzguOSIgLz48cGF0aAogICAgICAgICBpZD0icGF0aDI0IgogICAgICAgICBzdHlsZT0iZmlsbDojODVjZTI2O2ZpbGwtb3BhY2l0eToxO2ZpbGwtcnVsZTpub256ZXJvO3N0cm9rZTpub25lIgogICAgICAgICBkPSJtIDEzMjMuOCwyOTguNDEgYyA0MS43NCwtMzIuMDkgOTIuODMsLTQ5LjU5IDE0NC45OCwtNDkuNTkgMTMyLjI1LDAgMjM5LjgxLDEwNy41NTkgMjM5LjgxLDIzOS44MjEgMCwyOS4xNiAtNC44OCw1Ny4xNjggLTE0LjUzLDgzLjQxOCBsIC0zMDQuMDgsNzEuMTYgLTY2LjE4LC0zNDQuODA5IiAvPjxwYXRoCiAgICAgICAgIGlkPSJwYXRoMjYiCiAgICAgICAgIHN0eWxlPSJmaWxsOiMzMTc3YTc7ZmlsbC1vcGFjaXR5OjE7ZmlsbC1ydWxlOm5vbnplcm87c3Ryb2tlOm5vbmUiCiAgICAgICAgIGQ9Im0gMTM4NS42Nyw3MjIuOTMgMzM0Ljc2LC03OC4zMDEgYyAxMzYuMDIsNDQuOTYxIDIzMC41NiwxNzUuMzUxIDIzMC41NiwzMTguNzYyIDAsMTM5LjMzOSAtODYuNTQsMjYzLjg1OSAtMjE2LjM4LDMxMy4wMzkgbCAtNDM3Ljg0LC0zODMuNTkgODguOSwtMTY5LjkxIiAvPjwvZz48L2c+PC9zdmc+'; diff --git a/x-pack/plugins/canvas/common/interpreter/cast.js b/packages/kbn-interpreter/common/interpreter/cast.js similarity index 53% rename from x-pack/plugins/canvas/common/interpreter/cast.js rename to packages/kbn-interpreter/common/interpreter/cast.js index 7e559afcba40e..cc257a7dc55e0 100644 --- a/x-pack/plugins/canvas/common/interpreter/cast.js +++ b/packages/kbn-interpreter/common/interpreter/cast.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import { getType } from '../lib/get_type'; @@ -19,8 +32,9 @@ export function castProvider(types) { for (let i = 0; i < toTypeNames.length; i++) { // First check if the current type can cast to this type - if (fromTypeDef && fromTypeDef.castsTo(toTypeNames[i])) + if (fromTypeDef && fromTypeDef.castsTo(toTypeNames[i])) { return fromTypeDef.to(node, toTypeNames[i], types); + } // If that isn't possible, check if this type can cast from the current type const toTypeDef = types[toTypeNames[i]]; diff --git a/x-pack/plugins/canvas/common/interpreter/interpret.js b/packages/kbn-interpreter/common/interpreter/interpret.js similarity index 87% rename from x-pack/plugins/canvas/common/interpreter/interpret.js rename to packages/kbn-interpreter/common/interpreter/interpret.js index 2777e9d0b80ea..fb7da1e4f7f59 100644 --- a/x-pack/plugins/canvas/common/interpreter/interpret.js +++ b/packages/kbn-interpreter/common/interpreter/interpret.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import clone from 'lodash.clone'; @@ -122,7 +135,7 @@ export function interpretProvider(config) { const argDef = getByAlias(argDefs, argName); // TODO: Implement a system to allow for undeclared arguments if (!argDef) - throw new Error(`Unknown argument '${argName}' passed to function '${fnDef.name}'`); + {throw new Error(`Unknown argument '${argName}' passed to function '${fnDef.name}'`);} argAsts[argDef.name] = (argAsts[argDef.name] || []).concat(argAst); return argAsts; @@ -152,7 +165,7 @@ export function interpretProvider(config) { argDefs, (argAsts, argDef, argName) => { if (typeof argAsts[argName] === 'undefined' && typeof argDef.default !== 'undefined') - argAsts[argName] = [fromExpression(argDef.default, 'argument')]; + {argAsts[argName] = [fromExpression(argDef.default, 'argument')];} return argAsts; }, diff --git a/x-pack/plugins/canvas/common/interpreter/socket_interpret.js b/packages/kbn-interpreter/common/interpreter/socket_interpret.js similarity index 72% rename from x-pack/plugins/canvas/common/interpreter/socket_interpret.js rename to packages/kbn-interpreter/common/interpreter/socket_interpret.js index a9ddb8c19c3f9..373be9f45e79c 100644 --- a/x-pack/plugins/canvas/common/interpreter/socket_interpret.js +++ b/packages/kbn-interpreter/common/interpreter/socket_interpret.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import uuid from 'uuid/v4'; @@ -40,8 +53,9 @@ export function socketInterpreterProvider({ // Get the list of functions that are known elsewhere return Promise.resolve(referableFunctions).then(referableFunctionMap => { // Check if the not-found function is in the list of alternatives, if not, throw - if (!getByAlias(referableFunctionMap, functionName)) + if (!getByAlias(referableFunctionMap, functionName)) { throw new Error(`Function not found: ${functionName}`); + } // set a unique message ID so the code knows what response to process const id = uuid(); diff --git a/packages/kbn-interpreter/common/lib/__tests__/arg.js b/packages/kbn-interpreter/common/lib/__tests__/arg.js new file mode 100644 index 0000000000000..f1648be4a08ab --- /dev/null +++ b/packages/kbn-interpreter/common/lib/__tests__/arg.js @@ -0,0 +1,36 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import expect from 'expect.js'; +import { Arg } from '../arg'; + +describe('Arg', () => { + it('sets required to false by default', () => { + const isOptional = new Arg({ + name: 'optional_me', + }); + expect(isOptional.required).to.equal(false); + + const isRequired = new Arg({ + name: 'require_me', + required: true, + }); + expect(isRequired.required).to.equal(true); + }); +}); diff --git a/x-pack/plugins/canvas/common/lib/__tests__/ast.from_expression.js b/packages/kbn-interpreter/common/lib/__tests__/ast.from_expression.js similarity index 82% rename from x-pack/plugins/canvas/common/lib/__tests__/ast.from_expression.js rename to packages/kbn-interpreter/common/lib/__tests__/ast.from_expression.js index 631973247dc6c..58303b827e561 100644 --- a/x-pack/plugins/canvas/common/lib/__tests__/ast.from_expression.js +++ b/packages/kbn-interpreter/common/lib/__tests__/ast.from_expression.js @@ -1,12 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { fromExpression } from '../ast'; -import { getType } from '../../lib/get_type'; +import { getType } from '../get_type'; describe('ast fromExpression', () => { describe('invalid expression', () => { diff --git a/x-pack/plugins/canvas/common/lib/__tests__/ast.to_expression.js b/packages/kbn-interpreter/common/lib/__tests__/ast.to_expression.js similarity index 95% rename from x-pack/plugins/canvas/common/lib/__tests__/ast.to_expression.js rename to packages/kbn-interpreter/common/lib/__tests__/ast.to_expression.js index 4b5985832e6ab..960e62303507d 100644 --- a/x-pack/plugins/canvas/common/lib/__tests__/ast.to_expression.js +++ b/packages/kbn-interpreter/common/lib/__tests__/ast.to_expression.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; diff --git a/x-pack/plugins/canvas/common/lib/__tests__/get_by_alias.js b/packages/kbn-interpreter/common/lib/__tests__/get_by_alias.js similarity index 66% rename from x-pack/plugins/canvas/common/lib/__tests__/get_by_alias.js rename to packages/kbn-interpreter/common/lib/__tests__/get_by_alias.js index f00092d573d68..d528521569c59 100644 --- a/x-pack/plugins/canvas/common/lib/__tests__/get_by_alias.js +++ b/packages/kbn-interpreter/common/lib/__tests__/get_by_alias.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; diff --git a/x-pack/plugins/canvas/common/lib/__tests__/pivot_object_array.js b/packages/kbn-interpreter/common/lib/__tests__/pivot_object_array.js similarity index 66% rename from x-pack/plugins/canvas/common/lib/__tests__/pivot_object_array.js rename to packages/kbn-interpreter/common/lib/__tests__/pivot_object_array.js index 58173c4c5a872..671d6f07cc732 100644 --- a/x-pack/plugins/canvas/common/lib/__tests__/pivot_object_array.js +++ b/packages/kbn-interpreter/common/lib/__tests__/pivot_object_array.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; diff --git a/x-pack/plugins/canvas/common/lib/__tests__/registry.js b/packages/kbn-interpreter/common/lib/__tests__/registry.js similarity index 85% rename from x-pack/plugins/canvas/common/lib/__tests__/registry.js rename to packages/kbn-interpreter/common/lib/__tests__/registry.js index fd19bf0300417..149676b28707d 100644 --- a/x-pack/plugins/canvas/common/lib/__tests__/registry.js +++ b/packages/kbn-interpreter/common/lib/__tests__/registry.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; diff --git a/packages/kbn-interpreter/common/lib/arg.js b/packages/kbn-interpreter/common/lib/arg.js new file mode 100644 index 0000000000000..2852a06abd843 --- /dev/null +++ b/packages/kbn-interpreter/common/lib/arg.js @@ -0,0 +1,36 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { includes } from 'lodash'; + +export function Arg(config) { + if (config.name === '_') throw Error('Arg names must not be _. Use it in aliases instead.'); + this.name = config.name; + this.required = config.required || false; + this.help = config.help; + this.types = config.types || []; + this.default = config.default; + this.aliases = config.aliases || []; + this.multi = config.multi == null ? false : config.multi; + this.resolve = config.resolve == null ? true : config.resolve; + this.accepts = type => { + if (!this.types.length) return true; + return includes(config.types, type); + }; +} diff --git a/x-pack/plugins/canvas/common/lib/ast.js b/packages/kbn-interpreter/common/lib/ast.js similarity index 81% rename from x-pack/plugins/canvas/common/lib/ast.js rename to packages/kbn-interpreter/common/lib/ast.js index b31848944e9db..61cfe94ac955c 100644 --- a/x-pack/plugins/canvas/common/lib/ast.js +++ b/packages/kbn-interpreter/common/lib/ast.js @@ -1,10 +1,23 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ -import { getType } from '../lib/get_type'; +import { getType } from './get_type'; import { parse } from './grammar'; function getArgumentString(arg, argKey, level = 0) { @@ -48,8 +61,9 @@ function getExpressionArgs(block, level = 0) { const lineLength = acc.split('\n').pop().length; // if arg values are too long, move it to the next line - if (level === 0 && lineLength + argString.length > MAX_LINE_LENGTH) + if (level === 0 && lineLength + argString.length > MAX_LINE_LENGTH) { return `${acc}\n ${argString}`; + } // append arg values to existing arg values if (lineLength > 0) return `${acc} ${argString}`; diff --git a/packages/kbn-interpreter/common/lib/constants.js b/packages/kbn-interpreter/common/lib/constants.js new file mode 100644 index 0000000000000..2c90f7dace5b5 --- /dev/null +++ b/packages/kbn-interpreter/common/lib/constants.js @@ -0,0 +1,20 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const FETCH_TIMEOUT = 30000; // 30 seconds diff --git a/packages/kbn-interpreter/common/lib/fetch.js b/packages/kbn-interpreter/common/lib/fetch.js new file mode 100644 index 0000000000000..4296046d15770 --- /dev/null +++ b/packages/kbn-interpreter/common/lib/fetch.js @@ -0,0 +1,30 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import axios from 'axios'; +import { FETCH_TIMEOUT } from './constants'; + +export const fetch = axios.create({ + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + 'kbn-xsrf': 'professionally-crafted-string-of-text', + }, + timeout: FETCH_TIMEOUT, +}); diff --git a/x-pack/plugins/canvas/common/lib/fn.js b/packages/kbn-interpreter/common/lib/fn.js similarity index 54% rename from x-pack/plugins/canvas/common/lib/fn.js rename to packages/kbn-interpreter/common/lib/fn.js index 70948c76579b3..c6b2fcbe67799 100644 --- a/x-pack/plugins/canvas/common/lib/fn.js +++ b/packages/kbn-interpreter/common/lib/fn.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import { mapValues, includes } from 'lodash'; diff --git a/x-pack/plugins/canvas/common/lib/fonts.js b/packages/kbn-interpreter/common/lib/fonts.js similarity index 73% rename from x-pack/plugins/canvas/common/lib/fonts.js rename to packages/kbn-interpreter/common/lib/fonts.js index 69b3bf2bb71b7..e0d05db02ecb1 100644 --- a/x-pack/plugins/canvas/common/lib/fonts.js +++ b/packages/kbn-interpreter/common/lib/fonts.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ export const americanTypewriter = { diff --git a/packages/kbn-interpreter/common/lib/functions_registry.js b/packages/kbn-interpreter/common/lib/functions_registry.js new file mode 100644 index 0000000000000..1c71707d84722 --- /dev/null +++ b/packages/kbn-interpreter/common/lib/functions_registry.js @@ -0,0 +1,29 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { Registry } from './registry'; +import { Fn } from './fn'; + +class FunctionsRegistry extends Registry { + wrapper(obj) { + return new Fn(obj); + } +} + +export const functionsRegistry = new FunctionsRegistry(); diff --git a/packages/kbn-interpreter/common/lib/get_by_alias.js b/packages/kbn-interpreter/common/lib/get_by_alias.js new file mode 100644 index 0000000000000..a21cc8923b594 --- /dev/null +++ b/packages/kbn-interpreter/common/lib/get_by_alias.js @@ -0,0 +1,34 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * This is used for looking up function/argument definitions. It looks through + * the given object for a case-insensitive match, which could be either the + * name of the key itself, or something under the `aliases` property. + */ +export function getByAlias(specs, name) { + const lowerCaseName = name.toLowerCase(); + const key = Object.keys(specs).find(key => { + if (key.toLowerCase() === lowerCaseName) return true; + return (specs[key].aliases || []).some(alias => { + return alias.toLowerCase() === lowerCaseName; + }); + }); + if (typeof key !== undefined) return specs[key]; +} diff --git a/packages/kbn-interpreter/common/lib/get_plugin_paths.js b/packages/kbn-interpreter/common/lib/get_plugin_paths.js new file mode 100644 index 0000000000000..624cbf52490c5 --- /dev/null +++ b/packages/kbn-interpreter/common/lib/get_plugin_paths.js @@ -0,0 +1,40 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import fs from 'fs'; +import { resolve } from 'path'; +import { promisify } from 'util'; +import { flatten } from 'lodash'; +import { pathsRegistry } from './paths_registry'; + +const readdir = promisify(fs.readdir); + +export const getPluginPaths = type => { + const typePaths = pathsRegistry.get(type); + if (!typePaths) throw new Error(`Unknown type: ${type}`); + + return Promise.all(typePaths.map(path => { + + // Get the full path of all files in the directory + return readdir(path).then(files => files.map(file => { + if (!file.endsWith('.js')) return; + return resolve(path, file); + }).filter(path => path)).catch(); + })).then(flatten); +}; diff --git a/packages/kbn-interpreter/common/lib/get_type.js b/packages/kbn-interpreter/common/lib/get_type.js new file mode 100644 index 0000000000000..8764ce0b26310 --- /dev/null +++ b/packages/kbn-interpreter/common/lib/get_type.js @@ -0,0 +1,28 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export function getType(node) { + if (node == null) return 'null'; + if (typeof node === 'object') { + if (!node.type) throw new Error('Objects must have a type propery'); + return node.type; + } + + return typeof node; +} diff --git a/x-pack/plugins/canvas/common/lib/grammar.js b/packages/kbn-interpreter/common/lib/grammar.js similarity index 96% rename from x-pack/plugins/canvas/common/lib/grammar.js rename to packages/kbn-interpreter/common/lib/grammar.js index c72312321af60..75cf2f22a74f7 100644 --- a/x-pack/plugins/canvas/common/lib/grammar.js +++ b/packages/kbn-interpreter/common/lib/grammar.js @@ -1,10 +1,31 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* eslint-disable */ + /* * Generated by PEG.js 0.10.0. * * http://pegjs.org/ */ -"use strict"; + function peg$subclass(child, parent) { function ctor() { this.constructor = child; } diff --git a/x-pack/plugins/canvas/common/lib/grammar.peg b/packages/kbn-interpreter/common/lib/grammar.peg similarity index 100% rename from x-pack/plugins/canvas/common/lib/grammar.peg rename to packages/kbn-interpreter/common/lib/grammar.peg diff --git a/packages/kbn-interpreter/common/lib/load_server_plugins.js b/packages/kbn-interpreter/common/lib/load_server_plugins.js new file mode 100644 index 0000000000000..5c283e169198e --- /dev/null +++ b/packages/kbn-interpreter/common/lib/load_server_plugins.js @@ -0,0 +1,62 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { typesRegistry } from '@kbn/interpreter/common/lib/types_registry'; +import { functionsRegistry as serverFunctions } from '@kbn/interpreter/common/lib/functions_registry'; +import { getPluginPaths } from './get_plugin_paths'; + +let loaded = null; + +export const loadServerPlugins = () => { + const types = { + serverFunctions: serverFunctions, + commonFunctions: serverFunctions, + types: typesRegistry, + }; + + if (loaded) return loaded; + + loaded = new Promise(resolve => { + const remainingTypes = Object.keys(types); + + const loadType = () => { + const type = remainingTypes.pop(); + getPluginPaths(type).then(paths => { + global.canvas = global.canvas || {}; + global.canvas.register = d => { + types[type].register(d); + }; + + paths.forEach(path => { + require(path); + }); + + delete global.canvas; + delete global._; + + if (remainingTypes.length) loadType(); + else resolve(true); + }); + }; + + loadType(); + }); + + return loaded; +}; diff --git a/packages/kbn-interpreter/common/lib/paths_registry.js b/packages/kbn-interpreter/common/lib/paths_registry.js new file mode 100644 index 0000000000000..a38de7083142b --- /dev/null +++ b/packages/kbn-interpreter/common/lib/paths_registry.js @@ -0,0 +1,69 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import clone from 'lodash.clone'; + +class PathsRegistry { + constructor() { + this._paths = {}; + } + + register = (type, paths) => { + if (!type) throw new Error(`Register requires a type`); + const lowerCaseType = type.toLowerCase(); + + const pathArray = Array.isArray(paths) ? paths : [paths]; + if (!this._paths[lowerCaseType]) { + this._paths[lowerCaseType] = []; + } + + pathArray.forEach(p => { + this._paths[lowerCaseType].push(p); + }); + }; + + registerAll = (paths) => { + Object.keys(paths).forEach(key => { + this.register(key, paths[key]); + }); + }; + + toJS = () => { + return Object.keys(this._paths).reduce((acc, key) => { + acc[key] = this.get(key); + return acc; + }, {}); + }; + + toArray = () => { + return Object.keys(this._paths).map(key => this.get(key)); + }; + + get = (type) => { + if (type === undefined) return []; + const lowerCaseType = type.toLowerCase(); + return this._paths[lowerCaseType] ? clone(this._paths[lowerCaseType]) : []; + }; + + reset = () => { + this._paths = {}; + }; +} + +export const pathsRegistry = new PathsRegistry(); diff --git a/packages/kbn-interpreter/common/lib/pivot_object_array.js b/packages/kbn-interpreter/common/lib/pivot_object_array.js new file mode 100644 index 0000000000000..7dd0b47da9f35 --- /dev/null +++ b/packages/kbn-interpreter/common/lib/pivot_object_array.js @@ -0,0 +1,30 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { map, zipObject } from 'lodash'; + +const isString = val => typeof val === 'string'; + +export function pivotObjectArray(rows, columns) { + const columnNames = columns || Object.keys(rows[0]); + if (!columnNames.every(isString)) throw new Error('Columns should be an array of strings'); + + const columnValues = map(columnNames, name => map(rows, name)); + return zipObject(columnNames, columnValues); +} diff --git a/x-pack/plugins/canvas/common/lib/registry.js b/packages/kbn-interpreter/common/lib/registry.js similarity index 56% rename from x-pack/plugins/canvas/common/lib/registry.js rename to packages/kbn-interpreter/common/lib/registry.js index accabae4bc5eb..9882f3abde723 100644 --- a/x-pack/plugins/canvas/common/lib/registry.js +++ b/packages/kbn-interpreter/common/lib/registry.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import clone from 'lodash.clone'; @@ -22,8 +35,9 @@ export class Registry { const obj = fn(); - if (typeof obj !== 'object' || !obj[this._prop]) + if (typeof obj !== 'object' || !obj[this._prop]) { throw new Error(`Registered functions must return an object with a ${this._prop} property`); + } this._indexed[obj[this._prop].toLowerCase()] = this.wrapper(obj); } diff --git a/packages/kbn-interpreter/common/lib/serialize.js b/packages/kbn-interpreter/common/lib/serialize.js new file mode 100644 index 0000000000000..2f881db3c77e0 --- /dev/null +++ b/packages/kbn-interpreter/common/lib/serialize.js @@ -0,0 +1,37 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { get, identity } from 'lodash'; +import { getType } from './get_type'; + +export function serializeProvider(types) { + return { + serialize: provider('serialize'), + deserialize: provider('deserialize'), + }; + + function provider(key) { + return context => { + const type = getType(context); + const typeDef = types[type]; + const fn = get(typeDef, key) || identity; + return fn(context); + }; + } +} diff --git a/x-pack/plugins/canvas/common/lib/type.js b/packages/kbn-interpreter/common/lib/type.js similarity index 61% rename from x-pack/plugins/canvas/common/lib/type.js rename to packages/kbn-interpreter/common/lib/type.js index d917750e3848e..92d7bb8c0084d 100644 --- a/x-pack/plugins/canvas/common/lib/type.js +++ b/packages/kbn-interpreter/common/lib/type.js @@ -1,12 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ // All types must be universal and be castable on the client or on the server import { get } from 'lodash'; -import { getType } from '../lib/get_type'; +import { getType } from './get_type'; // TODO: Currently all casting functions must be syncronous. @@ -35,10 +48,11 @@ export function Type(config) { this.to = (node, toTypeName, types) => { const typeName = getType(node); - if (typeName !== this.name) + if (typeName !== this.name) { throw new Error(`Can not cast object of type '${typeName}' using '${this.name}'`); - else if (!this.castsTo(toTypeName)) + } else if (!this.castsTo(toTypeName)) { throw new Error(`Can not cast '${typeName}' to '${toTypeName}'`); + } return getToFn(toTypeName)(node, types); }; diff --git a/packages/kbn-interpreter/common/lib/types_registry.js b/packages/kbn-interpreter/common/lib/types_registry.js new file mode 100644 index 0000000000000..97e28875d7e20 --- /dev/null +++ b/packages/kbn-interpreter/common/lib/types_registry.js @@ -0,0 +1,29 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { Registry } from './registry'; +import { Type } from './type'; + +class TypesRegistry extends Registry { + wrapper(obj) { + return new Type(obj); + } +} + +export const typesRegistry = new TypesRegistry(); diff --git a/packages/kbn-interpreter/package.json b/packages/kbn-interpreter/package.json new file mode 100644 index 0000000000000..0178b9e2cfe32 --- /dev/null +++ b/packages/kbn-interpreter/package.json @@ -0,0 +1,10 @@ +{ + "name": "@kbn/interpreter", + "version": "1.0.0", + "license": "Apache-2.0", + "scripts": { + "build": "node tasks/build.js", + "canvas:peg": "pegjs common/lib/grammar.peg", + "kbn:bootstrap": "yarn build" + } +} diff --git a/packages/kbn-interpreter/plugin_src/functions/browser/browser.js b/packages/kbn-interpreter/plugin_src/functions/browser/browser.js new file mode 100644 index 0000000000000..4bac4afdf67db --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/browser/browser.js @@ -0,0 +1,25 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const browser = () => ({ + name: 'browser', + help: 'Force the interpreter to return to the browser', + args: {}, + fn: context => context, +}); diff --git a/packages/kbn-interpreter/plugin_src/functions/browser/index.js b/packages/kbn-interpreter/plugin_src/functions/browser/index.js new file mode 100644 index 0000000000000..27e3dd4cb8b50 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/browser/index.js @@ -0,0 +1,24 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { browser } from './browser'; +import { location } from './location'; +import { urlparam } from './urlparam'; + +export const clientFunctions = [browser, location, urlparam]; diff --git a/packages/kbn-interpreter/plugin_src/functions/browser/location.js b/packages/kbn-interpreter/plugin_src/functions/browser/location.js new file mode 100644 index 0000000000000..b95bb7b436ac2 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/browser/location.js @@ -0,0 +1,45 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +const noop = () => {}; + +export const location = () => ({ + name: 'location', + type: 'datatable', + context: { + types: ['null'], + }, + help: + 'Use the browser\'s location functionality to get your current location. Usually quite slow, but fairly accurate', + fn: () => { + return new Promise(resolve => { + function createLocation(geoposition) { + const { latitude, longitude } = geoposition.coords; + return resolve({ + type: 'datatable', + columns: [{ name: 'latitude', type: 'number' }, { name: 'longitude', type: 'number' }], + rows: [{ latitude, longitude }], + }); + } + return navigator.geolocation.getCurrentPosition(createLocation, noop, { + maximumAge: 5000, + }); + }); + }, +}); diff --git a/packages/kbn-interpreter/plugin_src/functions/browser/register.js b/packages/kbn-interpreter/plugin_src/functions/browser/register.js new file mode 100644 index 0000000000000..feb0be981be98 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/browser/register.js @@ -0,0 +1,22 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { clientFunctions } from './index'; + +clientFunctions.forEach(window.canvas.register); diff --git a/packages/kbn-interpreter/plugin_src/functions/browser/urlparam.js b/packages/kbn-interpreter/plugin_src/functions/browser/urlparam.js new file mode 100644 index 0000000000000..f2d2f3238ef55 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/browser/urlparam.js @@ -0,0 +1,48 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { parse } from 'url'; + +export const urlparam = () => ({ + name: 'urlparam', + aliases: [], + type: 'string', + help: + 'Access URL parameters and use them in expressions. Eg https://localhost:5601/app/canvas?myVar=20. This will always return a string', + context: { + types: ['null'], + }, + args: { + param: { + types: ['string'], + aliases: ['_', 'var', 'variable'], + help: 'The URL hash parameter to access', + multi: false, + }, + default: { + types: ['string'], + default: '""', + help: 'Return this string if the url parameter is not defined', + }, + }, + fn: (context, args) => { + const query = parse(window.location.href, true).query; + return query[args.param] || args.default; + }, +}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/all.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/all.js similarity index 58% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/all.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/all.js index 90558c26d5c87..1a99d087e2dbc 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/all.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/all.js @@ -1,12 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { all } from '../all'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; describe('all', () => { const fn = functionWrapper(all); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/alterColumn.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/alter_column.js similarity index 89% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/alterColumn.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/alter_column.js index 763fe0fe57200..687784b039527 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/alterColumn.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/alter_column.js @@ -1,13 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; -import { alterColumn } from '../alterColumn'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; -import { emptyTable, testTable } from './fixtures/test_tables'; +import { alterColumn } from '../alter_column'; +import { functionWrapper, emptyTable, testTable } from '@kbn/interpreter/test_utils'; describe('alterColumn', () => { const fn = functionWrapper(alterColumn); @@ -49,7 +61,7 @@ describe('alterColumn', () => { it('throws if column does not exists', () => { expect(() => fn(emptyTable, { column: 'foo', type: 'number' })).to.throwException(e => { - expect(e.message).to.be("Column not found: 'foo'"); + expect(e.message).to.be('Column not found: \'foo\''); }); }); }); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/any.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/any.js similarity index 59% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/any.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/any.js index bc67aa377e49d..c6fc0b44b6e6b 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/any.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/any.js @@ -1,12 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { any } from '../any'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; describe('any', () => { const fn = functionWrapper(any); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/as.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/as.js similarity index 51% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/as.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/as.js index 82a8d29742279..3f4bea7973043 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/as.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/as.js @@ -1,12 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { asFn } from '../as'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; describe('as', () => { const fn = functionWrapper(asFn); @@ -37,7 +50,7 @@ describe('as', () => { expect(fn(null, { name: 'foo' }).columns[0].name).to.eql('foo'); }); - it("returns a datatable with the column name 'value'", () => { + it('returns a datatable with the column name \'value\'', () => { expect(fn(null).columns[0].name).to.eql('value'); }); }); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/case.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/case.js similarity index 71% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/case.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/case.js index c5fd1f14e79db..5b960429d09b5 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/case.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/case.js @@ -1,12 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { caseFn } from '../case'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; describe('case', () => { const fn = functionWrapper(caseFn); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/columns.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/columns.js similarity index 82% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/columns.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/columns.js index ca1e647cbe9e7..815a231385677 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/columns.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/columns.js @@ -1,13 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { columns } from '../columns'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; -import { emptyTable, testTable } from './fixtures/test_tables'; +import { functionWrapper, emptyTable, testTable } from '@kbn/interpreter/test_utils'; describe('columns', () => { const fn = functionWrapper(columns); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/compare.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/compare.js similarity index 81% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/compare.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/compare.js index c7b61be4e4b25..36768d6aaf122 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/compare.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/compare.js @@ -1,12 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { compare } from '../compare'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; describe('compare', () => { const fn = functionWrapper(compare); @@ -17,7 +30,7 @@ describe('compare', () => { expect(fn(0, { op: 'lt', to: 1 })).to.be(true); }); - it("defaults to 'eq'", () => { + it('defaults to \'eq\'', () => { expect(fn(0, { to: 1 })).to.be(false); expect(fn(0, { to: 0 })).to.be(true); }); @@ -109,7 +122,7 @@ describe('compare', () => { }); describe('different type comparisons', () => { - it("returns true for 'ne' only", () => { + it('returns true for \'ne\' only', () => { expect(fn(0, { op: 'ne', to: '0' })).to.be(true); }); diff --git a/packages/kbn-interpreter/plugin_src/functions/common/__tests__/context.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/context.js new file mode 100644 index 0000000000000..9d198a234948b --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/context.js @@ -0,0 +1,36 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import expect from 'expect.js'; +import { context } from '../context'; +import { functionWrapper, emptyTable, testTable } from '@kbn/interpreter/test_utils'; + +describe('context', () => { + const fn = functionWrapper(context); + + it('returns whatever context you pass into', () => { + expect(fn(null)).to.be(null); + expect(fn(true)).to.be(true); + expect(fn(1)).to.be(1); + expect(fn('foo')).to.be('foo'); + expect(fn({})).to.eql({}); + expect(fn(emptyTable)).to.eql(emptyTable); + expect(fn(testTable)).to.eql(testTable); + }); +}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/csv.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/csv.js similarity index 71% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/csv.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/csv.js index 8f79cab5577af..39c127bd84319 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/csv.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/csv.js @@ -1,12 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { csv } from '../csv'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; describe('csv', () => { const fn = functionWrapper(csv); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/date.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/date.js similarity index 63% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/date.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/date.js index 94b6e7eb4f674..c8552179909a4 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/date.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/date.js @@ -1,13 +1,26 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import sinon from 'sinon'; import { date } from '../date'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; describe('date', () => { const fn = functionWrapper(date); diff --git a/packages/kbn-interpreter/plugin_src/functions/common/__tests__/do.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/do.js new file mode 100644 index 0000000000000..dceee5a49eb2f --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/do.js @@ -0,0 +1,33 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import expect from 'expect.js'; +import { doFn } from '../do'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; + +describe('do', () => { + const fn = functionWrapper(doFn); + + it('should only pass context', () => { + expect(fn(1, { fn: '1' })).to.equal(1); + expect(fn(true, {})).to.equal(true); + expect(fn(null, {})).to.equal(null); + expect(fn(null, { fn: 'not null' })).to.equal(null); + }); +}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/eq.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/eq.js similarity index 50% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/eq.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/eq.js index a419dd98f3ffa..1eb88cbb37cd1 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/eq.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/eq.js @@ -1,12 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { eq } from '../eq'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; describe('eq', () => { const fn = functionWrapper(eq); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/exactly.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/exactly.js similarity index 50% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/exactly.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/exactly.js index e9f37efd15c26..386b20372f867 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/exactly.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/exactly.js @@ -1,13 +1,31 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { exactly } from '../exactly'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; -import { emptyFilter } from './fixtures/test_filters'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; + +const emptyFilter = { + type: 'filter', + meta: {}, + and: [], +}; describe('exactly', () => { const fn = functionWrapper(exactly); @@ -17,7 +35,7 @@ describe('exactly', () => { expect(fn(emptyFilter, args)).to.have.property('type', 'filter'); }); - it("adds an exactly object to 'and'", () => { + it('adds an exactly object to \'and\'', () => { const result = fn(emptyFilter, { column: 'name', value: 'product2' }); expect(result.and[0]).to.have.property('type', 'exactly'); }); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/filterrows.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/filterrows.js similarity index 56% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/filterrows.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/filterrows.js index baa7096cb6324..95495452926f8 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/filterrows.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/filterrows.js @@ -1,13 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { filterrows } from '../filterrows'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; -import { testTable } from './fixtures/test_tables'; +import { functionWrapper, testTable } from '@kbn/interpreter/test_utils'; const inStock = datatable => datatable.rows[0].in_stock; const returnFalse = () => false; diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/font.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/font.js similarity index 86% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/font.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/font.js index 7816283c370b3..a118200e0ea7f 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/font.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/font.js @@ -1,13 +1,26 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; -import { openSans } from '../../../../common/lib/fonts'; +import { openSans } from '@kbn/interpreter/common/lib/fonts'; import { font } from '../font'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; describe('font', () => { const fn = functionWrapper(font); @@ -97,7 +110,7 @@ describe('font', () => { expect(result.css).to.contain('font-weight:400'); }); - it("defaults to 'normal'", () => { + it('defaults to \'normal\'', () => { const result = fn(null); expect(result.spec).to.have.property('fontWeight', 'normal'); expect(result.css).to.contain('font-weight:normal'); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/formatdate.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/formatdate.js similarity index 54% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/formatdate.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/formatdate.js index d9f9169079044..b14a94a363c3e 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/formatdate.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/formatdate.js @@ -1,12 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { formatdate } from '../formatdate'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; describe('formatdate', () => { const fn = functionWrapper(formatdate); diff --git a/packages/kbn-interpreter/plugin_src/functions/common/__tests__/formatnumber.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/formatnumber.js new file mode 100644 index 0000000000000..2316cc74e8e43 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/formatnumber.js @@ -0,0 +1,42 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import expect from 'expect.js'; +import { formatnumber } from '../formatnumber'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; + +describe('formatnumber', () => { + const fn = functionWrapper(formatnumber); + + it('returns number as formatted string with given format', () => { + expect(fn(140000, { format: '$0,0.00' })).to.be('$140,000.00'); + }); + + describe('args', () => { + describe('format', () => { + it('sets the format of the resulting number string', () => { + expect(fn(0.68, { format: '0.000%' })).to.be('68.000%'); + }); + + it('casts number to a string if format is not specified', () => { + expect(fn(140000.999999)).to.be('140000.999999'); + }); + }); + }); +}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/getCell.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/get_cell.js similarity index 71% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/getCell.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/get_cell.js index 9509c05f9229d..c91db2ba57adc 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/getCell.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/get_cell.js @@ -1,13 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; -import { getCell } from '../getCell'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; -import { emptyTable, testTable } from './fixtures/test_tables'; +import { getCell } from '../get_cell'; +import { functionWrapper, emptyTable, testTable } from '@kbn/interpreter/test_utils'; describe('getCell', () => { const fn = functionWrapper(getCell); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/gt.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/gt.js similarity index 52% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/gt.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/gt.js index ca490aa88fec7..c625f3cdc038d 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/gt.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/gt.js @@ -1,12 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { gt } from '../gt'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; describe('gt', () => { const fn = functionWrapper(gt); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/gte.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/gte.js similarity index 52% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/gte.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/gte.js index ba4f0f7a3f9b1..d29f60ab72454 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/gte.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/gte.js @@ -1,12 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { gte } from '../gte'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; describe('gte', () => { const fn = functionWrapper(gte); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/head.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/head.js similarity index 53% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/head.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/head.js index d9cd5a0743764..120446b9b990c 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/head.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/head.js @@ -1,13 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { head } from '../head'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; -import { emptyTable, testTable } from './fixtures/test_tables'; +import { functionWrapper, emptyTable, testTable } from '@kbn/interpreter/test_utils'; describe('head', () => { const fn = functionWrapper(head); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/if.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/if.js similarity index 71% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/if.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/if.js index aee9efc5203c2..3f88799255cb4 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/if.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/if.js @@ -1,12 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { ifFn } from '../if'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; describe('if', () => { const fn = functionWrapper(ifFn); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/lt.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/lt.js similarity index 52% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/lt.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/lt.js index 3597bd3805d6e..c0388aefe4639 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/lt.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/lt.js @@ -1,12 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { lt } from '../lt'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; describe('lt', () => { const fn = functionWrapper(lt); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/lte.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/lte.js similarity index 52% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/lte.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/lte.js index e800b72c13ff5..1fc103deb46f9 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/lte.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/lte.js @@ -1,12 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { lte } from '../lte'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; describe('lte', () => { const fn = functionWrapper(lte); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/mapColumn.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/map_column.js similarity index 68% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/mapColumn.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/map_column.js index 60121f9717abd..eb8fae2cc40c9 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/mapColumn.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/map_column.js @@ -1,13 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; -import { mapColumn } from '../mapColumn'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; -import { testTable } from './fixtures/test_tables'; +import { mapColumn } from '../map_column'; +import { functionWrapper, testTable } from '@kbn/interpreter/test_utils'; const pricePlusTwo = datatable => Promise.resolve(datatable.rows[0].price + 2); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/math.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/math.js similarity index 81% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/math.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/math.js index f290cce865153..63c01dcecb120 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/math.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/math.js @@ -1,13 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { math } from '../math'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; -import { emptyTable, testTable } from './fixtures/test_tables'; +import { functionWrapper, emptyTable, testTable } from '@kbn/interpreter/test_utils'; describe('math', () => { const fn = functionWrapper(math); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/neq.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/neq.js similarity index 50% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/neq.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/neq.js index 3d2ca7b90017c..744072de8c9e6 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/neq.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/neq.js @@ -1,12 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { neq } from '../neq'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; describe('neq', () => { const fn = functionWrapper(neq); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/render.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/render.js similarity index 66% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/render.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/render.js index 720242418e0a5..31a603eb8db36 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/render.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/render.js @@ -1,14 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; +import { functionWrapper, testTable, fontStyle, containerStyle } from '@kbn/interpreter/test_utils'; import { render } from '../render'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; -import { testTable } from './fixtures/test_tables'; -import { fontStyle, containerStyle } from './fixtures/test_styles'; const renderTable = { type: 'render', @@ -56,7 +67,7 @@ describe('render', () => { expect(result).to.have.property('css', '".canvasRenderEl { background-color: red; }"'); }); - it("defaults to '* > * {}'", () => { + it('defaults to \'* > * {}\'', () => { const result = fn(renderTable); expect(result).to.have.property('css', '"* > * {}"'); }); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/replace.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/replace.js similarity index 72% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/replace.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/replace.js index 55fcbf10d7fd7..cb803a76eed15 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/replace.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/replace.js @@ -1,12 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { replace } from '../replace'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; describe('replace', () => { const fn = functionWrapper(replace); @@ -62,7 +75,7 @@ describe('replace', () => { expect(fn('AaBbAaBb', { pattern: 'a', flags: '', replacement: '_' })).to.be('A_BbAaBb'); }); - it("defaults to 'g' if flag is not provided", () => { + it('defaults to \'g\' if flag is not provided', () => { expect(fn('This,is,a,test!', { pattern: ',', replacement: ' ' })).to.be('This is a test!'); }); }); diff --git a/packages/kbn-interpreter/plugin_src/functions/common/__tests__/rounddate.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/rounddate.js new file mode 100644 index 0000000000000..2d6274b92ca50 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/rounddate.js @@ -0,0 +1,44 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import expect from 'expect.js'; +import { rounddate } from '../rounddate'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; + +describe('rounddate', () => { + const fn = functionWrapper(rounddate); + const date = new Date('2011-10-31T00:00:00.000Z').valueOf(); + + it('returns date in ms from date in ms or ISO8601 string', () => { + expect(fn(date, { format: 'YYYY' })).to.be(1293840000000); + }); + + describe('args', () => { + describe('format', () => { + it('sets the format for the rounded date', () => { + expect(fn(date, { format: 'YYYY-MM' })).to.be(1317427200000); + expect(fn(date, { format: 'YYYY-MM-DD-hh' })).to.be(1320062400000); + }); + + it('returns original date if format is not provided', () => { + expect(fn(date)).to.be(date); + }); + }); + }); +}); diff --git a/packages/kbn-interpreter/plugin_src/functions/common/__tests__/row_count.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/row_count.js new file mode 100644 index 0000000000000..60ebfa179dd92 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/row_count.js @@ -0,0 +1,31 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import expect from 'expect.js'; +import { rowCount } from '../row_count'; +import { functionWrapper, emptyTable, testTable } from '@kbn/interpreter/test_utils'; + +describe('rowCount', () => { + const fn = functionWrapper(rowCount); + + it('returns the number of rows in the datatable', () => { + expect(fn(testTable)).to.equal(testTable.rows.length); + expect(fn(emptyTable)).to.equal(0); + }); +}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/series_style.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/series_style.js similarity index 71% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/series_style.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/series_style.js index a0de476435949..ebb68f26004ea 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/series_style.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/series_style.js @@ -1,12 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; -import { seriesStyle } from '../seriesStyle'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; +import { seriesStyle } from '../series_style'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; describe('seriesStyle', () => { const fn = functionWrapper(seriesStyle); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/sort.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/sort.js similarity index 69% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/sort.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/sort.js index 3cfaf7d46ae18..5c550bb43529c 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/sort.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/sort.js @@ -1,13 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { sort } from '../sort'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; -import { testTable } from './fixtures/test_tables'; +import { functionWrapper, testTable } from '@kbn/interpreter/test_utils'; describe('sort', () => { const fn = functionWrapper(sort); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/staticColumn.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/static_column.js similarity index 58% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/staticColumn.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/static_column.js index bce158140d1a0..822ff4e3df034 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/staticColumn.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/static_column.js @@ -1,13 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; -import { staticColumn } from '../staticColumn'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; -import { testTable } from './fixtures/test_tables'; +import { staticColumn } from '../static_column'; +import { functionWrapper, testTable } from '@kbn/interpreter/test_utils'; describe('staticColumn', () => { const fn = functionWrapper(staticColumn); diff --git a/packages/kbn-interpreter/plugin_src/functions/common/__tests__/string.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/string.js new file mode 100644 index 0000000000000..5659741d1abd8 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/string.js @@ -0,0 +1,38 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import expect from 'expect.js'; +import { string } from '../string'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; + +describe('string', () => { + const fn = functionWrapper(string); + + it('casts primitive types to strings', () => { + expect(fn(null, { value: [14000] })).to.be('14000'); + expect(fn(null, { value: ['foo'] })).to.be('foo'); + expect(fn(null, { value: [null] })).to.be(''); + expect(fn(null, { value: [true] })).to.be('true'); + }); + + it('concatenates all args to one string', () => { + expect(fn(null, { value: ['foo', 'bar', 'fizz', 'buzz'] })).to.be('foobarfizzbuzz'); + expect(fn(null, { value: ['foo', 1, true, null] })).to.be('foo1true'); + }); +}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/switch.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/switch.js similarity index 71% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/switch.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/switch.js index 769b0ae8c5579..847f7a77b0ec3 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/switch.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/switch.js @@ -1,12 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { switchFn } from '../switch'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; describe('switch', () => { const fn = functionWrapper(switchFn); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/table.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/table.js similarity index 73% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/table.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/table.js index 0980f4b124f49..16e418aa8ca9d 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/table.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/table.js @@ -1,14 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { table } from '../table'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; -import { testTable } from './fixtures/test_tables'; -import { fontStyle } from './fixtures/test_styles'; +import { functionWrapper, testTable, fontStyle } from '@kbn/interpreter/test_utils'; describe('table', () => { const fn = functionWrapper(table); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/tail.js b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/tail.js similarity index 55% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/tail.js rename to packages/kbn-interpreter/plugin_src/functions/common/__tests__/tail.js index 2e2148237df3d..a8f9da974d382 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/tail.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/__tests__/tail.js @@ -1,13 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import expect from 'expect.js'; import { tail } from '../tail'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; -import { emptyTable, testTable } from './fixtures/test_tables'; +import { functionWrapper, emptyTable, testTable } from '@kbn/interpreter/test_utils'; describe('tail', () => { const fn = functionWrapper(tail); diff --git a/packages/kbn-interpreter/plugin_src/functions/common/all.js b/packages/kbn-interpreter/plugin_src/functions/common/all.js new file mode 100644 index 0000000000000..a81a48f919585 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/all.js @@ -0,0 +1,37 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const all = () => ({ + name: 'all', + type: 'boolean', + help: 'Return true if all of the conditions are true', + args: { + condition: { + aliases: ['_'], + types: ['boolean', 'null'], + required: true, + multi: true, + help: 'One or more conditions to check', + }, + }, + fn: (context, args) => { + const conditions = args.condition || []; + return conditions.every(Boolean); + }, +}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/alterColumn.js b/packages/kbn-interpreter/plugin_src/functions/common/alter_column.js similarity index 71% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/alterColumn.js rename to packages/kbn-interpreter/plugin_src/functions/common/alter_column.js index b5fd1a1333fc3..64c4e77c42889 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/alterColumn.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/alter_column.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import { omit } from 'lodash'; @@ -66,7 +79,7 @@ export const alterColumn = () => ({ default: throw new Error(`Cannot convert to ${type}`); } - })(); + }()); } const rows = context.rows.map(row => ({ diff --git a/packages/kbn-interpreter/plugin_src/functions/common/any.js b/packages/kbn-interpreter/plugin_src/functions/common/any.js new file mode 100644 index 0000000000000..16635d300836a --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/any.js @@ -0,0 +1,37 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const any = () => ({ + name: 'any', + type: 'boolean', + help: 'Return true if any of the conditions are true', + args: { + condition: { + aliases: ['_'], + types: ['boolean', 'null'], + required: true, + multi: true, + help: 'One or more conditions to check', + }, + }, + fn: (context, args) => { + const conditions = args.condition || []; + return conditions.some(Boolean); + }, +}); diff --git a/packages/kbn-interpreter/plugin_src/functions/common/as.js b/packages/kbn-interpreter/plugin_src/functions/common/as.js new file mode 100644 index 0000000000000..99ce08fbb075c --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/as.js @@ -0,0 +1,53 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { getType } from '@kbn/interpreter/common/lib/get_type'; + +export const asFn = () => ({ + name: 'as', + type: 'datatable', + context: { + types: ['string', 'boolean', 'number', 'null'], + }, + help: 'Creates a datatable with a single value', + args: { + name: { + types: ['string'], + aliases: ['_'], + help: 'A name to give the column', + default: 'value', + }, + }, + fn: (context, args) => { + return { + type: 'datatable', + columns: [ + { + name: args.name, + type: getType(context), + }, + ], + rows: [ + { + [args.name]: context, + }, + ], + }; + }, +}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/case.js b/packages/kbn-interpreter/plugin_src/functions/common/case.js similarity index 60% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/case.js rename to packages/kbn-interpreter/plugin_src/functions/common/case.js index 84a5ba58a3731..786ab490f6456 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/case.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/case.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ export const caseFn = () => ({ diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/columns.js b/packages/kbn-interpreter/plugin_src/functions/common/columns.js similarity index 66% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/columns.js rename to packages/kbn-interpreter/plugin_src/functions/common/columns.js index 1dba45a459471..2893521cac184 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/columns.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/columns.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import { omit, pick, find } from 'lodash'; diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/compare.js b/packages/kbn-interpreter/plugin_src/functions/common/compare.js similarity index 60% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/compare.js rename to packages/kbn-interpreter/plugin_src/functions/common/compare.js index c3ff1f7e3f6ea..c4b197b0cb423 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/compare.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/compare.js @@ -1,13 +1,27 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ export const compare = () => ({ name: 'compare', help: - 'Compare the input to something else to determine true or false. Usually used in combination with `{if}`. This only works with primitive types, such as number, string, and boolean.', + 'Compare the input to something else to determine true or false. Usually used in combination with `{if}`. ' + + 'This only works with primitive types, such as number, string, and boolean.', aliases: ['condition'], example: 'math "random()" | compare gt this=0.5', type: 'boolean', diff --git a/packages/kbn-interpreter/plugin_src/functions/common/context.js b/packages/kbn-interpreter/plugin_src/functions/common/context.js new file mode 100644 index 0000000000000..2eed4b2e9589c --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/context.js @@ -0,0 +1,28 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const context = () => ({ + name: 'context', + help: + 'Returns whatever you pass into it. This can be useful when you need to use context as argument to a function as a sub-expression', + args: {}, + fn: context => { + return context; + }, +}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/csv.js b/packages/kbn-interpreter/plugin_src/functions/common/csv.js similarity index 68% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/csv.js rename to packages/kbn-interpreter/plugin_src/functions/common/csv.js index 4a02b901786c8..59d3cdd3f4dbf 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/csv.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/csv.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import Papa from 'papaparse'; diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/date.js b/packages/kbn-interpreter/plugin_src/functions/common/date.js similarity index 55% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/date.js rename to packages/kbn-interpreter/plugin_src/functions/common/date.js index 20fc9ca98f649..539a241c2eaea 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/date.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/date.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import moment from 'moment'; @@ -27,7 +40,8 @@ export const date = () => ({ types: ['string', 'null'], help: 'An optional date string to parse into milliseconds since epoch. ' + - 'Can be either a valid Javascript Date input or a string to parse using the format argument. Must be an ISO 8601 string or you must provide the format.', + 'Can be either a valid Javascript Date input or a string to parse using the format argument. ' + + 'Must be an ISO 8601 string or you must provide the format.', }, format: { types: ['string'], diff --git a/packages/kbn-interpreter/plugin_src/functions/common/do.js b/packages/kbn-interpreter/plugin_src/functions/common/do.js new file mode 100644 index 0000000000000..852a48373f019 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/do.js @@ -0,0 +1,34 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const doFn = () => ({ + name: 'do', + help: + 'Runs multiple sub-expressions. Returns the passed in context. Nice for running actions producing functions.', + args: { + fn: { + aliases: ['_'], + multi: true, + help: + 'One or more sub-expressions. The value of these is not available in the root ' + + 'pipeline as this function simply returns the passed in context', + }, + }, + fn: context => context, +}); diff --git a/packages/kbn-interpreter/plugin_src/functions/common/eq.js b/packages/kbn-interpreter/plugin_src/functions/common/eq.js new file mode 100644 index 0000000000000..8671557945964 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/eq.js @@ -0,0 +1,35 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const eq = () => ({ + name: 'eq', + type: 'boolean', + help: 'Return if the context is equal to the argument', + args: { + value: { + aliases: ['_'], + types: ['boolean', 'number', 'string', 'null'], + required: true, + help: 'The value to compare the context to', + }, + }, + fn: (context, args) => { + return context === args.value; + }, +}); diff --git a/packages/kbn-interpreter/plugin_src/functions/common/exactly.js b/packages/kbn-interpreter/plugin_src/functions/common/exactly.js new file mode 100644 index 0000000000000..1828c4954de1d --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/exactly.js @@ -0,0 +1,51 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const exactly = () => ({ + name: 'exactly', + aliases: [], + type: 'filter', + context: { + types: ['filter'], + }, + help: 'Create a filter that matches a given column for a perfectly exact value', + args: { + column: { + types: ['string'], + aliases: ['field', 'c'], + help: 'The column or field to attach the filter to', + }, + value: { + types: ['string'], + aliases: ['v', 'val'], + help: 'The value to match exactly, including white space and capitalization', + }, + }, + fn: (context, args) => { + const { value, column } = args; + + const filter = { + type: 'exactly', + value, + column, + }; + + return { ...context, and: [...context.and, filter] }; + }, +}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/filterrows.js b/packages/kbn-interpreter/plugin_src/functions/common/filterrows.js similarity index 51% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/filterrows.js rename to packages/kbn-interpreter/plugin_src/functions/common/filterrows.js index cfbf4c8a0fe5e..0efa496b77e6d 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/filterrows.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/filterrows.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ export const filterrows = () => ({ diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/font.js b/packages/kbn-interpreter/plugin_src/functions/common/font.js similarity index 73% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/font.js rename to packages/kbn-interpreter/plugin_src/functions/common/font.js index ef78b7b9dd847..a8f821e117c2b 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/font.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/font.js @@ -1,11 +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; - * you may not use this file except in compliance with the Elastic License. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import inlineStyle from 'inline-style'; -import { openSans } from '../../../common/lib/fonts'; +import { openSans } from '@kbn/interpreter/common/lib/fonts'; export const font = () => ({ name: 'font', diff --git a/packages/kbn-interpreter/plugin_src/functions/common/formatdate.js b/packages/kbn-interpreter/plugin_src/functions/common/formatdate.js new file mode 100644 index 0000000000000..d19242e0ee967 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/formatdate.js @@ -0,0 +1,39 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import moment from 'moment'; +export const formatdate = () => ({ + name: 'formatdate', + type: 'string', + help: 'Output a ms since epoch number as a formatted string', + context: { + types: ['number'], + }, + args: { + format: { + aliases: ['_'], + types: ['string'], + help: 'MomentJS Format with which to bucket (See https://momentjs.com/docs/#/displaying/)', + }, + }, + fn: (context, args) => { + if (!args.format) return moment.utc(new Date(context)).toISOString(); + return moment.utc(new Date(context)).format(args.format); + }, +}); diff --git a/packages/kbn-interpreter/plugin_src/functions/common/formatnumber.js b/packages/kbn-interpreter/plugin_src/functions/common/formatnumber.js new file mode 100644 index 0000000000000..68c852f17c1e5 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/formatnumber.js @@ -0,0 +1,40 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import numeral from '@elastic/numeral'; + +export const formatnumber = () => ({ + name: 'formatnumber', + type: 'string', + help: 'Turn a number into a string using a NumberJS format', + context: { + types: ['number'], + }, + args: { + format: { + aliases: ['_'], + types: ['string'], + help: 'NumeralJS format string http://numeraljs.com/#format', + }, + }, + fn: (context, args) => { + if (!args.format) return String(context); + return numeral(context).format(args.format); + }, +}); diff --git a/packages/kbn-interpreter/plugin_src/functions/common/get_cell.js b/packages/kbn-interpreter/plugin_src/functions/common/get_cell.js new file mode 100644 index 0000000000000..0580822706c5d --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/get_cell.js @@ -0,0 +1,50 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const getCell = () => ({ + name: 'getCell', + help: 'Fetch a single cell in a table', + context: { + types: ['datatable'], + }, + args: { + column: { + types: ['string'], + aliases: ['_', 'c'], + help: 'The name of the column value to fetch', + }, + row: { + types: ['number'], + aliases: ['r'], + help: 'The row number, starting at 0', + default: 0, + }, + }, + fn: (context, args) => { + const row = context.rows[args.row]; + if (!row) throw new Error(`Row not found: ${args.row}`); + + const { column = context.columns[0].name } = args; + const value = row[column]; + + if (typeof value === 'undefined') throw new Error(`Column not found: ${column}`); + + return value; + }, +}); diff --git a/packages/kbn-interpreter/plugin_src/functions/common/gt.js b/packages/kbn-interpreter/plugin_src/functions/common/gt.js new file mode 100644 index 0000000000000..f7c51180b195c --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/gt.js @@ -0,0 +1,36 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const gt = () => ({ + name: 'gt', + type: 'boolean', + help: 'Return if the context is greater than the argument', + args: { + value: { + aliases: ['_'], + types: ['boolean', 'number', 'string', 'null'], + required: true, + help: 'The value to compare the context to', + }, + }, + fn: (context, args) => { + if (typeof context !== typeof args.value) return false; + return context > args.value; + }, +}); diff --git a/packages/kbn-interpreter/plugin_src/functions/common/gte.js b/packages/kbn-interpreter/plugin_src/functions/common/gte.js new file mode 100644 index 0000000000000..b6b6bb6ebf765 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/gte.js @@ -0,0 +1,36 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const gte = () => ({ + name: 'gte', + type: 'boolean', + help: 'Return if the context is greater than or equal to the argument', + args: { + value: { + aliases: ['_'], + types: ['boolean', 'number', 'string', 'null'], + required: true, + help: 'The value to compare the context to', + }, + }, + fn: (context, args) => { + if (typeof context !== typeof args.value) return false; + return context >= args.value; + }, +}); diff --git a/packages/kbn-interpreter/plugin_src/functions/common/head.js b/packages/kbn-interpreter/plugin_src/functions/common/head.js new file mode 100644 index 0000000000000..95ef24c8bfa5c --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/head.js @@ -0,0 +1,42 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { take } from 'lodash'; + +export const head = () => ({ + name: 'head', + aliases: [], + type: 'datatable', + help: 'Get the first N rows from the datatable. Also see `tail`', + context: { + types: ['datatable'], + }, + args: { + count: { + aliases: ['_'], + types: ['number'], + help: 'Return this many rows from the beginning of the datatable', + default: 1, + }, + }, + fn: (context, args) => ({ + ...context, + rows: take(context.rows, args.count), + }), +}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/if.js b/packages/kbn-interpreter/plugin_src/functions/common/if.js similarity index 52% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/if.js rename to packages/kbn-interpreter/plugin_src/functions/common/if.js index fa54a14b0998a..a6915361a2bdc 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/if.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/if.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ export const ifFn = () => ({ diff --git a/packages/kbn-interpreter/plugin_src/functions/common/index.js b/packages/kbn-interpreter/plugin_src/functions/common/index.js new file mode 100644 index 0000000000000..c8f7c98baeaf2 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/index.js @@ -0,0 +1,102 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { alterColumn } from './alter_column'; +import { all } from './all'; +import { any } from './any'; +import { asFn } from './as'; +import { compare } from './compare'; +import { context } from './context'; +import { columns } from './columns'; +import { csv } from './csv'; +import { date } from './date'; +import { doFn } from './do'; +import { eq } from './eq'; +import { exactly } from './exactly'; +import { filterrows } from './filterrows'; +import { font } from './font'; +import { formatdate } from './formatdate'; +import { formatnumber } from './formatnumber'; +import { getCell } from './get_cell'; +import { gt } from './gt'; +import { gte } from './gte'; +import { head } from './head'; +import { ifFn } from './if'; +import { lt } from './lt'; +import { lte } from './lte'; +import { mapColumn } from './map_column'; +import { math } from './math'; +import { neq } from './neq'; +import { render } from './render'; +import { replace } from './replace'; +import { rounddate } from './rounddate'; +import { rowCount } from './row_count'; +import { seriesStyle } from './series_style'; +import { shape } from './shape'; +import { sort } from './sort'; +import { staticColumn } from './static_column'; +import { string } from './string'; +import { table } from './table'; +import { tail } from './tail'; +import { to } from './to'; +import { switchFn } from './switch'; +import { caseFn } from './case'; + +export const commonFunctions = [ + all, + alterColumn, + any, + asFn, + columns, + compare, + context, + csv, + date, + doFn, + eq, + exactly, + filterrows, + font, + formatdate, + formatnumber, + getCell, + gt, + gte, + head, + ifFn, + lt, + lte, + mapColumn, + math, + neq, + render, + replace, + rounddate, + rowCount, + seriesStyle, + shape, + sort, + staticColumn, + string, + table, + tail, + to, + switchFn, + caseFn, +]; diff --git a/packages/kbn-interpreter/plugin_src/functions/common/lt.js b/packages/kbn-interpreter/plugin_src/functions/common/lt.js new file mode 100644 index 0000000000000..a0524405a4ce0 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/lt.js @@ -0,0 +1,36 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const lt = () => ({ + name: 'lt', + type: 'boolean', + help: 'Return if the context is less than the argument', + args: { + value: { + aliases: ['_'], + types: ['boolean', 'number', 'string', 'null'], + required: true, + help: 'The value to compare the context to', + }, + }, + fn: (context, args) => { + if (typeof context !== typeof args.value) return false; + return context < args.value; + }, +}); diff --git a/packages/kbn-interpreter/plugin_src/functions/common/lte.js b/packages/kbn-interpreter/plugin_src/functions/common/lte.js new file mode 100644 index 0000000000000..607e107df9be0 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/lte.js @@ -0,0 +1,36 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const lte = () => ({ + name: 'lte', + type: 'boolean', + help: 'Return if the context is less than or equal to the argument', + args: { + value: { + aliases: ['_'], + types: ['boolean', 'number', 'string', 'null'], + required: true, + help: 'The value to compare the context to', + }, + }, + fn: (context, args) => { + if (typeof context !== typeof args.value) return false; + return context <= args.value; + }, +}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/mapColumn.js b/packages/kbn-interpreter/plugin_src/functions/common/map_column.js similarity index 64% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/mapColumn.js rename to packages/kbn-interpreter/plugin_src/functions/common/map_column.js index db55780205296..c21e3cde126b0 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/mapColumn.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/map_column.js @@ -1,10 +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; - * you may not use this file except in compliance with the Elastic License. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ -import { getType } from '../../../common/lib/get_type'; + +import { getType } from '@kbn/interpreter/common/lib/get_type'; export const mapColumn = () => ({ name: 'mapColumn', diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/math.js b/packages/kbn-interpreter/plugin_src/functions/common/math.js similarity index 61% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/math.js rename to packages/kbn-interpreter/plugin_src/functions/common/math.js index 190ad482153fa..07a970a45fcde 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/math.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/math.js @@ -1,11 +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; - * you may not use this file except in compliance with the Elastic License. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import { evaluate } from 'tinymath'; -import { pivotObjectArray } from '../../../common/lib/pivot_object_array'; +import { pivotObjectArray } from '@kbn/interpreter/common/lib/pivot_object_array'; export const math = () => ({ name: 'math', @@ -39,8 +52,9 @@ export const math = () => ({ 'Expressions must return a single number. Try wrapping your expression in mean() or sum()' ); } - if (isNaN(result)) + if (isNaN(result)) { throw new Error('Failed to execute math expression. Check your column names'); + } return result; } catch (e) { if (context.rows.length === 0) throw new Error('Empty datatable'); diff --git a/packages/kbn-interpreter/plugin_src/functions/common/neq.js b/packages/kbn-interpreter/plugin_src/functions/common/neq.js new file mode 100644 index 0000000000000..d3bf38bf7a54e --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/neq.js @@ -0,0 +1,35 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const neq = () => ({ + name: 'neq', + type: 'boolean', + help: 'Return if the context is not equal to the argument', + args: { + value: { + aliases: ['_'], + types: ['boolean', 'number', 'string', 'null'], + required: true, + help: 'The value to compare the context to', + }, + }, + fn: (context, args) => { + return context !== args.value; + }, +}); diff --git a/packages/kbn-interpreter/plugin_src/functions/common/register.js b/packages/kbn-interpreter/plugin_src/functions/common/register.js new file mode 100644 index 0000000000000..8b146b8f849c3 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/register.js @@ -0,0 +1,23 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { commonFunctions } from './index'; + +// eslint-disable-next-line no-undef +commonFunctions.forEach(canvas.register); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/render.js b/packages/kbn-interpreter/plugin_src/functions/common/render.js similarity index 53% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/render.js rename to packages/kbn-interpreter/plugin_src/functions/common/render.js index 716ca8abc340d..1630aa7e27036 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/render.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/render.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ export const render = () => ({ diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/replace.js b/packages/kbn-interpreter/plugin_src/functions/common/replace.js similarity index 54% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/replace.js rename to packages/kbn-interpreter/plugin_src/functions/common/replace.js index fb9d3462c8b19..e8bab5065a620 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/replace.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/replace.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ export const replace = () => ({ diff --git a/packages/kbn-interpreter/plugin_src/functions/common/rounddate.js b/packages/kbn-interpreter/plugin_src/functions/common/rounddate.js new file mode 100644 index 0000000000000..6a9f23f447ae2 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/rounddate.js @@ -0,0 +1,41 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import moment from 'moment'; + +export const rounddate = () => ({ + name: 'rounddate', + type: 'number', + help: 'Round ms since epoch using a moment formatting string. Returns ms since epoch', + context: { + types: ['number'], + }, + args: { + format: { + aliases: ['_'], + types: ['string'], + help: + 'MomentJS Format with which to bucket (See https://momentjs.com/docs/#/displaying/). For example "YYYY-MM" would round to the month', + }, + }, + fn: (context, args) => { + if (!args.format) return context; + return moment.utc(moment.utc(context).format(args.format), args.format).valueOf(); + }, +}); diff --git a/packages/kbn-interpreter/plugin_src/functions/common/row_count.js b/packages/kbn-interpreter/plugin_src/functions/common/row_count.js new file mode 100644 index 0000000000000..76dfc5e08da79 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/row_count.js @@ -0,0 +1,31 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const rowCount = () => ({ + name: 'rowCount', + aliases: [], + type: 'number', + context: { + types: ['datatable'], + }, + help: + 'Return the number of rows. Pair with ply to get the count of unique column values, or combinations of unique column values.', + args: {}, + fn: context => context.rows.length, +}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/seriesStyle.js b/packages/kbn-interpreter/plugin_src/functions/common/series_style.js similarity index 65% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/seriesStyle.js rename to packages/kbn-interpreter/plugin_src/functions/common/series_style.js index 5af8d345df4a1..5fe3cad3b432e 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/seriesStyle.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/series_style.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ const name = 'seriesStyle'; diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/shape.js b/packages/kbn-interpreter/plugin_src/functions/common/shape.js similarity index 55% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/shape.js rename to packages/kbn-interpreter/plugin_src/functions/common/shape.js index 27379a759608d..dc3f0c67610cb 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/shape.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/shape.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ export const shape = () => ({ diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/sort.js b/packages/kbn-interpreter/plugin_src/functions/common/sort.js similarity index 52% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/sort.js rename to packages/kbn-interpreter/plugin_src/functions/common/sort.js index b6b554c032281..37363837ce118 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/sort.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/sort.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import { sortBy } from 'lodash'; diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/staticColumn.js b/packages/kbn-interpreter/plugin_src/functions/common/static_column.js similarity index 55% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/staticColumn.js rename to packages/kbn-interpreter/plugin_src/functions/common/static_column.js index 4b9e96bda7354..0b7d2270e5cfc 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/staticColumn.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/static_column.js @@ -1,10 +1,23 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ -import { getType } from '../../../common/lib/get_type'; +import { getType } from '@kbn/interpreter/common/lib/get_type'; export const staticColumn = () => ({ name: 'staticColumn', diff --git a/packages/kbn-interpreter/plugin_src/functions/common/string.js b/packages/kbn-interpreter/plugin_src/functions/common/string.js new file mode 100644 index 0000000000000..8ec938ebe90e0 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/string.js @@ -0,0 +1,36 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const string = () => ({ + name: 'string', + aliases: [], + type: 'string', + help: + 'Output a string made of other strings. Mostly useful when combined with sub-expressions that output a string, ' + + ' or something castable to a string', + args: { + value: { + aliases: ['_'], + types: ['string'], + multi: true, + help: 'One or more strings to join together. Don\'t forget spaces where needed!', + }, + }, + fn: (context, args) => args.value.join(''), +}); diff --git a/packages/kbn-interpreter/plugin_src/functions/common/switch.js b/packages/kbn-interpreter/plugin_src/functions/common/switch.js new file mode 100644 index 0000000000000..b764c2bb37e70 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/switch.js @@ -0,0 +1,46 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const switchFn = () => ({ + name: 'switch', + help: 'Perform conditional logic with multiple conditions', + args: { + case: { + types: ['case'], + aliases: ['_'], + resolve: false, + multi: true, + help: 'The list of conditions to check', + }, + default: { + aliases: ['finally'], + resolve: false, + help: 'The default case if no cases match', + }, + }, + fn: async (context, args) => { + const cases = args.case || []; + for (let i = 0; i < cases.length; i++) { + const { matches, result } = await cases[i](); + if (matches) return result; + } + if (typeof args.default !== 'undefined') return await args.default(); + return context; + }, +}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/table.js b/packages/kbn-interpreter/plugin_src/functions/common/table.js similarity index 56% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/table.js rename to packages/kbn-interpreter/plugin_src/functions/common/table.js index 3afd345c65c4a..7e6f41e6ea64a 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/table.js +++ b/packages/kbn-interpreter/plugin_src/functions/common/table.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ export const table = () => ({ diff --git a/packages/kbn-interpreter/plugin_src/functions/common/tail.js b/packages/kbn-interpreter/plugin_src/functions/common/tail.js new file mode 100644 index 0000000000000..f272dc2e76adf --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/tail.js @@ -0,0 +1,41 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { takeRight } from 'lodash'; + +export const tail = () => ({ + name: 'tail', + aliases: [], + type: 'datatable', + help: 'Get the last N rows from the end of a datatable. Also see `head`', + context: { + types: ['datatable'], + }, + args: { + count: { + aliases: ['_'], + types: ['number'], + help: 'Return this many rows from the end of the datatable', + }, + }, + fn: (context, args) => ({ + ...context, + rows: takeRight(context.rows, args.count), + }), +}); diff --git a/packages/kbn-interpreter/plugin_src/functions/common/to.js b/packages/kbn-interpreter/plugin_src/functions/common/to.js new file mode 100644 index 0000000000000..a32288d3fe0b3 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/common/to.js @@ -0,0 +1,40 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { castProvider } from '@kbn/interpreter/common/interpreter/cast'; + +export const to = () => ({ + name: 'to', + aliases: [], + help: 'Explicitly cast from one type to another.', + context: {}, + args: { + type: { + types: ['string'], + help: 'A known type', + aliases: ['_'], + multi: true, + }, + }, + fn: (context, args, { types }) => { + if (!args.type) throw new Error('Must specify a casting type'); + + return castProvider(types)(context, args.type); + }, +}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/esdocs.js b/packages/kbn-interpreter/plugin_src/functions/server/esdocs/index.js similarity index 75% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/esdocs.js rename to packages/kbn-interpreter/plugin_src/functions/server/esdocs/index.js index 7cb559b10464b..0c7ad3ba23082 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/esdocs.js +++ b/packages/kbn-interpreter/plugin_src/functions/server/esdocs/index.js @@ -1,14 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import squel from 'squel'; import { map, zipObject } from 'lodash'; -import { buildBoolArray } from '../../../../server/lib/build_bool_array'; -import { normalizeType } from '../../../../server/lib/normalize_type'; -import { sanitizeName } from '../../../../server/lib/sanitize_name'; +import { buildBoolArray, normalizeType, sanitizeName } from '@kbn/interpreter/server/index'; export const esdocs = () => ({ name: 'esdocs', diff --git a/packages/kbn-interpreter/plugin_src/functions/server/index.js b/packages/kbn-interpreter/plugin_src/functions/server/index.js new file mode 100644 index 0000000000000..ba4abe7f235eb --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/server/index.js @@ -0,0 +1,23 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { esdocs } from './esdocs/index'; +import { timelion } from './timelion'; + +export const serverFunctions = [esdocs, timelion]; diff --git a/packages/kbn-interpreter/plugin_src/functions/server/register.js b/packages/kbn-interpreter/plugin_src/functions/server/register.js new file mode 100644 index 0000000000000..572758671ffd7 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/functions/server/register.js @@ -0,0 +1,23 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { serverFunctions } from './index'; + +// eslint-disable-next-line no-undef +serverFunctions.forEach(canvas.register); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/timelion.js b/packages/kbn-interpreter/plugin_src/functions/server/timelion.js similarity index 71% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/timelion.js rename to packages/kbn-interpreter/plugin_src/functions/server/timelion.js index a19460edb03d4..5a066a0734acc 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/timelion.js +++ b/packages/kbn-interpreter/plugin_src/functions/server/timelion.js @@ -1,12 +1,25 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import { flatten } from 'lodash'; -import { fetch } from '../../../../common/lib/fetch'; -import { buildBoolArray } from '../../../../server/lib/build_bool_array'; +import { fetch } from '@kbn/interpreter/common/lib/fetch'; +import { buildBoolArray } from '@kbn/interpreter/server/index'; export const timelion = () => ({ name: 'timelion', diff --git a/packages/kbn-interpreter/plugin_src/types/boolean.js b/packages/kbn-interpreter/plugin_src/types/boolean.js new file mode 100644 index 0000000000000..cc5f0a79e39a8 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/types/boolean.js @@ -0,0 +1,42 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const boolean = () => ({ + name: 'boolean', + from: { + null: () => false, + number: n => Boolean(n), + string: s => Boolean(s), + }, + to: { + render: value => { + const text = `${value}`; + return { + type: 'render', + as: 'text', + value: { text }, + }; + }, + datatable: value => ({ + type: 'datatable', + columns: [{ name: 'value', type: 'boolean' }], + rows: [{ value }], + }), + }, +}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/types/datatable.js b/packages/kbn-interpreter/plugin_src/types/datatable.js similarity index 63% rename from x-pack/plugins/canvas/canvas_plugin_src/types/datatable.js rename to packages/kbn-interpreter/plugin_src/types/datatable.js index cfe75605f1ebf..92bd2c9b1b59e 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/types/datatable.js +++ b/packages/kbn-interpreter/plugin_src/types/datatable.js @@ -1,7 +1,20 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import { map, zipObject } from 'lodash'; @@ -10,8 +23,9 @@ export const datatable = () => ({ name: 'datatable', validate: datatable => { // TODO: Check columns types. Only string, boolean, number, date, allowed for now. - if (!datatable.columns) + if (!datatable.columns) { throw new Error('datatable must have a columns array, even if it is empty'); + } if (!datatable.rows) throw new Error('datatable must have a rows array, even if it is empty'); }, diff --git a/packages/kbn-interpreter/plugin_src/types/error.js b/packages/kbn-interpreter/plugin_src/types/error.js new file mode 100644 index 0000000000000..1415a065d810e --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/types/error.js @@ -0,0 +1,35 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const error = () => ({ + name: 'error', + to: { + render: input => { + const { error, info } = input; + return { + type: 'render', + as: 'error', + value: { + error, + info, + }, + }; + }, + }, +}); diff --git a/packages/kbn-interpreter/plugin_src/types/filter.js b/packages/kbn-interpreter/plugin_src/types/filter.js new file mode 100644 index 0000000000000..484050671b2f9 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/types/filter.js @@ -0,0 +1,33 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const filter = () => ({ + name: 'filter', + from: { + null: () => { + return { + type: 'filter', + // Any meta data you wish to pass along. + meta: {}, + // And filters. If you need an "or", create a filter type for it. + and: [], + }; + }, + }, +}); diff --git a/packages/kbn-interpreter/plugin_src/types/image.js b/packages/kbn-interpreter/plugin_src/types/image.js new file mode 100644 index 0000000000000..7666451145f5d --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/types/image.js @@ -0,0 +1,31 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const image = () => ({ + name: 'image', + to: { + render: input => { + return { + type: 'render', + as: 'image', + value: input, + }; + }, + }, +}); diff --git a/packages/kbn-interpreter/plugin_src/types/index.js b/packages/kbn-interpreter/plugin_src/types/index.js new file mode 100644 index 0000000000000..1ae5f874835c3 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/types/index.js @@ -0,0 +1,46 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { boolean } from './boolean'; +import { datatable } from './datatable'; +import { error } from './error'; +import { filter } from './filter'; +import { image } from './image'; +import { nullType } from './null'; +import { number } from './number'; +import { pointseries } from './pointseries'; +import { render } from './render'; +import { shape } from './shape'; +import { string } from './string'; +import { style } from './style'; + +export const typeSpecs = [ + boolean, + datatable, + error, + filter, + image, + number, + nullType, + pointseries, + render, + shape, + string, + style, +]; diff --git a/packages/kbn-interpreter/plugin_src/types/null.js b/packages/kbn-interpreter/plugin_src/types/null.js new file mode 100644 index 0000000000000..2789ce330ac6c --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/types/null.js @@ -0,0 +1,25 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const nullType = () => ({ + name: 'null', + from: { + '*': () => null, + }, +}); diff --git a/packages/kbn-interpreter/plugin_src/types/number.js b/packages/kbn-interpreter/plugin_src/types/number.js new file mode 100644 index 0000000000000..8f8f31ea8a2fb --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/types/number.js @@ -0,0 +1,42 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const number = () => ({ + name: 'number', + from: { + null: () => 0, + boolean: b => Number(b), + string: n => Number(n), + }, + to: { + render: value => { + const text = `${value}`; + return { + type: 'render', + as: 'text', + value: { text }, + }; + }, + datatable: value => ({ + type: 'datatable', + columns: [{ name: 'value', type: 'number' }], + rows: [{ value }], + }), + }, +}); diff --git a/packages/kbn-interpreter/plugin_src/types/pointseries.js b/packages/kbn-interpreter/plugin_src/types/pointseries.js new file mode 100644 index 0000000000000..2275ea9e04094 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/types/pointseries.js @@ -0,0 +1,44 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const pointseries = () => ({ + name: 'pointseries', + from: { + null: () => { + return { + type: 'pointseries', + rows: [], + columns: [], + }; + }, + }, + to: { + render: (pointseries, types) => { + const datatable = types.datatable.from(pointseries, types); + return { + type: 'render', + as: 'table', + value: { + datatable, + showHeader: true, + }, + }; + }, + }, +}); diff --git a/packages/kbn-interpreter/plugin_src/types/register.js b/packages/kbn-interpreter/plugin_src/types/register.js new file mode 100644 index 0000000000000..283583b40073b --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/types/register.js @@ -0,0 +1,23 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { typeSpecs } from './index'; + +// eslint-disable-next-line no-undef +typeSpecs.forEach(canvas.register); diff --git a/packages/kbn-interpreter/plugin_src/types/render.js b/packages/kbn-interpreter/plugin_src/types/render.js new file mode 100644 index 0000000000000..99ce3ca7d1cd7 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/types/render.js @@ -0,0 +1,29 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const render = () => ({ + name: 'render', + from: { + '*': v => ({ + type: 'render', + as: 'debug', + value: v, + }), + }, +}); diff --git a/packages/kbn-interpreter/plugin_src/types/shape.js b/packages/kbn-interpreter/plugin_src/types/shape.js new file mode 100644 index 0000000000000..1ed7a111268d1 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/types/shape.js @@ -0,0 +1,31 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const shape = () => ({ + name: 'shape', + to: { + render: input => { + return { + type: 'render', + as: 'shape', + value: input, + }; + }, + }, +}); diff --git a/packages/kbn-interpreter/plugin_src/types/string.js b/packages/kbn-interpreter/plugin_src/types/string.js new file mode 100644 index 0000000000000..90e6b17cc9dcf --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/types/string.js @@ -0,0 +1,41 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const string = () => ({ + name: 'string', + from: { + null: () => '', + boolean: b => String(b), + number: n => String(n), + }, + to: { + render: text => { + return { + type: 'render', + as: 'text', + value: { text }, + }; + }, + datatable: value => ({ + type: 'datatable', + columns: [{ name: 'value', type: 'string' }], + rows: [{ value }], + }), + }, +}); diff --git a/packages/kbn-interpreter/plugin_src/types/style.js b/packages/kbn-interpreter/plugin_src/types/style.js new file mode 100644 index 0000000000000..97057b415a475 --- /dev/null +++ b/packages/kbn-interpreter/plugin_src/types/style.js @@ -0,0 +1,31 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const style = () => ({ + name: 'style', + from: { + null: () => { + return { + type: 'style', + spec: {}, + css: '', + }; + }, + }, +}); diff --git a/packages/kbn-interpreter/public/index.js b/packages/kbn-interpreter/public/index.js new file mode 100644 index 0000000000000..80da4315e844b --- /dev/null +++ b/packages/kbn-interpreter/public/index.js @@ -0,0 +1,22 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { renderFunctionsRegistry } from './render_functions_registry'; + +export { renderFunctionsRegistry }; diff --git a/packages/kbn-interpreter/public/load_browser_plugins.js b/packages/kbn-interpreter/public/load_browser_plugins.js new file mode 100644 index 0000000000000..19908a28fbf62 --- /dev/null +++ b/packages/kbn-interpreter/public/load_browser_plugins.js @@ -0,0 +1,68 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import chrome from 'ui/chrome'; +import $script from 'scriptjs'; +import { typesRegistry } from '@kbn/interpreter/common/lib/types_registry'; +import { functionsRegistry as browserFunctions } from '@kbn/interpreter/common/lib/functions_registry'; + +let loading; +export const loadBrowserPlugins = (additionalTypes) => { + + const types = { + browserFunctions: browserFunctions, + commonFunctions: browserFunctions, + types: typesRegistry, + }; + + // if we are already loading, we need to wait till its done before starting again + if (loading) { + return loading.then(() => { + loading = null; + return loadBrowserPlugins(additionalTypes); + }); + } + + loading = new Promise(resolve => { + if (additionalTypes) { + Object.keys(additionalTypes).forEach(key => { + types[key] = additionalTypes[key]; + }); + } + + const remainingTypes = Object.keys(types); + + function loadType() { + const type = remainingTypes.pop(); + window.canvas = window.canvas || {}; + window.canvas.register = d => types[type].register(d); + // Load plugins one at a time because each needs a different loader function + // $script will only load each of these once, we so can call this as many times as we need? + const pluginPath = chrome.addBasePath(`/api/canvas/plugins?type=${type}`); + $script(pluginPath, () => { + if (remainingTypes.length) loadType(); + else resolve(true); + }); + } + + loadType(); + }); + + return loading; +}; diff --git a/packages/kbn-interpreter/public/render_function.js b/packages/kbn-interpreter/public/render_function.js new file mode 100644 index 0000000000000..04aa05951be70 --- /dev/null +++ b/packages/kbn-interpreter/public/render_function.js @@ -0,0 +1,42 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export function RenderFunction(config) { + // This must match the name of the function that is used to create the `type: render` object + this.name = config.name; + + // Use this to set a more friendly name + this.displayName = config.displayName || this.name; + + // A sentence or few about what this element does + this.help = config.help; + + // used to validate the data before calling the render function + this.validate = config.validate || function validate() {}; + + // tell the renderer if the dom node should be reused, it's recreated each time by default + this.reuseDomNode = Boolean(config.reuseDomNode); + + // the function called to render the data + this.render = + config.render || + function render(domNode, data, done) { + done(); + }; +} diff --git a/packages/kbn-interpreter/public/render_functions_registry.js b/packages/kbn-interpreter/public/render_functions_registry.js new file mode 100644 index 0000000000000..b7436eb697104 --- /dev/null +++ b/packages/kbn-interpreter/public/render_functions_registry.js @@ -0,0 +1,29 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { Registry } from '../common/lib/registry'; +import { RenderFunction } from './render_function'; + +class RenderFunctionsRegistry extends Registry { + wrapper(obj) { + return new RenderFunction(obj); + } +} + +export const renderFunctionsRegistry = new RenderFunctionsRegistry(); diff --git a/packages/kbn-interpreter/server/build_bool_array.js b/packages/kbn-interpreter/server/build_bool_array.js new file mode 100644 index 0000000000000..4668ed48a6426 --- /dev/null +++ b/packages/kbn-interpreter/server/build_bool_array.js @@ -0,0 +1,34 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { getESFilter } from './get_es_filter'; + +const compact = arr => (Array.isArray(arr) ? arr.filter(val => Boolean(val)) : []); + +export function buildBoolArray(canvasQueryFilterArray) { + return compact( + canvasQueryFilterArray.map(clause => { + try { + return getESFilter(clause); + } catch (e) { + return; + } + }) + ); +} diff --git a/packages/kbn-interpreter/server/build_es_request.js b/packages/kbn-interpreter/server/build_es_request.js new file mode 100644 index 0000000000000..d8c57ffe8e593 --- /dev/null +++ b/packages/kbn-interpreter/server/build_es_request.js @@ -0,0 +1,28 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { buildBoolArray } from './build_bool_array'; + +export function buildESRequest(esRequest, canvasQuery) { + if (canvasQuery.size) esRequest = { ...esRequest, size: canvasQuery.size }; + + if (canvasQuery.and) esRequest.body.query.bool.must = buildBoolArray(canvasQuery.and); + + return esRequest; +} diff --git a/packages/kbn-interpreter/server/create_handlers.js b/packages/kbn-interpreter/server/create_handlers.js new file mode 100644 index 0000000000000..caa4bddcdbbfd --- /dev/null +++ b/packages/kbn-interpreter/server/create_handlers.js @@ -0,0 +1,35 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { partial } from 'lodash'; + +export const createHandlers = (request, server) => { + const { callWithRequest } = server.plugins.elasticsearch.getCluster('data'); + const config = server.config(); + + return { + environment: 'server', + serverUri: + config.has('server.rewriteBasePath') && config.get('server.rewriteBasePath') + ? `${server.info.uri}${config.get('server.basePath')}` + : server.info.uri, + httpHeaders: request.headers, + elasticsearchClient: partial(callWithRequest, request), + }; +}; diff --git a/packages/kbn-interpreter/server/filters.js b/packages/kbn-interpreter/server/filters.js new file mode 100644 index 0000000000000..5223015d2fac1 --- /dev/null +++ b/packages/kbn-interpreter/server/filters.js @@ -0,0 +1,49 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + TODO: This could be pluggable +*/ + +export function time(filter) { + if (!filter.column) throw new Error('column is required for Elasticsearch range filters'); + return { + range: { + [filter.column]: { gte: filter.from, lte: filter.to }, + }, + }; +} + +export function luceneQueryString(filter) { + return { + query_string: { + query: filter.query || '*', + }, + }; +} + +export function exactly(filter) { + return { + term: { + [filter.column]: { + value: filter.value, + }, + }, + }; +} diff --git a/packages/kbn-interpreter/server/get_es_filter.js b/packages/kbn-interpreter/server/get_es_filter.js new file mode 100644 index 0000000000000..f7ee2e843990c --- /dev/null +++ b/packages/kbn-interpreter/server/get_es_filter.js @@ -0,0 +1,37 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + boolArray is the array of bool filter clauses to push filters into. Usually this would be + the value of must, should or must_not. + filter is the abstracted canvas filter. +*/ + +/*eslint import/namespace: ['error', { allowComputed: true }]*/ +import * as filters from './filters'; + +export function getESFilter(filter) { + if (!filters[filter.type]) throw new Error(`Unknown filter type: ${filter.type}`); + + try { + return filters[filter.type](filter); + } catch (e) { + throw new Error(`Could not create elasticsearch filter from ${filter.type}`); + } +} diff --git a/packages/kbn-interpreter/server/index.js b/packages/kbn-interpreter/server/index.js new file mode 100644 index 0000000000000..8267ed175238d --- /dev/null +++ b/packages/kbn-interpreter/server/index.js @@ -0,0 +1,27 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { buildBoolArray } from './build_bool_array'; +import { buildESRequest } from './build_es_request'; +import { createHandlers } from './create_handlers'; +import { getESFilter } from './get_es_filter'; +import { normalizeType } from './normalize_type'; +import { sanitizeName } from './sanitize_name'; + +export { buildBoolArray, buildESRequest, createHandlers, getESFilter, normalizeType, sanitizeName }; diff --git a/packages/kbn-interpreter/server/normalize_type.js b/packages/kbn-interpreter/server/normalize_type.js new file mode 100644 index 0000000000000..635f2907436ad --- /dev/null +++ b/packages/kbn-interpreter/server/normalize_type.js @@ -0,0 +1,44 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export function normalizeType(type) { + const normalTypes = { + string: ['string', 'text', 'keyword', '_type', '_id', '_index'], + number: [ + 'float', + 'half_float', + 'scaled_float', + 'double', + 'integer', + 'long', + 'short', + 'byte', + 'token_count', + '_version', + ], + date: ['date'], + boolean: ['boolean'], + null: ['null'], + }; + + const normalizedType = Object.keys(normalTypes).find(t => normalTypes[t].includes(type)); + + if (normalizedType) return normalizedType; + throw new Error(`Canvas does not yet support type: ${type}`); +} diff --git a/packages/kbn-interpreter/server/sanitize_name.js b/packages/kbn-interpreter/server/sanitize_name.js new file mode 100644 index 0000000000000..872d42cc577c6 --- /dev/null +++ b/packages/kbn-interpreter/server/sanitize_name.js @@ -0,0 +1,30 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export function sanitizeName(name) { + // blacklisted characters + const blacklist = ['(', ')']; + const pattern = blacklist.map(v => escapeRegExp(v)).join('|'); + const regex = new RegExp(pattern, 'g'); + return name.replace(regex, '_'); +} + +function escapeRegExp(string) { + return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); +} diff --git a/packages/kbn-interpreter/tasks/build.js b/packages/kbn-interpreter/tasks/build.js new file mode 100644 index 0000000000000..37776e8d74cca --- /dev/null +++ b/packages/kbn-interpreter/tasks/build.js @@ -0,0 +1,40 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +const webpack = require('webpack'); +const webpackConfig = require('./webpack.plugins'); + +const devtool = 'inline-cheap-module-source-map'; + +const onComplete = function (done) { + return function (err, stats) { + if (err) { + done && done(err); + } else { + const seconds = ((stats.endTime - stats.startTime) / 1000).toFixed(2); + console.log(`Plugins built in ${seconds} seconds`); + done && done(); + } + }; +}; + +webpack({ ...webpackConfig, devtool }, onComplete(function () { + console.log('all done'); +})); + diff --git a/packages/kbn-interpreter/tasks/webpack.plugins.js b/packages/kbn-interpreter/tasks/webpack.plugins.js new file mode 100644 index 0000000000000..124e88994c0f5 --- /dev/null +++ b/packages/kbn-interpreter/tasks/webpack.plugins.js @@ -0,0 +1,144 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +const path = require('path'); + +const sourceDir = path.resolve(__dirname, '../plugin_src'); +const buildDir = path.resolve(__dirname, '../plugin'); + +module.exports = { + entry: { + 'types/all': path.join(sourceDir, 'types/register.js'), + 'functions/browser/all': path.join(sourceDir, 'functions/browser/register.js'), + 'functions/common/all': path.join(sourceDir, 'functions/common/register.js'), + 'functions/server/all': path.join(sourceDir, 'functions/server/register.js'), + }, + target: 'webworker', + + output: { + path: buildDir, + filename: '[name].js', // Need long paths here. + libraryTarget: 'umd', + }, + + resolve: { + extensions: ['.js', '.json'], + }, + + plugins: [ + function loaderFailHandler() { + // bails on error, including loader errors + // see https://github.com/webpack/webpack/issues/708, which does not fix loader errors + let isWatch = true; + + this.plugin('run', function (compiler, callback) { + isWatch = false; + callback.call(compiler); + }); + + this.plugin('done', function (stats) { + if (stats.compilation.errors && stats.compilation.errors.length) { + if (isWatch) console.error(stats.compilation.errors[0]); + else throw stats.compilation.errors[0]; + } + }); + }, + ], + + module: { + rules: [ + // There's some React 15 propTypes funny business in EUI, this strips out propTypes and fixes it + { + test: /(@elastic\/eui|moment)\/.*\.js$/, + loaders: 'babel-loader', + options: { + babelrc: false, + presets: [ + 'react', + [ + 'env', + { + targets: { + node: 'current', + }, + }, + ], + ], + plugins: [ + 'transform-react-remove-prop-types', // specifically this, strips out propTypes + 'pegjs-inline-precompile', + 'transform-object-rest-spread', + 'transform-async-to-generator', + 'transform-class-properties', + [ + 'inline-react-svg', + { + ignorePattern: 'images/*', + svgo: { + plugins: [{ cleanupIDs: false }, { removeViewBox: false }], + }, + }, + ], + ], + }, + }, + { + test: /\.js$/, + loaders: 'babel-loader', + options: { + plugins: [ + 'transform-object-rest-spread', + 'transform-async-to-generator', + 'transform-class-properties', + ], + presets: [ + 'react', + [ + 'env', + { + targets: { + node: 'current', + }, + }, + ], + ], + }, + exclude: [/node_modules/], + }, + { + test: /\.(png|jpg|gif|jpeg|svg)$/, + loaders: ['url-loader'], + }, + { + test: /\.(css|scss)$/, + loaders: ['style-loader', 'css-loader', 'sass-loader'], + }, + ], + }, + + node: { + // Don't replace built-in globals + __filename: false, + __dirname: false, + }, + + watchOptions: { + ignored: [/node_modules/], + }, +}; diff --git a/packages/kbn-interpreter/test_utils.js b/packages/kbn-interpreter/test_utils.js new file mode 100644 index 0000000000000..ad98dc8cb76e6 --- /dev/null +++ b/packages/kbn-interpreter/test_utils.js @@ -0,0 +1,48 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { emptyTable, testTable, stringTable } from './common/__tests__/fixtures/test_tables'; +import { + fontStyle, + containerStyle, + defaultStyle, + seriesStyle, + grayscalePalette, + gradientPalette, + xAxisConfig, + yAxisConfig, + hideAxis +} from './common/__tests__/fixtures/test_styles'; +import { functionWrapper } from './common/__tests__/helpers/function_wrapper'; + +export { + emptyTable, + testTable, + stringTable, + fontStyle, + containerStyle, + defaultStyle, + seriesStyle, + grayscalePalette, + gradientPalette, + xAxisConfig, + yAxisConfig, + hideAxis, + functionWrapper +}; diff --git a/src/core_plugins/interpreter/index.js b/src/core_plugins/interpreter/index.js new file mode 100644 index 0000000000000..21ab23f1b8373 --- /dev/null +++ b/src/core_plugins/interpreter/index.js @@ -0,0 +1,36 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { resolve } from 'path'; +import init from './init'; + +export default function (kibana) { + return new kibana.Plugin({ + id: 'interpreter', + require: ['kibana', 'elasticsearch'], + publicDir: resolve(__dirname, 'public'), + uiExports: { + hacks: [ + 'plugins/interpreter/load_browser_plugins.js', + ], + }, + init, + }); +} + diff --git a/src/core_plugins/interpreter/init.js b/src/core_plugins/interpreter/init.js new file mode 100644 index 0000000000000..e4535946efc0c --- /dev/null +++ b/src/core_plugins/interpreter/init.js @@ -0,0 +1,44 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { routes } from './server/routes'; +import { functionsRegistry } from '@kbn/interpreter/common/lib/functions_registry'; +import { pathsRegistry } from '@kbn/interpreter/common/lib/paths_registry'; +import { loadServerPlugins } from '@kbn/interpreter/common/lib/load_server_plugins'; +import { pluginPaths } from './plugin_paths'; + +export default function (server /*options*/) { + server.injectUiAppVars('canvas', () => { + const config = server.config(); + const basePath = config.get('server.basePath'); + const reportingBrowserType = config.get('xpack.reporting.capture.browser.type'); + + return { + kbnIndex: config.get('kibana.index'), + esShardTimeout: config.get('elasticsearch.shardTimeout'), + esApiVersion: config.get('elasticsearch.apiVersion'), + serverFunctions: functionsRegistry.toArray(), + basePath, + reportingBrowserType, + }; + }); + + pathsRegistry.registerAll(pluginPaths); + loadServerPlugins().then(() => routes(server)); +} diff --git a/src/core_plugins/interpreter/package.json b/src/core_plugins/interpreter/package.json new file mode 100644 index 0000000000000..3265dadd7fbfc --- /dev/null +++ b/src/core_plugins/interpreter/package.json @@ -0,0 +1,4 @@ +{ + "name": "interpreter", + "version": "kibana" +} diff --git a/src/core_plugins/interpreter/plugin_paths.js b/src/core_plugins/interpreter/plugin_paths.js new file mode 100644 index 0000000000000..bf1edd0eef2d6 --- /dev/null +++ b/src/core_plugins/interpreter/plugin_paths.js @@ -0,0 +1,29 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { resolve } from 'path'; + +const dir = resolve(__dirname, '..', '..', '..'); + +export const pluginPaths = { + serverFunctions: resolve(dir, 'node_modules/@kbn/interpreter/plugin/functions/server'), + browserFunctions: resolve(dir, 'node_modules/@kbn/interpreter/plugin/functions/browser'), + commonFunctions: resolve(dir, 'node_modules/@kbn/interpreter/plugin/functions/common'), + types: resolve(dir, 'node_modules/@kbn/interpreter/plugin/types'), +}; diff --git a/src/core_plugins/interpreter/public/create_handlers.js b/src/core_plugins/interpreter/public/create_handlers.js new file mode 100644 index 0000000000000..3446a945ae76e --- /dev/null +++ b/src/core_plugins/interpreter/public/create_handlers.js @@ -0,0 +1,24 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export function createHandlers(/*socket*/) { + return { + environment: 'client', + }; +} diff --git a/src/core_plugins/interpreter/public/interpreter.js b/src/core_plugins/interpreter/public/interpreter.js new file mode 100644 index 0000000000000..e2ef8e9628fd2 --- /dev/null +++ b/src/core_plugins/interpreter/public/interpreter.js @@ -0,0 +1,54 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { socketInterpreterProvider } from '@kbn/interpreter/common/interpreter/socket_interpret'; +import { serializeProvider } from '@kbn/interpreter/common/lib/serialize'; +import { typesRegistry } from '@kbn/interpreter/common/lib/types_registry'; +import { functionsRegistry } from '@kbn/interpreter/common/lib/functions_registry'; +import { socket } from './socket'; +import { createHandlers } from './create_handlers'; +import { loadBrowserPlugins } from '@kbn/interpreter/public/load_browser_plugins'; + +// Create the function list +socket.emit('getFunctionList'); +export const getServerFunctions = new Promise(resolve => socket.once('functionList', resolve)); + +// Use the above promise to seed the interpreter with the functions it can defer to +export function interpretAst(ast, context) { + // Load plugins before attempting to get functions, otherwise this gets racey + return Promise.all([getServerFunctions, loadBrowserPlugins()]) + .then(([serverFunctionList]) => { + return socketInterpreterProvider({ + types: typesRegistry.toJS(), + handlers: createHandlers(socket), + functions: functionsRegistry.toJS(), + referableFunctions: serverFunctionList, + socket: socket, + }); + }) + .then(interpretFn => interpretFn(ast, context)); +} + +socket.on('run', ({ ast, context, id }) => { + const types = typesRegistry.toJS(); + const { serialize, deserialize } = serializeProvider(types); + interpretAst(ast, deserialize(context)).then(value => { + socket.emit(`resp:${id}`, { value: serialize(value) }); + }); +}); diff --git a/src/core_plugins/interpreter/public/load_browser_plugins.js b/src/core_plugins/interpreter/public/load_browser_plugins.js new file mode 100644 index 0000000000000..f29cabfb56edf --- /dev/null +++ b/src/core_plugins/interpreter/public/load_browser_plugins.js @@ -0,0 +1,22 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { loadBrowserPlugins } from '@kbn/interpreter/public/load_browser_plugins'; + +loadBrowserPlugins(); diff --git a/src/core_plugins/interpreter/public/plugins_loading_state.js b/src/core_plugins/interpreter/public/plugins_loading_state.js new file mode 100644 index 0000000000000..b7701f8bfdc60 --- /dev/null +++ b/src/core_plugins/interpreter/public/plugins_loading_state.js @@ -0,0 +1,52 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import uuid from 'uuid/v4'; + +class PluginsLoadingState { + constructor() { + this.states = {}; + this.promises = []; + } + + setLoading() { + const id = uuid(); + this.states[id] = true; + return id; + } + + setComplete(id) { + delete this.states[id]; + if (!Object.keys(this.states).length) { + this.promises.forEach(resolve => (resolve())); + } + } + + loadingComplete() { + return new Promise(resolve => { + if (Object.keys(this.states).length) { + this.promises.push(resolve); + } else { + resolve(); + } + }); + } +} + +export const pluginsLoadingState = new PluginsLoadingState(); diff --git a/src/core_plugins/interpreter/public/socket.js b/src/core_plugins/interpreter/public/socket.js new file mode 100644 index 0000000000000..8bd79d191e2a0 --- /dev/null +++ b/src/core_plugins/interpreter/public/socket.js @@ -0,0 +1,32 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import chrome from 'ui/chrome'; +import io from 'socket.io-client'; +import { functionsRegistry } from '@kbn/interpreter/common/lib/functions_registry'; +import { pluginsLoadingState } from './plugins_loading_state'; + +const basePath = chrome.getBasePath(); +export const socket = io(undefined, { path: `${basePath}/socket.io` }); + +socket.on('getFunctionList', () => { + pluginsLoadingState.loadingComplete().then(() => { + socket.emit('functionList', functionsRegistry.toJS()); + }); +}); diff --git a/src/core_plugins/interpreter/server/lib/get_plugin_stream.js b/src/core_plugins/interpreter/server/lib/get_plugin_stream.js new file mode 100644 index 0000000000000..f86aa8a23c291 --- /dev/null +++ b/src/core_plugins/interpreter/server/lib/get_plugin_stream.js @@ -0,0 +1,37 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import fs from 'fs'; +import ss from 'stream-stream'; +import { getPluginPaths } from '@kbn/interpreter/common/lib/get_plugin_paths'; + +export const getPluginStream = type => { + const stream = ss({ + separator: '\n', + }); + + getPluginPaths(type).then(files => { + files.forEach(file => { + stream.write(fs.createReadStream(file)); + }); + stream.end(); + }); + + return stream; +}; diff --git a/src/core_plugins/interpreter/server/routes/es_fields/get_es_field_types.js b/src/core_plugins/interpreter/server/routes/es_fields/get_es_field_types.js new file mode 100644 index 0000000000000..631e26ab47492 --- /dev/null +++ b/src/core_plugins/interpreter/server/routes/es_fields/get_es_field_types.js @@ -0,0 +1,42 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { mapValues, keys } from 'lodash'; +import { normalizeType } from '@kbn/interpreter/server'; + +export function getESFieldTypes(index, fields, elasticsearchClient) { + const config = { + index: index, + fields: fields || '*', + }; + + if (fields && fields.length === 0) return Promise.resolve({}); + + return elasticsearchClient('fieldCaps', config).then(resp => { + return mapValues(resp.fields, types => { + if (keys(types).length > 1) return 'conflict'; + + try { + return normalizeType(keys(types)[0]); + } catch (e) { + return 'unsupported'; + } + }); + }); +} diff --git a/src/core_plugins/interpreter/server/routes/es_fields/index.js b/src/core_plugins/interpreter/server/routes/es_fields/index.js new file mode 100644 index 0000000000000..ea6db5f087b1c --- /dev/null +++ b/src/core_plugins/interpreter/server/routes/es_fields/index.js @@ -0,0 +1,37 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { partial } from 'lodash'; +import { getESFieldTypes } from './get_es_field_types'; + +// TODO: Error handling, note: esErrors +export function esFields(server) { + const { callWithRequest } = server.plugins.elasticsearch.getCluster('data'); + + server.route({ + method: 'GET', + path: '/api/canvas/es_fields', + handler: function (request, reply) { + const { index, fields } = request.query; + if (!index) return reply({ error: '"index" query is required' }).code(400); + + reply(getESFieldTypes(index, fields, partial(callWithRequest, request))); + }, + }); +} diff --git a/src/core_plugins/interpreter/server/routes/es_indices/get_es_indices.js b/src/core_plugins/interpreter/server/routes/es_indices/get_es_indices.js new file mode 100644 index 0000000000000..ddf5d7c9a8cf1 --- /dev/null +++ b/src/core_plugins/interpreter/server/routes/es_indices/get_es_indices.js @@ -0,0 +1,33 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { map } from 'lodash'; + +export function getESIndices(kbnIndex, elasticsearchClient) { + const config = { + index: kbnIndex, + _source: 'index-pattern.title', + q: 'type:"index-pattern"', + size: 100, + }; + + return elasticsearchClient('search', config).then(resp => { + return map(resp.hits.hits, '_source["index-pattern"].title'); + }); +} diff --git a/src/core_plugins/interpreter/server/routes/es_indices/index.js b/src/core_plugins/interpreter/server/routes/es_indices/index.js new file mode 100644 index 0000000000000..0cafe6b3381d9 --- /dev/null +++ b/src/core_plugins/interpreter/server/routes/es_indices/index.js @@ -0,0 +1,36 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { partial } from 'lodash'; +import { getESIndices } from './get_es_indices'; + +// TODO: Error handling, note: esErrors +// TODO: Allow filtering by pattern name +export function esIndices(server) { + const kbnIndex = server.config().get('kibana.index'); + const { callWithRequest } = server.plugins.elasticsearch.getCluster('data'); + + server.route({ + method: 'GET', + path: '/api/canvas/es_indices', + handler: function (request, reply) { + reply(getESIndices(kbnIndex, partial(callWithRequest, request))); + }, + }); +} diff --git a/src/core_plugins/interpreter/server/routes/get_auth/get_auth_header.js b/src/core_plugins/interpreter/server/routes/get_auth/get_auth_header.js new file mode 100644 index 0000000000000..e30ff7fbeac92 --- /dev/null +++ b/src/core_plugins/interpreter/server/routes/get_auth/get_auth_header.js @@ -0,0 +1,34 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { insecureAuthRoute } from './insecure_auth_route'; + +// TODO: OMG. No. Need a better way of setting to this than our wacky route thing. +export function getAuthHeader(request, server) { + const basePath = server.config().get('server.basePath') || ''; + const fullPath = `${basePath}${insecureAuthRoute}`; + + return server + .inject({ + method: 'GET', + url: fullPath, + headers: request.headers, + }) + .then(res => res.result); +} diff --git a/src/core_plugins/interpreter/server/routes/get_auth/index.js b/src/core_plugins/interpreter/server/routes/get_auth/index.js new file mode 100644 index 0000000000000..9dd4a56164776 --- /dev/null +++ b/src/core_plugins/interpreter/server/routes/get_auth/index.js @@ -0,0 +1,35 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { insecureAuthRoute } from './insecure_auth_route'; + +// TODO: Fix this first. This route returns decrypts the cookie and returns the basic auth header. It is used because +// the pre-route hapi hook doesn't work on the socket and there are no exposed methods for doing the conversion from cookie +// to auth header. We will need to add that to x-pack security +// In theory this is pretty difficult to exploit, but not impossible. +// +export function getAuth(server) { + server.route({ + method: 'GET', + path: insecureAuthRoute, + handler: function (request, reply) { + reply(request.headers.authorization); + }, + }); +} diff --git a/src/core_plugins/interpreter/server/routes/get_auth/insecure_auth_route.js b/src/core_plugins/interpreter/server/routes/get_auth/insecure_auth_route.js new file mode 100644 index 0000000000000..3a5c8c6d768fa --- /dev/null +++ b/src/core_plugins/interpreter/server/routes/get_auth/insecure_auth_route.js @@ -0,0 +1,22 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import uuid from 'uuid/v4'; + +export const insecureAuthRoute = `/api/canvas/ar-${uuid()}`; diff --git a/src/core_plugins/interpreter/server/routes/index.js b/src/core_plugins/interpreter/server/routes/index.js new file mode 100644 index 0000000000000..105c7adeed325 --- /dev/null +++ b/src/core_plugins/interpreter/server/routes/index.js @@ -0,0 +1,34 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { socketApi } from './socket'; +import { translate } from './translate'; +import { esFields } from './es_fields'; +import { esIndices } from './es_indices'; +import { getAuth } from './get_auth'; +import { plugins } from './plugins'; + +export function routes(server) { + socketApi(server); + translate(server); + esFields(server); + esIndices(server); + getAuth(server); + plugins(server); +} diff --git a/src/core_plugins/interpreter/server/routes/plugins.js b/src/core_plugins/interpreter/server/routes/plugins.js new file mode 100644 index 0000000000000..fca9114cad7d9 --- /dev/null +++ b/src/core_plugins/interpreter/server/routes/plugins.js @@ -0,0 +1,34 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { getPluginStream } from '../lib/get_plugin_stream'; + +export function plugins(server) { + server.route({ + method: 'GET', + path: '/api/canvas/plugins', + handler: function (request, reply) { + const { type } = request.query; + reply(getPluginStream(type)); + }, + config: { + auth: false, + }, + }); +} diff --git a/x-pack/plugins/canvas/server/routes/socket.js b/src/core_plugins/interpreter/server/routes/socket.js similarity index 57% rename from x-pack/plugins/canvas/server/routes/socket.js rename to src/core_plugins/interpreter/server/routes/socket.js index 4e99e20768a9a..f3f24002c134b 100644 --- a/x-pack/plugins/canvas/server/routes/socket.js +++ b/src/core_plugins/interpreter/server/routes/socket.js @@ -1,17 +1,30 @@ /* - * 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. + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ import socket from 'socket.io'; -import { createHandlers } from '../lib/create_handlers'; -import { socketInterpreterProvider } from '../../common/interpreter/socket_interpret'; -import { serializeProvider } from '../../common/lib/serialize'; -import { functionsRegistry } from '../../common/lib/functions_registry'; -import { typesRegistry } from '../../common/lib/types_registry'; -import { loadServerPlugins } from '../lib/load_server_plugins'; +import { createHandlers } from '@kbn/interpreter/server'; +import { socketInterpreterProvider } from '@kbn/interpreter/common/interpreter/socket_interpret'; +import { serializeProvider } from '@kbn/interpreter/common/lib/serialize'; +import { functionsRegistry } from '@kbn/interpreter/common/lib/functions_registry'; +import { typesRegistry } from '@kbn/interpreter/common/lib/types_registry'; import { getAuthHeader } from './get_auth/get_auth_header'; +import { loadServerPlugins } from '@kbn/interpreter/common/lib/load_server_plugins'; export function socketApi(server) { const io = socket(server.listener, { path: '/socket.io' }); diff --git a/src/core_plugins/interpreter/server/routes/translate.js b/src/core_plugins/interpreter/server/routes/translate.js new file mode 100644 index 0000000000000..0d607fe6358b5 --- /dev/null +++ b/src/core_plugins/interpreter/server/routes/translate.js @@ -0,0 +1,49 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { fromExpression, toExpression } from '@kbn/interpreter/common/lib/ast'; + +export function translate(server) { + /* + Get AST from expression + */ + server.route({ + method: 'GET', + path: '/api/canvas/ast', + handler: function (request, reply) { + if (!request.query.expression) { + return reply({ error: '"expression" query is required' }).code(400); + } + reply(fromExpression(request.query.expression)); + }, + }); + + server.route({ + method: 'POST', + path: '/api/canvas/expression', + handler: function (request, reply) { + try { + const exp = toExpression(request.payload); + reply(exp); + } catch (e) { + reply({ error: e.message }).code(400); + } + }, + }); +} diff --git a/src/dev/license_checker/config.js b/src/dev/license_checker/config.js index f51f89d323319..f9b6443501c67 100644 --- a/src/dev/license_checker/config.js +++ b/src/dev/license_checker/config.js @@ -75,4 +75,7 @@ export const LICENSE_OVERRIDES = { 'uglify-js@2.2.5': ['BSD'], 'png-js@0.1.1': ['MIT'], 'sha.js@2.4.11': ['BSD-3-Clause AND MIT'], + + // TODO: can be removed once a new version is published + 'babel-plugin-mock-imports@0.0.5': ['MIT'], }; diff --git a/src/optimize/base_optimizer.js b/src/optimize/base_optimizer.js index ff8cae3fd2bc1..8fe0f5e5ea2e9 100644 --- a/src/optimize/base_optimizer.js +++ b/src/optimize/base_optimizer.js @@ -150,8 +150,8 @@ export default class BaseOptimizer { }, { test, - include: /[\/\\]node_modules[\/\\]x-pack[\/\\]/, - exclude: /[\/\\]node_modules[\/\\]x-pack[\/\\]node_modules[\/\\]/, + include: /[\/\\]node_modules[\/\\](x-pack|@kbn[\/\\]interpreter)[\/\\]/, + exclude: /[\/\\]node_modules[\/\\](x-pack|@kbn[\/\\]interpreter)[\/\\]node_modules[\/\\]/, } ]; }; diff --git a/src/setup_node_env/babel_register/register.js b/src/setup_node_env/babel_register/register.js index 2d909636a02a8..12690eab8f02c 100644 --- a/src/setup_node_env/babel_register/register.js +++ b/src/setup_node_env/babel_register/register.js @@ -39,7 +39,7 @@ var ignore = [ // ignore paths matching `/node_modules/{a}/{b}`, unless `a` // is `x-pack` and `b` is not `node_modules` - /\/node_modules\/(?!x-pack\/(?!node_modules)([^\/]+))([^\/]+\/[^\/]+)/ + /\/node_modules\/(?!(x-pack\/|@kbn\/interpreter\/)(?!node_modules)([^\/]+))([^\/]+\/[^\/]+)/ ]; if (global.__BUILT_WITH_BABEL__) { diff --git a/x-pack/package.json b/x-pack/package.json index a1dc4986433d2..342ec32fff2a0 100644 --- a/x-pack/package.json +++ b/x-pack/package.json @@ -35,12 +35,7 @@ "ansi-colors": "^3.0.5", "ansicolors": "0.3.2", "aws-sdk": "2.2.33", - "axios": "^0.18.0", "babel-jest": "^23.4.2", - "babel-plugin-inline-react-svg": "^0.5.4", - "babel-plugin-mock-imports": "^0.0.5", - "babel-plugin-pegjs-inline-precompile": "^0.1.0", - "babel-plugin-transform-react-remove-prop-types": "^0.4.14", "babel-register": "^6.26.0", "chalk": "^2.4.1", "chance": "1.0.10", @@ -99,6 +94,7 @@ "@kbn/datemath": "link:../packages/kbn-datemath", "@kbn/i18n": "link:../packages/kbn-i18n", "@kbn/ui-framework": "link:../packages/kbn-ui-framework", + "@kbn/interpreter": "link:../packages/kbn-interpreter", "@samverschueren/stream-to-observable": "^0.3.0", "@scant/router": "^0.1.0", "@slack/client": "^4.2.2", diff --git a/x-pack/plugins/canvas/__tests__/helpers/function_wrapper.js b/x-pack/plugins/canvas/__tests__/helpers/function_wrapper.js deleted file mode 100644 index 4f078169f699f..0000000000000 --- a/x-pack/plugins/canvas/__tests__/helpers/function_wrapper.js +++ /dev/null @@ -1,18 +0,0 @@ -/* - * 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 { mapValues } from 'lodash'; - -// It takes a function spec and passes in default args into the spec fn -export const functionWrapper = (fnSpec, mockReduxStore) => { - const spec = fnSpec(); - const defaultArgs = mapValues(spec.args, argSpec => { - return argSpec.default; - }); - - return (context, args, handlers) => - spec.fn(context, { ...defaultArgs, ...args }, handlers, mockReduxStore); -}; diff --git a/x-pack/plugins/canvas/canvas_plugin_src/elements/horizontal_progress_bar/index.js b/x-pack/plugins/canvas/canvas_plugin_src/elements/horizontal_progress_bar/index.js index c1e5386c86de7..4485b2bf0df09 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/elements/horizontal_progress_bar/index.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/elements/horizontal_progress_bar/index.js @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { openSans } from '../../../common/lib/fonts'; +import { openSans } from '@kbn/interpreter/common/lib/fonts'; import header from './header.png'; export const horizontalProgressBar = () => ({ diff --git a/x-pack/plugins/canvas/canvas_plugin_src/elements/horizontal_progress_pill/index.js b/x-pack/plugins/canvas/canvas_plugin_src/elements/horizontal_progress_pill/index.js index 9e6590b68d72b..0e27180edb21e 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/elements/horizontal_progress_pill/index.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/elements/horizontal_progress_pill/index.js @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { openSans } from '../../../common/lib/fonts'; +import { openSans } from '@kbn/interpreter/common/lib/fonts'; import header from './header.png'; export const horizontalProgressPill = () => ({ diff --git a/x-pack/plugins/canvas/canvas_plugin_src/elements/metric/index.js b/x-pack/plugins/canvas/canvas_plugin_src/elements/metric/index.js index 258aa2dc0dc23..80d87b77f2955 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/elements/metric/index.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/elements/metric/index.js @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { openSans } from '../../../common/lib/fonts'; +import { openSans } from '@kbn/interpreter/common/lib/fonts'; import header from './header.png'; export const metric = () => ({ diff --git a/x-pack/plugins/canvas/canvas_plugin_src/elements/progress_gauge/index.js b/x-pack/plugins/canvas/canvas_plugin_src/elements/progress_gauge/index.js index cb2151cc9643b..881ba41192320 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/elements/progress_gauge/index.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/elements/progress_gauge/index.js @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { openSans } from '../../../common/lib/fonts'; +import { openSans } from '@kbn/interpreter/common/lib/fonts'; import header from './header.png'; export const progressGauge = () => ({ diff --git a/x-pack/plugins/canvas/canvas_plugin_src/elements/progress_semicircle/index.js b/x-pack/plugins/canvas/canvas_plugin_src/elements/progress_semicircle/index.js index b0b79c452c18f..aa19e6dde1258 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/elements/progress_semicircle/index.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/elements/progress_semicircle/index.js @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { openSans } from '../../../common/lib/fonts'; +import { openSans } from '@kbn/interpreter/common/lib/fonts'; import header from './header.png'; export const progressSemicircle = () => ({ diff --git a/x-pack/plugins/canvas/canvas_plugin_src/elements/progress_wheel/index.js b/x-pack/plugins/canvas/canvas_plugin_src/elements/progress_wheel/index.js index 1e68bb406f978..8eda450c01292 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/elements/progress_wheel/index.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/elements/progress_wheel/index.js @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { openSans } from '../../../common/lib/fonts'; +import { openSans } from '@kbn/interpreter/common/lib/fonts'; import header from './header.png'; export const progressWheel = () => ({ diff --git a/x-pack/plugins/canvas/canvas_plugin_src/elements/vertical_progress_bar/index.js b/x-pack/plugins/canvas/canvas_plugin_src/elements/vertical_progress_bar/index.js index 559da533b13fc..888d35d722663 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/elements/vertical_progress_bar/index.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/elements/vertical_progress_bar/index.js @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { openSans } from '../../../common/lib/fonts'; +import { openSans } from '@kbn/interpreter/common/lib/fonts'; import header from './header.png'; export const verticalProgressBar = () => ({ diff --git a/x-pack/plugins/canvas/canvas_plugin_src/elements/vertical_progress_pill/index.js b/x-pack/plugins/canvas/canvas_plugin_src/elements/vertical_progress_pill/index.js index 1096fb4cdf2df..5aaec5324c446 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/elements/vertical_progress_pill/index.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/elements/vertical_progress_pill/index.js @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { openSans } from '../../../common/lib/fonts'; +import { openSans } from '@kbn/interpreter/common/lib/fonts'; import header from './header.png'; export const verticalProgressPill = () => ({ diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/browser/__tests__/markdown.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/browser/__tests__/markdown.js index d4ea050951c9a..c9978334d5e2c 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/browser/__tests__/markdown.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/browser/__tests__/markdown.js @@ -5,10 +5,8 @@ */ import expect from 'expect.js'; +import { functionWrapper, testTable, fontStyle } from '@kbn/interpreter/test_utils'; import { markdown } from '../markdown'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; -import { testTable } from '../../common/__tests__/fixtures/test_tables'; -import { fontStyle } from '../../common/__tests__/fixtures/test_styles'; describe('markdown', () => { const fn = functionWrapper(markdown); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/browser/browser.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/browser/browser.js deleted file mode 100644 index 3e6fa268ad49e..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/browser/browser.js +++ /dev/null @@ -1,12 +0,0 @@ -/* - * 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. - */ - -export const browser = () => ({ - name: 'browser', - help: 'Force the interpreter to return to the browser', - args: {}, - fn: context => context, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/browser/index.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/browser/index.js index 3a5fa0c573316..0a8412ecc7577 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/browser/index.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/browser/index.js @@ -4,9 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import { browser } from './browser'; -import { location } from './location'; -import { urlparam } from './urlparam'; import { markdown } from './markdown'; -export const functions = [browser, location, urlparam, markdown]; +export const functions = [markdown]; diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/browser/location.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/browser/location.js deleted file mode 100644 index cd21d51015133..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/browser/location.js +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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. - */ - -const noop = () => {}; - -export const location = () => ({ - name: 'location', - type: 'datatable', - context: { - types: ['null'], - }, - help: - "Use the browser's location functionality to get your current location. Usually quite slow, but fairly accurate", - fn: () => { - return new Promise(resolve => { - function createLocation(geoposition) { - const { latitude, longitude } = geoposition.coords; - return resolve({ - type: 'datatable', - columns: [{ name: 'latitude', type: 'number' }, { name: 'longitude', type: 'number' }], - rows: [{ latitude, longitude }], - }); - } - return navigator.geolocation.getCurrentPosition(createLocation, noop, { - maximumAge: 5000, - }); - }); - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/browser/urlparam.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/browser/urlparam.js deleted file mode 100644 index 71c231070336d..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/browser/urlparam.js +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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 { parse } from 'url'; - -export const urlparam = () => ({ - name: 'urlparam', - aliases: [], - type: 'string', - help: - 'Access URL parameters and use them in expressions. Eg https://localhost:5601/app/canvas?myVar=20. This will always return a string', - context: { - types: ['null'], - }, - args: { - param: { - types: ['string'], - aliases: ['_', 'var', 'variable'], - help: 'The URL hash parameter to access', - multi: false, - }, - default: { - types: ['string'], - default: '""', - help: 'Return this string if the url parameter is not defined', - }, - }, - fn: (context, args) => { - const query = parse(window.location.href, true).query; - return query[args.param] || args.default; - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/axis_config.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/axis_config.js index f74f568080dc9..f89e7145064b9 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/axis_config.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/axis_config.js @@ -5,9 +5,8 @@ */ import expect from 'expect.js'; +import { functionWrapper, testTable } from '@kbn/interpreter/test_utils'; import { axisConfig } from '../axisConfig'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; -import { testTable } from '../__tests__/fixtures/test_tables'; describe('axisConfig', () => { const fn = functionWrapper(axisConfig); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/container_style.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/container_style.js index 7daef98b0929c..da7280e5f341e 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/container_style.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/container_style.js @@ -5,8 +5,8 @@ */ import expect from 'expect.js'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; import { containerStyle } from '../containerStyle'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; import { elasticLogo } from '../../../lib/elastic_logo'; describe('containerStyle', () => { diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/context.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/context.js deleted file mode 100644 index da162dc9150b1..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/context.js +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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 expect from 'expect.js'; -import { context } from '../context'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; -import { testTable, emptyTable } from './fixtures/test_tables'; - -describe('context', () => { - const fn = functionWrapper(context); - - it('returns whatever context you pass into', () => { - expect(fn(null)).to.be(null); - expect(fn(true)).to.be(true); - expect(fn(1)).to.be(1); - expect(fn('foo')).to.be('foo'); - expect(fn({})).to.eql({}); - expect(fn(emptyTable)).to.eql(emptyTable); - expect(fn(testTable)).to.eql(testTable); - }); -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/do.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/do.js deleted file mode 100644 index 000e614a35c98..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/do.js +++ /dev/null @@ -1,20 +0,0 @@ -/* - * 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 expect from 'expect.js'; -import { doFn } from '../do'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; - -describe('do', () => { - const fn = functionWrapper(doFn); - - it('should only pass context', () => { - expect(fn(1, { fn: '1' })).to.equal(1); - expect(fn(true, {})).to.equal(true); - expect(fn(null, {})).to.equal(null); - expect(fn(null, { fn: 'not null' })).to.equal(null); - }); -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/dropdown_control.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/dropdown_control.js index 437e55a197c40..d86708f821061 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/dropdown_control.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/dropdown_control.js @@ -5,9 +5,8 @@ */ import expect from 'expect.js'; +import { functionWrapper, testTable } from '@kbn/interpreter/test_utils'; import { dropdownControl } from '../dropdownControl'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; -import { testTable } from './fixtures/test_tables'; describe('dropdownControl', () => { const fn = functionWrapper(dropdownControl); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/formatnumber.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/formatnumber.js deleted file mode 100644 index b0d8a7df62f1f..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/formatnumber.js +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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 expect from 'expect.js'; -import { formatnumber } from '../formatnumber'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; - -describe('formatnumber', () => { - const fn = functionWrapper(formatnumber); - - it('returns number as formatted string with given format', () => { - expect(fn(140000, { format: '$0,0.00' })).to.be('$140,000.00'); - }); - - describe('args', () => { - describe('format', () => { - it('sets the format of the resulting number string', () => { - expect(fn(0.68, { format: '0.000%' })).to.be('68.000%'); - }); - - it('casts number to a string if format is not specified', () => { - expect(fn(140000.999999)).to.be('140000.999999'); - }); - }); - }); -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/get_flot_axis_config.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/get_flot_axis_config.js index 2a1fca38f2685..79a1c6c3e58b0 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/get_flot_axis_config.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/get_flot_axis_config.js @@ -5,8 +5,8 @@ */ import expect from 'expect.js'; +import { xAxisConfig, yAxisConfig, hideAxis } from '@kbn/interpreter/test_utils'; import { getFlotAxisConfig } from '../plot/get_flot_axis_config'; -import { xAxisConfig, yAxisConfig, hideAxis } from './fixtures/test_styles'; describe('getFlotAxisConfig', () => { const columns = { diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/get_font_spec.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/get_font_spec.js index 4f2fb67cd938f..f721a60161f7d 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/get_font_spec.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/get_font_spec.js @@ -5,8 +5,8 @@ */ import expect from 'expect.js'; +import { fontStyle } from '@kbn/interpreter/test_utils'; import { defaultSpec, getFontSpec } from '../plot/get_font_spec'; -import { fontStyle } from './fixtures/test_styles'; describe('getFontSpec', () => { describe('default output', () => { diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/image.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/image.js index 304d970882bf4..20b4647417443 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/image.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/image.js @@ -5,10 +5,10 @@ */ import expect from 'expect.js'; -import { image } from '../image'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; import { elasticLogo } from '../../../lib/elastic_logo'; import { elasticOutline } from '../../../lib/elastic_outline'; +import { image } from '../image'; describe('image', () => { const fn = functionWrapper(image); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/metric.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/metric.js index 587e7fbf1f8b0..84751ff043325 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/metric.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/metric.js @@ -5,9 +5,8 @@ */ import expect from 'expect.js'; +import { functionWrapper, fontStyle } from '@kbn/interpreter/test_utils'; import { metric } from '../metric'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; -import { fontStyle } from './fixtures/test_styles'; describe('metric', () => { const fn = functionWrapper(metric); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/palette.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/palette.js index ecb3ed8b2bbd0..b4eeef43d220b 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/palette.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/palette.js @@ -5,9 +5,9 @@ */ import expect from 'expect.js'; -import { palette } from '../palette'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; import { palettes } from '../../../../common/lib/palettes'; +import { palette } from '../palette'; describe('palette', () => { const fn = functionWrapper(palette); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/pie.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/pie.js index d869227ae2dc8..260362ef6b518 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/pie.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/pie.js @@ -5,10 +5,14 @@ */ import expect from 'expect.js'; +import { + functionWrapper, + fontStyle, + grayscalePalette, + seriesStyle, +} from '@kbn/interpreter/test_utils'; import { pie } from '../pie'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; import { testPie } from './fixtures/test_pointseries'; -import { fontStyle, grayscalePalette, seriesStyle } from './fixtures/test_styles'; describe('pie', () => { const fn = functionWrapper(pie); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/plot.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/plot.js index 3d6c62419ef48..cdea5baac214a 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/plot.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/plot.js @@ -5,10 +5,8 @@ */ import expect from 'expect.js'; -import { plot } from '../plot'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; -import { testPlot } from './fixtures/test_pointseries'; import { + functionWrapper, fontStyle, grayscalePalette, gradientPalette, @@ -16,7 +14,9 @@ import { xAxisConfig, seriesStyle, defaultStyle, -} from './fixtures/test_styles'; +} from '@kbn/interpreter/test_utils'; +import { plot } from '../plot'; +import { testPlot } from './fixtures/test_pointseries'; describe('plot', () => { const fn = functionWrapper(plot); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/ply.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/ply.js index 0e72cb38885b9..e70eef0e39ec2 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/ply.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/ply.js @@ -5,9 +5,8 @@ */ import expect from 'expect.js'; +import { functionWrapper, testTable } from '@kbn/interpreter/test_utils'; import { ply } from '../ply'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; -import { testTable } from './fixtures/test_tables'; const averagePrice = datatable => { const average = datatable.rows.reduce((sum, row) => sum + row.price, 0) / datatable.rows.length; diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/progress.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/progress.js index 5b16b80ddd744..35236f0dee674 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/progress.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/progress.js @@ -5,9 +5,8 @@ */ import expect from 'expect.js'; +import { functionWrapper, fontStyle } from '@kbn/interpreter/test_utils'; import { progress } from '../progress'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; -import { fontStyle } from './fixtures/test_styles'; describe('progress', () => { const fn = functionWrapper(progress); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/repeat_image.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/repeat_image.js index 0a18b7d25071e..320edcffab264 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/repeat_image.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/repeat_image.js @@ -5,10 +5,10 @@ */ import expect from 'expect.js'; -import { repeatImage } from '../repeatImage'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; import { elasticOutline } from '../../../lib/elastic_outline'; import { elasticLogo } from '../../../lib/elastic_logo'; +import { repeatImage } from '../repeatImage'; describe('repeatImage', () => { const fn = functionWrapper(repeatImage); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/reveal_image.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/reveal_image.js index 3dc00b3c55592..579fa07837529 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/reveal_image.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/reveal_image.js @@ -5,10 +5,10 @@ */ import expect from 'expect.js'; -import { revealImage } from '../revealImage'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; import { elasticOutline } from '../../../lib/elastic_outline'; import { elasticLogo } from '../../../lib/elastic_logo'; +import { revealImage } from '../revealImage'; describe('revealImage', () => { const fn = functionWrapper(revealImage); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/rounddate.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/rounddate.js deleted file mode 100644 index ab66ef05f4f2b..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/rounddate.js +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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 expect from 'expect.js'; -import { rounddate } from '../rounddate'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; - -describe('rounddate', () => { - const fn = functionWrapper(rounddate); - const date = new Date('2011-10-31T00:00:00.000Z').valueOf(); - - it('returns date in ms from date in ms or ISO8601 string', () => { - expect(fn(date, { format: 'YYYY' })).to.be(1293840000000); - }); - - describe('args', () => { - describe('format', () => { - it('sets the format for the rounded date', () => { - expect(fn(date, { format: 'YYYY-MM' })).to.be(1317427200000); - expect(fn(date, { format: 'YYYY-MM-DD-hh' })).to.be(1320062400000); - }); - - it('returns original date if format is not provided', () => { - expect(fn(date)).to.be(date); - }); - }); - }); -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/rowCount.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/rowCount.js deleted file mode 100644 index c50f39f7b3084..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/rowCount.js +++ /dev/null @@ -1,19 +0,0 @@ -/* - * 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 expect from 'expect.js'; -import { rowCount } from '../rowCount'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; -import { emptyTable, testTable } from './fixtures/test_tables'; - -describe('rowCount', () => { - const fn = functionWrapper(rowCount); - - it('returns the number of rows in the datatable', () => { - expect(fn(testTable)).to.equal(testTable.rows.length); - expect(fn(emptyTable)).to.equal(0); - }); -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/string.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/string.js deleted file mode 100644 index 7961d0553644d..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/string.js +++ /dev/null @@ -1,25 +0,0 @@ -/* - * 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 expect from 'expect.js'; -import { string } from '../string'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; - -describe('string', () => { - const fn = functionWrapper(string); - - it('casts primitive types to strings', () => { - expect(fn(null, { value: [14000] })).to.be('14000'); - expect(fn(null, { value: ['foo'] })).to.be('foo'); - expect(fn(null, { value: [null] })).to.be(''); - expect(fn(null, { value: [true] })).to.be('true'); - }); - - it('concatenates all args to one string', () => { - expect(fn(null, { value: ['foo', 'bar', 'fizz', 'buzz'] })).to.be('foobarfizzbuzz'); - expect(fn(null, { value: ['foo', 1, true, null] })).to.be('foo1true'); - }); -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/timefilter.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/timefilter.js index a63adc3d05335..6489a0f4245f8 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/timefilter.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/timefilter.js @@ -6,8 +6,8 @@ import expect from 'expect.js'; import sinon from 'sinon'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; import { timefilter } from '../timefilter'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; import { emptyFilter } from './fixtures/test_filters'; let clock = null; diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/timefilter_control.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/timefilter_control.js index 1341f961412da..f9c5dad513ef4 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/timefilter_control.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/__tests__/timefilter_control.js @@ -5,8 +5,8 @@ */ import expect from 'expect.js'; +import { functionWrapper } from '@kbn/interpreter/test_utils'; import { timefilterControl } from '../timefilterControl'; -import { functionWrapper } from '../../../../__tests__/helpers/function_wrapper'; describe('timefilterControl', () => { const fn = functionWrapper(timefilterControl); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/all.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/all.js deleted file mode 100644 index 417803830f451..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/all.js +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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. - */ - -export const all = () => ({ - name: 'all', - type: 'boolean', - help: 'Return true if all of the conditions are true', - args: { - condition: { - aliases: ['_'], - types: ['boolean', 'null'], - required: true, - multi: true, - help: 'One or more conditions to check', - }, - }, - fn: (context, args) => { - const conditions = args.condition || []; - return conditions.every(Boolean); - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/any.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/any.js deleted file mode 100644 index 9e0b2fd08928e..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/any.js +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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. - */ - -export const any = () => ({ - name: 'any', - type: 'boolean', - help: 'Return true if any of the conditions are true', - args: { - condition: { - aliases: ['_'], - types: ['boolean', 'null'], - required: true, - multi: true, - help: 'One or more conditions to check', - }, - }, - fn: (context, args) => { - const conditions = args.condition || []; - return conditions.some(Boolean); - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/as.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/as.js deleted file mode 100644 index c85cc9e0d5baf..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/as.js +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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 { getType } from '../../../common/lib/get_type'; - -export const asFn = () => ({ - name: 'as', - type: 'datatable', - context: { - types: ['string', 'boolean', 'number', 'null'], - }, - help: 'Creates a datatable with a single value', - args: { - name: { - types: ['string'], - aliases: ['_'], - help: 'A name to give the column', - default: 'value', - }, - }, - fn: (context, args) => { - return { - type: 'datatable', - columns: [ - { - name: args.name, - type: getType(context), - }, - ], - rows: [ - { - [args.name]: context, - }, - ], - }; - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/context.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/context.js deleted file mode 100644 index 20d03c578fd64..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/context.js +++ /dev/null @@ -1,15 +0,0 @@ -/* - * 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. - */ - -export const context = () => ({ - name: 'context', - help: - 'Returns whatever you pass into it. This can be useful when you need to use context as argument to a function as a sub-expression', - args: {}, - fn: context => { - return context; - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/do.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/do.js deleted file mode 100644 index f67fbbc81434d..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/do.js +++ /dev/null @@ -1,20 +0,0 @@ -/* - * 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. - */ - -export const doFn = () => ({ - name: 'do', - help: - 'Runs multiple sub-expressions. Returns the passed in context. Nice for running actions producing functions.', - args: { - fn: { - aliases: ['_'], - multi: true, - help: - 'One or more sub-expressions. The value of these is not available in the root pipeline as this function simply returns the passed in context', - }, - }, - fn: context => context, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/eq.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/eq.js deleted file mode 100644 index 9aaf5d3ad9390..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/eq.js +++ /dev/null @@ -1,22 +0,0 @@ -/* - * 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. - */ - -export const eq = () => ({ - name: 'eq', - type: 'boolean', - help: 'Return if the context is equal to the argument', - args: { - value: { - aliases: ['_'], - types: ['boolean', 'number', 'string', 'null'], - required: true, - help: 'The value to compare the context to', - }, - }, - fn: (context, args) => { - return context === args.value; - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/exactly.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/exactly.js deleted file mode 100644 index f4ccbd6623122..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/exactly.js +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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. - */ - -export const exactly = () => ({ - name: 'exactly', - aliases: [], - type: 'filter', - context: { - types: ['filter'], - }, - help: 'Create a filter that matches a given column for a perfectly exact value', - args: { - column: { - types: ['string'], - aliases: ['field', 'c'], - help: 'The column or field to attach the filter to', - }, - value: { - types: ['string'], - aliases: ['v', 'val'], - help: 'The value to match exactly, including white space and capitalization', - }, - }, - fn: (context, args) => { - const { value, column } = args; - - const filter = { - type: 'exactly', - value, - column, - }; - - return { ...context, and: [...context.and, filter] }; - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/formatdate.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/formatdate.js deleted file mode 100644 index 489317928e035..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/formatdate.js +++ /dev/null @@ -1,26 +0,0 @@ -/* - * 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 moment from 'moment'; -export const formatdate = () => ({ - name: 'formatdate', - type: 'string', - help: 'Output a ms since epoch number as a formatted string', - context: { - types: ['number'], - }, - args: { - format: { - aliases: ['_'], - types: ['string'], - help: 'MomentJS Format with which to bucket (See https://momentjs.com/docs/#/displaying/)', - }, - }, - fn: (context, args) => { - if (!args.format) return moment.utc(new Date(context)).toISOString(); - return moment.utc(new Date(context)).format(args.format); - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/formatnumber.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/formatnumber.js deleted file mode 100644 index ff3cd5f243d46..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/formatnumber.js +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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 numeral from '@elastic/numeral'; - -export const formatnumber = () => ({ - name: 'formatnumber', - type: 'string', - help: 'Turn a number into a string using a NumberJS format', - context: { - types: ['number'], - }, - args: { - format: { - aliases: ['_'], - types: ['string'], - help: 'NumeralJS format string http://numeraljs.com/#format', - }, - }, - fn: (context, args) => { - if (!args.format) return String(context); - return numeral(context).format(args.format); - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/getCell.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/getCell.js deleted file mode 100644 index 625db0a434a4d..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/getCell.js +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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. - */ - -export const getCell = () => ({ - name: 'getCell', - help: 'Fetch a single cell in a table', - context: { - types: ['datatable'], - }, - args: { - column: { - types: ['string'], - aliases: ['_', 'c'], - help: 'The name of the column value to fetch', - }, - row: { - types: ['number'], - aliases: ['r'], - help: 'The row number, starting at 0', - default: 0, - }, - }, - fn: (context, args) => { - const row = context.rows[args.row]; - if (!row) throw new Error(`Row not found: ${args.row}`); - - const { column = context.columns[0].name } = args; - const value = row[column]; - - if (typeof value === 'undefined') throw new Error(`Column not found: ${column}`); - - return value; - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/gt.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/gt.js deleted file mode 100644 index ccde455f20cba..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/gt.js +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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. - */ - -export const gt = () => ({ - name: 'gt', - type: 'boolean', - help: 'Return if the context is greater than the argument', - args: { - value: { - aliases: ['_'], - types: ['boolean', 'number', 'string', 'null'], - required: true, - help: 'The value to compare the context to', - }, - }, - fn: (context, args) => { - if (typeof context !== typeof args.value) return false; - return context > args.value; - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/gte.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/gte.js deleted file mode 100644 index 691deae146c05..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/gte.js +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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. - */ - -export const gte = () => ({ - name: 'gte', - type: 'boolean', - help: 'Return if the context is greater than or equal to the argument', - args: { - value: { - aliases: ['_'], - types: ['boolean', 'number', 'string', 'null'], - required: true, - help: 'The value to compare the context to', - }, - }, - fn: (context, args) => { - if (typeof context !== typeof args.value) return false; - return context >= args.value; - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/head.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/head.js deleted file mode 100644 index b301ab3cf1aa2..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/head.js +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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 { take } from 'lodash'; - -export const head = () => ({ - name: 'head', - aliases: [], - type: 'datatable', - help: 'Get the first N rows from the datatable. Also see `tail`', - context: { - types: ['datatable'], - }, - args: { - count: { - aliases: ['_'], - types: ['number'], - help: 'Return this many rows from the beginning of the datatable', - default: 1, - }, - }, - fn: (context, args) => ({ - ...context, - rows: take(context.rows, args.count), - }), -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/index.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/index.js index 410dbc60db952..59078b8bef520 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/index.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/index.js @@ -4,114 +4,36 @@ * you may not use this file except in compliance with the Elastic License. */ -import { alterColumn } from './alterColumn'; -import { all } from './all'; -import { any } from './any'; -import { asFn } from './as'; import { axisConfig } from './axisConfig'; -import { compare } from './compare'; import { containerStyle } from './containerStyle'; import { clog } from './clog'; -import { context } from './context'; -import { columns } from './columns'; -import { csv } from './csv'; -import { date } from './date'; -import { doFn } from './do'; import { dropdownControl } from './dropdownControl'; -import { eq } from './eq'; -import { exactly } from './exactly'; -import { filterrows } from './filterrows'; -import { font } from './font'; -import { formatdate } from './formatdate'; -import { formatnumber } from './formatnumber'; -import { getCell } from './getCell'; -import { gt } from './gt'; -import { gte } from './gte'; -import { head } from './head'; -import { ifFn } from './if'; import { image } from './image'; -import { lt } from './lt'; -import { lte } from './lte'; -import { mapColumn } from './mapColumn'; -import { math } from './math'; import { metric } from './metric'; -import { neq } from './neq'; import { palette } from './palette'; import { pie } from './pie'; import { plot } from './plot'; import { ply } from './ply'; import { progress } from './progress'; -import { render } from './render'; -import { replace } from './replace'; -import { rounddate } from './rounddate'; -import { rowCount } from './rowCount'; import { repeatImage } from './repeatImage'; import { revealImage } from './revealImage'; -import { seriesStyle } from './seriesStyle'; -import { shape } from './shape'; -import { sort } from './sort'; -import { staticColumn } from './staticColumn'; -import { string } from './string'; -import { table } from './table'; -import { tail } from './tail'; import { timefilter } from './timefilter'; import { timefilterControl } from './timefilterControl'; -import { switchFn } from './switch'; -import { caseFn } from './case'; export const functions = [ - all, - alterColumn, - any, - asFn, axisConfig, clog, - columns, - compare, containerStyle, - context, - csv, - date, - doFn, dropdownControl, - eq, - exactly, - filterrows, - font, - formatdate, - formatnumber, - getCell, - gt, - gte, - head, - ifFn, image, - lt, - lte, - mapColumn, - math, metric, - neq, palette, pie, plot, ply, progress, - render, repeatImage, - replace, revealImage, - rounddate, - rowCount, - seriesStyle, - shape, - sort, - staticColumn, - string, - table, - tail, timefilter, timefilterControl, - switchFn, - caseFn, ]; diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/lt.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/lt.js deleted file mode 100644 index f2d90639fc995..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/lt.js +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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. - */ - -export const lt = () => ({ - name: 'lt', - type: 'boolean', - help: 'Return if the context is less than the argument', - args: { - value: { - aliases: ['_'], - types: ['boolean', 'number', 'string', 'null'], - required: true, - help: 'The value to compare the context to', - }, - }, - fn: (context, args) => { - if (typeof context !== typeof args.value) return false; - return context < args.value; - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/lte.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/lte.js deleted file mode 100644 index ed9a413a71ede..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/lte.js +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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. - */ - -export const lte = () => ({ - name: 'lte', - type: 'boolean', - help: 'Return if the context is less than or equal to the argument', - args: { - value: { - aliases: ['_'], - types: ['boolean', 'number', 'string', 'null'], - required: true, - help: 'The value to compare the context to', - }, - }, - fn: (context, args) => { - if (typeof context !== typeof args.value) return false; - return context <= args.value; - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/metric.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/metric.js index f7faa9cc039b1..ba81f828f889f 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/metric.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/metric.js @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { openSans } from '../../../common/lib/fonts'; +import { openSans } from '@kbn/interpreter/common/lib/fonts'; export const metric = () => ({ name: 'metric', aliases: [], diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/neq.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/neq.js deleted file mode 100644 index 85b060ce882aa..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/neq.js +++ /dev/null @@ -1,22 +0,0 @@ -/* - * 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. - */ - -export const neq = () => ({ - name: 'neq', - type: 'boolean', - help: 'Return if the context is not equal to the argument', - args: { - value: { - aliases: ['_'], - types: ['boolean', 'number', 'string', 'null'], - required: true, - help: 'The value to compare the context to', - }, - }, - fn: (context, args) => { - return context !== args.value; - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/plot/get_flot_axis_config.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/plot/get_flot_axis_config.js index 1a8ee7daf7370..ce4b9170d1710 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/plot/get_flot_axis_config.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/plot/get_flot_axis_config.js @@ -5,7 +5,7 @@ */ import { get, map } from 'lodash'; -import { getType } from '../../../../common/lib/get_type'; +import { getType } from '@kbn/interpreter/common/lib/get_type'; export const getFlotAxisConfig = (axis, argValue, { columns, ticks, font } = {}) => { if (!argValue || argValue.show === false) return { show: false }; diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/plot/get_font_spec.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/plot/get_font_spec.js index 1d9242833b646..254991f62dda2 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/plot/get_font_spec.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/plot/get_font_spec.js @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { openSans } from '../../../../common/lib/fonts'; +import { openSans } from '@kbn/interpreter/common/lib/fonts'; // converts the output of the font function to a flot font spec // for font spec, see https://github.com/flot/flot/blob/master/API.md#customizing-the-axes export const defaultSpec = { diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/progress.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/progress.js index 859804c75a39e..42c4809b40881 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/progress.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/progress.js @@ -5,7 +5,7 @@ */ import { get } from 'lodash'; -import { openSans } from '../../../common/lib/fonts'; +import { openSans } from '@kbn/interpreter/common/lib/fonts'; import { shapes } from '../../renderers/progress/shapes'; export const progress = () => ({ diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/rounddate.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/rounddate.js deleted file mode 100644 index 1eeccd1b19432..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/rounddate.js +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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 moment from 'moment'; - -export const rounddate = () => ({ - name: 'rounddate', - type: 'number', - help: 'Round ms since epoch using a moment formatting string. Returns ms since epoch', - context: { - types: ['number'], - }, - args: { - format: { - aliases: ['_'], - types: ['string'], - help: - 'MomentJS Format with which to bucket (See https://momentjs.com/docs/#/displaying/). For example "YYYY-MM" would round to the month', - }, - }, - fn: (context, args) => { - if (!args.format) return context; - return moment.utc(moment.utc(context).format(args.format), args.format).valueOf(); - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/rowCount.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/rowCount.js deleted file mode 100644 index 389c036462834..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/rowCount.js +++ /dev/null @@ -1,18 +0,0 @@ -/* - * 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. - */ - -export const rowCount = () => ({ - name: 'rowCount', - aliases: [], - type: 'number', - context: { - types: ['datatable'], - }, - help: - 'Return the number of rows. Pair with ply to get the count of unique column values, or combinations of unique column values.', - args: {}, - fn: context => context.rows.length, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/string.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/string.js deleted file mode 100644 index 9374dc424ce51..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/string.js +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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. - */ - -export const string = () => ({ - name: 'string', - aliases: [], - type: 'string', - help: - 'Output a string made of other strings. Mostly useful when combined with sub-expressions that output a string, ' + - ' or something castable to a string', - args: { - value: { - aliases: ['_'], - types: ['string'], - multi: true, - help: "One or more strings to join together. Don't forget spaces where needed!", - }, - }, - fn: (context, args) => args.value.join(''), -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/switch.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/switch.js deleted file mode 100644 index 3cb3577cf74c8..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/switch.js +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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. - */ - -export const switchFn = () => ({ - name: 'switch', - help: 'Perform conditional logic with multiple conditions', - args: { - case: { - types: ['case'], - aliases: ['_'], - resolve: false, - multi: true, - help: 'The list of conditions to check', - }, - default: { - aliases: ['finally'], - resolve: false, - help: 'The default case if no cases match', - }, - }, - fn: async (context, args) => { - const cases = args.case || []; - for (let i = 0; i < cases.length; i++) { - const { matches, result } = await cases[i](); - if (matches) return result; - } - if (typeof args.default !== 'undefined') return await args.default(); - return context; - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/tail.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/tail.js deleted file mode 100644 index 5ee56d69e7462..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/tail.js +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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 { takeRight } from 'lodash'; - -export const tail = () => ({ - name: 'tail', - aliases: [], - type: 'datatable', - help: 'Get the last N rows from the end of a datatable. Also see `head`', - context: { - types: ['datatable'], - }, - args: { - count: { - aliases: ['_'], - types: ['number'], - help: 'Return this many rows from the end of the datatable', - }, - }, - fn: (context, args) => ({ - ...context, - rows: takeRight(context.rows, args.count), - }), -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/__tests__/get_expression_type.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/__tests__/get_expression_type.js index 93db2accedd0e..84553a01bb0ca 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/__tests__/get_expression_type.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/__tests__/get_expression_type.js @@ -5,8 +5,8 @@ */ import expect from 'expect.js'; +import { emptyTable, testTable } from '@kbn/interpreter/test_utils'; import { getExpressionType } from '../pointseries/lib/get_expression_type'; -import { emptyTable, testTable } from '../../../common/__tests__/fixtures/test_tables'; describe('getExpressionType', () => { it('returns the result type of an evaluated math expression', () => { diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/__tests__/pointseries.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/__tests__/pointseries.js index 42de75f69efb7..bbe2f33e612dd 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/__tests__/pointseries.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/__tests__/pointseries.js @@ -5,8 +5,8 @@ */ import expect from 'expect.js'; +import { emptyTable, testTable } from '@kbn/interpreter/test_utils'; import { pointseries } from '../pointseries'; -import { emptyTable, testTable } from '../../../common/__tests__/fixtures/test_tables'; describe('pointseries', () => { const fn = pointseries().fn; diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/escount.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/escount.js index 674f60d5333dc..a64dd973cd7d4 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/escount.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/escount.js @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { buildESRequest } from '../../../../server/lib/build_es_request'; +import { buildESRequest } from '@kbn/interpreter/server'; export const escount = () => ({ name: 'escount', diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/essql.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/essql.js index aeffee3bc9a44..38731462077e6 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/essql.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/essql.js @@ -5,9 +5,7 @@ */ import { map, zipObject } from 'lodash'; -import { normalizeType } from '../../../../server/lib/normalize_type'; -import { buildBoolArray } from '../../../../server/lib/build_bool_array'; -import { sanitizeName } from '../../../../server/lib/sanitize_name'; +import { normalizeType, buildBoolArray, sanitizeName } from '@kbn/interpreter/server'; export const essql = () => ({ name: 'essql', diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/index.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/index.js index 03402d36709a9..affb0b88ec7a0 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/index.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/index.js @@ -6,10 +6,8 @@ import { demodata } from './demodata'; import { escount } from './escount'; -import { esdocs } from './esdocs'; import { pointseries } from './pointseries'; import { server } from './server'; -import { timelion } from './timelion'; import { essql } from './essql'; -export const functions = [demodata, esdocs, escount, essql, pointseries, server, timelion]; +export const functions = [demodata, escount, essql, pointseries, server]; diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/pointseries/index.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/pointseries/index.js index e18d8cf1c5fda..11e741c8f715d 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/pointseries/index.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/server/src/pointseries/index.js @@ -8,7 +8,7 @@ import uniqBy from 'lodash.uniqby'; import { evaluate } from 'tinymath'; import { groupBy, zipObject, omit, values } from 'lodash'; import moment from 'moment'; -import { pivotObjectArray } from '../../../../../common/lib/pivot_object_array'; +import { pivotObjectArray } from '@kbn/interpreter/common/lib/pivot_object_array'; import { unquoteString } from '../../../../../common/lib/unquote_string'; import { isColumnReference } from './lib/is_column_reference'; import { getExpressionType } from './lib/get_expression_type'; diff --git a/x-pack/plugins/canvas/canvas_plugin_src/renderers/dropdown_filter/index.js b/x-pack/plugins/canvas/canvas_plugin_src/renderers/dropdown_filter/index.js index caef38781bdb6..382e0ff1b73c6 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/renderers/dropdown_filter/index.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/renderers/dropdown_filter/index.js @@ -7,7 +7,7 @@ import ReactDOM from 'react-dom'; import React from 'react'; import { get } from 'lodash'; -import { fromExpression, toExpression } from '../../../common/lib/ast'; +import { fromExpression, toExpression } from '@kbn/interpreter/common/lib/ast'; import { DropdownFilter } from './component'; export const dropdownFilter = () => ({ diff --git a/x-pack/plugins/canvas/canvas_plugin_src/renderers/time_filter/components/time_filter/time_filter.js b/x-pack/plugins/canvas/canvas_plugin_src/renderers/time_filter/components/time_filter/time_filter.js index d04a8f0b54d96..7ace73bc430d5 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/renderers/time_filter/components/time_filter/time_filter.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/renderers/time_filter/components/time_filter/time_filter.js @@ -7,7 +7,7 @@ import React from 'react'; import PropTypes from 'prop-types'; import { get } from 'lodash'; -import { fromExpression } from '../../../../../common/lib/ast'; +import { fromExpression } from '@kbn/interpreter/common/lib/ast'; import { TimePicker } from '../time_picker'; import { TimePickerMini } from '../time_picker_mini'; diff --git a/x-pack/plugins/canvas/canvas_plugin_src/renderers/time_filter/index.js b/x-pack/plugins/canvas/canvas_plugin_src/renderers/time_filter/index.js index d9d1ae3c2d754..5a0d16e81f23d 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/renderers/time_filter/index.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/renderers/time_filter/index.js @@ -7,7 +7,7 @@ import ReactDOM from 'react-dom'; import React from 'react'; import { get, set } from 'lodash'; -import { fromExpression, toExpression } from '../../../common/lib/ast'; +import { fromExpression, toExpression } from '@kbn/interpreter/common/lib/ast'; import { TimeFilter } from './components/time_filter'; export const timeFilter = () => ({ diff --git a/x-pack/plugins/canvas/canvas_plugin_src/types/boolean.js b/x-pack/plugins/canvas/canvas_plugin_src/types/boolean.js deleted file mode 100644 index 697277a471fea..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/types/boolean.js +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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. - */ - -export const boolean = () => ({ - name: 'boolean', - from: { - null: () => false, - number: n => Boolean(n), - string: s => Boolean(s), - }, - to: { - render: value => { - const text = `${value}`; - return { - type: 'render', - as: 'text', - value: { text }, - }; - }, - datatable: value => ({ - type: 'datatable', - columns: [{ name: 'value', type: 'boolean' }], - rows: [{ value }], - }), - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/types/error.js b/x-pack/plugins/canvas/canvas_plugin_src/types/error.js deleted file mode 100644 index 51051c804db56..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/types/error.js +++ /dev/null @@ -1,22 +0,0 @@ -/* - * 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. - */ - -export const error = () => ({ - name: 'error', - to: { - render: input => { - const { error, info } = input; - return { - type: 'render', - as: 'error', - value: { - error, - info, - }, - }; - }, - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/types/filter.js b/x-pack/plugins/canvas/canvas_plugin_src/types/filter.js deleted file mode 100644 index 8627dd20bb89f..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/types/filter.js +++ /dev/null @@ -1,20 +0,0 @@ -/* - * 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. - */ - -export const filter = () => ({ - name: 'filter', - from: { - null: () => { - return { - type: 'filter', - // Any meta data you wish to pass along. - meta: {}, - // And filters. If you need an "or", create a filter type for it. - and: [], - }; - }, - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/types/image.js b/x-pack/plugins/canvas/canvas_plugin_src/types/image.js deleted file mode 100644 index f63d3f1b8b2aa..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/types/image.js +++ /dev/null @@ -1,18 +0,0 @@ -/* - * 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. - */ - -export const image = () => ({ - name: 'image', - to: { - render: input => { - return { - type: 'render', - as: 'image', - value: input, - }; - }, - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/types/index.js b/x-pack/plugins/canvas/canvas_plugin_src/types/index.js deleted file mode 100644 index 2e9a4fa02ef8e..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/types/index.js +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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 { boolean } from './boolean'; -import { datatable } from './datatable'; -import { error } from './error'; -import { filter } from './filter'; -import { image } from './image'; -import { nullType } from './null'; -import { number } from './number'; -import { pointseries } from './pointseries'; -import { render } from './render'; -import { shape } from './shape'; -import { string } from './string'; -import { style } from './style'; - -export const typeSpecs = [ - boolean, - datatable, - error, - filter, - image, - number, - nullType, - pointseries, - render, - shape, - string, - style, -]; diff --git a/x-pack/plugins/canvas/canvas_plugin_src/types/null.js b/x-pack/plugins/canvas/canvas_plugin_src/types/null.js deleted file mode 100644 index 27e9cdf59b004..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/types/null.js +++ /dev/null @@ -1,12 +0,0 @@ -/* - * 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. - */ - -export const nullType = () => ({ - name: 'null', - from: { - '*': () => null, - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/types/number.js b/x-pack/plugins/canvas/canvas_plugin_src/types/number.js deleted file mode 100644 index 63ee587075fdd..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/types/number.js +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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. - */ - -export const number = () => ({ - name: 'number', - from: { - null: () => 0, - boolean: b => Number(b), - string: n => Number(n), - }, - to: { - render: value => { - const text = `${value}`; - return { - type: 'render', - as: 'text', - value: { text }, - }; - }, - datatable: value => ({ - type: 'datatable', - columns: [{ name: 'value', type: 'number' }], - rows: [{ value }], - }), - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/types/pointseries.js b/x-pack/plugins/canvas/canvas_plugin_src/types/pointseries.js deleted file mode 100644 index 1a00738620050..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/types/pointseries.js +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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. - */ - -export const pointseries = () => ({ - name: 'pointseries', - from: { - null: () => { - return { - type: 'pointseries', - rows: [], - columns: [], - }; - }, - }, - to: { - render: (pointseries, types) => { - const datatable = types.datatable.from(pointseries, types); - return { - type: 'render', - as: 'table', - value: { - datatable, - showHeader: true, - }, - }; - }, - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/types/register.js b/x-pack/plugins/canvas/canvas_plugin_src/types/register.js deleted file mode 100644 index d5c6e98288e89..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/types/register.js +++ /dev/null @@ -1,9 +0,0 @@ -/* - * 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 { typeSpecs } from './index'; - -typeSpecs.forEach(canvas.register); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/types/render.js b/x-pack/plugins/canvas/canvas_plugin_src/types/render.js deleted file mode 100644 index 0f261f0398816..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/types/render.js +++ /dev/null @@ -1,16 +0,0 @@ -/* - * 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. - */ - -export const render = () => ({ - name: 'render', - from: { - '*': v => ({ - type: 'render', - as: 'debug', - value: v, - }), - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/types/shape.js b/x-pack/plugins/canvas/canvas_plugin_src/types/shape.js deleted file mode 100644 index 1b306b7b1c391..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/types/shape.js +++ /dev/null @@ -1,18 +0,0 @@ -/* - * 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. - */ - -export const shape = () => ({ - name: 'shape', - to: { - render: input => { - return { - type: 'render', - as: 'shape', - value: input, - }; - }, - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/types/string.js b/x-pack/plugins/canvas/canvas_plugin_src/types/string.js deleted file mode 100644 index c8d58aaaffbca..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/types/string.js +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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. - */ - -export const string = () => ({ - name: 'string', - from: { - null: () => '', - boolean: b => String(b), - number: n => String(n), - }, - to: { - render: text => { - return { - type: 'render', - as: 'text', - value: { text }, - }; - }, - datatable: value => ({ - type: 'datatable', - columns: [{ name: 'value', type: 'string' }], - rows: [{ value }], - }), - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/types/style.js b/x-pack/plugins/canvas/canvas_plugin_src/types/style.js deleted file mode 100644 index 62632c03231ad..0000000000000 --- a/x-pack/plugins/canvas/canvas_plugin_src/types/style.js +++ /dev/null @@ -1,18 +0,0 @@ -/* - * 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. - */ - -export const style = () => ({ - name: 'style', - from: { - null: () => { - return { - type: 'style', - spec: {}, - css: '', - }; - }, - }, -}); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/datacolumn/index.js b/x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/datacolumn/index.js index ded0600cdb3ff..69e68db2120c6 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/datacolumn/index.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/datacolumn/index.js @@ -9,8 +9,8 @@ import { compose, withPropsOnChange, withHandlers } from 'recompose'; import PropTypes from 'prop-types'; import { EuiSelect, EuiFlexItem, EuiFlexGroup } from '@elastic/eui'; import { sortBy } from 'lodash'; +import { getType } from '@kbn/interpreter/common/lib/get_type'; import { createStatefulPropHoc } from '../../../../public/components/enhance/stateful_prop'; -import { getType } from '../../../../common/lib/get_type'; import { templateFromReactComponent } from '../../../../public/lib/template_from_react_component'; import { SimpleMathFunction } from './simple_math_function'; import { getFormObject } from './get_form_object'; diff --git a/x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/palette.js b/x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/palette.js index 61c5208b108f5..077d594de9eae 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/palette.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/palette.js @@ -7,8 +7,8 @@ import React from 'react'; import PropTypes from 'prop-types'; import { get } from 'lodash'; +import { getType } from '@kbn/interpreter/common/lib/get_type'; import { PalettePicker } from '../../../public/components/palette_picker'; -import { getType } from '../../../common/lib/get_type'; import { templateFromReactComponent } from '../../../public/lib/template_from_react_component'; const PaletteArgInput = ({ onValueChange, argValue, renderError }) => { diff --git a/x-pack/plugins/canvas/canvas_plugin_src/uis/views/metric.js b/x-pack/plugins/canvas/canvas_plugin_src/uis/views/metric.js index e03f4d5359a6b..abd6635044842 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/uis/views/metric.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/uis/views/metric.js @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { openSans } from '../../../common/lib/fonts'; +import { openSans } from '@kbn/interpreter/common/lib/fonts'; export const metric = () => ({ name: 'metric', diff --git a/x-pack/plugins/canvas/canvas_plugin_src/uis/views/progress.js b/x-pack/plugins/canvas/canvas_plugin_src/uis/views/progress.js index 4c9a885fe12cc..037c9a55efdea 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/uis/views/progress.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/uis/views/progress.js @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { openSans } from '../../../common/lib/fonts'; +import { openSans } from '@kbn/interpreter/common/lib/fonts'; import { shapes } from '../../renderers/progress/shapes'; export const progress = () => ({ diff --git a/x-pack/plugins/canvas/common/functions/index.js b/x-pack/plugins/canvas/common/functions/index.js index 00e60b13b0b6a..d476c11b3e16b 100644 --- a/x-pack/plugins/canvas/common/functions/index.js +++ b/x-pack/plugins/canvas/common/functions/index.js @@ -4,6 +4,4 @@ * you may not use this file except in compliance with the Elastic License. */ -import { to } from './to'; - -export const commonFunctions = [to]; +export const commonFunctions = []; diff --git a/x-pack/plugins/canvas/common/functions/to.js b/x-pack/plugins/canvas/common/functions/to.js deleted file mode 100644 index 142ffe490ca38..0000000000000 --- a/x-pack/plugins/canvas/common/functions/to.js +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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 { castProvider } from '../interpreter/cast'; - -export const to = () => ({ - name: 'to', - aliases: [], - help: 'Explicitly cast from one type to another.', - context: {}, - args: { - type: { - types: ['string'], - help: 'A known type', - aliases: ['_'], - multi: true, - }, - }, - fn: (context, args, { types }) => { - if (!args.type) throw new Error('Must specify a casting type'); - - return castProvider(types)(context, args.type); - }, -}); diff --git a/x-pack/plugins/canvas/common/lib/__tests__/arg.js b/x-pack/plugins/canvas/common/lib/__tests__/arg.js deleted file mode 100644 index f8badc67175ac..0000000000000 --- a/x-pack/plugins/canvas/common/lib/__tests__/arg.js +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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 expect from 'expect.js'; -import { Arg } from '../arg'; - -describe('Arg', () => { - it('sets required to false by default', () => { - const isOptional = new Arg({ - name: 'optional_me', - }); - expect(isOptional.required).to.equal(false); - - const isRequired = new Arg({ - name: 'require_me', - required: true, - }); - expect(isRequired.required).to.equal(true); - }); -}); diff --git a/x-pack/plugins/canvas/common/lib/__tests__/get_colors_from_palette.js b/x-pack/plugins/canvas/common/lib/__tests__/get_colors_from_palette.js index 79824edc2e2f7..a22358e17eae2 100644 --- a/x-pack/plugins/canvas/common/lib/__tests__/get_colors_from_palette.js +++ b/x-pack/plugins/canvas/common/lib/__tests__/get_colors_from_palette.js @@ -5,11 +5,8 @@ */ import expect from 'expect.js'; +import { grayscalePalette, gradientPalette } from '@kbn/interpreter/test_utils'; import { getColorsFromPalette } from '../../lib/get_colors_from_palette'; -import { - grayscalePalette, - gradientPalette, -} from '../../../canvas_plugin_src/functions/common/__tests__/fixtures/test_styles'; describe('getColorsFromPalette', () => { it('returns the array of colors from a palette object when gradient is false', () => { diff --git a/x-pack/plugins/canvas/common/lib/__tests__/get_field_type.js b/x-pack/plugins/canvas/common/lib/__tests__/get_field_type.js index cb8f7f68a796c..3f00803585161 100644 --- a/x-pack/plugins/canvas/common/lib/__tests__/get_field_type.js +++ b/x-pack/plugins/canvas/common/lib/__tests__/get_field_type.js @@ -5,11 +5,8 @@ */ import expect from 'expect.js'; +import { emptyTable, testTable } from '@kbn/interpreter/test_utils'; import { getFieldType } from '../get_field_type'; -import { - emptyTable, - testTable, -} from '../../../canvas_plugin_src/functions/common/__tests__/fixtures/test_tables'; describe('getFieldType', () => { it('returns type of a field in a datatable', () => { diff --git a/x-pack/plugins/canvas/common/lib/arg.js b/x-pack/plugins/canvas/common/lib/arg.js deleted file mode 100644 index d220e30d23237..0000000000000 --- a/x-pack/plugins/canvas/common/lib/arg.js +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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 { includes } from 'lodash'; - -export function Arg(config) { - if (config.name === '_') throw Error('Arg names must not be _. Use it in aliases instead.'); - this.name = config.name; - this.required = config.required || false; - this.help = config.help; - this.types = config.types || []; - this.default = config.default; - this.aliases = config.aliases || []; - this.multi = config.multi == null ? false : config.multi; - this.resolve = config.resolve == null ? true : config.resolve; - this.accepts = type => { - if (!this.types.length) return true; - return includes(config.types, type); - }; -} diff --git a/x-pack/plugins/canvas/common/lib/fetch.js b/x-pack/plugins/canvas/common/lib/fetch.js deleted file mode 100644 index 8af72b9aef25b..0000000000000 --- a/x-pack/plugins/canvas/common/lib/fetch.js +++ /dev/null @@ -1,17 +0,0 @@ -/* - * 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 axios from 'axios'; -import { FETCH_TIMEOUT } from './constants'; - -export const fetch = axios.create({ - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - 'kbn-xsrf': 'professionally-crafted-string-of-text', - }, - timeout: FETCH_TIMEOUT, -}); diff --git a/x-pack/plugins/canvas/common/lib/functions_registry.js b/x-pack/plugins/canvas/common/lib/functions_registry.js deleted file mode 100644 index af8e8f0b122d0..0000000000000 --- a/x-pack/plugins/canvas/common/lib/functions_registry.js +++ /dev/null @@ -1,16 +0,0 @@ -/* - * 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 { Registry } from '../../common/lib/registry'; -import { Fn } from '../lib/fn'; - -class FunctionsRegistry extends Registry { - wrapper(obj) { - return new Fn(obj); - } -} - -export const functionsRegistry = new FunctionsRegistry(); diff --git a/x-pack/plugins/canvas/common/lib/get_by_alias.js b/x-pack/plugins/canvas/common/lib/get_by_alias.js deleted file mode 100644 index ff705f07af516..0000000000000 --- a/x-pack/plugins/canvas/common/lib/get_by_alias.js +++ /dev/null @@ -1,21 +0,0 @@ -/* - * 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. - */ - -/** - * This is used for looking up function/argument definitions. It looks through - * the given object for a case-insensitive match, which could be either the - * name of the key itself, or something under the `aliases` property. - */ -export function getByAlias(specs, name) { - const lowerCaseName = name.toLowerCase(); - const key = Object.keys(specs).find(key => { - if (key.toLowerCase() === lowerCaseName) return true; - return (specs[key].aliases || []).some(alias => { - return alias.toLowerCase() === lowerCaseName; - }); - }); - if (typeof key !== undefined) return specs[key]; -} diff --git a/x-pack/plugins/canvas/common/lib/get_type.js b/x-pack/plugins/canvas/common/lib/get_type.js deleted file mode 100644 index 8d2b5a13cb283..0000000000000 --- a/x-pack/plugins/canvas/common/lib/get_type.js +++ /dev/null @@ -1,15 +0,0 @@ -/* - * 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. - */ - -export function getType(node) { - if (node == null) return 'null'; - if (typeof node === 'object') { - if (!node.type) throw new Error('Objects must have a type propery'); - return node.type; - } - - return typeof node; -} diff --git a/x-pack/plugins/canvas/common/lib/handlebars.js b/x-pack/plugins/canvas/common/lib/handlebars.js index ee1b7b40fde59..ea807165f3b2c 100644 --- a/x-pack/plugins/canvas/common/lib/handlebars.js +++ b/x-pack/plugins/canvas/common/lib/handlebars.js @@ -6,7 +6,7 @@ import Hbars from 'handlebars/dist/handlebars'; import { evaluate } from 'tinymath'; -import { pivotObjectArray } from './pivot_object_array'; +import { pivotObjectArray } from '@kbn/interpreter/common/lib/pivot_object_array'; // example use: {{math rows 'mean(price - cost)' 2}} Hbars.registerHelper('math', (rows, expression, precision) => { diff --git a/x-pack/plugins/canvas/common/lib/pivot_object_array.js b/x-pack/plugins/canvas/common/lib/pivot_object_array.js deleted file mode 100644 index 974e327a46e7b..0000000000000 --- a/x-pack/plugins/canvas/common/lib/pivot_object_array.js +++ /dev/null @@ -1,17 +0,0 @@ -/* - * 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 { map, zipObject } from 'lodash'; - -const isString = val => typeof val === 'string'; - -export function pivotObjectArray(rows, columns) { - const columnNames = columns || Object.keys(rows[0]); - if (!columnNames.every(isString)) throw new Error('Columns should be an array of strings'); - - const columnValues = map(columnNames, name => map(rows, name)); - return zipObject(columnNames, columnValues); -} diff --git a/x-pack/plugins/canvas/common/lib/serialize.js b/x-pack/plugins/canvas/common/lib/serialize.js deleted file mode 100644 index 0786f6f06b3a3..0000000000000 --- a/x-pack/plugins/canvas/common/lib/serialize.js +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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 { get, identity } from 'lodash'; -import { getType } from '../lib/get_type'; - -export function serializeProvider(types) { - return { - serialize: provider('serialize'), - deserialize: provider('deserialize'), - }; - - function provider(key) { - return context => { - const type = getType(context); - const typeDef = types[type]; - const fn = get(typeDef, key) || identity; - return fn(context); - }; - } -} diff --git a/x-pack/plugins/canvas/common/lib/types_registry.js b/x-pack/plugins/canvas/common/lib/types_registry.js deleted file mode 100644 index 3d2bb65e9fa0f..0000000000000 --- a/x-pack/plugins/canvas/common/lib/types_registry.js +++ /dev/null @@ -1,16 +0,0 @@ -/* - * 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 { Registry } from '../../common/lib/registry'; -import { Type } from '../../common/lib/type'; - -class TypesRegistry extends Registry { - wrapper(obj) { - return new Type(obj); - } -} - -export const typesRegistry = new TypesRegistry(); diff --git a/x-pack/plugins/canvas/index.js b/x-pack/plugins/canvas/index.js index f9353180518ac..dc7abea4976b6 100644 --- a/x-pack/plugins/canvas/index.js +++ b/x-pack/plugins/canvas/index.js @@ -29,6 +29,7 @@ export function canvas(kibana) { 'plugins/canvas/lib/window_error_handler.js', // Client side plugins go here + 'plugins/canvas/lib/load', 'plugins/canvas/lib/load_expression_types.js', 'plugins/canvas/lib/load_transitions.js', ], diff --git a/x-pack/plugins/canvas/init.js b/x-pack/plugins/canvas/init.js index bc41ff43c8ebb..d03296ef0b9be 100644 --- a/x-pack/plugins/canvas/init.js +++ b/x-pack/plugins/canvas/init.js @@ -4,11 +4,13 @@ * you may not use this file except in compliance with the Elastic License. */ +import { functionsRegistry } from '@kbn/interpreter/common/lib/functions_registry'; +import { pathsRegistry } from '@kbn/interpreter/common/lib/paths_registry'; +import { loadServerPlugins } from '@kbn/interpreter/common/lib/load_server_plugins'; import { routes } from './server/routes'; -import { functionsRegistry } from './common/lib/functions_registry'; import { commonFunctions } from './common/functions'; -import { loadServerPlugins } from './server/lib/load_server_plugins'; import { registerCanvasUsageCollector } from './server/usage'; +import { pluginPaths } from './plugin_paths'; export default function(server /*options*/) { server.injectUiAppVars('canvas', () => { @@ -29,6 +31,7 @@ export default function(server /*options*/) { // There are some common functions that use private APIs, load them here commonFunctions.forEach(func => functionsRegistry.register(func)); + pathsRegistry.registerAll(pluginPaths); loadServerPlugins().then(() => routes(server)); registerCanvasUsageCollector(server); } diff --git a/x-pack/plugins/canvas/plugin_paths.js b/x-pack/plugins/canvas/plugin_paths.js new file mode 100644 index 0000000000000..51d294ee7e36e --- /dev/null +++ b/x-pack/plugins/canvas/plugin_paths.js @@ -0,0 +1,20 @@ +/* + * 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 { resolve } from 'path'; + +export const pluginPaths = { + serverFunctions: resolve(__dirname, 'canvas_plugin/functions/server'), + browserFunctions: resolve(__dirname, 'canvas_plugin/functions/browser'), + commonFunctions: resolve(__dirname, 'canvas_plugin/functions/common'), + elements: resolve(__dirname, 'canvas_plugin/elements'), + renderers: resolve(__dirname, 'canvas_plugin/renderers'), + interfaces: resolve(__dirname, 'canvas_plugin/interfaces'), + transformUIs: resolve(__dirname, 'canvas_plugin/uis/transforms'), + datasourceUIs: resolve(__dirname, 'canvas_plugin/uis/datasources'), + modelUIs: resolve(__dirname, 'canvas_plugin/uis/models'), + viewUIs: resolve(__dirname, 'canvas_plugin/uis/views'), + argumentUIs: resolve(__dirname, 'canvas_plugin/uis/arguments'), +}; diff --git a/x-pack/plugins/canvas/public/angular/services/store.js b/x-pack/plugins/canvas/public/angular/services/store.js index 181f9bf75cbb0..a608276932a1e 100644 --- a/x-pack/plugins/canvas/public/angular/services/store.js +++ b/x-pack/plugins/canvas/public/angular/services/store.js @@ -6,9 +6,9 @@ import { uiModules } from 'ui/modules'; import uniqBy from 'lodash.uniqby'; +import { functionsRegistry } from '@kbn/interpreter/common/lib/functions_registry'; import { createStore } from '../../state/store'; import { getInitialState } from '../../state/initial_state'; -import { functionsRegistry } from '../../../common/lib/functions_registry'; const app = uiModules.get('apps/canvas'); app.service('canvasStore', (kbnVersion, basePath, reportingBrowserType, serverFunctions) => { diff --git a/x-pack/plugins/canvas/public/components/arg_form/advanced_failure.js b/x-pack/plugins/canvas/public/components/arg_form/advanced_failure.js index 2bd779de759d9..13ecfe89bb922 100644 --- a/x-pack/plugins/canvas/public/components/arg_form/advanced_failure.js +++ b/x-pack/plugins/canvas/public/components/arg_form/advanced_failure.js @@ -8,8 +8,8 @@ import React from 'react'; import PropTypes from 'prop-types'; import { compose, withProps, withPropsOnChange } from 'recompose'; import { EuiForm, EuiTextArea, EuiButton, EuiButtonEmpty, EuiFormRow } from '@elastic/eui'; +import { fromExpression, toExpression } from '@kbn/interpreter/common/lib/ast'; import { createStatefulPropHoc } from '../../components/enhance/stateful_prop'; -import { fromExpression, toExpression } from '../../../common/lib/ast'; export const AdvancedFailureComponent = props => { const { diff --git a/x-pack/plugins/canvas/public/components/datasource/datasource_preview/index.js b/x-pack/plugins/canvas/public/components/datasource/datasource_preview/index.js index 8b21c38a5f6f7..0e696aafbe5bc 100644 --- a/x-pack/plugins/canvas/public/components/datasource/datasource_preview/index.js +++ b/x-pack/plugins/canvas/public/components/datasource/datasource_preview/index.js @@ -4,10 +4,10 @@ * you may not use this file except in compliance with the Elastic License. */ +import { interpretAst } from 'plugins/interpreter/interpreter'; import { pure, compose, lifecycle, withState, branch, renderComponent } from 'recompose'; import { PropTypes } from 'prop-types'; import { Loading } from '../../loading'; -import { interpretAst } from '../../../lib/interpreter'; import { DatasourcePreview as Component } from './datasource_preview'; export const DatasourcePreview = compose( diff --git a/x-pack/plugins/canvas/public/components/element_content/element_content.js b/x-pack/plugins/canvas/public/components/element_content/element_content.js index 8eba2aa2ba438..efb856795ee4e 100644 --- a/x-pack/plugins/canvas/public/components/element_content/element_content.js +++ b/x-pack/plugins/canvas/public/components/element_content/element_content.js @@ -8,7 +8,7 @@ import React from 'react'; import PropTypes from 'prop-types'; import { pure, compose, branch, renderComponent } from 'recompose'; import Style from 'style-it'; -import { getType } from '../../../common/lib/get_type'; +import { getType } from '@kbn/interpreter/common/lib/get_type'; import { Loading } from '../loading'; import { RenderWithFn } from '../render_with_fn'; import { ElementShareContainer } from '../element_share_container'; diff --git a/x-pack/plugins/canvas/public/components/element_content/index.js b/x-pack/plugins/canvas/public/components/element_content/index.js index 969e96a994a3b..c4fed00acee41 100644 --- a/x-pack/plugins/canvas/public/components/element_content/index.js +++ b/x-pack/plugins/canvas/public/components/element_content/index.js @@ -7,7 +7,7 @@ import PropTypes from 'prop-types'; import { compose, withProps } from 'recompose'; import { get } from 'lodash'; -import { renderFunctionsRegistry } from '../../lib/render_functions_registry'; +import { renderFunctionsRegistry } from '@kbn/interpreter/public'; import { ElementContent as Component } from './element_content'; export const ElementContent = compose( diff --git a/x-pack/plugins/canvas/public/components/expression/index.js b/x-pack/plugins/canvas/public/components/expression/index.js index d5f706765380f..ae6c502124916 100644 --- a/x-pack/plugins/canvas/public/components/expression/index.js +++ b/x-pack/plugins/canvas/public/components/expression/index.js @@ -14,9 +14,9 @@ import { branch, renderComponent, } from 'recompose'; +import { fromExpression } from '@kbn/interpreter/common/lib/ast'; import { getSelectedPage, getSelectedElement } from '../../state/selectors/workpad'; import { setExpression, flushContext } from '../../state/actions/elements'; -import { fromExpression } from '../../../common/lib/ast'; import { ElementNotSelected } from './element_not_selected'; import { Expression as Component } from './expression'; diff --git a/x-pack/plugins/canvas/public/components/font_picker/font_picker.js b/x-pack/plugins/canvas/public/components/font_picker/font_picker.js index 64ebbe658697b..9f85fa7a26505 100644 --- a/x-pack/plugins/canvas/public/components/font_picker/font_picker.js +++ b/x-pack/plugins/canvas/public/components/font_picker/font_picker.js @@ -7,7 +7,7 @@ import React from 'react'; import PropTypes from 'prop-types'; import { EuiSuperSelect } from '@elastic/eui'; -import { fonts } from '../../../common/lib/fonts'; +import { fonts } from '@kbn/interpreter/common/lib/fonts'; export const FontPicker = ({ onSelect, value }) => ( ({ name: 'filters', diff --git a/x-pack/plugins/canvas/public/lib/arg_helpers.js b/x-pack/plugins/canvas/public/lib/arg_helpers.js index e53e26b62dd15..e1cd8b64b323f 100644 --- a/x-pack/plugins/canvas/public/lib/arg_helpers.js +++ b/x-pack/plugins/canvas/public/lib/arg_helpers.js @@ -5,7 +5,7 @@ */ import { includes } from 'lodash'; -import { getType } from '../../common/lib/get_type'; +import { getType } from '@kbn/interpreter/common/lib/get_type'; /* diff --git a/x-pack/plugins/canvas/public/lib/create_handlers.js b/x-pack/plugins/canvas/public/lib/create_handlers.js deleted file mode 100644 index 93247210eb291..0000000000000 --- a/x-pack/plugins/canvas/public/lib/create_handlers.js +++ /dev/null @@ -1,11 +0,0 @@ -/* - * 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. - */ - -export function createHandlers(/*socket*/) { - return { - environment: 'client', - }; -} diff --git a/x-pack/plugins/canvas/public/lib/elements_registry.js b/x-pack/plugins/canvas/public/lib/elements_registry.js index 898fba183c9f5..dc3d743f49877 100644 --- a/x-pack/plugins/canvas/public/lib/elements_registry.js +++ b/x-pack/plugins/canvas/public/lib/elements_registry.js @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Registry } from '../../common/lib/registry'; +import { Registry } from '@kbn/interpreter/common/lib/registry'; import { Element } from './element'; class ElementsRegistry extends Registry { diff --git a/x-pack/plugins/canvas/public/lib/es_service.js b/x-pack/plugins/canvas/public/lib/es_service.js index 879bf69624819..0890a9688ad2e 100644 --- a/x-pack/plugins/canvas/public/lib/es_service.js +++ b/x-pack/plugins/canvas/public/lib/es_service.js @@ -5,8 +5,8 @@ */ import chrome from 'ui/chrome'; +import { fetch } from '@kbn/interpreter/common/lib/fetch'; import { API_ROUTE } from '../../common/lib/constants'; -import { fetch } from '../../common/lib/fetch'; import { notify } from './notify'; const basePath = chrome.getBasePath(); diff --git a/x-pack/plugins/canvas/public/lib/functions_registry.js b/x-pack/plugins/canvas/public/lib/functions_registry.js index 3cc084d8ca66e..36f9a631f06ea 100644 --- a/x-pack/plugins/canvas/public/lib/functions_registry.js +++ b/x-pack/plugins/canvas/public/lib/functions_registry.js @@ -5,4 +5,4 @@ */ // export the common registry here, so it's available in plugin public code -export { functionsRegistry } from '../../common/lib/functions_registry'; +export { functionsRegistry } from '@kbn/interpreter/common/lib/functions_registry'; diff --git a/x-pack/plugins/canvas/public/lib/interpreter.js b/x-pack/plugins/canvas/public/lib/interpreter.js deleted file mode 100644 index 48491be48ef67..0000000000000 --- a/x-pack/plugins/canvas/public/lib/interpreter.js +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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 { socketInterpreterProvider } from '../../common/interpreter/socket_interpret'; -import { serializeProvider } from '../../common/lib/serialize'; -import { socket } from '../socket'; -import { typesRegistry } from '../../common/lib/types_registry'; -import { createHandlers } from './create_handlers'; -import { functionsRegistry } from './functions_registry'; -import { loadBrowserPlugins } from './load_browser_plugins'; - -// Create the function list -socket.emit('getFunctionList'); -export const getServerFunctions = new Promise(resolve => socket.once('functionList', resolve)); - -// Use the above promise to seed the interpreter with the functions it can defer to -export function interpretAst(ast, context) { - // Load plugins before attempting to get functions, otherwise this gets racey - return Promise.all([getServerFunctions, loadBrowserPlugins()]) - .then(([serverFunctionList]) => { - return socketInterpreterProvider({ - types: typesRegistry.toJS(), - handlers: createHandlers(socket), - functions: functionsRegistry.toJS(), - referableFunctions: serverFunctionList, - socket: socket, - }); - }) - .then(interpretFn => interpretFn(ast, context)); -} - -socket.on('run', ({ ast, context, id }) => { - const types = typesRegistry.toJS(); - const { serialize, deserialize } = serializeProvider(types); - interpretAst(ast, deserialize(context)).then(value => { - socket.emit(`resp:${id}`, { value: serialize(value) }); - }); -}); diff --git a/x-pack/plugins/canvas/public/lib/load.js b/x-pack/plugins/canvas/public/lib/load.js new file mode 100644 index 0000000000000..92e51a5cc6272 --- /dev/null +++ b/x-pack/plugins/canvas/public/lib/load.js @@ -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; + * you may not use this file except in compliance with the Elastic License. + */ + +import { pluginsLoadingState } from 'plugins/interpreter/plugins_loading_state'; +import { loadBrowserPlugins } from './load_browser_plugins'; + +const identifier = pluginsLoadingState.setLoading(); +loadBrowserPlugins().then(() => { + pluginsLoadingState.setComplete(identifier); +}); diff --git a/x-pack/plugins/canvas/public/lib/load_browser_plugins.js b/x-pack/plugins/canvas/public/lib/load_browser_plugins.js index 8f1f5b2e90894..9f9d2790ab972 100644 --- a/x-pack/plugins/canvas/public/lib/load_browser_plugins.js +++ b/x-pack/plugins/canvas/public/lib/load_browser_plugins.js @@ -4,9 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ -import chrome from 'ui/chrome'; -import $script from 'scriptjs'; -import { typesRegistry } from '../../common/lib/types_registry'; +import { renderFunctionsRegistry } from '@kbn/interpreter/public'; +import { loadBrowserPlugins as loadCorePlugins } from '@kbn/interpreter/public/load_browser_plugins'; import { argTypeRegistry, datasourceRegistry, @@ -15,15 +14,10 @@ import { viewRegistry, } from '../expression_types'; import { elementsRegistry } from './elements_registry'; -import { renderFunctionsRegistry } from './render_functions_registry'; -import { functionsRegistry as browserFunctions } from './functions_registry'; import { loadPrivateBrowserFunctions } from './load_private_browser_functions'; const types = { - browserFunctions: browserFunctions, - commonFunctions: browserFunctions, elements: elementsRegistry, - types: typesRegistry, renderers: renderFunctionsRegistry, transformUIs: transformRegistry, datasourceUIs: datasourceRegistry, @@ -35,19 +29,5 @@ const types = { export const loadBrowserPlugins = () => new Promise(resolve => { loadPrivateBrowserFunctions(); - const remainingTypes = Object.keys(types); - function loadType() { - const type = remainingTypes.pop(); - window.canvas = window.canvas || {}; - window.canvas.register = d => types[type].register(d); - // Load plugins one at a time because each needs a different loader function - // $script will only load each of these once, we so can call this as many times as we need? - const pluginPath = chrome.addBasePath(`/api/canvas/plugins?type=${type}`); - $script(pluginPath, () => { - if (remainingTypes.length) loadType(); - else resolve(true); - }); - } - - loadType(); + loadCorePlugins(types).then(resolve); }); diff --git a/x-pack/plugins/canvas/public/lib/load_private_browser_functions.js b/x-pack/plugins/canvas/public/lib/load_private_browser_functions.js index 85b995c026a19..fe5e9db519d6c 100644 --- a/x-pack/plugins/canvas/public/lib/load_private_browser_functions.js +++ b/x-pack/plugins/canvas/public/lib/load_private_browser_functions.js @@ -4,9 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ +import { functionsRegistry } from '@kbn/interpreter/common/lib/functions_registry'; import { commonFunctions } from '../../common/functions'; import { clientFunctions } from '../functions'; -import { functionsRegistry } from './functions_registry'; /* Functions loaded here use PRIVATE APIs diff --git a/x-pack/plugins/canvas/public/lib/parse_single_function_chain.js b/x-pack/plugins/canvas/public/lib/parse_single_function_chain.js index f8eec880af624..696c058e34a2b 100644 --- a/x-pack/plugins/canvas/public/lib/parse_single_function_chain.js +++ b/x-pack/plugins/canvas/public/lib/parse_single_function_chain.js @@ -5,7 +5,7 @@ */ import { get, mapValues, map } from 'lodash'; -import { fromExpression } from '../../common/lib/ast'; +import { fromExpression } from '@kbn/interpreter/common/lib/ast'; export function parseSingleFunctionChain(filterString) { const ast = fromExpression(filterString); diff --git a/x-pack/plugins/canvas/public/lib/render_function.js b/x-pack/plugins/canvas/public/lib/render_function.js deleted file mode 100644 index 68a6d8485db83..0000000000000 --- a/x-pack/plugins/canvas/public/lib/render_function.js +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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. - */ - -export function RenderFunction(config) { - // This must match the name of the function that is used to create the `type: render` object - this.name = config.name; - - // Use this to set a more friendly name - this.displayName = config.displayName || this.name; - - // A sentence or few about what this element does - this.help = config.help; - - // used to validate the data before calling the render function - this.validate = config.validate || function validate() {}; - - // tell the renderer if the dom node should be reused, it's recreated each time by default - this.reuseDomNode = Boolean(config.reuseDomNode); - - // the function called to render the data - this.render = - config.render || - function render(domNode, data, done) { - done(); - }; -} diff --git a/x-pack/plugins/canvas/public/lib/render_functions_registry.js b/x-pack/plugins/canvas/public/lib/render_functions_registry.js deleted file mode 100644 index 3d040047aeb9a..0000000000000 --- a/x-pack/plugins/canvas/public/lib/render_functions_registry.js +++ /dev/null @@ -1,16 +0,0 @@ -/* - * 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 { Registry } from '../../common/lib/registry'; -import { RenderFunction } from './render_function'; - -class RenderFunctionsRegistry extends Registry { - wrapper(obj) { - return new RenderFunction(obj); - } -} - -export const renderFunctionsRegistry = new RenderFunctionsRegistry(); diff --git a/x-pack/plugins/canvas/public/lib/run_interpreter.js b/x-pack/plugins/canvas/public/lib/run_interpreter.js index cc0d9a7544786..b56597eba42dc 100644 --- a/x-pack/plugins/canvas/public/lib/run_interpreter.js +++ b/x-pack/plugins/canvas/public/lib/run_interpreter.js @@ -4,9 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ -import { fromExpression } from '../../common/lib/ast'; -import { getType } from '../../common/lib/get_type'; -import { interpretAst } from './interpreter'; +import { interpretAst } from 'plugins/interpreter/interpreter'; +import { fromExpression } from '@kbn/interpreter/common/lib/ast'; +import { getType } from '@kbn/interpreter/common/lib/get_type'; import { notify } from './notify'; /** diff --git a/x-pack/plugins/canvas/public/lib/transitions_registry.js b/x-pack/plugins/canvas/public/lib/transitions_registry.js index 8d2e421b8233c..8ead0aa896ab7 100644 --- a/x-pack/plugins/canvas/public/lib/transitions_registry.js +++ b/x-pack/plugins/canvas/public/lib/transitions_registry.js @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Registry } from '../../common/lib/registry'; +import { Registry } from '@kbn/interpreter/common/lib/registry'; import { Transition } from '../transitions/transition'; class TransitionsRegistry extends Registry { diff --git a/x-pack/plugins/canvas/public/lib/types_registry.js b/x-pack/plugins/canvas/public/lib/types_registry.js index c1f13b1ae4612..05b82c744c383 100644 --- a/x-pack/plugins/canvas/public/lib/types_registry.js +++ b/x-pack/plugins/canvas/public/lib/types_registry.js @@ -5,4 +5,4 @@ */ // export the common registry here, so it's available in plugin public code -export { typesRegistry } from '../../common/lib/types_registry'; +export { typesRegistry } from '@kbn/interpreter/common/lib/types_registry'; diff --git a/x-pack/plugins/canvas/public/lib/workpad_service.js b/x-pack/plugins/canvas/public/lib/workpad_service.js index 78464a78cefc9..169a49b97340f 100644 --- a/x-pack/plugins/canvas/public/lib/workpad_service.js +++ b/x-pack/plugins/canvas/public/lib/workpad_service.js @@ -5,8 +5,8 @@ */ import chrome from 'ui/chrome'; +import { fetch } from '@kbn/interpreter/common/lib/fetch'; import { API_ROUTE_WORKPAD } from '../../common/lib/constants'; -import { fetch } from '../../common/lib/fetch'; const basePath = chrome.getBasePath(); const apiPath = `${basePath}${API_ROUTE_WORKPAD}`; diff --git a/x-pack/plugins/canvas/public/socket.js b/x-pack/plugins/canvas/public/socket.js deleted file mode 100644 index eb381f17d1f9b..0000000000000 --- a/x-pack/plugins/canvas/public/socket.js +++ /dev/null @@ -1,19 +0,0 @@ -/* - * 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 chrome from 'ui/chrome'; -import io from 'socket.io-client'; -import { functionsRegistry } from '../common/lib/functions_registry'; -import { loadBrowserPlugins } from './lib/load_browser_plugins'; - -const basePath = chrome.getBasePath(); -export const socket = io(undefined, { path: `${basePath}/socket.io` }); - -socket.on('getFunctionList', () => { - const pluginsLoaded = loadBrowserPlugins(); - - pluginsLoaded.then(() => socket.emit('functionList', functionsRegistry.toJS())); -}); diff --git a/x-pack/plugins/canvas/public/state/actions/elements.js b/x-pack/plugins/canvas/public/state/actions/elements.js index fb82de32fc0ef..53a693eddfcf8 100644 --- a/x-pack/plugins/canvas/public/state/actions/elements.js +++ b/x-pack/plugins/canvas/public/state/actions/elements.js @@ -4,17 +4,17 @@ * you may not use this file except in compliance with the Elastic License. */ +import { interpretAst } from 'plugins/interpreter/interpreter'; import { createAction } from 'redux-actions'; import { createThunk } from 'redux-thunks'; import { set, del } from 'object-path-immutable'; import { get, pick, cloneDeep, without } from 'lodash'; +import { toExpression, safeElementFromExpression } from '@kbn/interpreter/common/lib/ast'; import { getPages, getElementById, getSelectedPageIndex } from '../selectors/workpad'; import { getValue as getResolvedArgsValue } from '../selectors/resolved_args'; import { getDefaultElement } from '../defaults'; -import { toExpression, safeElementFromExpression } from '../../../common/lib/ast'; import { notify } from '../../lib/notify'; import { runInterpreter } from '../../lib/run_interpreter'; -import { interpretAst } from '../../lib/interpreter'; import { selectElement } from './transient'; import * as args from './resolved_args'; diff --git a/x-pack/plugins/canvas/public/state/selectors/workpad.js b/x-pack/plugins/canvas/public/state/selectors/workpad.js index 4dff29be51c01..4f71ad0e76c18 100644 --- a/x-pack/plugins/canvas/public/state/selectors/workpad.js +++ b/x-pack/plugins/canvas/public/state/selectors/workpad.js @@ -5,7 +5,7 @@ */ import { get, omit } from 'lodash'; -import { safeElementFromExpression } from '../../../common/lib/ast'; +import { safeElementFromExpression } from '@kbn/interpreter/common/lib/ast'; import { append } from '../../lib/modify_path'; import { getAssets } from './assets'; diff --git a/x-pack/plugins/canvas/server/lib/build_bool_array.js b/x-pack/plugins/canvas/server/lib/build_bool_array.js deleted file mode 100644 index 2dc6447753526..0000000000000 --- a/x-pack/plugins/canvas/server/lib/build_bool_array.js +++ /dev/null @@ -1,21 +0,0 @@ -/* - * 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 { getESFilter } from './get_es_filter'; - -const compact = arr => (Array.isArray(arr) ? arr.filter(val => Boolean(val)) : []); - -export function buildBoolArray(canvasQueryFilterArray) { - return compact( - canvasQueryFilterArray.map(clause => { - try { - return getESFilter(clause); - } catch (e) { - return; - } - }) - ); -} diff --git a/x-pack/plugins/canvas/server/lib/build_es_request.js b/x-pack/plugins/canvas/server/lib/build_es_request.js deleted file mode 100644 index 037138cc11576..0000000000000 --- a/x-pack/plugins/canvas/server/lib/build_es_request.js +++ /dev/null @@ -1,15 +0,0 @@ -/* - * 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 { buildBoolArray } from './build_bool_array'; - -export function buildESRequest(esRequest, canvasQuery) { - if (canvasQuery.size) esRequest = { ...esRequest, size: canvasQuery.size }; - - if (canvasQuery.and) esRequest.body.query.bool.must = buildBoolArray(canvasQuery.and); - - return esRequest; -} diff --git a/x-pack/plugins/canvas/server/lib/create_handlers.js b/x-pack/plugins/canvas/server/lib/create_handlers.js deleted file mode 100644 index 0825804960dbb..0000000000000 --- a/x-pack/plugins/canvas/server/lib/create_handlers.js +++ /dev/null @@ -1,22 +0,0 @@ -/* - * 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 { partial } from 'lodash'; - -export const createHandlers = (request, server) => { - const { callWithRequest } = server.plugins.elasticsearch.getCluster('data'); - const config = server.config(); - - return { - environment: 'server', - serverUri: - config.has('server.rewriteBasePath') && config.get('server.rewriteBasePath') - ? `${server.info.uri}${config.get('server.basePath')}` - : server.info.uri, - httpHeaders: request.headers, - elasticsearchClient: partial(callWithRequest, request), - }; -}; diff --git a/x-pack/plugins/canvas/server/lib/filters.js b/x-pack/plugins/canvas/server/lib/filters.js deleted file mode 100644 index 5e4c2c64845c6..0000000000000 --- a/x-pack/plugins/canvas/server/lib/filters.js +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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. - */ - -/* - TODO: This could be pluggable -*/ - -export function time(filter) { - if (!filter.column) throw new Error('column is required for Elasticsearch range filters'); - return { - range: { - [filter.column]: { gte: filter.from, lte: filter.to }, - }, - }; -} - -export function luceneQueryString(filter) { - return { - query_string: { - query: filter.query || '*', - }, - }; -} - -export function exactly(filter) { - return { - term: { - [filter.column]: { - value: filter.value, - }, - }, - }; -} diff --git a/x-pack/plugins/canvas/server/lib/get_es_filter.js b/x-pack/plugins/canvas/server/lib/get_es_filter.js deleted file mode 100644 index ccc296bc550f9..0000000000000 --- a/x-pack/plugins/canvas/server/lib/get_es_filter.js +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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. - */ - -/* - boolArray is the array of bool filter clauses to push filters into. Usually this would be - the value of must, should or must_not. - filter is the abstracted canvas filter. -*/ - -/*eslint import/namespace: ['error', { allowComputed: true }]*/ -import * as filters from './filters'; - -export function getESFilter(filter) { - if (!filters[filter.type]) throw new Error(`Unknown filter type: ${filter.type}`); - - try { - return filters[filter.type](filter); - } catch (e) { - throw new Error(`Could not create elasticsearch filter from ${filter.type}`); - } -} diff --git a/x-pack/plugins/canvas/server/lib/get_plugin_paths.js b/x-pack/plugins/canvas/server/lib/get_plugin_paths.js deleted file mode 100644 index 6ebb5574e57d5..0000000000000 --- a/x-pack/plugins/canvas/server/lib/get_plugin_paths.js +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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 path from 'path'; -import fs from 'fs'; -import { promisify } from 'util'; -import { flatten } from 'lodash'; -import { pluginPaths } from './plugin_paths'; - -const lstat = promisify(fs.lstat); -const readdir = promisify(fs.readdir); - -const kibanaPluginPath = path.resolve(__dirname, '..', '..', '..'); -const canvasPluginDirectoryName = 'canvas_plugin'; - -export const getPluginPaths = type => { - const typePath = pluginPaths[type]; - if (!typePath) throw new Error(`Unknown type: ${type}`); - - return readdir(kibanaPluginPath) // Get names of everything in Kibana plugin path - .then(names => names.filter(name => name[0] !== '.')) // Filter out names that start with . - .then(names => { - // Get full paths to stuff that might have a canvas plugin of the provided type - return names.map(name => - path.resolve(kibanaPluginPath, name, canvasPluginDirectoryName, ...typePath) - ); - }) - .then(possibleCanvasPlugins => { - // Check how many are directories. If lstat fails it doesn't exist anyway. - return Promise.all( - // An array - possibleCanvasPlugins.map( - pluginPath => - lstat(pluginPath) - .then(stat => stat.isDirectory()) // Exists and is a directory - .catch(() => false) // I guess it doesn't exist, so whaevs - ) - ).then(isDirectory => possibleCanvasPlugins.filter((pluginPath, i) => isDirectory[i])); - }) - .then(canvasPluginDirectories => { - return Promise.all( - canvasPluginDirectories.map(dir => - // Get the full path of all files in the directory - readdir(dir).then(files => files.map(file => path.resolve(dir, file))) - ) - ).then(flatten); - }); -}; diff --git a/x-pack/plugins/canvas/server/lib/get_plugin_stream.js b/x-pack/plugins/canvas/server/lib/get_plugin_stream.js deleted file mode 100644 index 51f3d234afdb1..0000000000000 --- a/x-pack/plugins/canvas/server/lib/get_plugin_stream.js +++ /dev/null @@ -1,22 +0,0 @@ -/* - * 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 fs from 'fs'; -import ss from 'stream-stream'; -import { getPluginPaths } from './get_plugin_paths'; - -export const getPluginStream = type => { - const stream = ss(); - - getPluginPaths(type).then(files => { - files.forEach(file => { - stream.write(fs.createReadStream(file)); - }); - stream.end(); - }); - - return stream; -}; diff --git a/x-pack/plugins/canvas/server/lib/load_server_plugins.js b/x-pack/plugins/canvas/server/lib/load_server_plugins.js deleted file mode 100644 index 0373261e96067..0000000000000 --- a/x-pack/plugins/canvas/server/lib/load_server_plugins.js +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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 { typesRegistry } from '../../common/lib/types_registry'; -import { functionsRegistry as serverFunctions } from '../../common/lib/functions_registry'; -import { getPluginPaths } from './get_plugin_paths'; - -const types = { - serverFunctions: serverFunctions, - commonFunctions: serverFunctions, - types: typesRegistry, -}; - -const loaded = new Promise(resolve => { - const remainingTypes = Object.keys(types); - - const loadType = () => { - const type = remainingTypes.pop(); - getPluginPaths(type).then(paths => { - global.canvas = global.canvas || {}; - global.canvas.register = d => types[type].register(d); - - paths.forEach(path => { - require(path); - }); - - global.canvas = undefined; - if (remainingTypes.length) loadType(); - else resolve(true); - }); - }; - - loadType(); -}); - -export const loadServerPlugins = () => loaded; diff --git a/x-pack/plugins/canvas/server/lib/normalize_type.js b/x-pack/plugins/canvas/server/lib/normalize_type.js deleted file mode 100644 index 7dd20b94096ba..0000000000000 --- a/x-pack/plugins/canvas/server/lib/normalize_type.js +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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. - */ - -export function normalizeType(type) { - const normalTypes = { - string: ['string', 'text', 'keyword', '_type', '_id', '_index'], - number: [ - 'float', - 'half_float', - 'scaled_float', - 'double', - 'integer', - 'long', - 'short', - 'byte', - 'token_count', - '_version', - ], - date: ['date'], - boolean: ['boolean'], - null: ['null'], - }; - - const normalizedType = Object.keys(normalTypes).find(t => normalTypes[t].includes(type)); - - if (normalizedType) return normalizedType; - throw new Error(`Canvas does not yet support type: ${type}`); -} diff --git a/x-pack/plugins/canvas/server/lib/plugin_paths.js b/x-pack/plugins/canvas/server/lib/plugin_paths.js deleted file mode 100644 index cb90cc0c0f06c..0000000000000 --- a/x-pack/plugins/canvas/server/lib/plugin_paths.js +++ /dev/null @@ -1,20 +0,0 @@ -/* - * 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. - */ - -export const pluginPaths = { - serverFunctions: ['functions', 'server'], - browserFunctions: ['functions', 'browser'], - commonFunctions: ['functions', 'common'], - types: ['types'], - elements: ['elements'], - renderers: ['renderers'], - interfaces: ['interfaces'], - transformUIs: ['uis', 'transforms'], - datasourceUIs: ['uis', 'datasources'], - modelUIs: ['uis', 'models'], - viewUIs: ['uis', 'views'], - argumentUIs: ['uis', 'arguments'], -}; diff --git a/x-pack/plugins/canvas/server/lib/sanitize_name.js b/x-pack/plugins/canvas/server/lib/sanitize_name.js deleted file mode 100644 index 623b64ca04ae8..0000000000000 --- a/x-pack/plugins/canvas/server/lib/sanitize_name.js +++ /dev/null @@ -1,17 +0,0 @@ -/* - * 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. - */ - -export function sanitizeName(name) { - // blacklisted characters - const blacklist = ['(', ')']; - const pattern = blacklist.map(v => escapeRegExp(v)).join('|'); - const regex = new RegExp(pattern, 'g'); - return name.replace(regex, '_'); -} - -function escapeRegExp(string) { - return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); -} diff --git a/x-pack/plugins/canvas/server/routes/es_fields/get_es_field_types.js b/x-pack/plugins/canvas/server/routes/es_fields/get_es_field_types.js deleted file mode 100644 index 8e454a595f99c..0000000000000 --- a/x-pack/plugins/canvas/server/routes/es_fields/get_es_field_types.js +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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 { mapValues, keys } from 'lodash'; -import { normalizeType } from '../../lib/normalize_type'; - -export function getESFieldTypes(index, fields, elasticsearchClient) { - const config = { - index: index, - fields: fields || '*', - }; - - if (fields && fields.length === 0) return Promise.resolve({}); - - return elasticsearchClient('fieldCaps', config).then(resp => { - return mapValues(resp.fields, types => { - if (keys(types).length > 1) return 'conflict'; - - try { - return normalizeType(keys(types)[0]); - } catch (e) { - return 'unsupported'; - } - }); - }); -} diff --git a/x-pack/plugins/canvas/server/routes/es_fields/index.js b/x-pack/plugins/canvas/server/routes/es_fields/index.js deleted file mode 100644 index 9ceca324cc017..0000000000000 --- a/x-pack/plugins/canvas/server/routes/es_fields/index.js +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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 { partial } from 'lodash'; -import { getESFieldTypes } from './get_es_field_types'; - -// TODO: Error handling, note: esErrors -export function esFields(server) { - const { callWithRequest } = server.plugins.elasticsearch.getCluster('data'); - - server.route({ - method: 'GET', - path: '/api/canvas/es_fields', - handler: function(request, reply) { - const { index, fields } = request.query; - if (!index) return reply({ error: '"index" query is required' }).code(400); - - reply(getESFieldTypes(index, fields, partial(callWithRequest, request))); - }, - }); -} diff --git a/x-pack/plugins/canvas/server/routes/es_indices/get_es_indices.js b/x-pack/plugins/canvas/server/routes/es_indices/get_es_indices.js deleted file mode 100644 index 9a28d3da42409..0000000000000 --- a/x-pack/plugins/canvas/server/routes/es_indices/get_es_indices.js +++ /dev/null @@ -1,20 +0,0 @@ -/* - * 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 { map } from 'lodash'; - -export function getESIndices(kbnIndex, elasticsearchClient) { - const config = { - index: kbnIndex, - _source: 'index-pattern.title', - q: 'type:"index-pattern"', - size: 100, - }; - - return elasticsearchClient('search', config).then(resp => { - return map(resp.hits.hits, '_source["index-pattern"].title'); - }); -} diff --git a/x-pack/plugins/canvas/server/routes/es_indices/index.js b/x-pack/plugins/canvas/server/routes/es_indices/index.js deleted file mode 100644 index 67a92fa72aecd..0000000000000 --- a/x-pack/plugins/canvas/server/routes/es_indices/index.js +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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 { partial } from 'lodash'; -import { getESIndices } from './get_es_indices'; - -// TODO: Error handling, note: esErrors -// TODO: Allow filtering by pattern name -export function esIndices(server) { - const kbnIndex = server.config().get('kibana.index'); - const { callWithRequest } = server.plugins.elasticsearch.getCluster('data'); - - server.route({ - method: 'GET', - path: '/api/canvas/es_indices', - handler: function(request, reply) { - reply(getESIndices(kbnIndex, partial(callWithRequest, request))); - }, - }); -} diff --git a/x-pack/plugins/canvas/server/routes/get_auth/get_auth_header.js b/x-pack/plugins/canvas/server/routes/get_auth/get_auth_header.js deleted file mode 100644 index 466c601d02204..0000000000000 --- a/x-pack/plugins/canvas/server/routes/get_auth/get_auth_header.js +++ /dev/null @@ -1,21 +0,0 @@ -/* - * 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 { insecureAuthRoute } from './insecure_auth_route'; - -// TODO: OMG. No. Need a better way of setting to this than our wacky route thing. -export function getAuthHeader(request, server) { - const basePath = server.config().get('server.basePath') || ''; - const fullPath = `${basePath}${insecureAuthRoute}`; - - return server - .inject({ - method: 'GET', - url: fullPath, - headers: request.headers, - }) - .then(res => res.result); -} diff --git a/x-pack/plugins/canvas/server/routes/get_auth/index.js b/x-pack/plugins/canvas/server/routes/get_auth/index.js deleted file mode 100644 index bdc8e596f6f8b..0000000000000 --- a/x-pack/plugins/canvas/server/routes/get_auth/index.js +++ /dev/null @@ -1,22 +0,0 @@ -/* - * 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 { insecureAuthRoute } from './insecure_auth_route'; - -// TODO: Fix this first. This route returns decrypts the cookie and returns the basic auth header. It is used because -// the pre-route hapi hook doesn't work on the socket and there are no exposed methods for doing the conversion from cookie -// to auth header. We will need to add that to x-pack security -// In theory this is pretty difficult to exploit, but not impossible. -// -export function getAuth(server) { - server.route({ - method: 'GET', - path: insecureAuthRoute, - handler: function(request, reply) { - reply(request.headers.authorization); - }, - }); -} diff --git a/x-pack/plugins/canvas/server/routes/get_auth/insecure_auth_route.js b/x-pack/plugins/canvas/server/routes/get_auth/insecure_auth_route.js deleted file mode 100644 index 62d78e4eab4a7..0000000000000 --- a/x-pack/plugins/canvas/server/routes/get_auth/insecure_auth_route.js +++ /dev/null @@ -1,9 +0,0 @@ -/* - * 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 uuid from 'uuid/v4'; - -export const insecureAuthRoute = `/api/canvas/ar-${uuid()}`; diff --git a/x-pack/plugins/canvas/server/routes/index.js b/x-pack/plugins/canvas/server/routes/index.js index 6d8cc6aefd839..5729701d51f69 100644 --- a/x-pack/plugins/canvas/server/routes/index.js +++ b/x-pack/plugins/canvas/server/routes/index.js @@ -5,19 +5,7 @@ */ import { workpad } from './workpad'; -import { socketApi } from './socket'; -import { translate } from './translate'; -import { esFields } from './es_fields'; -import { esIndices } from './es_indices'; -import { getAuth } from './get_auth'; -import { plugins } from './plugins'; export function routes(server) { workpad(server); - socketApi(server); - translate(server); - esFields(server); - esIndices(server); - getAuth(server); - plugins(server); } diff --git a/x-pack/plugins/canvas/server/routes/plugins.js b/x-pack/plugins/canvas/server/routes/plugins.js deleted file mode 100644 index 66e73c71876a3..0000000000000 --- a/x-pack/plugins/canvas/server/routes/plugins.js +++ /dev/null @@ -1,25 +0,0 @@ -/* - * 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 { getPluginStream } from '../lib/get_plugin_stream'; -import { pluginPaths } from '../lib/plugin_paths'; - -export function plugins(server) { - server.route({ - method: 'GET', - path: '/api/canvas/plugins', - handler: function(request, reply) { - const { type } = request.query; - - if (!pluginPaths[type]) return reply({ error: 'Invalid type' }).code(400); - - reply(getPluginStream(type)); - }, - config: { - auth: false, - }, - }); -} diff --git a/x-pack/plugins/canvas/server/routes/translate.js b/x-pack/plugins/canvas/server/routes/translate.js deleted file mode 100644 index f2b412c37e9b9..0000000000000 --- a/x-pack/plugins/canvas/server/routes/translate.js +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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 { fromExpression, toExpression } from '../../common/lib/ast'; - -export function translate(server) { - /* - Get AST from expression - */ - server.route({ - method: 'GET', - path: '/api/canvas/ast', - handler: function(request, reply) { - if (!request.query.expression) - return reply({ error: '"expression" query is required' }).code(400); - reply(fromExpression(request.query.expression)); - }, - }); - - server.route({ - method: 'POST', - path: '/api/canvas/expression', - handler: function(request, reply) { - try { - const exp = toExpression(request.payload); - reply(exp); - } catch (e) { - reply({ error: e.message }).code(400); - } - }, - }); -} diff --git a/x-pack/plugins/canvas/server/usage/collector.js b/x-pack/plugins/canvas/server/usage/collector.js index f0c55e6ae0a62..c4121987015a7 100644 --- a/x-pack/plugins/canvas/server/usage/collector.js +++ b/x-pack/plugins/canvas/server/usage/collector.js @@ -5,8 +5,8 @@ */ import { sum as arraySum, min as arrayMin, max as arrayMax, get } from 'lodash'; +import { fromExpression } from '@kbn/interpreter/common/lib/ast'; import { CANVAS_USAGE_TYPE, CANVAS_TYPE } from '../../common/lib/constants'; -import { fromExpression } from '../../common/lib/ast'; /* * @param ast: an ast that includes functions to track diff --git a/x-pack/plugins/canvas/tasks/helpers/webpack.plugins.js b/x-pack/plugins/canvas/tasks/helpers/webpack.plugins.js index 3a92675189041..f49e7a7abc5c7 100644 --- a/x-pack/plugins/canvas/tasks/helpers/webpack.plugins.js +++ b/x-pack/plugins/canvas/tasks/helpers/webpack.plugins.js @@ -21,7 +21,7 @@ module.exports = { 'uis/arguments/all': path.join(sourceDir, 'uis/arguments/register.js'), 'functions/browser/all': path.join(sourceDir, 'functions/browser/register.js'), 'functions/common/all': path.join(sourceDir, 'functions/common/register.js'), - 'types/all': path.join(sourceDir, 'types/register.js'), + 'functions/server/all': path.join(sourceDir, 'functions/server/register.js'), }, target: 'webworker', diff --git a/x-pack/plugins/canvas/tasks/index.js b/x-pack/plugins/canvas/tasks/index.js index 9835f76f12609..2d529ce139187 100644 --- a/x-pack/plugins/canvas/tasks/index.js +++ b/x-pack/plugins/canvas/tasks/index.js @@ -5,14 +5,12 @@ */ import dev from './dev'; -import peg from './peg'; import plugins from './plugins'; import prepare from './prepare'; import test from './test'; export default function canvasTasks(gulp, gulpHelpers) { dev(gulp, gulpHelpers); - peg(gulp, gulpHelpers); plugins(gulp, gulpHelpers); prepare(gulp, gulpHelpers); test(gulp, gulpHelpers); diff --git a/x-pack/plugins/canvas/tasks/peg.js b/x-pack/plugins/canvas/tasks/peg.js deleted file mode 100644 index 0d202c4ac79dd..0000000000000 --- a/x-pack/plugins/canvas/tasks/peg.js +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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 { resolve } from 'path'; - -const grammarDir = resolve(__dirname, '..', 'common', 'lib'); - -export default function pegTask(gulp, { pegjs }) { - gulp.task('canvas:peg:build', function() { - return gulp - .src(`${grammarDir}/*.peg`) - .pipe( - pegjs({ - format: 'commonjs', - allowedStartRules: ['expression', 'argument'], - }) - ) - .pipe(gulp.dest(grammarDir)); - }); - - gulp.task('canvas:peg:dev', function() { - gulp.watch(`${grammarDir}/*.peg`, ['peg']); - }); -} diff --git a/x-pack/yarn.lock b/x-pack/yarn.lock index 04c5942c8c965..4afbb0a59fb39 100644 --- a/x-pack/yarn.lock +++ b/x-pack/yarn.lock @@ -76,6 +76,10 @@ version "0.0.0" uid "" +"@kbn/interpreter@link:../packages/kbn-interpreter": + version "0.0.0" + uid "" + "@kbn/plugin-helpers@link:../packages/kbn-plugin-helpers": version "0.0.0" uid "" @@ -885,17 +889,6 @@ babel-plugin-check-es2015-constants@^6.22.0: dependencies: babel-runtime "^6.22.0" -babel-plugin-inline-react-svg@^0.5.4: - version "0.5.4" - resolved "https://registry.yarnpkg.com/babel-plugin-inline-react-svg/-/babel-plugin-inline-react-svg-0.5.4.tgz#bc818f351cd9d78f5b3bfa7cc1da5f83e7b4010a" - dependencies: - babel-template "^6.26.0" - babel-traverse "^6.26.0" - babylon "^6.18.0" - lodash.isplainobject "^4.0.6" - resolve "^1.8.1" - svgo "^0.7.2" - babel-plugin-istanbul@^4.1.6: version "4.1.6" resolved "https://registry.yarnpkg.com/babel-plugin-istanbul/-/babel-plugin-istanbul-4.1.6.tgz#36c59b2192efce81c5b378321b74175add1c9a45" @@ -909,17 +902,6 @@ babel-plugin-jest-hoist@^23.2.0: version "23.2.0" resolved "https://registry.yarnpkg.com/babel-plugin-jest-hoist/-/babel-plugin-jest-hoist-23.2.0.tgz#e61fae05a1ca8801aadee57a6d66b8cefaf44167" -babel-plugin-mock-imports@^0.0.5: - version "0.0.5" - resolved "https://registry.yarnpkg.com/babel-plugin-mock-imports/-/babel-plugin-mock-imports-0.0.5.tgz#caa865f017d8972fe47772e0fb57f2924e5ce3c5" - -babel-plugin-pegjs-inline-precompile@^0.1.0: - version "0.1.0" - resolved "https://registry.yarnpkg.com/babel-plugin-pegjs-inline-precompile/-/babel-plugin-pegjs-inline-precompile-0.1.0.tgz#3307f2b373a844296385311a7c528c53414dc57e" - dependencies: - babylon "^6.18.0" - pegjs "^0.10.0" - babel-plugin-syntax-object-rest-spread@^6.13.0: version "6.13.0" resolved "http://registry.npmjs.org/babel-plugin-syntax-object-rest-spread/-/babel-plugin-syntax-object-rest-spread-6.13.0.tgz#fd6536f2bce13836ffa3a5458c4903a597bb3bf5" @@ -1092,10 +1074,6 @@ babel-plugin-transform-es2015-unicode-regex@^6.24.1: babel-runtime "^6.22.0" regexpu-core "^2.0.0" -babel-plugin-transform-react-remove-prop-types@^0.4.14: - version "0.4.15" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-react-remove-prop-types/-/babel-plugin-transform-react-remove-prop-types-0.4.15.tgz#7ba830e77276a0e788cd58ea527b5f70396e12a7" - babel-plugin-transform-regenerator@^6.24.1: version "6.26.0" resolved "https://registry.yarnpkg.com/babel-plugin-transform-regenerator/-/babel-plugin-transform-regenerator-6.26.0.tgz#e0703696fbde27f0a3efcacf8b4dca2f7b3a8f2f" @@ -1616,12 +1594,6 @@ ci-info@^1.0.0: version "1.1.2" resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-1.1.2.tgz#03561259db48d0474c8bdc90f5b47b068b6bbfb4" -clap@^1.0.9: - version "1.2.3" - resolved "https://registry.yarnpkg.com/clap/-/clap-1.2.3.tgz#4f36745b32008492557f46412d66d50cb99bce51" - dependencies: - chalk "^1.1.3" - class-utils@^0.3.5: version "0.3.5" resolved "https://registry.yarnpkg.com/class-utils/-/class-utils-0.3.5.tgz#17e793103750f9627b2176ea34cfd1b565903c80" @@ -1733,12 +1705,6 @@ co@^4.6.0: version "4.6.0" resolved "https://registry.yarnpkg.com/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184" -coa@~1.0.1: - version "1.0.4" - resolved "https://registry.yarnpkg.com/coa/-/coa-1.0.4.tgz#a9ef153660d6a86a8bdec0289a5c684d217432fd" - dependencies: - q "^1.1.2" - code-point-at@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77" @@ -1768,10 +1734,6 @@ colors@0.5.x: version "0.5.1" resolved "https://registry.yarnpkg.com/colors/-/colors-0.5.1.tgz#7d0023eaeb154e8ee9fce75dcb923d0ed1667774" -colors@~1.1.2: - version "1.1.2" - resolved "https://registry.yarnpkg.com/colors/-/colors-1.1.2.tgz#168a4701756b6a7f51a12ce0c97bfa28c084ed63" - combined-stream@1.0.6, combined-stream@~1.0.6: version "1.0.6" resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.6.tgz#723e7df6e801ac5613113a7e445a9b69cb632818" @@ -2026,13 +1988,6 @@ css@^2.2.1: source-map-resolve "^0.3.0" urix "^0.1.0" -csso@~2.3.1: - version "2.3.2" - resolved "https://registry.yarnpkg.com/csso/-/csso-2.3.2.tgz#ddd52c587033f49e94b71fc55569f252e8ff5f85" - dependencies: - clap "^1.0.9" - source-map "^0.5.3" - cssom@0.3.x, "cssom@>= 0.3.2 < 0.4.0": version "0.3.2" resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.3.2.tgz#b8036170c79f07a90ff2f16e22284027a243848b" @@ -2677,10 +2632,6 @@ escodegen@~1.3.2: optionalDependencies: source-map "~0.1.33" -esprima@^2.6.0: - version "2.7.3" - resolved "https://registry.yarnpkg.com/esprima/-/esprima-2.7.3.tgz#96e3b70d5779f6ad49cd032673d1c312767ba581" - esprima@^3.1.3: version "3.1.3" resolved "https://registry.yarnpkg.com/esprima/-/esprima-3.1.3.tgz#fdca51cee6133895e3c88d535ce49dbff62a4633" @@ -4914,13 +4865,6 @@ js-yaml@^3.7.0: argparse "^1.0.7" esprima "^4.0.0" -js-yaml@~3.7.0: - version "3.7.0" - resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.7.0.tgz#5c967ddd837a9bfdca5f2de84253abe8a1c03b80" - dependencies: - argparse "^1.0.7" - esprima "^2.6.0" - jsbn@~0.1.0: version "0.1.1" resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-0.1.1.tgz#a5e654c2e5a2deb5f201d96cefbca80c0ef2f513" @@ -5350,10 +5294,6 @@ lodash.isobject@^3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/lodash.isobject/-/lodash.isobject-3.0.2.tgz#3c8fb8d5b5bf4bf90ae06e14f2a530a4ed935e1d" -lodash.isplainobject@^4.0.6: - version "4.0.6" - resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb" - lodash.istypedarray@^3.0.0: version "3.0.6" resolved "https://registry.yarnpkg.com/lodash.istypedarray/-/lodash.istypedarray-3.0.6.tgz#c9a477498607501d8e8494d283b87c39281cef62" @@ -5819,7 +5759,7 @@ mkdirp@0.5.0: dependencies: minimist "0.0.8" -mkdirp@0.5.1, "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.1: +mkdirp@0.5.1, "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1: version "0.5.1" resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903" dependencies: @@ -6955,10 +6895,6 @@ puppeteer-core@^1.7.0: rimraf "^2.6.1" ws "^5.1.1" -q@^1.1.2: - version "1.5.1" - resolved "https://registry.yarnpkg.com/q/-/q-1.5.1.tgz#7e32f75b41381291d04611f1bf14109ac00651d7" - qs@^6.5.1, qs@~6.5.1: version "6.5.1" resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.1.tgz#349cdf6eef89ec45c12d7d5eb3fc0c870343a6d8" @@ -7830,12 +7766,6 @@ resolve@^1.1.5, resolve@^1.1.6, resolve@^1.1.7: dependencies: path-parse "^1.0.5" -resolve@^1.8.1: - version "1.8.1" - resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.8.1.tgz#82f1ec19a423ac1fbd080b0bab06ba36e84a7a26" - dependencies: - path-parse "^1.0.5" - responselike@1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/responselike/-/responselike-1.0.2.tgz#918720ef3b631c5642be068f15ade5a46f4ba1e7" @@ -7982,7 +7912,7 @@ sax@0.5.x: version "0.5.8" resolved "https://registry.yarnpkg.com/sax/-/sax-0.5.8.tgz#d472db228eb331c2506b0e8c15524adb939d12c1" -sax@>=0.6.0, sax@^1.2.1, sax@^1.2.4, sax@~1.2.1: +sax@>=0.6.0, sax@^1.2.1, sax@^1.2.4: version "1.2.4" resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9" @@ -8623,18 +8553,6 @@ supports-color@^5.3.0: dependencies: has-flag "^3.0.0" -svgo@^0.7.2: - version "0.7.2" - resolved "https://registry.yarnpkg.com/svgo/-/svgo-0.7.2.tgz#9f5772413952135c6fefbf40afe6a4faa88b4bb5" - dependencies: - coa "~1.0.1" - colors "~1.1.2" - csso "~2.3.1" - js-yaml "~3.7.0" - mkdirp "~0.5.1" - sax "~1.2.1" - whet.extend "~0.9.9" - symbol-observable@^1.0.4, symbol-observable@^1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.2.0.tgz#c22688aed4eab3cdc2dfeacbb561660560a00804" @@ -9368,10 +9286,6 @@ whatwg-url@^7.0.0: tr46 "^1.0.1" webidl-conversions "^4.0.2" -whet.extend@~0.9.9: - version "0.9.9" - resolved "https://registry.yarnpkg.com/whet.extend/-/whet.extend-0.9.9.tgz#f877d5bf648c97e5aa542fadc16d6a259b9c11a1" - which-module@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/which-module/-/which-module-1.0.0.tgz#bba63ca861948994ff307736089e3b96026c2a4f" diff --git a/yarn.lock b/yarn.lock index 036d311c54795..6398b1720eb51 100644 --- a/yarn.lock +++ b/yarn.lock @@ -199,6 +199,10 @@ version "0.0.0" uid "" +"@kbn/interpreter@link:packages/kbn-interpreter": + version "0.0.0" + uid "" + "@kbn/plugin-generator@link:packages/kbn-plugin-generator": version "0.0.0" uid "" @@ -1477,6 +1481,17 @@ babel-plugin-check-es2015-constants@^6.22.0: dependencies: babel-runtime "^6.22.0" +babel-plugin-inline-react-svg@^0.5.4: + version "0.5.4" + resolved "https://registry.yarnpkg.com/babel-plugin-inline-react-svg/-/babel-plugin-inline-react-svg-0.5.4.tgz#bc818f351cd9d78f5b3bfa7cc1da5f83e7b4010a" + dependencies: + babel-template "^6.26.0" + babel-traverse "^6.26.0" + babylon "^6.18.0" + lodash.isplainobject "^4.0.6" + resolve "^1.8.1" + svgo "^0.7.2" + babel-plugin-istanbul@^4.1.6: version "4.1.6" resolved "https://registry.yarnpkg.com/babel-plugin-istanbul/-/babel-plugin-istanbul-4.1.6.tgz#36c59b2192efce81c5b378321b74175add1c9a45" @@ -1490,6 +1505,17 @@ babel-plugin-jest-hoist@^23.2.0: version "23.2.0" resolved "https://registry.yarnpkg.com/babel-plugin-jest-hoist/-/babel-plugin-jest-hoist-23.2.0.tgz#e61fae05a1ca8801aadee57a6d66b8cefaf44167" +babel-plugin-mock-imports@^0.0.5: + version "0.0.5" + resolved "https://registry.yarnpkg.com/babel-plugin-mock-imports/-/babel-plugin-mock-imports-0.0.5.tgz#caa865f017d8972fe47772e0fb57f2924e5ce3c5" + +babel-plugin-pegjs-inline-precompile@^0.1.0: + version "0.1.0" + resolved "https://registry.yarnpkg.com/babel-plugin-pegjs-inline-precompile/-/babel-plugin-pegjs-inline-precompile-0.1.0.tgz#3307f2b373a844296385311a7c528c53414dc57e" + dependencies: + babylon "^6.18.0" + pegjs "^0.10.0" + babel-plugin-syntax-async-functions@^6.8.0: version "6.13.0" resolved "https://registry.yarnpkg.com/babel-plugin-syntax-async-functions/-/babel-plugin-syntax-async-functions-6.13.0.tgz#cad9cad1191b5ad634bf30ae0872391e0647be95" @@ -1772,6 +1798,10 @@ babel-plugin-transform-react-jsx@^6.24.1: babel-plugin-syntax-jsx "^6.8.0" babel-runtime "^6.22.0" +babel-plugin-transform-react-remove-prop-types@^0.4.14: + version "0.4.18" + resolved "https://registry.yarnpkg.com/babel-plugin-transform-react-remove-prop-types/-/babel-plugin-transform-react-remove-prop-types-0.4.18.tgz#85ff79d66047b34288c6f7cc986b8854ab384f8c" + babel-plugin-transform-regenerator@^6.22.0, babel-plugin-transform-regenerator@^6.24.1: version "6.26.0" resolved "https://registry.yarnpkg.com/babel-plugin-transform-regenerator/-/babel-plugin-transform-regenerator-6.26.0.tgz#e0703696fbde27f0a3efcacf8b4dca2f7b3a8f2f" @@ -8632,6 +8662,10 @@ lodash.isobject@^3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/lodash.isobject/-/lodash.isobject-3.0.2.tgz#3c8fb8d5b5bf4bf90ae06e14f2a530a4ed935e1d" +lodash.isplainobject@^4.0.6: + version "4.0.6" + resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb" + lodash.isstring@^4.0.1: version "4.0.1" resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451" @@ -10221,6 +10255,10 @@ pegjs@0.9.0, pegjs@~0.9.0: version "0.9.0" resolved "https://registry.yarnpkg.com/pegjs/-/pegjs-0.9.0.tgz#f6aefa2e3ce56169208e52179dfe41f89141a369" +pegjs@^0.10.0: + version "0.10.0" + resolved "http://registry.npmjs.org/pegjs/-/pegjs-0.10.0.tgz#cf8bafae6eddff4b5a7efb185269eaaf4610ddbd" + pend@~1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/pend/-/pend-1.2.0.tgz#7a57eb550a6783f9115331fcf4663d5c8e007a50" @@ -12091,7 +12129,7 @@ resolve@^1.1.5, resolve@^1.5.0: dependencies: path-parse "^1.0.5" -resolve@^1.1.6, resolve@^1.4.0, resolve@^1.6.0: +resolve@^1.1.6, resolve@^1.4.0, resolve@^1.6.0, resolve@^1.8.1: version "1.8.1" resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.8.1.tgz#82f1ec19a423ac1fbd080b0bab06ba36e84a7a26" dependencies: @@ -12802,6 +12840,10 @@ sprintf-js@~1.0.2: version "1.0.3" resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c" +squel@^5.12.2: + version "5.12.2" + resolved "https://registry.yarnpkg.com/squel/-/squel-5.12.2.tgz#8c7b54fd5462d95fe2432663c8762b65d29efe4c" + sshpk@^1.7.0: version "1.14.1" resolved "https://registry.yarnpkg.com/sshpk/-/sshpk-1.14.1.tgz#130f5975eddad963f1d56f92b9ac6c51fa9f83eb" @@ -13193,7 +13235,7 @@ supports-color@^5.3.0: dependencies: has-flag "^3.0.0" -svgo@^0.7.0: +svgo@^0.7.0, svgo@^0.7.2: version "0.7.2" resolved "https://registry.yarnpkg.com/svgo/-/svgo-0.7.2.tgz#9f5772413952135c6fefbf40afe6a4faa88b4bb5" dependencies: