diff --git a/.gitignore b/.gitignore index 59d842b..2907739 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,5 @@ node_modules # Users Environment Variables .lock-wscript + +.idea/* diff --git a/config/default.json b/config/default.json new file mode 100644 index 0000000..640ff17 --- /dev/null +++ b/config/default.json @@ -0,0 +1,5 @@ +{ + "Mongo": { + "ConnectionString": "mongodb://localhost:27017/hooq" + } +} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index e69de29..188eac0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -0,0 +1,42 @@ +var mongoose = require('mongoose'); + +var db = mongoose.connection; +db.on('error', console.error.bind(console, 'connection error:')); + +var engine = {}; + +// SCHEMAS +var logSchema = mongoose.Schema({ + UserId: String, + WebhookId: String, + Created: { type: Date, default: Date.now }, + RemoteIp: String, + Method: String, + Size: Number, + UserAgent: String, + ContentType: String, + Headers: [String] +}); + +var Log = mongoose.model('Logs', logSchema); + +engine.getData = function (startDate, endDate, callback) { + Log.aggregate([ + { + $match: {Created: {$gte: startDate, $lte: endDate}} + }, + { + $group: { + _id: { + year: {$year: "$Created"}, + month: {$month: "$Created"}, + day: {$dayOfMonth: "$Created"} + }, + hits: {$sum: 1} + } + }, + {$sort: {"_id.year": 1, "_id.month": 1, "_id.day": 1}} + ], callback); +}; + +module.exports = engine; \ No newline at end of file diff --git a/package.json b/package.json index cb15b20..2cb75c9 100644 --- a/package.json +++ b/package.json @@ -18,5 +18,9 @@ "homepage": "https://github.com/dwnz/hooq-reporting-drivers-mongo", "devDependencies": { "nodeunit": "^0.9.0" + }, + "dependencies": { + "config": "^1.2.2", + "mongoose": "^3.8.17" } } diff --git a/test/test.js b/test/test.js index e69de29..6031b6f 100644 --- a/test/test.js +++ b/test/test.js @@ -0,0 +1,24 @@ +var config = require('config'); +var mongoose = require('mongoose'); + +var driver = require('../lib/index'); + +exports.checkThatDataIsReturned = function (test) { + mongoose.connect(config.Mongo.ConnectionString); + driver.getData(new Date(2014, 01, 01), new Date(2014, 12, 31), function (err, data) { + test.ok(data); + test.notEqual(0, data.length); + test.done(); + mongoose.disconnect(); + }); +}; + +exports.checkThatDataIsReturnedForMonth = function (test) { + mongoose.connect(config.Mongo.ConnectionString); + driver.getData(new Date(2014, 09, 01), new Date(2014, 09, 31), function (err, data) { + test.ok(data); + test.equal(4, data.length); + test.done(); + mongoose.disconnect(); + }); +};