Skip to content

Commit

Permalink
Driver returning data for engine
Browse files Browse the repository at this point in the history
  • Loading branch information
dwnz committed Oct 7, 2014
1 parent 09dc656 commit 0b2f447
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ node_modules

# Users Environment Variables
.lock-wscript

.idea/*
5 changes: 5 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Mongo": {
"ConnectionString": "mongodb://localhost:27017/hooq"
}
}
42 changes: 42 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
24 changes: 24 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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();
});
};

0 comments on commit 0b2f447

Please sign in to comment.