Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove WebSockets from Canvas expressions interpreter #29792

Merged
merged 20 commits into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
94c02be
Make Canvas use socket.io polling only so that Kibana
chrisdavies Jan 30, 2019
0ee52e7
Merge branch 'master' of github.com:elastic/kibana into canvas/rest
chrisdavies Jan 31, 2019
4f17b41
Merge branch 'master' of github.com:elastic/kibana into canvas/rest
chrisdavies Jan 31, 2019
95859d2
Merge branch 'master' of github.com:elastic/kibana into canvas/rest
chrisdavies Jan 31, 2019
76d2d12
Remove WebSockets from Canvas expressions interpreter
chrisdavies Feb 1, 2019
cd91964
Remove console log and commented code...
chrisdavies Feb 1, 2019
5e0b0b9
Merge branch 'master' of github.com:elastic/kibana into canvas/rest
chrisdavies Feb 1, 2019
1c5c087
Add tests for kbn interpreter
chrisdavies Feb 1, 2019
d715fe6
Merge branch 'master' of github.com:elastic/kibana into canvas/rest
chrisdavies Feb 1, 2019
374342b
Fix Canvas kfetch dependency
chrisdavies Feb 1, 2019
f05202b
Rename socketInterpreterProvider to interpreterProvider
chrisdavies Feb 1, 2019
d32ccf8
Remove remaining socket references, remove unused interpreter threading
chrisdavies Feb 4, 2019
d239ba3
Merge master
chrisdavies Feb 4, 2019
b43dd04
Fix invalid fnName reference
chrisdavies Feb 4, 2019
1a8c06c
Remove unecessary comment
chrisdavies Feb 4, 2019
fe7e2da
Remove Canvas' socket-based get_request
chrisdavies Feb 4, 2019
d7270b4
Fix interpreter reference
chrisdavies Feb 4, 2019
021cb9c
Merge branch 'master' of github.com:elastic/kibana into canvas/rest
chrisdavies Feb 4, 2019
116e99d
Fix a problem with interpreter error handler
chrisdavies Feb 4, 2019
67286a6
Re-remove scriptjs from kbn-interpreter
chrisdavies Feb 5, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@
"rxjs": "^6.2.1",
"script-loader": "0.7.2",
"semver": "^5.5.0",
"socket.io": "^2.1.1",
"stream-stream": "^1.2.6",
"style-loader": "0.23.1",
"tar": "2.2.0",
Expand Down
1 change: 0 additions & 1 deletion packages/kbn-interpreter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"lodash": "npm:@elastic/lodash@3.10.1-kibana1",
"lodash.clone": "^4.5.0",
"scriptjs": "^2.5.8",
chrisdavies marked this conversation as resolved.
Show resolved Hide resolved
"socket.io-client": "^2.1.1",
"uuid": "3.0.1"
},
"devDependencies": {
Expand Down
1 change: 0 additions & 1 deletion packages/kbn-interpreter/src/public/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@
*/

export { loadBrowserRegistries } from './browser_registries';
export { createSocket } from './socket';
export { initializeInterpreter } from './interpreter';
export { RenderFunctionsRegistry } from './render_functions_registry';
63 changes: 27 additions & 36 deletions packages/kbn-interpreter/src/public/interpreter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,51 +21,42 @@ import { socketInterpreterProvider } from '../common/interpreter/socket_interpre
import { serializeProvider } from '../common/lib/serialize';
import { createHandlers } from './create_handlers';

export async function initializeInterpreter(socket, typesRegistry, functionsRegistry) {
let resolve;
const functionList = new Promise(_resolve => (resolve = _resolve));

const getInitializedFunctions = async () => {
return functionList;
};
export async function initializeInterpreter(kfetch, typesRegistry, functionsRegistry) {
chrisdavies marked this conversation as resolved.
Show resolved Hide resolved
const serverFunctionList = await kfetch({ pathname: '/api/canvas/fns' });

// For every sever-side function, register a client-side
// function that matches its definition, but which simply
// calls the server-side function endpoint.
Object.keys(serverFunctionList).forEach(functionName => {
chrisdavies marked this conversation as resolved.
Show resolved Hide resolved
functionsRegistry.register(() => ({
...serverFunctionList[functionName],
async fn(context, args) {
const types = typesRegistry.toJS();
const { serialize } = serializeProvider(types);
const result = await kfetch({
pathname: `/api/canvas/fns/${functionName}`,
method: 'POST',
body: JSON.stringify({
args,
context: serialize(context),
}),
});

return result;
},
}));
});

const interpretAst = async (ast, context, handlers) => {
// Load plugins before attempting to get functions, otherwise this gets racey
const serverFunctionList = await functionList;
const interpretFn = await socketInterpreterProvider({
chrisdavies marked this conversation as resolved.
Show resolved Hide resolved
types: typesRegistry.toJS(),
handlers: { ...handlers, ...createHandlers(socket) },
handlers: { ...handlers, ...createHandlers() },
functions: functionsRegistry.toJS(),
referableFunctions: serverFunctionList,
socket: socket,
});
return interpretFn(ast, context);
};

// Listen for interpreter runs
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) });
});
});

