Skip to content

Commit

Permalink
Revert "feat/search by section code (#1129)"
Browse files Browse the repository at this point in the history
This reverts commit 4915764.
  • Loading branch information
MinhxNguyen7 committed Jan 22, 2025
1 parent 4915764 commit b82f6d0
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 248 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const emojiMap: Record<string, string> = {
GE_CATEGORY: '🏫', // U+1F3EB :school:
DEPARTMENT: '🏢', // U+1F3E2 :office:
COURSE: '📚', // U+1F4DA :books:
SECTION: '📝', // U+1F4DD :memo:
};

const romanArr = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII'];
Expand Down Expand Up @@ -80,10 +79,6 @@ class FuzzySearch extends PureComponent<FuzzySearchProps, FuzzySearchState> {
RightPaneStore.updateFormValue('courseNumber', ident[0].split(' ').slice(-1)[0]);
break;
}
case emojiMap.SECTION: {
RightPaneStore.updateFormValue('sectionCode', ident[0].split(' ').slice(0)[0]);
break;
}
default:
break;
}
Expand Down Expand Up @@ -111,8 +106,6 @@ class FuzzySearch extends PureComponent<FuzzySearchProps, FuzzySearchState> {
return `${emojiMap.DEPARTMENT} ${option}: ${object.name}`;
case 'COURSE':
return `${emojiMap.COURSE} ${object.metadata.department} ${object.metadata.number}: ${object.name}`;
case 'SECTION':
return `${emojiMap.SECTION} ${object.sectionCode} ${object.sectionType} ${object.sectionNum}: ${object.department} ${object.courseNumber}`;
default:
return '';
}
Expand All @@ -128,7 +121,7 @@ class FuzzySearch extends PureComponent<FuzzySearchProps, FuzzySearchState> {
maybeDoSearchFactory = (requestTimestamp: number) => () => {
if (!this.requestIsCurrent(requestTimestamp)) return;
trpc.search.doSearch
.query({ query: this.state.value, term: RightPaneStore.getFormData().term })
.query({ query: this.state.value })
.then((result) => {
if (!this.requestIsCurrent(requestTimestamp)) return;
this.setState({
Expand Down
40 changes: 2 additions & 38 deletions apps/backend/scripts/get-search-data.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import { mkdir, writeFile, appendFile} from 'node:fs/promises';
import { mkdir, writeFile } from 'node:fs/promises';
import {Course, CourseSearchResult, DepartmentSearchResult} from '@packages/antalmanac-types';
import { queryGraphQL } from 'src/lib/helpers';
import { parseSectionCodes, SectionCodesGraphQLResponse, termData } from 'src/lib/term-section-codes';

import "dotenv/config";

Expand Down Expand Up @@ -52,46 +50,12 @@ async function main() {
});
}
console.log(`Fetched ${deptMap.size} departments.`);

const QUERY_TEMPLATE = `{
websoc(query: {year: "$$YEAR$$", quarter: $$QUARTER$$}) {
schools {
departments {
deptCode
courses {
courseTitle
courseNumber
sections {
sectionCode
sectionType
sectionNum
}
}
}
}
}
}`;
await mkdir(join(__dirname, "../src/generated/"), { recursive: true });
await writeFile(join(__dirname, "../src/generated/searchData.ts"), `
import type { CourseSearchResult, DepartmentSearchResult, SectionSearchResult } from "@packages/antalmanac-types";
import type { CourseSearchResult, DepartmentSearchResult } from "@packages/antalmanac-types";
export const departments: Array<DepartmentSearchResult & { id: string }> = ${JSON.stringify(Array.from(deptMap.values()))};
export const courses: Array<CourseSearchResult & { id: string }> = ${JSON.stringify(Array.from(courseMap.values()))};
`)
let count = 0;
for (const term of termData){
const [year, quarter] = term.shortName.split(" ");
const query = QUERY_TEMPLATE.replace("$$YEAR$$", year).replace("$$QUARTER$$", quarter);
const res = await queryGraphQL<SectionCodesGraphQLResponse>(query);
if (!res) {
throw new Error("Error fetching section codes.");
}
const parsedSectionData = parseSectionCodes(res);
console.log(`Fetched ${Object.keys(parsedSectionData).length} course codes for ${term.shortName} from Anteater API.`);
count += Object.keys(parsedSectionData).length;
await appendFile(join(__dirname, "../src/generated/searchData.ts"), `export const ${quarter}${year}: Record<string, SectionSearchResult> = ${JSON.stringify(parsedSectionData)};
`)
}
console.log(`Fetched ${count} course codes for ${termData.length} terms from Anteater API.`);
console.log("Cache generated.");
}

Expand Down
20 changes: 0 additions & 20 deletions apps/backend/src/lib/helpers.ts

This file was deleted.

133 changes: 0 additions & 133 deletions apps/backend/src/lib/term-section-codes.ts

This file was deleted.

50 changes: 11 additions & 39 deletions apps/backend/src/routers/search.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { z } from 'zod';
import type { GESearchResult, SearchResult, SectionSearchResult } from '@packages/antalmanac-types';
import type { GESearchResult, SearchResult } from '@packages/antalmanac-types';
import uFuzzy from '@leeoniya/ufuzzy';
import * as fuzzysort from "fuzzysort";
import { procedure, router } from '../trpc';
import * as searchData from "../generated/searchData";

type SearchDataExports = keyof typeof searchData;
import {courses, departments} from "../generated/searchData";

const geCategoryKeys = ['ge1a', 'ge1b', 'ge2', 'ge3', 'ge4', 'ge5a', 'ge5b', 'ge6', 'ge7', 'ge8'] as const;

Expand Down Expand Up @@ -33,50 +31,24 @@ const toMutable = <T>(arr: readonly T[]): T[] => arr as T[];

const searchRouter = router({
doSearch: procedure
.input(z.object({ query: z.string(), term: z.string()}))
.input(z.object({ query: z.string() }))
.query(async ({ input }): Promise<Record<string, SearchResult>> => {
const { query } = input;

const [year, quarter] = input.term.split(" ");
const parsedTerm = `${quarter}${year}`;
const termData = searchData[parsedTerm as SearchDataExports] as Record<string, SectionSearchResult>;

const num = Number(input.query);
const matchedSections: SectionSearchResult[] = [];
if (!isNaN(num) && num >= 0 && Number.isInteger(num)) {
const baseCourseCode = input.query;
if (input.query.length === 4) {
for (let i =0; i < 10; i++){
const possibleCourseCode = `${baseCourseCode}${i}`;
if (termData[possibleCourseCode]) {
matchedSections.push(termData[possibleCourseCode]);
}
}
} else if (input.query.length === 5) {
if (termData[baseCourseCode]) {
matchedSections.push(termData[baseCourseCode]);
}
}
}

const u = new uFuzzy();
const matchedGEs = u.search(toMutable(geCategoryKeys), query)[0]?.map((i) => geCategoryKeys[i]) ?? [];
if (matchedGEs.length) return Object.fromEntries(matchedGEs.map(toGESearchResult));

const matchedDepts = matchedSections.length === 10 ? [] : fuzzysort.go(query, searchData.departments, {
const matchedDepts = fuzzysort.go(query, departments, {
keys: ['id', 'alias'],
limit: 10 - matchedSections.length
limit: 10
})
const matchedCourses = matchedSections.length + matchedDepts.length === 10 ? [] : fuzzysort.go(query, searchData.courses, {
const matchedCourses = matchedDepts.length === 10 ? [] : fuzzysort.go(query, courses, {
keys: ['id', 'name', 'alias', 'metadata.department', 'metadata.number'],
limit: 10 - matchedDepts.length - matchedSections.length
limit: 10 - matchedDepts.length
})

return Object.fromEntries([
...matchedSections.map(x => [x.sectionCode, x]),
...matchedDepts.map(x => [x.obj.id, x.obj]),
...matchedCourses.map(x => [x.obj.id, x.obj]),
]);
return Object.fromEntries(
[...matchedDepts.map(x => [x.obj.id, x.obj]),
...matchedCourses.map(x => [x.obj.id, x.obj]),]
);
}),
});

Expand Down
11 changes: 1 addition & 10 deletions packages/types/src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,4 @@ export type CourseSearchResult = {
};
};

export type SectionSearchResult = {
type: 'SECTION';
department: string;
courseNumber: string;
sectionCode: string;
sectionNum: string;
sectionType: string;
};

export type SearchResult = GESearchResult | DepartmentSearchResult | CourseSearchResult | SectionSearchResult;
export type SearchResult = GESearchResult | DepartmentSearchResult | CourseSearchResult;

0 comments on commit b82f6d0

Please sign in to comment.