-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats.js
33 lines (26 loc) · 1009 Bytes
/
stats.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
var mongodb = require('mongodb');
module.exports.get_item_history = function(req, res, next, mongourl) {
mongodb.connect(mongourl, function(err, conn) {
conn.collection('shortened', function(err, shortened) {
shortened.findOne({_id: req.params.stub}, function(err, data) {
conn.collection('shortened_views', function(err, shortened_views){
shortened_views.find({_id: { $regex: data._id + '_' } }, {'total':1}, {'sort':[['_id','asc']]}).toArray(function(err, stats){
// Stats manipulation goes here
var hourly_stats = generate_hourly_stats(stats);
res.render('stats', { short: data._id, outurl: data.url, hourly_stats: JSON.stringify(hourly_stats) });
conn.close();
});
});
});
});
});
}
generate_hourly_stats = function(stats){
var output = [];
for(var i in stats){
date = stats[i]._id.split(/_/)[1];
value = stats[i].total;
output.push([date,value]);
}
return output;
}