-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.js
244 lines (229 loc) · 8.91 KB
/
helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
var calendar_helper = require("./calendar_helper.js");
var db_helper = require("./db_helper.js");
var fs = require('fs');
require('dotenv').config({path: __dirname + '/.env'});
async function isCoursePresent(courseId, callback) {
db_helper.isCoursePresent(courseId, callback)
}
async function getCourses(callback) {
db_helper.getCourses(callback);
}
/*
~~ Parameters ~~
courseId: datatype - string
duration: datatype - JSON, format - {date: .., hours: .., minutes: ..}
minDueDate: datatype - Date
maxDueDate: datatype - Date
~~ Returns ~~
Some due date suggestions for an assignment of duration `duration`, this due date being between the min and max due date.
Score and clashes with other events.
*/
async function suggestDueDate(courseId, duration, minDueDate, maxDueDate, callback) {
db_helper.isCoursePresent(courseId, (err, isPresent) => {
if(err) return callback(err);
if(! isPresent) {
return callback(new Error(`course ${courseId} does not exist according to our database. visit /courses to view the list of courses.`))
}
console.log(`course present`);
db_helper.getStudents(courseId, (err, students) => {
if(err) return callback(err);
calendar_helper.getAllEvents(minDueDate, (err, allCourseWork) => {
if(err) return callback(err);
db_helper.getCalendarNames((err, courseNames) => {
if(err) return callback(err);
courseNameToId = {}
for(var id in courseNames) {
courseNameToId[courseNames[id]] = id;
}
for(var i in allCourseWork) {
if(allCourseWork[i].course_name in courseNameToId) {
allCourseWork[i].course_id = courseNameToId[allCourseWork[i].course_name]
}
}
var commonStudents = getCommonStudents(students, courseId);
console.log(commonStudents);
console.log(allCourseWork);
console.log(courseNameToId);
var suggestions = [];
var suggestion = new Date(minDueDate);
var lastDate = new Date(maxDueDate);
lastDate.setDate(lastDate.getDate() - duration.days);
lastDate.setHours(lastDate.getHours() - duration.hours);
lastDate.setMinutes(lastDate.getMinutes() - duration.minutes);
while(suggestion <= lastDate) {
var start_date = new Date(suggestion);
var end_date = new Date(suggestion);
end_date.setDate(end_date.getDate() + duration.days);
end_date.setHours(end_date.getHours() + duration.hours);
end_date.setMinutes(end_date.getMinutes() + duration.minutes);
suggestions.push({
start_date: start_date,
end_date: end_date,
clash: calculateScore(start_date, end_date, allCourseWork, commonStudents),
});
suggestion.setDate(suggestion.getDate() + 1);
}
suggestions.sort((a, b) => {return a.clash.score - b.clash.score;});
var flexi_suggestions = [];
if(duration.days > 0) {
// one day more than allowed duration
duration.days ++;
suggestion = new Date(minDueDate);
lastDate = new Date(maxDueDate);
lastDate.setDate(lastDate.getDate() - duration.days);
lastDate.setHours(lastDate.getHours() - duration.hours);
lastDate.setMinutes(lastDate.getMinutes() - duration.minutes);
while(suggestion <= lastDate) {
var start_date = new Date(suggestion);
var end_date = new Date(suggestion);
end_date.setDate(end_date.getDate() + duration.days);
end_date.setHours(end_date.getHours() + duration.hours);
end_date.setMinutes(end_date.getMinutes() + duration.minutes);
flexi_suggestions.push({
start_date: start_date,
end_date: end_date,
clash: calculateScore(start_date, end_date, allCourseWork, commonStudents, duration.days/(duration.days - 1)),
});
suggestion.setDate(suggestion.getDate() + 1);
}
// two days more than allowed duration
duration.days ++;
suggestion = new Date(minDueDate);
lastDate = new Date(maxDueDate);
lastDate.setDate(lastDate.getDate() - duration.days);
lastDate.setHours(lastDate.getHours() - duration.hours);
lastDate.setMinutes(lastDate.getMinutes() - duration.minutes);
while(suggestion <= lastDate) {
var start_date = new Date(suggestion);
var end_date = new Date(suggestion);
end_date.setDate(end_date.getDate() + duration.days);
end_date.setHours(end_date.getHours() + duration.hours);
end_date.setMinutes(end_date.getMinutes() + duration.minutes);
flexi_suggestions.push({
start_date: start_date,
end_date: end_date,
clash: calculateScore(start_date, end_date, allCourseWork, commonStudents, duration.days/(duration.days - 2)),
});
suggestion.setDate(suggestion.getDate() + 1);
}
flexi_suggestions.sort((a, b) => {return a.clash.score - b.clash.score;});
}
return callback(null, {suggestions: suggestions, flexi_suggestions: flexi_suggestions});
});
});
})
})
}
/*
~~ Parameters ~~
courseId: datatype - string
duration: datatype - JSON, format - {date: .., hours: .., minutes: ..}
~~ Returns ~~
Score relating to how free the students are and the clashes with other events from today to today + duration
*/
async function getStudentSchedule(courseId, duration, callback) {
db_helper.isCoursePresent(courseId, (err, isPresent) => {
if(err) return callback(err);
if(! isPresent) {
return callback(new Error(`course ${courseId} does not exist according to our database`))
}
db_helper.getStudents(courseId, (err, students) => {
if(err) return callback(err);
var start_date = new Date();
var end_date = new Date();
end_date.setDate(end_date.getDate() + duration.days);
end_date.setHours(end_date.getHours() + duration.hours);
end_date.setMinutes(end_date.getMinutes() + duration.minutes);
var commonStudents = getCommonStudents(students, courseId);
calendar_helper.getAllEvents(start_date, (err, allCourseWork) => {
if(err) return callback(err);
db_helper.getCalendarNames((err, courseNames) => {
if(err) return callback(err);
courseNameToId = {}
for(var id in courseNames) {
courseNameToId[courseNames[id]] = id;
}
for(var i in allCourseWork) {
allCourseWork[i].course_id = courseNameToId[allCourseWork[i].course_name]
}
var score = calculateScore(start_date, end_date, allCourseWork, commonStudents);
callback(null, score);
})
});
})
})
}
async function addEventToCalendar(eventName, eventStartDate, eventEndDate, callback) {
calendar_helper.insertEvent(eventName, eventStartDate, eventEndDate, callback)
}
function getCommonStudents(students, courseId) {
var commonStudents = {};
var total = 0;
for(var i in students) {
var studentInCourse = false;
for(var j in students[i]) {
if(courseId == students[i][j]) {
studentInCourse = true;
}
}
if(!studentInCourse) continue;
total ++;
for(var j = 0; j < students[i].length; ++ j) {
if(commonStudents[students[i][j]] == null) {
commonStudents[students[i][j]] = 0;
}
commonStudents[students[i][j]] ++;
}
}
for(student in commonStudents) {
commonStudents[student] /= total;
}
return commonStudents;
}
/*
~~ Parameters ~~
start_date: datatype - Date
end_date: datatype - Date
allCourseWork: datatype - list of courseWorks, format - {course_name: .., course_id: ..., coursework_name: .., start_date: (datatype - Date), end_date: (datatype - Date)}
commonStudents: datatype - JSON, format - {courseId: studentCount}
*/
function calculateScore(start_date, end_date, allCourseWork, commonStudents, flexi_factor = 1) {
var score = 0;
var reason = [];
for(var i = 0; i < allCourseWork.length; ++ i) {
var courseWork = allCourseWork[i];
if(commonStudents[courseWork.course_id] == null) continue;
console.log(courseWork)
var currScore = clash(start_date, end_date, courseWork.start_date, courseWork.end_date) + distance_between_deadlines(start_date, end_date, courseWork.start_date, courseWork.end_date)
if(clash(start_date, end_date, courseWork.start_date, courseWork.end_date) != 0) {
score += commonStudents[courseWork.course_id] * currScore;
reason.push({
courseWork: courseWork,
fraction_of_students: commonStudents[courseWork.course_id],
fraction_of_overlap: currScore,
});
}
}
return {score: 2 * score/flexi_factor, reason: reason};
}
function clash(startDate, endDate, c_startDate, c_endDate) {
if(startDate >= c_endDate || c_startDate > endDate) return 0;
if(endDate >= c_endDate && startDate <= c_startDate) {
return 1;
}
if(startDate < c_startDate) {
return (endDate - c_startDate)/(c_endDate - c_startDate);
} else {
return (c_endDate - startDate)/(c_endDate - c_startDate);
}
}
function distance_between_deadlines(startDate, endDate, c_startDate, c_endDate) {
return 2/(1 + Math.abs(endDate - c_endDate));
}
module.exports = {
suggestDueDate: suggestDueDate,
getStudentSchedule: getStudentSchedule,
addEventToCalendar: addEventToCalendar,
isCoursePresent: isCoursePresent,
getCourses: getCourses,
};