Skip to content

Instantly share code, notes, and snippets.

@anandgeorge
Last active December 29, 2015 09:59
Show Gist options
  • Save anandgeorge/7654187 to your computer and use it in GitHub Desktop.
Save anandgeorge/7654187 to your computer and use it in GitHub Desktop.
Analytics using MongoDB aggregation and Node.js
// The code is based on a MongoDB database myhome with a typical record as below:
// {
// "rec_time" : ISODate("2013-11-25T16:53:43.808Z"),
// "rec_data" : {
// "power" : 372.201,
// "current" : 1.73,
// "voltage" : 221.678,
// },
// "rec_asset" : {
// "name" : "Airconditioner",
// "room" : "Bed_1",
// },
// }
var MongoClient = require('mongodb').MongoClient;
var express = require('express');
var app = express.createServer();
app.use(express.bodyParser());
MongoClient.connect("mongodb://mongodbhost:port/myhome", function(err, db) {
app.get('/setup', function(req, res) {
db.createCollection('test', function(err, collection) {
db.collection('records').find({}, {rec_time:1}, {sort:{rec_time:1}, limit:1}, function(err, cursor) {
cursor.toArray(function(err, docs) {
if(!err){
var startDate = docs[0].rec_time;
startDate.setUTCHours(0,0,0,0);
startDate.setDate(1);
db.collection('records').find({}, {rec_time:1}, {sort:{rec_time:-1}, limit:1}, function(err, cursor) {
cursor.toArray(function(err, docs) {
if(!err){
var endDate = docs[0].rec_time;
endDate.setUTCHours(0,0,0,0);
endDate.setDate(1);
endDate.setMonth(endDate.getMonth() + 1);
db.collection('readings').aggregate([
{$match: {
"rec_time":{$gt:startDate, $lt:endDate},
}},
{$project: {
"rec_time":1,
"rec_Year" : { $year : "$rec_time"},
"rec_Month": { $month : "$rec_time" },
"rec_Day": { $dayOfMonth : "$rec_time" },
"rec_Hour": { $hour : "$rec_time" },
"rec_data":1,
"rec_asset":1,
}},
{$group: {
_id: {rec_asset_name:"$rec_asset.name", rec_asset_room:"$rec_asset.room",
rec_Year: "$rec_Year", rec_Month:"$rec_Month", rec_Day:"$rec_Day", rec_Hour:"$rec_Hour"},
count: { $sum: 1 },
avgVolt : {$avg:"$rec_data.voltage"},
avgCurrent:{$avg:"$rec_data.current"},
sumPower:{$sum:"$rec_data.power"},
avgPower:{$avg:"$rec_data.power"},
}}
], function(err, result) {
db.collection("agg").insert(result, function(err, docs) {
if(err) console.log('Error inserting data');
else {
db.collection("agg").insert({"lastrun":endDate}, function(err, docs) {
if(err) console.log('Error inserting lastrun');
else {
console.log('Completed aggregation');
res.end('Initial aggregation completed successfully');
}
});
}
});
});
}
});
});
}
});
});
});
});
app.post('/room/year', function(req, res) {
db.collection('agg').aggregate([
{$match: {"_id.rec_asset_room": req.body.room, "_id.rec_Year": parseInt(req.body.year)
}},
{$group: {
_id: null,
avgVolt : {$avg:"$avgVolt"},
avgCurrent:{$avg:"$avgCurrent"},
sumPower:{$sum:"$sumPower"},
avgPower:{$avg:"$avgPower"},
}}
], function(err, result) {
if(err) {
console.log(err);
}
else {
res.json(result);
}
}
);
});
app.post('/room/month', function(req, res) {
db.collection('agg').aggregate([
{$match: {"_id.rec_asset_room": req.body.room, "_id.rec_Year": parseInt(req.body.year), "_id.rec_Month": parseInt(req.body.month)
}},
{$group: {
_id: null,
avgVolt : {$avg:"$avgVolt"},
avgCurrent:{$avg:"$avgCurrent"},
sumPower:{$sum:"$sumPower"},
avgPower:{$avg:"$avgPower"},
}}
], function(err, result) {
if(err) {
console.log(err);
}
else {
res.json(result);
}
}
);
});
app.post('/room/day', function(req, res) {
db.collection('agg').aggregate([
{$match: {"_id.rec_asset_room": req.body.room, "_id.rec_Year": parseInt(req.body.year), "_id.rec_Month": parseInt(req.body.month),
"_id.rec_Day": parseInt(req.body.day)
}},
{$group: {
_id: null,
avgVolt : {$avg:"$avgVolt"},
avgCurrent:{$avg:"$avgCurrent"},
sumPower:{$sum:"$sumPower"},
avgPower:{$avg:"$avgPower"},
}}
], function(err, result) {
if(err) {
console.log(err);
}
else {
res.json(result);
}
}
);
});
app.listen(3000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment