Skip to content

Commit 5f2e0bb

Browse files
thomasyzy7dawangk
andauthored
Ty/scrum 155 generator refinement (#107)
Co-authored-by: dawangk <[email protected]>
1 parent 440f93a commit 5f2e0bb

File tree

16 files changed

+440
-90
lines changed

16 files changed

+440
-90
lines changed
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
import { describe, expect, it, test } from "@jest/globals";
2+
3+
import { Offering } from "../src/types/generatorTypes";
4+
import {
5+
createOffering,
6+
getMinHour,
7+
getMinHourDay,
8+
} from "../src/utils/generatorHelpers";
9+
10+
describe("getMinHourDay function", () => {
11+
it("Back to back to back courses", async () => {
12+
const offering1: Offering = createOffering({
13+
id: 1,
14+
course_id: 101,
15+
day: "MO",
16+
start: "09:00:00",
17+
end: "10:00:00",
18+
});
19+
const offering2: Offering = createOffering({
20+
id: 2,
21+
course_id: 102,
22+
day: "MO",
23+
start: "10:00:00",
24+
end: "11:00:00",
25+
});
26+
const offering3: Offering = createOffering({
27+
id: 3,
28+
course_id: 103,
29+
day: "MO",
30+
start: "11:00:00",
31+
end: "12:00:00",
32+
});
33+
const schedule: Offering[] = [offering1, offering2, offering3];
34+
35+
const result = getMinHourDay(schedule, 0);
36+
37+
expect(result).toBe(true);
38+
});
39+
40+
it("courses that has a max gap of 4 hours", async () => {
41+
const offering1: Offering = createOffering({
42+
id: 1,
43+
course_id: 101,
44+
day: "MO",
45+
start: "09:00:00",
46+
end: "10:00:00",
47+
});
48+
const offering2: Offering = createOffering({
49+
id: 2,
50+
course_id: 102,
51+
day: "MO",
52+
start: "10:00:00",
53+
end: "11:00:00",
54+
});
55+
const offering3: Offering = createOffering({
56+
id: 3,
57+
course_id: 103,
58+
day: "MO",
59+
start: "15:00:00",
60+
end: "16:00:00",
61+
});
62+
const schedule: Offering[] = [offering3, offering2, offering1];
63+
64+
const result = getMinHourDay(schedule, 3);
65+
66+
expect(result).toBe(false);
67+
});
68+
69+
it("only 1 offering in list, return 0", async () => {
70+
const offering1: Offering = createOffering({
71+
id: 1,
72+
course_id: 101,
73+
day: "MO",
74+
start: "09:00:00",
75+
end: "10:00:00",
76+
});
77+
const schedule: Offering[] = [offering1];
78+
79+
const result = getMinHourDay(schedule, 23);
80+
81+
expect(result).toBe(true);
82+
});
83+
84+
it("getMinHour test", async () => {
85+
const arr_day = [
86+
"MO",
87+
"MO",
88+
"TU",
89+
"TH",
90+
"FR",
91+
"MO",
92+
"TU",
93+
"TH",
94+
"MO",
95+
"MO",
96+
];
97+
const arr_start = [
98+
"09:00:00",
99+
"10:00:00",
100+
"09:00:00",
101+
"12:00:00",
102+
"13:00:00",
103+
"12:00:00",
104+
"14:00:00",
105+
"16:00:00",
106+
"13:00:00",
107+
"15:00:00",
108+
];
109+
const arr_end = [
110+
"10:00:00",
111+
"11:00:00",
112+
"10:00:00",
113+
"15:00:00",
114+
"16:00:00",
115+
"13:00:00",
116+
"19:00:00",
117+
"18:00:00",
118+
"14:00:00",
119+
"18:00:00",
120+
];
121+
const schedule: Offering[] = [];
122+
for (let i = 0; i < 10; i++) {
123+
schedule.push(
124+
createOffering({
125+
id: i,
126+
course_id: 100 + i,
127+
day: arr_day[i],
128+
start: arr_start[i],
129+
end: arr_end[i],
130+
}),
131+
);
132+
}
133+
134+
const result = getMinHour(schedule, 4);
135+
136+
expect(result).toEqual(true);
137+
});
138+
139+
it("getMinHour test 2", async () => {
140+
const arr_day = [
141+
"MO",
142+
"MO",
143+
"TU",
144+
"TH",
145+
"FR",
146+
"MO",
147+
"TU",
148+
"TH",
149+
"MO",
150+
"MO",
151+
];
152+
const arr_start = [
153+
"09:00:00",
154+
"10:00:00",
155+
"09:00:00",
156+
"12:00:00",
157+
"13:00:00",
158+
"12:00:00",
159+
"14:00:00",
160+
"16:00:00",
161+
"13:00:00",
162+
"15:00:00",
163+
];
164+
const arr_end = [
165+
"10:00:00",
166+
"11:00:00",
167+
"10:00:00",
168+
"15:00:00",
169+
"16:00:00",
170+
"13:00:00",
171+
"19:00:00",
172+
"18:00:00",
173+
"14:00:00",
174+
"18:00:00",
175+
];
176+
const schedule: Offering[] = [];
177+
for (let i = 0; i < 10; i++) {
178+
schedule.push(
179+
createOffering({
180+
id: i,
181+
course_id: 100 + i,
182+
day: arr_day[i],
183+
start: arr_start[i],
184+
end: arr_end[i],
185+
}),
186+
);
187+
}
188+
189+
const result = getMinHour(schedule, 3);
190+
191+
expect(result).toEqual(false);
192+
});
193+
});

course-matrix/backend/__tests__/isValidOffering.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { describe, expect, it, test } from "@jest/globals";
22

3-
import { createOffering, isValidOffering } from "../src/utils/generatorHelpers";
43
import {
54
Offering,
65
Restriction,
76
RestrictionType,
87
} from "../src/types/generatorTypes";
8+
import { createOffering, isValidOffering } from "../src/utils/generatorHelpers";
99

1010
describe("isValidOffering", () => {
1111
const sampleOffering: Offering = createOffering({
@@ -29,6 +29,7 @@ describe("isValidOffering", () => {
2929
endTime: "09:00:00",
3030
disabled: true,
3131
numDays: 0,
32+
maxGap: 24,
3233
},
3334
];
3435
expect(isValidOffering(sampleOffering, restrictions)).toBe(true);
@@ -43,6 +44,7 @@ describe("isValidOffering", () => {
4344
endTime: "11:00:00",
4445
disabled: false,
4546
numDays: 0,
47+
maxGap: 24,
4648
},
4749
];
4850
expect(isValidOffering(sampleOffering, restrictions)).toBe(false);
@@ -57,6 +59,7 @@ describe("isValidOffering", () => {
5759
endTime: "",
5860
disabled: false,
5961
numDays: 0,
62+
maxGap: 24,
6063
},
6164
];
6265
expect(isValidOffering(sampleOffering, restrictions)).toBe(false);
@@ -71,6 +74,7 @@ describe("isValidOffering", () => {
7174
endTime: "12:00:00",
7275
disabled: false,
7376
numDays: 0,
77+
maxGap: 24,
7478
},
7579
];
7680
expect(isValidOffering(sampleOffering, restrictions)).toBe(false);
@@ -85,6 +89,7 @@ describe("isValidOffering", () => {
8589
endTime: "",
8690
disabled: false,
8791
numDays: 0,
92+
maxGap: 24,
8893
},
8994
];
9095
expect(isValidOffering(sampleOffering, restrictions)).toBe(false);
@@ -99,6 +104,7 @@ describe("isValidOffering", () => {
99104
endTime: "",
100105
disabled: false,
101106
numDays: 0,
107+
maxGap: 24,
102108
},
103109
];
104110
expect(isValidOffering(sampleOffering, restrictions)).toBe(true);

course-matrix/backend/src/constants/availableFunctions.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ import {
1515
categorizeValidOfferings,
1616
getFreq,
1717
getMaxDays,
18+
getMaxHour,
1819
getValidOfferings,
1920
groupOfferings,
2021
trim,
22+
shuffle,
2123
} from "../utils/generatorHelpers";
2224

2325
// Add all possible function names here
@@ -199,6 +201,7 @@ export const availableFunctions: AvailableFunctions = {
199201
const courseOfferingsList: OfferingList[] = [];
200202
const validCourseOfferingsList: GroupedOfferingList[] = [];
201203
const maxdays = getMaxDays(restrictions);
204+
const maxhours = getMaxHour(restrictions);
202205
const validSchedules: Offering[][] = [];
203206
// Fetch offerings for each course
204207
for (const course of courses) {
@@ -244,19 +247,22 @@ export const availableFunctions: AvailableFunctions = {
244247
validCourseOfferingsList.push(groupedOfferings);
245248
}
246249

247-
const categorizedOfferings = categorizeValidOfferings(
250+
let categorizedOfferings = categorizeValidOfferings(
248251
validCourseOfferingsList,
249252
);
253+
// console.log(JSON.stringify(categorizedOfferings));
250254
// Generate valid schedules for the given courses and restrictions
251-
await getValidSchedules(
255+
categorizedOfferings = shuffle(categorizedOfferings);
256+
getValidSchedules(
252257
validSchedules,
253258
categorizedOfferings,
254259
[],
255260
0,
256261
categorizedOfferings.length,
257262
maxdays,
263+
maxhours,
264+
false,
258265
);
259-
260266
// Return error if no valid schedules are found
261267
if (validSchedules.length === 0) {
262268
return { status: 404, error: "No valid schedules found." };
@@ -445,6 +451,7 @@ ${offeringData.meeting_section} `;
445451
disabled: restriction?.disabled,
446452
num_days: restriction?.numDays,
447453
calendar_id: timetableData?.id,
454+
max_gap: restriction?.maxGap,
448455
},
449456
])
450457
.select();

0 commit comments

Comments
 (0)