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

User-Story Node with multiple output handles #105

Merged
merged 17 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/executor/source/executor/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function execute( input: DataflowInput, linearizedDataflow: Lineari
for ( const node of linearizedNodes ) {
const inputs = linearizedDataflow.edges
.filter( e => e.toNode === node.id )
.map( e => ( { [e.toHandle]: results[e.fromNode] } ) )
.map( e => ( { [e.toHandle]: results[e.fromNode][e.toHandle] } ) )
.reduce( ( acc, cur ) => ( { ...acc, ...cur } ), {} )

results[node.id] = await executeNode( node, inputs )
Expand Down
9 changes: 8 additions & 1 deletion src/executor/source/executor/nodes/HttpInputNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ export interface HttpInputNode {
*
* @param node to execute
* @param input to be handled
* @returns the data to execute the next node
* @returns the data to execute the next node wraped into an object in fields of the outgoingHandle IDs
*/
export const executeHttpInputNode = async ( node: Node<HttpInputNode>, input: DataflowInput ): Promise<any> => {
const dataFieldName = ['GET', 'DELETE'].includes( node.data.params.method?.toUpperCase() || 'GET' ) ? 'query' : 'body'
if ( node.data.outgoingHandles.length > 0 ){
const results = {}
for ( const outgoingHandle of node.data.outgoingHandles ){
Object.assign( results, { [outgoingHandle.id]: input[dataFieldName] } )
}
return results
}
return input[dataFieldName]
Geissemanu marked this conversation as resolved.
Show resolved Hide resolved
}
10 changes: 8 additions & 2 deletions src/executor/source/executor/nodes/ScriptNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface ScriptNode {
*
* @param node to execute
* @param inputs of the node as an object, with the handle ids as the keys and the inputs as the values
* @returns the output of the JavaScript function
* @returns the output of the JavaScript function wraped into an object in fields of the outgoingHandle IDs
Copy link
Contributor

Choose a reason for hiding this comment

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

typo: wrapped. Also: Call it inputs everywhere if there might be multiple inputs (check other files too please).

*/
export async function executeScriptNode( node: Node<ScriptNode>, input: Record<string, any> ): Promise<any> {
const vm = new VM( {
Expand All @@ -19,6 +19,12 @@ export async function executeScriptNode( node: Node<ScriptNode>, input: Record<s
sandbox: { inputs: Object.values( input ) }
} )
const result = vm.run( `${node.data.params.function}\nhandler(...inputs)` )

if ( node.data.outgoingHandles.length > 0 ){
const results = {}
for ( const outgoingHandle of node.data.outgoingHandles ){
Object.assign( results, { [outgoingHandle.id]: result } )
}
return Promise.resolve( results )
}
Geissemanu marked this conversation as resolved.
Show resolved Hide resolved
return Promise.resolve( result )
}
105 changes: 92 additions & 13 deletions src/executor/spec/executor/Executor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,36 @@ const scriptNode: Node<ScriptNode> = {
}
}

const httpOutputNode: Node<HttpOutputNode> = {
const multipleOutputScriptNode: Node<ScriptNode> = {
id: '5',
type: 'script',
data: {
incomingHandles: [{ name: 'a', id: 'a' }],
outgoingHandles: [{ name: 'b', id: 'b' }, { name: 'c', id: 'c' }],
params: {
function: 'const handler = (a) => {\nreturn 2*a.num\n}'
}
}
}

const httpOutputNodeA: Node<HttpOutputNode> = {
id: '2',
type: 'httpOut',
data: {
outgoingHandles: [],
incomingHandles: [{ name: 'a', id: 'a' }],
params: {
url: 'http://localhost:8080/xyz',
method: 'POST',
headers: {},
body: '{}'
}
},
}

const httpOutputNodeB: Node<HttpOutputNode> = {
id: '4',
type: 'httpOut',
data: {
outgoingHandles: [],
incomingHandles: [{ name: 'b', id: 'b' }],
Expand All @@ -67,7 +94,7 @@ describe( 'Executor', () => {
body: {}
}

const linearisedGraph = await Linearization.linearize ( dataFlow )
const linearisedGraph = Linearization.linearize ( dataFlow )

expect( linearizationSpy ).toBeCalledWith( dataFlow )

Expand All @@ -87,14 +114,14 @@ describe( 'Executor', () => {
body: { hello: 'world' }
}

const linearisedGraph = await Linearization.linearize( dataFlow )
const linearisedGraph = Linearization.linearize( dataFlow )

expect( linearizationSpy ).toBeCalledWith( dataFlow )

const result = await execute( input, linearisedGraph )

expect( result ).not.toBeUndefined()
expect( result[httpInputNode.id] ).toBe( input.body )
expect( result[httpInputNode.id] ).toStrictEqual( { 'a': input.body } )
} )

it( 'should execute a data flow with input and output node', async () => {
Expand All @@ -103,11 +130,11 @@ describe( 'Executor', () => {
fromNode: '1',
toNode: '2',
fromHandle: 'a',
toHandle: 'b'
toHandle: 'a'
}

const dataFlow: Dataflow = {
nodes: [httpInputNode, httpOutputNode],
nodes: [httpInputNode, httpOutputNodeA],
edges: [edge]
}
const input: DataflowInput = {
Expand All @@ -116,14 +143,14 @@ describe( 'Executor', () => {
body: { hello: 'world' }
}

const linearisedGraph = await Linearization.linearize( dataFlow )
const linearisedGraph = Linearization.linearize( dataFlow )

expect( linearizationSpy ).toBeCalledWith( dataFlow )

const result = await execute( input, linearisedGraph )

expect( result ).not.toBeUndefined()
expect( result[httpInputNode.id] ).toBe( input.body )
expect( result[httpInputNode.id] ).toStrictEqual( { 'a': input.body } )
expect( webRequest ).toBeCalledWith( {
data: input.body,
headers: {},
Expand All @@ -150,7 +177,7 @@ describe( 'Executor', () => {
}

const dataFlow: Dataflow = {
nodes: [httpInputNode, scriptNode, httpOutputNode],
nodes: [httpInputNode, scriptNode, httpOutputNodeA],
edges: [edge, edge2]
}
const input: DataflowInput = {
Expand All @@ -159,23 +186,75 @@ describe( 'Executor', () => {
body: { num: 2 }
}

const linearisedGraph = await Linearization.linearize( dataFlow )
const linearisedGraph = Linearization.linearize( dataFlow )

expect( linearizationSpy ).toBeCalledWith( dataFlow )

const result = await execute( input, linearisedGraph )

expect( result ).not.toBeUndefined()
expect( result[httpInputNode.id] ).toStrictEqual( { 'a': input.body } )
expect( webRequest ).toBeCalledWith( {
data: { result: input.body.num * 2 },
headers: {},
method: 'POST',
url: 'http://localhost:8080/xyz',
} )
} )

it( 'should execute a data flow with input, script and output node with multipe outputHandles', async () => {
const edge: Edge = {
id: 'EDGE 1',
fromNode: '1',
toNode: '5',
fromHandle: 'a',
toHandle: 'a'
}

const edge2: Edge = {
id: 'EDGE 2',
fromNode: '5',
toNode: '2',
fromHandle: 'b',
toHandle: 'b'
}

const edge3: Edge = {
id: 'EDGE 3',
fromNode: '5',
toNode: '4',
fromHandle: 'c',
toHandle: 'c'
}

const dataFlow: Dataflow = {
nodes: [httpInputNode, multipleOutputScriptNode, httpOutputNodeA, httpOutputNodeB],
edges: [edge, edge2, edge3]
}
const input: DataflowInput = {
method: 'POST',
query: '',
body: { num: 2 }
}

const linearisedGraph = Linearization.linearize( dataFlow )

expect( linearizationSpy ).toBeCalledWith( dataFlow )

const result = await execute( input, linearisedGraph )

expect( result ).not.toBeUndefined()
expect( result[httpInputNode.id] ).toBe( input.body )
expect( result[httpInputNode.id] ).toStrictEqual( { 'a': input.body } )
expect( webRequest ).toBeCalledWith( {
data: { result: input.body.num * 2 },
headers: {},
method: 'POST',
url: 'http://localhost:8080/xyz',
} )
expect( webRequest ).toBeCalledTimes( 2 )
} )

it( 'should not execute anything for an unknown node type', async () => {
it( 'should nod execute anything for an unknown node type', async () => {

const nodeWithWrongType: Node<any> = {
id: '1',
Expand All @@ -197,7 +276,7 @@ describe( 'Executor', () => {
query: '',
body: { hello: 'world' }
}
const linearisedGraph = await Linearization.linearize( dataFlow )
const linearisedGraph = Linearization.linearize( dataFlow )

expect( linearizationSpy ).toBeCalledWith( dataFlow )

Expand Down
21 changes: 21 additions & 0 deletions src/executor/spec/executor/nodes/HttpInputNode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,25 @@ describe( 'HttpInputNode', () => {

expect( await executeHttpInputNode( node, data ) ).toEqual( data.query )
} )

it( 'should return result in object with Handle ID', async () => {
const node: Node<HttpInputNode> = {
id: '',
type: 'httpIn',
data: {
incomingHandles: [],
outgoingHandles: [{ name: 'a', id: 'a' }],
params: {
method: 'GET'
}
}
}
const data = {
body: 'a',
query: 'b',
method: 'GET'
}

expect( await executeHttpInputNode( node, data ) ).toStrictEqual( { ['a']: data.query } )
} )
} )
7 changes: 7 additions & 0 deletions src/executor/spec/executor/nodes/ScriptNode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,11 @@ describe ( 'ScriptNode', () => {
const node = getScriptNode( 'process.exit()', args )
expect( executeScriptNode( node, args ) ).rejects.toThrow()
} )

it( 'should return result in object with Handle ID', () => {
const args = {}
const node = getScriptNode( 'return 2', args )
node.data.outgoingHandles = [{ name: 'a', id: 'a' }]
expect( executeScriptNode( node, args ) ).resolves.toStrictEqual( { ['a']: 2 } )
} )
} )