-
Notifications
You must be signed in to change notification settings - Fork 3
/
getRequests.js
32 lines (25 loc) · 999 Bytes
/
getRequests.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
module.exports = function (callback) {
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var url = process.env.MONGODB_URL;
MongoClient.connect(url, function (e, db) {
if (e) {
console.error('Unable to connect to the mongoDB server. Error:' + e);
callback( "error: " + e );
db.close();
} else {
var collection = db.collection('request_log');
var now = new Date();
// 15 minutes ago: now minus 15 minutes times 60000 ms per minute
var window = now - 15 * 60000;
collection.find().toArray((err, docs) => {
if (err) console.error(err);
docs = typeof docs == undefined ? [] : docs.filter( x => new Date(x.time) <= now && new Date(x.time) >= window);
// console.log(docs);
console.log("Requests in the last 15 minutes: " + docs.length);
callback( docs.length );
db.close();
});
}
});
}