From db96d0836b7f803dc06717ab484a3166eac0118b Mon Sep 17 00:00:00 2001 From: sai3000pro <96095408+sai3000pro@users.noreply.github.com> Date: Wed, 22 Jan 2025 14:16:26 -0500 Subject: [PATCH 1/3] Added a function to convert a termCode to a proper offering name. --- .../api/controllers/term-name-controller.ts | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/server/api/controllers/term-name-controller.ts b/server/api/controllers/term-name-controller.ts index f34398c..ee151c6 100644 --- a/server/api/controllers/term-name-controller.ts +++ b/server/api/controllers/term-name-controller.ts @@ -37,10 +37,33 @@ export class TermNameController { } } + static convertTermCodeToTermName(termCode: string) { + // termCodes are either in the format of abcd or bcd. + // assuming that a term code that starts with "2" i.e: 2bcd is a year in 2100 + // bc refers to the year XXbc + let term = parseInt(termCode); + let termYear = 1900; + while (term > 1000) { + termYear += 100; + term -= 1000; + } + let monthNum = term % 10; // in theory, termCodes should always end in either 1, 5, or 9 + let season = "FALL"; + if (monthNum == 1) { + season = "WINTER"; + } else if (monthNum == 5) { + season = "SPRING"; + } + term /= 10; // get rid of the ones digit + term = Math.trunc(term); + termYear += term; + return season + " " + termYear; + } + static getTermNameFromTermCode(termCode: string) { const terms = this.getTermsFile(); const target = terms.find((item) => item.termCode === termCode); - return target?.name ?? termCode; + return target?.name ?? this.convertTermCodeToTermName(termCode); } static async overwriteTermsFile() { From c2a7db431e85ae1ab4a390014b57a6b1ea6ed51f Mon Sep 17 00:00:00 2001 From: sai3000pro <96095408+sai3000pro@users.noreply.github.com> Date: Tue, 28 Jan 2025 21:53:28 -0500 Subject: [PATCH 2/3] Implementing a cache --- server/api/controllers/term-name-controller.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/server/api/controllers/term-name-controller.ts b/server/api/controllers/term-name-controller.ts index ee151c6..a7a8ea4 100644 --- a/server/api/controllers/term-name-controller.ts +++ b/server/api/controllers/term-name-controller.ts @@ -6,6 +6,7 @@ import tokens from "../../../config"; export class TermNameController { static logger = new Logger("Term Name Controller"); + static termCache = new Map(); // cache of termCodes static async getTermNames(): Promise { const url = `${tokens.WATERLOO_OPEN_API_BASE_URL}/Terms`; @@ -38,6 +39,9 @@ export class TermNameController { } static convertTermCodeToTermName(termCode: string) { + if (this.termCache.has(termCode)) { + return this.termCache.get(termCode); // Return from cache + } // termCodes are either in the format of abcd or bcd. // assuming that a term code that starts with "2" i.e: 2bcd is a year in 2100 // bc refers to the year XXbc @@ -56,8 +60,12 @@ export class TermNameController { } term /= 10; // get rid of the ones digit term = Math.trunc(term); - termYear += term; - return season + " " + termYear; + const termName = season + " " + termYear; + + // Cache the result + this.termCache.set(termCode, termName); + + return termName; } static getTermNameFromTermCode(termCode: string) { From 9bc6af90e070cd246d509378d7e24bf2db41d151 Mon Sep 17 00:00:00 2001 From: sai3000pro <96095408+sai3000pro@users.noreply.github.com> Date: Tue, 28 Jan 2025 22:05:29 -0500 Subject: [PATCH 3/3] Fixed minor bug --- server/api/controllers/term-name-controller.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/server/api/controllers/term-name-controller.ts b/server/api/controllers/term-name-controller.ts index a7a8ea4..24945c2 100644 --- a/server/api/controllers/term-name-controller.ts +++ b/server/api/controllers/term-name-controller.ts @@ -60,6 +60,7 @@ export class TermNameController { } term /= 10; // get rid of the ones digit term = Math.trunc(term); + termYear += term; const termName = season + " " + termYear; // Cache the result