Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: caching & linking to prof #15

Merged
merged 9 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ png/.DS_Store
.vscode
node_modules
dist
.parcel-cache
.parcel-cache
*.zip
8 changes: 8 additions & 0 deletions .parcelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "@parcel/config-default",
"transformers": {
"*.ts": [
"@parcel/transformer-typescript-tsc"
]
}
}
Binary file removed VandyScheduler.zip
Binary file not shown.
11 changes: 11 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ export default [
languageOptions: {
globals: { ...globals.browser, ...globals.webextensions },
},
rules: {
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': [
'error',
{
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
caughtErrorsIgnorePattern: '^_',
},
],
},
},
pluginJs.configs.recommended,
...tseslint.configs.recommended,
Expand Down
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"service_worker": {
"source": "src/service_worker/background.ts",
"includeNodeModules": true,
"outputFormat": "esmodule"
}
},
Expand Down Expand Up @@ -62,7 +63,10 @@
"release": "chrome-webstore-upload"
},
"devDependencies": {
"@babel/core": "^7.12.0",
"@eslint/js": "^9.4.0",
"@parcel/config-default": "^2.12.0",
"@parcel/transformer-typescript-tsc": "^2.12.0",
"@semantic-release/exec": "^6.0.3",
"@tsconfig/recommended": "^1.0.2",
"@types/chrome": "^0.0.241",
Expand All @@ -72,12 +76,13 @@
"eslint": "^9.4.0",
"globals": "^15.4.0",
"nodemon": "^3.1.3",
"parcel": "^2.9.0",
"parcel": "^2.12.0",
"semantic-release": "^24.0.0",
"typescript": "^5.2.2",
"typescript": "<5.5.0",
"typescript-eslint": "^7.13.0"
},
"dependencies": {
"jquery": "^3.7.0"
"jquery": "^3.7.0",
"string-similarity-js": "^2.1.4"
}
}
1,526 changes: 948 additions & 578 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions src/api/RateMyProfessor/RateMyProfessorApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,38 @@ export const getResult = async <T>(
return result.filter((r) => r && r.length > 0).reverse()[0] ?? [];
};

export function withCache<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Method extends (...args: any[]) => Promise<Teacher[]>
>(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
target: any,
propertyKey: string,
descriptor: PropertyDescriptor
) {
const originalMethod = descriptor.value;
descriptor.value = async function replacementMethod(
this: RateMyProfessorApi,
...args: Parameters<Method>
) {
const key = args.join('');

if (key in this.cache) {
console.log('found in cache', key, this.cache[key]);
return this.cache[key];
}
const result = await originalMethod?.call(this, ...args);

console.log('not found in cache', key, result);

this.cache[key] = result;
return result;
};
}

export abstract class RateMyProfessorApi implements IRateMyProfessor {
protected cache: Record<string, Teacher[]> = {};

protected apiEndpoint = 'https://www.ratemyprofessors.com/';

protected getUrl(
Expand Down
40 changes: 25 additions & 15 deletions src/api/RateMyProfessor/RateMyProfessorGraphql.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DEFAULT_SCHOOL_ID, DEFAULT_SCHOOL_NAME } from '../../constants';
import { RateMyProfessorApi, getResult } from './RateMyProfessorApi';
import { RateMyProfessorApi, getResult, withCache } from './RateMyProfessorApi';
import { stringSimilarity } from 'string-similarity-js';
import {
TeacherQueryVariables,
TeacherQueryResponse,
Expand Down Expand Up @@ -39,6 +40,7 @@ export class RateMyProfessorGraphql extends RateMyProfessorApi {
avgDifficultyRounded
avgRatingRounded
id
legacyId
firstName
lastName
numRatings
Expand Down Expand Up @@ -141,32 +143,40 @@ export class RateMyProfessorGraphql extends RateMyProfessorApi {
return this.parseSchoolQueryResponse(res)[0]?.id;
}

async getProfId(
@withCache
private async getTeachers(
profName: string,
schoolId = DEFAULT_SCHOOL_ID
): Promise<string | undefined> {
const queryResult = await getResult(profName, (n) =>
): Promise<Teacher[]> {
const result = await getResult(profName, (n) =>
this.queryTeachers({
teacherName: n,
schoolId,
number: 1,
number: 3,
}).then(this.getTeachersFromQuery)
);

return queryResult[0]?.id;
return result
.map((t) => ({
similarity: stringSimilarity(profName, `${t.lastName}, ${t.firstName}`),
teacher: t,
}))
.sort((a, b) => b.similarity - a.similarity)
.map(({ teacher }) => teacher);
}

async getProfId(
...args: Parameters<typeof this.getTeachers>
): Promise<string | undefined> {
const queryResult = await this.getTeachers(...args);
// TODO: if more than one result, we should allow the user to see different options
return queryResult[0]?.legacyId;
}

async getOverallScore(
profName: string,
schoolId = DEFAULT_SCHOOL_ID
...args: Parameters<typeof this.getTeachers>
): Promise<number | undefined> {
const queryResult = await getResult(profName, (n) =>
this.queryTeachers({
teacherName: n,
schoolId,
number: 1,
}).then(this.getTeachersFromQuery)
);
const queryResult = await this.getTeachers(...args);

return queryResult[0]?.avgRatingRounded;
}
Expand Down
1 change: 1 addition & 0 deletions src/api/RateMyProfessor/graphql.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type Teacher = {
avgDifficultyRounded: number;
avgRatingRounded: number;
id: string;
legacyId: string;
firstName: string;
lastName: string;
numRatings: number;
Expand Down
3 changes: 2 additions & 1 deletion src/content_scripts/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './ratemyprof';

export * from './content';
export * from './ratemyprof';
export * from './constants';
Loading