Skip to content

Commit

Permalink
run readChartNode in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
HUNZIKER Dominique committed Sep 29, 2022
1 parent bb5ba11 commit d01c2d2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/lib/flows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { removeCreateDir } from '../utils/checks';
import { pullLocales } from './locales';
import translateFlowNode, { translateIntentExampleSentence, translateSayNode } from '../utils/translators';
import { makeAxiosRequest } from '../utils/axiosClient';
import { runInParallel } from '../utils/parallel';

import { indexAll } from '../utils/indexAll';

Expand Down Expand Up @@ -96,17 +97,19 @@ export const pullFlow = async (flowName: string, availableProgress: number): Pro
const progressPerNode = progressPerLocale / 2 / chart.nodes.length;

// iterate through all Nodes for this chart and add the information into the chart
for (let node of chart.nodes) {
const Node = await CognigyClient.readChartNode({
"nodeId": node._id,
"resourceId": flow._id,
"resourceType": "flow",
"preferredLocaleId": locale._id
});
node["config"] = Node.config;
delete node["preview"];
addToProgressBar(progressPerNode);
}
await runInParallel(async (node: any) => {
const Node = await CognigyClient.readChartNode({
"nodeId": node._id,
"resourceId": flow._id,
"resourceType": "flow",
"preferredLocaleId": locale._id
});
node["config"] = Node.config;
delete node["preview"];
addToProgressBar(progressPerNode);
},
chart.nodes,
10);

fs.writeFileSync(localeDir + "/chart.json", JSON.stringify(chart, undefined, 4));

Expand Down Expand Up @@ -415,7 +418,7 @@ export interface ITranslateFlowOptions {
}

/**
*
*
* @param flowName The name of the flow
* @param fromLanguage The locale in the flow that should be translated
* @param targetLanguage The target langauge to translate to
Expand Down
19 changes: 19 additions & 0 deletions src/utils/parallel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const runInParallel = async <T> (
fn: (args: T) => void,
array: T[],
numParallelJobs: number) => {
const iter = array.values();

const run = async () => {
for (;;) {
const { value, done } = iter.next();

if (done)
break;

await fn(value);
}
};

await Promise.all(Array.apply(null, Array(numParallelJobs)).map(_ => run()));
}

0 comments on commit d01c2d2

Please sign in to comment.