Skip to content

Commit 4013017

Browse files
committed
Improve task and tag creation to speed things up
The `createTasks` function is now a bit overly complex but it is at least faster, and still the 50+ channel creation works properly now.
1 parent 01ac962 commit 4013017

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

front/src/components/Dialogs/TaskImportDialog.vue

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
<script lang="ts">
118118
import { useDialogPluginComponent } from 'quasar';
119119
import ctfnote from 'src/ctfnote';
120-
import { Ctf, makeId } from 'src/ctfnote/models';
120+
import { Ctf, makeId, Id, Task } from 'src/ctfnote/models';
121121
import parsers, { ParsedTask } from 'src/ctfnote/parsers';
122122
import { defineComponent, ref } from 'vue';
123123
import TaskTagsList from 'src/components/Task/TaskTagsList.vue';
@@ -284,27 +284,66 @@ export default defineComponent({
284284
const batchSize = 10;
285285
for (let i = 0; i < tasks.length; i += batchSize) {
286286
const batch = tasks.slice(i, i + batchSize);
287+
287288
// Process the first task in the batch serially
288289
// to make sure the Discord bot will create the categories correctly.
290+
let firstTaskResult: {
291+
task: ParsedTask;
292+
taskId: ReturnType<typeof makeId>;
293+
} | null = null;
289294
if (batch.length > 0) {
290295
const firstTask = batch[0];
291296
const r = await this.createTask(this.ctf.id, firstTask);
292297
const newTask = r?.data?.createTask?.task;
293298
if (newTask) {
294-
await this.addTagsForTask(firstTask.tags, makeId(newTask.id));
299+
firstTaskResult = { task: firstTask, taskId: makeId(newTask.id) };
295300
}
296301
}
297-
// Process the rest of the batch in parallel
302+
303+
// Process the rest of the batch in parallel for task creation
304+
const remainingTaskResults: Array<{
305+
task: ParsedTask;
306+
taskId: ReturnType<typeof makeId>;
307+
}> = [];
298308
if (batch.length > 1) {
299-
await Promise.all(
309+
const results = await Promise.all(
300310
batch.slice(1).map(async (task) => {
301311
const r = await this.createTask(this.ctf.id, task);
302312
const newTask = r?.data?.createTask?.task;
303313
if (newTask) {
304-
return this.addTagsForTask(task.tags, makeId(newTask.id));
314+
return { task, taskId: makeId(newTask.id) };
305315
}
316+
return null;
306317
}),
307318
);
319+
remainingTaskResults.push(
320+
...results.filter(
321+
(
322+
t,
323+
): t is { task: ParsedTask; taskId: ReturnType<typeof makeId> } =>
324+
t != null,
325+
),
326+
);
327+
}
328+
329+
// Now process all tag additions in parallel
330+
const tagPromises: Promise<any>[] = [];
331+
if (firstTaskResult) {
332+
tagPromises.push(
333+
this.addTagsForTask(
334+
firstTaskResult.task.tags,
335+
firstTaskResult.taskId as Id<Task>,
336+
),
337+
);
338+
}
339+
tagPromises.push(
340+
...remainingTaskResults.map((result) =>
341+
this.addTagsForTask(result.task.tags, result.taskId as Id<Task>),
342+
),
343+
);
344+
345+
if (tagPromises.length > 0) {
346+
await Promise.all(tagPromises);
308347
}
309348
}
310349
},

0 commit comments

Comments
 (0)