-
Notifications
You must be signed in to change notification settings - Fork 0
/
order_students_by_category.js
82 lines (67 loc) · 1.93 KB
/
order_students_by_category.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
var aggregate = {};
var async = require('async');
var mongo = require('./ktbs_mongo.js');
var aggregationType = 'orderStudentsByCategoy';
aggregate.orderStudentsByCategory = function(args, callback) {
console.log(aggregationType + ', category : ' + args.category);
var timeCalculation, connection, obselModel;
var filter = {};
filter.student = true;
filter['course.category1.moodleId'] = args.category;
filter['begin'] = { $gt : args.fromDate };
filter['end'] = { $lt : args.toDate };
connection = mongo.createMongoConnection();
obselModel = mongo.getObselModel(connection);
obselModel.aggregate([
{
$match : filter
}, {
$group : {
_id : { subject : "$subject" },
number : { $sum : 1 }
}
}, {
$sort : { "number": -1 }
}
]
).exec(function(err, results) {
if (err) return callback(err);
mongo.disconnectMongo(connection);
callback(null, results);
});
}
aggregate.findOrComputeStudentsByCategory = function(category, fromDate, toDate, callback) {
var args = { };
var now = new Date();
if (fromDate === undefined || fromDate === null) {
fromDate = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate() - 30);
}
if (toDate === undefined || toDate === null) {
toDate = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate());
}
args.category = category;
args.fromDate = fromDate;
args.toDate = toDate;
mongo.findResults(aggregationType, args, function(err, results) {
if (err) callback(err);
if (results.length === 0) {
aggregate.orderStudentsByCategory(
args,
function(err, results) {
if (err) return callback(err);
mongo.storeResults(aggregationType,
args,
results,
function(err) {
if (err) return callback(err);
aggregate.findOrComputeStudentsByCategory(category, fromDate, toDate, callback);
});
}
)
} else {
// Results found
callback(null, results);
}
});
}
module.exports = aggregate;