From c120fe477516c4bd22380421563f2e2ea20c8f20 Mon Sep 17 00:00:00 2001 From: skoriop Date: Thu, 25 Jul 2024 20:23:48 +0530 Subject: [PATCH 1/2] feat: update courses in search service --- backend/src/ingestJSON.ts | 71 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/backend/src/ingestJSON.ts b/backend/src/ingestJSON.ts index a9b0583c..3ebdc300 100644 --- a/backend/src/ingestJSON.ts +++ b/backend/src/ingestJSON.ts @@ -1,5 +1,6 @@ import { QueryRunner } from "typeorm"; import { sectionTypeEnum } from "../../lib/src/index.js"; +import { env } from "./config/server.js"; import { Course, Section, Timetable } from "./entity/entities.js"; interface ExamJSON { @@ -325,6 +326,76 @@ export const ingestJSON = async ( .execute(); console.log("sections inserted!"); + console.log("removing old courses from search service..."); + const [oldCourseIds, oldCourseCount] = await queryRunner.manager + .createQueryBuilder() + .select("course.id") + .from(Course, "course") + .where("course.archived = :archived", { archived: true }) + .getManyAndCount(); + console.log(`${oldCourseCount} old courses found`); + for (const { id } of oldCourseIds) { + try { + const searchServiceURL = `${env.SEARCH_SERVICE_URL}/course/remove`; + const res = await fetch(searchServiceURL, { + method: "DELETE", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ id }), + }); + if (!res.ok) { + const resJson = await res.json(); + console.log( + `error while removing course ${id} from search service: ${resJson.error}`, + ); + } + } catch (err: any) { + console.log( + `error while removing course ${id} from search service: ${err.message}`, + ); + } + } + console.log("removed old courses from search service!"); + + console.log("adding updated courses into search service..."); + const [updatedCourseIds, updatedCourseCount] = await queryRunner.manager + .createQueryBuilder() + .select("course.id") + .from(Course, "course") + .getManyAndCount(); + console.log(`${updatedCourseCount} courses found`); + for (const { id } of updatedCourseIds) { + const course = await queryRunner.manager + .createQueryBuilder() + .select("course") + .from(Course, "course") + .leftJoinAndSelect("course.sections", "section") + .where("course.id = :id", { id: id }) + .getOne(); + try { + const searchServiceURL = `${env.SEARCH_SERVICE_URL}/course/add`; + const res = await fetch(searchServiceURL, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(course), + }); + if (!res.ok) { + const resJson = await res.json(); + console.log( + `error while adding course ${id} to search service: ${resJson.error}`, + ); + } + } catch (err: any) { + console.log( + `error while adding course ${id} to search service: ${err.message}`, + ); + } + } + console.log("added updated courses to search service!"); + await queryRunner.commitTransaction(); // show summary of the transaction console.log("================= SUMMARY ================="); From f08baeab3c41b8f70392a83a1ef7e99929bc7243 Mon Sep 17 00:00:00 2001 From: skoriop Date: Thu, 25 Jul 2024 21:37:36 +0530 Subject: [PATCH 2/2] feat: update timetables in search service --- backend/src/ingestJSON.ts | 65 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/backend/src/ingestJSON.ts b/backend/src/ingestJSON.ts index 3ebdc300..8ceb6815 100644 --- a/backend/src/ingestJSON.ts +++ b/backend/src/ingestJSON.ts @@ -2,6 +2,7 @@ import { QueryRunner } from "typeorm"; import { sectionTypeEnum } from "../../lib/src/index.js"; import { env } from "./config/server.js"; import { Course, Section, Timetable } from "./entity/entities.js"; +import sqids from "./utils/sqids.js"; interface ExamJSON { midsem: string | null; @@ -396,6 +397,70 @@ export const ingestJSON = async ( } console.log("added updated courses to search service!"); + console.log("updating timetables in search service..."); + const [timetableIds, timetableCount] = await queryRunner.manager + .createQueryBuilder() + .select("timetable.id") + .from(Timetable, "timetable") + .where("timetable.archived = :archived", { archived: true }) + .getManyAndCount(); + console.log(`${timetableCount} timetables are to be updated`); + for (const { id } of timetableIds) { + const encodedId = sqids.encode([id]); + try { + const searchServiceURL = `${env.SEARCH_SERVICE_URL}/timetable/remove`; + const res = await fetch(searchServiceURL, { + method: "DELETE", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ id: encodedId }), + }); + if (!res.ok) { + const resJson = await res.json(); + console.log( + `error while removing timetable ${id} from search service: ${resJson.error}`, + ); + } + } catch (err: any) { + console.log( + `error while removing timetable ${id} from search service: ${err.message}`, + ); + } + const timetable = await queryRunner.manager + .createQueryBuilder() + .select("timetable") + .from(Timetable, "timetable") + .leftJoinAndSelect("timetable.sections", "section") + .where("timetable.id = :id", { id }) + .getOne(); + const timetableWithSqid = { + ...timetable, + id: encodedId, + }; + try { + const searchServiceURL = `${env.SEARCH_SERVICE_URL}/timetable/add`; + const res = await fetch(searchServiceURL, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(timetableWithSqid), + }); + if (!res.ok) { + const resJson = await res.json(); + console.log( + `error while adding timetable ${id} to search service: ${resJson.error}`, + ); + } + } catch (err: any) { + console.log( + `error while adding timetable ${id} to search service: ${err.message}`, + ); + } + } + console.log("updated timetables in search service!"); + await queryRunner.commitTransaction(); // show summary of the transaction console.log("================= SUMMARY =================");