-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmodels.js
218 lines (198 loc) · 8.85 KB
/
models.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
var _ = require('underscore');
function prependPlusToQuery(q) {
return _.map(q.match(/\w+|"[^"]+"/g), function(x) {
if (x.length > 0 && x[0] == '-') return;
return '+'+x;
}).join(' ');
}
module.exports = function(sequelize, DataTypes) {
var Student = sequelize.define('Student', {
dn: DataTypes.STRING,
matric: DataTypes.STRING,
displayName: DataTypes.TEXT,
firstName: DataTypes.TEXT,
lastName: DataTypes.TEXT,
email: DataTypes.STRING,
forwardTo: DataTypes.STRING,
enrolledOn: DataTypes.INTEGER
}, {
classMethods: {
search: function(query) {
return sequelize.query('SELECT Students.*, faculties.name AS `faculties.name`, faculties.id AS `faculties.id`, ' +
' MATCH (displayName, firstName, lastName) AGAINST (? IN NATURAL LANGUAGE MODE) AS relevance FROM Students' +
' LEFT OUTER JOIN StudentFaculties ON Students.id = StudentFaculties.StudentId' +
' LEFT OUTER JOIN Faculties AS faculties ON faculties.id = StudentFaculties.FacultyId' +
' WHERE MATCH (displayName, firstName, lastName) AGAINST (? IN BOOLEAN MODE) HAVING relevance > 0.3' +
' ORDER BY relevance DESC LIMIT 100', null, { raw: true },
[
query,
prependPlusToQuery(query)
]);
},
suggest: function(query) {
return sequelize.query('SELECT matric, displayName, firstName, lastName,' +
' MATCH (displayName, firstName, lastName) AGAINST (? IN NATURAL LANGUAGE MODE) AS relevance FROM Students' +
' WHERE MATCH (displayName, firstName, lastName) AGAINST (? IN BOOLEAN MODE)' +
' ORDER BY relevance DESC LIMIT 10', null, { raw:true },
[
query+'*',
prependPlusToQuery(query)+'*'
]);
},
similarStudents: function(id, _limit) {
var limit = _limit || 10;
return sequelize.query('SELECT s.id, s.matric, s.displayName, intersection.cnt AS common,' +
' CASE WHEN not_in.cnt IS NULL' +
' THEN intersection.cnt / (SELECT COUNT(ModuleId) FROM StudentModules WHERE StudentId = ?)' +
' ELSE intersection.cnt / (not_in.cnt + (SELECT COUNT(ModuleId) FROM StudentModules WHERE StudentId = ?))' +
' END AS jaccardIndex' +
' FROM Students s' +
' INNER JOIN (SELECT StudentId, COUNT(*) AS cnt FROM StudentModules WHERE ModuleId IN (SELECT ModuleId FROM StudentModules WHERE StudentId = ?)' +
' AND StudentId != ? GROUP BY StudentId) AS intersection' +
' ON s.id = intersection.StudentId' +
' LEFT JOIN (SELECT StudentId, COUNT(ModuleId) AS cnt FROM StudentModules WHERE StudentId != ? AND NOT ModuleId IN' +
' (SELECT ModuleId FROM StudentModules WHERE StudentId = ?) GROUP BY StudentId) AS not_in' +
' ON s.id = not_in.StudentId ORDER BY jaccardIndex DESC LIMIT ?',
null, { raw: true },
[
id,
id,
id,
id,
id,
id,
limit
]);
}
},
instanceMethods: {
similarStudents: function(_limit) {
var id = this.id;
var limit = _limit || 10;
return sequelize.query('SELECT s.id, s.matric, s.displayName, intersection.cnt AS common,' +
' CASE WHEN not_in.cnt IS NULL' +
' THEN intersection.cnt / (SELECT COUNT(ModuleId) FROM StudentModules WHERE StudentId = ?)' +
' ELSE intersection.cnt / (not_in.cnt + (SELECT COUNT(ModuleId) FROM StudentModules WHERE StudentId = ?))' +
' END AS jaccardIndex' +
' FROM Students s' +
' INNER JOIN (SELECT StudentId, COUNT(*) AS cnt FROM StudentModules WHERE ModuleId IN (SELECT ModuleId FROM StudentModules WHERE StudentId = ?)' +
' AND StudentId != ? GROUP BY StudentId) AS intersection' +
' ON s.id = intersection.StudentId' +
' LEFT JOIN (SELECT StudentId, COUNT(ModuleId) AS cnt FROM StudentModules WHERE StudentId != ? AND NOT ModuleId IN' +
' (SELECT ModuleId FROM StudentModules WHERE StudentId = ?) GROUP BY StudentId) AS not_in' +
' ON s.id = not_in.StudentId ORDER BY jaccardIndex DESC LIMIT ?',
null, { raw: true },
[
id,
id,
id,
id,
id,
id,
limit
]);
}
}
});
var Module = sequelize.define('Module', {
dn: DataTypes.STRING,
code: DataTypes.STRING,
name: DataTypes.TEXT,
description: DataTypes.TEXT,
mc: DataTypes.INTEGER
}, {
classMethods: {
search: function(query) {
return sequelize.query('SELECT Modules.*, ModuleDepartments.name AS `department.name`,' +
' MATCH (Modules.name) AGAINST (? IN NATURAL LANGUAGE MODE) AS relevance FROM Modules' +
' LEFT OUTER JOIN ModuleDepartments ON ModuleDepartments.id = Modules.ModuleDepartmentId' +
' WHERE MATCH (Modules.name) AGAINST (? IN BOOLEAN MODE) OR code LIKE ?' +
' OR CONCAT_WS(" ", Modules.code, Modules.name) LIKE ?' +
' ORDER BY relevance DESC LIMIT 100', null, { raw:true },
[
query,
prependPlusToQuery(query),
query,
'%'+query+'%'
]);
},
suggest: function(query) {
return sequelize.query('SELECT code, name,' +
' MATCH (name) AGAINST (? IN NATURAL LANGUAGE MODE) AS relevance FROM Modules' +
' WHERE MATCH (name) AGAINST (? IN BOOLEAN MODE) OR code LIKE ?' +
' OR CONCAT_WS(" ", code, name) LIKE ?' +
' ORDER BY relevance DESC LIMIT 10', null, { raw:true },
[
query+'*',
prependPlusToQuery(query)+'*',
query,
query+'%'
]);
},
},
instanceMethods: {
alsoTook: function() {
var id = this.id;
return sequelize.query('SELECT code, name, mc, COUNT(*) count FROM StudentModules f' +
' INNER JOIN StudentModules s ON s.StudentId = f.StudentId' +
' LEFT OUTER JOIN Modules ON s.ModuleId = Modules.id' +
' WHERE f.ModuleId = ? AND s.ModuleId != ?' +
' GROUP BY s.ModuleId' +
' HAVING COUNT(*) > 1' +
' ORDER BY COUNT(*) DESC LIMIT 10', null, { raw:true },
[
id,
id
]);
}
}
});
var ModuleDepartment = sequelize.define('ModuleDepartment', {
name: DataTypes.TEXT
});
Module.belongsTo(ModuleDepartment);
ModuleDepartment.hasMany(Module);
var Career = sequelize.define('Career', {
dn: DataTypes.STRING,
name: DataTypes.TEXT,
});
var Faculty = sequelize.define('Faculty', {
name: DataTypes.TEXT,
});
var Course = sequelize.define('Course', {
dn: DataTypes.STRING,
name: DataTypes.TEXT,
});
var StudentModule = sequelize.define('StudentModule', {
year: DataTypes.INTEGER,
semester: DataTypes.INTEGER
});
Student.hasMany(Module, { joinTableModel: StudentModule });
Module.hasMany(Student, { joinTableModel: StudentModule });
var StudentCareer = sequelize.define('StudentCareer', {
year: DataTypes.INTEGER,
semester: DataTypes.INTEGER
});
Student.hasMany(Career, { joinTableModel: StudentCareer });
Career.hasMany(Student, { joinTableModel: StudentCareer });
var StudentFaculty = sequelize.define('StudentFaculty', {
year: DataTypes.INTEGER,
semester: DataTypes.INTEGER
});
Student.hasMany(Faculty, { joinTableModel: StudentFaculty });
Faculty.hasMany(Student, { joinTableModel: StudentFaculty });
var StudentCourse = sequelize.define('StudentCourse', {
year: DataTypes.INTEGER,
semester: DataTypes.INTEGER
});
Student.hasMany(Course, { joinTableModel: StudentCourse });
Course.hasMany(Student, { joinTableModel: StudentCourse });
return {
Student: Student,
Module: Module,
ModuleDepartment: ModuleDepartment,
Career: Career,
Faculty: Faculty,
Course: Course
};
}