Skip to content

Commit

Permalink
Merge pull request elastic#161 from rashidkpc/enc/fn_handlers
Browse files Browse the repository at this point in the history
Add handlers argument to functions
  • Loading branch information
Rashid Khan authored Sep 12, 2017
2 parents f6e5067 + 86d4576 commit 8c74b0d
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 16 deletions.
5 changes: 2 additions & 3 deletions common/interpreter/interpret.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { getType } from '../types/get_type';

export function interpretProvider(config) {
const cast = castProvider(config.types);
const functions = config.functions;
const onFunctionNotFound = config.onFunctionNotFound;
const { functions, onFunctionNotFound, handlers } = config;

return interpret;

Expand Down Expand Up @@ -62,7 +61,7 @@ export function interpretProvider(config) {
const fnDef = functions[name];
const acceptableContext = cast(context, fnDef.context.types);

return fnDef.fn(acceptableContext, args).then(output => {
return fnDef.fn(acceptableContext, args, handlers).then(output => {
// Validate that the function returned the type it said it would.
// This isn't really required, but it keeps function developers honest.
const returnType = getType(output);
Expand Down
6 changes: 3 additions & 3 deletions common/interpreter/socket_interpret.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import { interpretProvider } from './interpret';
socket: the socket to communicate over
*/

export function socketInterpreterProvider({ types, functions, referableFunctions, socket }) {
export function socketInterpreterProvider({ types, functions, handlers, referableFunctions, socket }) {
// Return the interpet() function
return interpretProvider({
types: types,
functions: functions,
types, functions, handlers,

onFunctionNotFound: (chain, context) => {
// Get the name of the function that wasn't found
const functionName = chain.chain[0].function;
Expand Down
5 changes: 5 additions & 0 deletions public/lib/create_handlers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function createHandlers(/*socket*/) {
return {
environment: 'client',
};
}
2 changes: 2 additions & 0 deletions public/lib/interpreter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { socketInterpreterProvider } from '../../common/interpreter/socket_inter
import { socket } from '../socket';
import { types } from '../lib/types';
import { functions } from '../lib/functions';
import { createHandlers } from './create_handlers';

// Create the function list
socket.emit('getFunctionList');
Expand All @@ -13,6 +14,7 @@ export function interpretAst(ast, context) {
.then((serverFunctionList) => {
return socketInterpreterProvider({
types: types.toJS(),
handlers: createHandlers(socket),
functions: functions.toJS(),
referableFunctions: serverFunctionList,
socket: socket,
Expand Down
12 changes: 2 additions & 10 deletions server/functions/esdocs/esdocs.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import elasticsearch from 'elasticsearch';
import Fn from '../../../common/functions/fn.js';
import flattenHit from './lib/flatten_hit';
import { buildESRequest } from './lib/build_es_request';
import { keys, map } from 'lodash';
import { getESFieldTypes } from '../../routes/es_fields/get_es_field_types';

const client = new elasticsearch.Client({
host: 'localhost:9200',
});



export default new Fn({
name: 'esdocs',
context: {
Expand Down Expand Up @@ -40,8 +33,7 @@ export default new Fn({
},
type: 'datatable',
help: 'Query elasticsearch and get back raw documents.',
fn: (context, args) => {

fn: (context, args, handlers) => {
context.and = context.and
.concat([{ // q
type: 'luceneQueryString',
Expand Down Expand Up @@ -69,7 +61,7 @@ export default new Fn({
},
}, context);

return client.search(esRequest)
return handlers.elasticsearchClient('search', esRequest)
.then(resp => {
const flatHits = map(resp.hits.hits, (hit, i) => {
return Object.assign(flattenHit(hit), { _rowId: i });
Expand Down
10 changes: 10 additions & 0 deletions server/lib/create_handlers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { partial } from 'lodash';

export const createHandlers = (socket, server) => {
const { callWithRequest } = server.plugins.elasticsearch.getCluster('data');

return {
environment: 'server',
elasticsearchClient: partial(callWithRequest, socket.handshake),
};
};
2 changes: 2 additions & 0 deletions server/routes/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import socket from 'socket.io';
import { functions } from '../lib/functions';
import { socketInterpreterProvider } from '../../common/interpreter/socket_interpret';
import { types } from '../../common/lib/types';
import { createHandlers } from '../lib/create_handlers';

export function socketApi(server) {
const io = socket(server.listener);
Expand All @@ -23,6 +24,7 @@ export function socketApi(server) {
const interpret = socketInterpreterProvider({
types: types.toJS(),
functions: functions.toJS(),
handlers: createHandlers(socket, server),
referableFunctions: clientFunctions,
socket: socket,
});
Expand Down

0 comments on commit 8c74b0d

Please sign in to comment.