// Create the function list
let gotFunctionList = false;
socket.once('functionList', (fl) => {
gotFunctionList = true;
resolve(fl);
});

const interval = setInterval(() => {
if (gotFunctionList) {
clearInterval(interval);
return;
}
socket.emit('getFunctionList');
}, 1000);

return { getInitializedFunctions, interpretAst };
return { interpretAst };
}

75 changes: 75 additions & 0 deletions packages/kbn-interpreter/src/public/interpreter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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 { initializeInterpreter } from './interpreter';

jest.mock('../common/interpreter/socket_interpret', () => ({
socketInterpreterProvider: () => () => ({}),
}));

jest.mock('../common/lib/serialize', () => ({
serializeProvider: () => ({ serialize: () => ({}) }),
}));

jest.mock('./create_handlers', () => ({
createHandlers: () => ({}),
}));

describe('kbn-interpreter/interpreter', () => {
it('loads server-side functions', async () => {
const kfetch = jest.fn(async () => ({}));

await initializeInterpreter(kfetch, { toJS: () => ({}) }, ({ register: () => {} }));

expect(kfetch).toHaveBeenCalledTimes(1);
expect(kfetch).toHaveBeenCalledWith({ pathname: '/api/canvas/fns' });
chrisdavies marked this conversation as resolved.
Show resolved Hide resolved
});

it('registers client-side functions that pass through to the server', async () => {
const kfetch = jest.fn(async () => ({
hello: { name: 'hello' },
world: { name: 'world' },
}));

const register = jest.fn();

await initializeInterpreter(kfetch, { toJS: () => ({}) }, ({ register }));

expect(register).toHaveBeenCalledTimes(2);

const [ hello, world ] = register.mock.calls.map(([fn]) => fn());

expect(hello.name).toEqual('hello');
expect(typeof hello.fn).toEqual('function');
expect(world.name).toEqual('world');
expect(typeof world.fn).toEqual('function');

const context = {};
const args = { quote: 'All we have to decide is what to do with the time that is given us.' };

await hello.fn(context, args);

expect(kfetch).toHaveBeenCalledWith({
pathname: '/api/canvas/fns/hello',
method: 'POST',
body: JSON.stringify({ args, context }),
});
});

});
63 changes: 0 additions & 63 deletions packages/kbn-interpreter/src/public/socket.js

This file was deleted.

13 changes: 4 additions & 9 deletions src/legacy/core_plugins/interpreter/public/interpreter.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
*/


import { initializeInterpreter, loadBrowserRegistries, createSocket } from '@kbn/interpreter/public';
import { initializeInterpreter, loadBrowserRegistries } from '@kbn/interpreter/public';
import { kfetch } from 'ui/kfetch';
import chrome from 'ui/chrome';
import { functions } from './functions';
import { functionsRegistry } from './functions_registry';
Expand Down Expand Up @@ -46,9 +47,8 @@ let _interpreterPromise;

const initialize = async () => {
await loadBrowserRegistries(types, basePath);
const socket = await createSocket(basePath, functionsRegistry);
initializeInterpreter(socket, typesRegistry, functionsRegistry).then(interpreter => {
_resolve({ interpreter, socket });
initializeInterpreter(kfetch, typesRegistry, functionsRegistry).then(interpreter => {
_resolve({ interpreter });
});
};

Expand All @@ -64,8 +64,3 @@ export const interpretAst = async (...params) => {
const { interpreter } = await getInterpreter();
return await interpreter.interpretAst(...params);
};

export const updateInterpreterFunctions = async () => {
const { socket } = await getInterpreter();
socket.emit('updateFunctionList');
};
78 changes: 0 additions & 78 deletions src/legacy/core_plugins/interpreter/public/interpreter.test.js

This file was deleted.

4 changes: 2 additions & 2 deletions src/legacy/core_plugins/interpreter/server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
* under the License.
*/

import { socketApi } from './socket';
import { translate } from './translate';
import { plugins } from './plugins';
import { registerServerFunctions } from './server_functions';

export function routes(server) {
plugins(server);
socketApi(server);
translate(server);
registerServerFunctions(server);
}
Loading