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

Update exam clash detection in the Module search page #3678

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
70 changes: 62 additions & 8 deletions website/src/views/modules/ModuleFinderSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
import { Filter } from 'react-feather';
import { State as StoreState } from 'types/state';

import { attributeDescription, NUSModuleAttributes, Semester, Semesters } from 'types/modules';
import {
attributeDescription,
Module,
NUSModuleAttributes,
Semester,
Semesters,
} from 'types/modules';
import { RefinementItem } from 'types/views';

import SideMenu, { OPEN_MENU_LABEL } from 'views/components/SideMenu';
Expand All @@ -27,6 +33,11 @@
import styles from './ModuleFinderSidebar.scss';
import ChecklistFilter, { FilterItem } from '../components/filters/ChecklistFilter';

type ExamTiming = {
start: string;
duration: number;
};

const RESET_FILTER_OPTIONS = { filter: true };

const STATIC_EXAM_FILTER_ITEMS: FilterItem[] = [
Expand All @@ -50,7 +61,42 @@
},
];

function getExamClashFilter(semester: Semester, examDates: string[]): FilterItem {
function getExamClashFilter(semester: Semester, examTimings: ExamTiming[]): FilterItem {

Check warning on line 64 in website/src/views/modules/ModuleFinderSidebar.tsx

View check run for this annotation

Codecov / codecov/patch

website/src/views/modules/ModuleFinderSidebar.tsx#L64

Added line #L64 was not covered by tests
// @param startTime is an ISO string in UTC timezone
const getEndTime = (startTime: string, duration: number): string => {
const endTime = new Date(startTime);
endTime.setMinutes(endTime.getMinutes() + duration);
return endTime.toISOString();

Check warning on line 69 in website/src/views/modules/ModuleFinderSidebar.tsx

View check run for this annotation

Codecov / codecov/patch

website/src/views/modules/ModuleFinderSidebar.tsx#L66-L69

Added lines #L66 - L69 were not covered by tests
};
// For each exam1, map it to an Elasticsearch query that will return True
// if another exam clashes with exam1. For example, exam2 clashes (i.e. overlaps)
// with exam1 iff (exam2.start < exam1.end) && (exam2.end > exam1.start)
const clashRanges = examTimings.map((exam1) => ({

Check warning on line 74 in website/src/views/modules/ModuleFinderSidebar.tsx

View check run for this annotation

Codecov / codecov/patch

website/src/views/modules/ModuleFinderSidebar.tsx#L74

Added line #L74 was not covered by tests
bool: {
must: [
{
range: {
'semesterData.examDate': {
lt: getEndTime(exam1.start, exam1.duration),
},
},
},
{
script: {
script: {
source: `doc.containsKey('semesterData.examDate') &&
doc.containsKey('semesterData.examDuration') &&
doc['semesterData.examDate'].value.plusMinutes(doc['semesterData.examDuration'].value).isAfter(ZonedDateTime.parse(params.exam1start))`,
params: {
exam1start: exam1.start,
},
},
},
},
],
},
}));

return {
key: `no-exam-clash-${semester}`,
label: `No Exam Clash (${config.shortSemesterNames[semester]})`,
Expand All @@ -60,8 +106,8 @@
nested: {
path: 'semesterData',
query: {
terms: {
'semesterData.examDate': examDates,
bool: {
must_not: clashRanges,
},
},
},
Expand All @@ -83,10 +129,18 @@
const examClashFilters = Semesters.map((semester): FilterItem | null => {
const timetable = getSemesterTimetable(semester);
const modules = getSemesterModules(timetable, allModules);
const examDates = modules
.map((module) => getModuleSemesterData(module, semester)?.examDate)
.filter(notNull);
return examDates.length ? getExamClashFilter(semester, examDates) : null;
// Filter for modules with non-empty exam timings, and map them to new ExamTiming objects
const examTimings = modules.reduce<ExamTiming[]>((result: ExamTiming[], mod: Module) => {
const data = getModuleSemesterData(mod, semester);
if (data?.examDate && data?.examDuration) {
result.push({

Check warning on line 136 in website/src/views/modules/ModuleFinderSidebar.tsx

View check run for this annotation

Codecov / codecov/patch

website/src/views/modules/ModuleFinderSidebar.tsx#L133-L136

Added lines #L133 - L136 were not covered by tests
start: data.examDate,
duration: data.examDuration,
});
}
return result;

Check warning on line 141 in website/src/views/modules/ModuleFinderSidebar.tsx

View check run for this annotation

Codecov / codecov/patch

website/src/views/modules/ModuleFinderSidebar.tsx#L141

Added line #L141 was not covered by tests
}, []);
return examTimings.length ? getExamClashFilter(semester, examTimings) : null;

Check warning on line 143 in website/src/views/modules/ModuleFinderSidebar.tsx

View check run for this annotation

Codecov / codecov/patch

website/src/views/modules/ModuleFinderSidebar.tsx#L143

Added line #L143 was not covered by tests
}).filter(notNull);
return [...STATIC_EXAM_FILTER_ITEMS, ...examClashFilters];
}, [getSemesterTimetable, allModules]);
Expand Down