Skip to content

Commit 8467f8e

Browse files
committed
fix(scraper): reorder group objects after fetching
today's session of pointless debugging was brought to you by today's sponsor, adonis! do you want your code to absolutely explode every time you attempt to do a bulk SQL action? do you despise the common-sense assumptions, such as the bulk fetch function returning items in the same order as in the list you provided? do you like wasting hours sitting in the debugger, inventing new debugging techniques, such as setting a conditional breakpoint on `Math.random() < 0.001`? then adonis is perfect for you! rewrite your web project in adonis today! use promo code `mini_bomba` to get 50% more pointless debugging for your first rewrite and a free database implosion on your first tests in production!
1 parent 0c7dba0 commit 8467f8e

File tree

1 file changed

+31
-46
lines changed

1 file changed

+31
-46
lines changed

backend/commands/scraper.ts

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -317,32 +317,6 @@ export default class Scraper extends BaseCommand {
317317
WHERE "group_lecturers"."lecturer_id" NOT IN (SELECT DISTINCT "lecturer_id" FROM "group_archive_lecturers" WHERE "group_archive_lecturers"."group_id" = "group_lecturers"."group_id");
318318
COMMIT;
319319
`);
320-
//
321-
//const currentGroups = await Group.all();
322-
//
323-
//const archivedGroups = await Promise.all(
324-
// chunkArray(currentGroups, QUERY_CHUNK_SIZE).map((chunk) =>
325-
// this.dbSemaphore.runTask(() =>
326-
// GroupArchive.updateOrCreateMany(
327-
// "id",
328-
// chunk.map((g) => g.$attributes),
329-
// ),
330-
// ),
331-
// ),
332-
//).then((a) => a.flat());
333-
//
334-
//task.update("Synchronizing archived group lecturers");
335-
//await Promise.all(
336-
// zip(currentGroups, archivedGroups).map(([group, groupArchive]) =>
337-
// this.dbSemaphore.runTask(async () => {
338-
// const lecturers = await group.related("lecturers").query();
339-
//
340-
// await groupArchive
341-
// .related("lecturers")
342-
// .sync(lecturers.map((lecturer) => lecturer.id));
343-
// }),
344-
// ),
345-
//);
346320
}
347321

348322
async scrapeGroupsTask(task: TaskHandle) {
@@ -404,21 +378,26 @@ export default class Scraper extends BaseCommand {
404378
);
405379

406380
// Then fetch/create their IDs from the DB
407-
const lecturersToInsert = lecturerSet.map((lecturer) => {
408-
const [name, ...surnameParts] = lecturer.split(" ");
409-
const surname = surnameParts.join(" ");
410-
return { name, surname };
411-
});
412-
const lecturersIds = await Promise.all(
413-
chunkArray(lecturersToInsert, QUERY_CHUNK_SIZE).map((chunk) =>
414-
this.dbSemaphore.runTask(() =>
415-
Lecturer.fetchOrCreateMany(["name", "surname"], chunk),
381+
// and collect it into a map
382+
const lecturerMap = new Map<string, number>(
383+
await Promise.all(
384+
chunkArray(lecturerSet, QUERY_CHUNK_SIZE).map((chunk) =>
385+
this.dbSemaphore.runTask(async () => {
386+
return zip(
387+
chunk,
388+
await Lecturer.fetchOrCreateMany(
389+
["name", "surname"],
390+
chunk.map((lecturer) => {
391+
const [name, ...surnameParts] = lecturer.split(" ");
392+
const surname = surnameParts.join(" ");
393+
return { name, surname };
394+
}),
395+
).then((r) => r.map((l) => l.id)),
396+
);
397+
}),
416398
),
417-
),
418-
).then((a) => a.flat().map((l) => l.id));
419-
420-
// Finally, create a map of lecturer name to ID
421-
const lecturerMap = new Map(zip(lecturerSet, lecturersIds));
399+
).then((r) => r.flat(1)),
400+
);
422401

423402
task.update("Updating groups");
424403
const currentDate = DateTime.now();
@@ -500,14 +479,20 @@ export default class Scraper extends BaseCommand {
500479
)
501480
.merge(mergedProps)
502481
.returning("id")) as { id: number }[];
503-
const updatedGroups = await Group.findMany(ids.map((i) => i.id));
504-
return zip(updatedGroups, chunk).map(([group, { lecturers }]) => {
482+
// thanks adonis for returning objects in an arbitrary order
483+
const updatedGroups = new Map(
484+
await Group.findMany(ids.map((i) => i.id)).then((l) =>
485+
l.map((group) => [group.id, group]),
486+
),
487+
);
488+
const reorderedGroups = ids.map(({ id }) => {
489+
const group = updatedGroups.get(id);
490+
assert(group !== undefined);
491+
return group;
492+
});
493+
return zip(reorderedGroups, chunk).map(([group, { lecturers }]) => {
505494
return { group, lecturers };
506495
});
507-
//Group.updateOrCreateMany(
508-
// ["url", "startTime", "day", "week", "courseId"],
509-
// chunk,
510-
//),
511496
}),
512497
),
513498
).then((a) => a.flat());

0 commit comments

Comments
 (0)