Skip to content

Commit a61dac6

Browse files
committed
extract out graph run as separate component
1 parent c8e553b commit a61dac6

6 files changed

Lines changed: 66 additions & 60 deletions

File tree

src/components/molecules/flow/graph/Graph.js

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,27 @@
22

33
import { cloneDeep } from 'lodash';
44
import { readFlowTestSync } from 'service/collection';
5-
import useCanvasStore from 'stores/CanvasStore';
65
import authNode from './compute/authnode';
76
import nestedFlowNode from './compute/nestedflownode';
87
import assertNode from './compute/assertnode';
98
import requestNode from './compute/requestnode';
109
import setVarNode from './compute/setvarnode';
1110
import { LogLevel } from './GraphLogger';
11+
import { useTabStore } from 'stores/TabStore';
1212

1313
class Graph {
14-
constructor(nodes, edges, startTime, initialEnvVars, logger, caller, collectionPath) {
14+
constructor(nodes, edges, startTime, initialEnvVars, logger, collectionPath, timeout, tab) {
1515
this.nodes = nodes;
1616
this.edges = edges;
1717
this.logger = logger;
18-
this.timeout = useCanvasStore.getState().timeout;
18+
this.timeout = timeout;
1919
this.startTime = startTime;
2020
this.graphRunNodeOutput = {};
2121
this.auth = undefined;
2222
this.envVariables = initialEnvVars;
23-
this.caller = caller;
23+
//this.caller = caller;
2424
this.collectionPath = collectionPath;
25+
this.tab = tab;
2526
}
2627

2728
#checkTimeout() {
@@ -71,8 +72,21 @@ class Graph {
7172

7273
if (node.type === 'outputNode') {
7374
this.logger.add(LogLevel.INFO, '', { type: 'outputNode', data: { output: prevNodeOutputData } });
74-
if (this.caller === 'main') {
75-
useCanvasStore.getState().setOutputNode(node.id, prevNodeOutputData);
75+
if (this.tab) {
76+
const updatedNodes = this.nodes.map((nd) => {
77+
if (nd.id === node.id) {
78+
return {
79+
...nd,
80+
data: {
81+
...nd.data,
82+
output: prevNodeOutputData,
83+
},
84+
};
85+
}
86+
87+
return nd;
88+
});
89+
useTabStore.getState().updateFlowTestNodes(updatedNodes);
7690
}
7791
result = {
7892
status: 'Success',
@@ -153,8 +167,9 @@ class Graph {
153167
this.startTime,
154168
this.envVariables,
155169
this.logger,
156-
node.type,
170+
//node.type,
157171
this.collectionPath,
172+
this.timeout,
158173
);
159174
result = await cNode.evaluate();
160175
this.envVariables = result.envVars;
@@ -228,14 +243,20 @@ class Graph {
228243
}
229244

230245
async run() {
231-
// reset every output node for a fresh run
232-
if (this.caller === 'main') {
233-
this.nodes.forEach((node) => {
246+
if (this.tab) {
247+
const updatedNodes = this.nodes.map((node) => {
234248
if (node.type === 'outputNode') {
235-
useCanvasStore.getState().unSetOutputNode(node.id);
249+
if (node.data.output) {
250+
const { ['output']: _, ...data } = node.data;
251+
node.data = data;
252+
}
236253
}
254+
255+
return node;
237256
});
257+
useTabStore.getState().updateFlowTestNodes(updatedNodes);
238258
}
259+
239260
this.graphRunNodeOutput = {};
240261

241262
this.logger.add(LogLevel.INFO, 'Start Flowtest');

src/components/molecules/flow/graph/GraphRun.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import GraphLogger, { LogLevel } from './GraphLogger';
33
import Graph from './Graph';
44
import { useTabStore } from 'stores/TabStore';
55
import { cloneDeep } from 'lodash';
6+
import { uploadGraphRunLogs } from 'service/collection';
7+
import { toast } from 'react-toastify';
68

7-
export const graphRun = async (tab, nodes, edges, collectionPath, selectedEnv) => {
9+
export const graphRun = async (tab, nodes, edges, timeout, collectionPath, selectedEnv) => {
810
useTabStore.getState().updateFlowTestRunStatus(tab.id, true);
911

1012
//runnableEdges(true);
@@ -26,13 +28,25 @@ export const graphRun = async (tab, nodes, edges, collectionPath, selectedEnv) =
2628
startTime,
2729
envVariables,
2830
logger,
29-
'main',
31+
//'main',
3032
collectionPath,
33+
timeout,
34+
tab,
3135
);
3236
const result = await g.run();
3337
const time = Date.now() - startTime;
3438
logger.add(LogLevel.INFO, `Total time: ${time} ms`);
39+
//useTabStore.getState().updateFlowTestRunStatus(tab.id, false);
40+
const logs = logger.get();
41+
console.log(logs);
42+
const response = await uploadGraphRunLogs(tab.name, result.status, time, logs);
43+
useTabStore.getState().updateFlowTestLogs(tab.id, result.status, logs, response);
3544
useTabStore.getState().updateFlowTestRunStatus(tab.id, false);
45+
if (result.status == 'Success') {
46+
toast.success(`FlowTest Run Success!`);
47+
} else if (result.status == 'Failed') {
48+
toast.error(`FlowTest Run Failed!`);
49+
}
3650
//await onGraphComplete(result.status, time, logger.get());
3751
} catch (error) {
3852
const time = Date.now() - startTime;

src/components/molecules/flow/graph/compute/authnode.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,31 @@ import Node from './node';
33
import { LogLevel } from '../GraphLogger';
44

55
class authNode extends Node {
6-
constructor(auth, envVariables, logger) {
6+
constructor(nodeData, envVariables, logger) {
77
super('authNode');
8-
(this.auth = auth), (this.envVariables = envVariables);
8+
(this.nodeData = nodeData), (this.envVariables = envVariables);
99
this.logger = logger;
1010
}
1111

1212
evaluate() {
1313
console.log('Evaluating an auth node');
14-
if (this.auth.type === 'basic-auth') {
14+
if (this.nodeData.type === 'basic-auth') {
1515
this.logger.add(LogLevel.INFO, '', { type: 'authNode', data: { authType: 'Basic Authentication' } });
16-
this.auth.username = computeVariables(this.auth.username, this.envVariables);
17-
this.auth.password = computeVariables(this.auth.password, this.envVariables);
16+
const username = computeVariables(this.nodeData.username, this.envVariables);
17+
const password = computeVariables(this.nodeData.password, this.envVariables);
1818

19-
return this.auth;
19+
return {
20+
type: 'basic-auth',
21+
username,
22+
password,
23+
};
2024
} else if (this.auth.type === 'no-auth') {
2125
this.logger.add(LogLevel.INFO, '', { type: 'authNode', data: { authType: 'No Authentication' } });
22-
return this.auth;
26+
return {
27+
type: 'no-auth',
28+
};
2329
} else {
24-
throw Error(`auth type: ${this.auth.type} is not valid`);
30+
throw Error(`auth type: ${this.nodeData.type} is not valid`);
2531
}
2632
}
2733
}

src/components/molecules/flow/graph/compute/nestedflownode.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import Graph1 from '../Graph';
22
import Node from './node';
33

44
class nestedFlowNode extends Node {
5-
constructor(nodes, edges, startTime, initialEnvVars, logger, caller, collectionPath) {
5+
constructor(nodes, edges, startTime, initialEnvVars, logger, collectionPath, timeout) {
66
super('flowNode');
7-
this.internalGraph = new Graph1(nodes, edges, startTime, initialEnvVars, logger, caller, collectionPath);
7+
this.internalGraph = new Graph1(nodes, edges, startTime, initialEnvVars, logger, collectionPath, timeout);
88
}
99

1010
async evaluate() {

src/components/molecules/flow/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,9 @@ const Flow = ({ tab, collectionId }) => {
269269
const activeEnv = activeCollection?.environments.find((e) => e.name === useTabStore.getState().selectedEnv);
270270
const nodes = cloneDeep(reactFlowInstance.getNodes());
271271
const edges = cloneDeep(reactFlowInstance.getEdges());
272+
const timeout = useCanvasStore.getState().timeout;
272273

273-
graphRun(tab, nodes, edges, activeCollection?.pathname, activeEnv);
274+
graphRun(tab, nodes, edges, timeout, activeCollection?.pathname, activeEnv);
274275
}}
275276
// onClickHandle={async () => {
276277
// runnableEdges(true);

src/stores/CanvasStore.js

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -392,42 +392,6 @@ const useCanvasStore = create((set, get) => ({
392392
},
393393
};
394394
}
395-
396-
return node;
397-
}),
398-
});
399-
useTabStore.getState().updateFlowTestNodes(get().nodes);
400-
},
401-
setOutputNode: (nodeId, output) => {
402-
set({
403-
nodes: get().nodes.map((node) => {
404-
if (node.id === nodeId) {
405-
// it's important to create a new object here, to inform React Flow about the cahnges
406-
return {
407-
...node,
408-
data: {
409-
...node.data,
410-
output,
411-
},
412-
};
413-
}
414-
415-
return node;
416-
}),
417-
});
418-
useTabStore.getState().updateFlowTestNodes(get().nodes);
419-
},
420-
unSetOutputNode: (nodeId) => {
421-
set({
422-
nodes: get().nodes.map((node) => {
423-
if (node.id === nodeId) {
424-
// it's important to create a new object here, to inform React Flow about the cahnges
425-
if (node.data.output) {
426-
const { ['output']: _, ...data } = node.data;
427-
node.data = data;
428-
}
429-
}
430-
431395
return node;
432396
}),
433397
});

0 commit comments

Comments
 (0)