2023-02-20 03:52:06 +00:00
|
|
|
import { Evaluator, Processor } from '..';
|
2022-01-25 18:27:23 +00:00
|
|
|
import { JOB_STATUS } from "../constants";
|
2023-02-20 03:52:06 +00:00
|
|
|
import FlowNodeModel from "../models/FlowNode";
|
|
|
|
import { Instruction } from ".";
|
2022-01-09 14:22:26 +00:00
|
|
|
|
2023-02-20 03:52:06 +00:00
|
|
|
function logicCalculate(calculation, evaluator, scope) {
|
2022-02-27 14:58:41 +00:00
|
|
|
if (!calculation) {
|
2022-01-09 14:22:26 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-20 03:52:06 +00:00
|
|
|
if (typeof calculation === 'object' && calculation.group) {
|
|
|
|
const method = calculation.group.type === 'and' ? 'every' : 'some';
|
|
|
|
return calculation.group.calculations[method](item => logicCalculate(item, evaluator, scope));
|
2022-01-09 14:22:26 +00:00
|
|
|
}
|
|
|
|
|
2023-02-20 03:52:06 +00:00
|
|
|
return evaluator(calculation, scope);
|
2022-01-09 14:22:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default {
|
2023-02-20 03:52:06 +00:00
|
|
|
async run(node: FlowNodeModel, prevJob, processor: Processor) {
|
2022-06-20 15:29:21 +00:00
|
|
|
// TODO(optimize): loading of jobs could be reduced and turned into incrementally in processor
|
|
|
|
// const jobs = await processor.getJobs();
|
2023-02-20 03:52:06 +00:00
|
|
|
const { engine = 'basic', calculation, rejectOnFalse } = node.config || {};
|
|
|
|
const evaluator = <Evaluator | undefined>processor.options.plugin.calculators.get(engine);
|
|
|
|
if (!evaluator) {
|
|
|
|
return {
|
|
|
|
status: JOB_STATUS.ERROR,
|
|
|
|
result: new Error('no calculator engine configured')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const scope = processor.getScope();
|
|
|
|
let result = true;
|
2022-12-06 10:18:40 +00:00
|
|
|
|
2023-02-20 03:52:06 +00:00
|
|
|
try {
|
|
|
|
result = logicCalculate(processor.getParsedValue(calculation), evaluator, scope);
|
|
|
|
} catch (e) {
|
|
|
|
return {
|
|
|
|
result: e.toString(),
|
|
|
|
status: JOB_STATUS.ERROR
|
|
|
|
}
|
|
|
|
}
|
2022-01-25 18:27:23 +00:00
|
|
|
|
2022-02-27 14:58:41 +00:00
|
|
|
if (!result && rejectOnFalse) {
|
2022-01-25 18:27:23 +00:00
|
|
|
return {
|
2023-02-20 03:52:06 +00:00
|
|
|
status: JOB_STATUS.FAILED,
|
2022-01-25 18:27:23 +00:00
|
|
|
result
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const job = {
|
|
|
|
status: JOB_STATUS.RESOLVED,
|
|
|
|
result,
|
|
|
|
// TODO(optimize): try unify the building of job
|
2022-06-24 15:28:49 +00:00
|
|
|
nodeId: node.id,
|
2022-02-27 14:58:41 +00:00
|
|
|
upstreamId: prevJob && prevJob.id || null
|
2022-01-25 18:27:23 +00:00
|
|
|
};
|
|
|
|
|
2022-06-20 15:29:21 +00:00
|
|
|
const branchNode = processor.nodes
|
2022-06-24 15:28:49 +00:00
|
|
|
.find(item => item.upstream === node && Boolean(item.branchIndex) === result);
|
2022-01-25 18:27:23 +00:00
|
|
|
|
|
|
|
if (!branchNode) {
|
|
|
|
return job;
|
|
|
|
}
|
|
|
|
|
2022-06-20 15:29:21 +00:00
|
|
|
const savedJob = await processor.saveJob(job);
|
2022-01-25 18:27:23 +00:00
|
|
|
|
2022-06-20 15:29:21 +00:00
|
|
|
return processor.run(branchNode, savedJob);
|
2022-01-25 18:27:23 +00:00
|
|
|
},
|
|
|
|
|
2023-02-20 03:52:06 +00:00
|
|
|
async resume(node: FlowNodeModel, branchJob, processor: Processor) {
|
2022-01-25 18:27:23 +00:00
|
|
|
if (branchJob.status === JOB_STATUS.RESOLVED) {
|
2022-06-24 15:28:49 +00:00
|
|
|
// return to continue node.downstream
|
2022-02-01 04:04:08 +00:00
|
|
|
return branchJob;
|
2022-01-25 18:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pass control to upper scope by ending current scope
|
2022-06-24 15:28:49 +00:00
|
|
|
return processor.end(node, branchJob);
|
2022-01-09 14:22:26 +00:00
|
|
|
}
|
2023-02-20 03:52:06 +00:00
|
|
|
} as Instruction;
|