Skip to content

Instantly share code, notes, and snippets.

@tinybike
Last active January 21, 2016 01:51
Show Gist options
  • Select an option

  • Save tinybike/33857462e852ecb7f52f to your computer and use it in GitHub Desktop.

Select an option

Save tinybike/33857462e852ecb7f52f to your computer and use it in GitHub Desktop.
// FIRST APPROACH: USE AN ACCUMULATOR.
function accumulator_getSpecificNodeByQuarter(givenNodeID, quarter) {
// because this analytics is fixed I check the first quarter
var quarterlyResults = [];
dbA.utilization.find({nodeID: givenNodeID}, {_id: 0, day: 1, spaces: 1}, function (err, docs) {
if (err) return console.log('did not find node');
// set min and max
var minday;
var maxday;
// set the fiscal quarters
if (quarter === "1") {
minday = new Date('2015-01-01');
maxday = new Date('2015-03-31');
} else if (quarter==="2") {
minday = new Date('2015-04-01');
maxday = new Date('2015-06-31');
} else if (quarter ==="3") {
minday = new Date('2015-06-01');
maxday = new Date('2015-09-31');
} else if (quarter ==="4") {
minday = new Date('2015-10-01');
maxday = new Date('2015-12-31');
} else {
minday = new Date('2016-01-01');
maxday = new Date('2016-03-31');
}
for (var i = 0; i < docs.length; i++) {
// figure out the day in database
var today = new Date(docs[i].day);
// is it in the quarter and also a weekday?
if (today >= minday && today <= maxday && today.getDay() >=1 && today.getDay() <=5){
var thisSpace = docs[i].spaces;
for (var ii = 0; ii < thisSpace.length; ++ii) {
quarterlyResults.push({'day': docs[i].day, 'name': thisSpace[ii].name, 'stats': statsForUtils(thisSpace[ii].util)})
}
}
}
});
// this works as long as find() has completed by the time the
// execution flow reaches these commands...
console.log("quarterlyResults:", quarterlyResults);
console.log("quarterlyResults[0]:", quarterlyResults[0]);
}
// SECOND APPROACH (usually better): USE A CALLBACK.
// "callback" is the function that executes AFTER the find is complete
function callback_getSpecificNodeByQuarter(givenNodeID, quarter, callback) {
// because this analytics is fixed I check the first quarter
dbA.utilization.find({nodeID: givenNodeID}, {_id: 0, day: 1, spaces: 1}, function (err, docs) {
if (err) {
console.log('did not find node');
return callback(err);
}
var quarterlyResults = [];
// set min and max
var minday;
var maxday;
// set the fiscal quarters
if (quarter === "1") {
minday = new Date('2015-01-01');
maxday = new Date('2015-03-31');
} else if (quarter==="2") {
minday = new Date('2015-04-01');
maxday = new Date('2015-06-31');
} else if (quarter ==="3") {
minday = new Date('2015-06-01');
maxday = new Date('2015-09-31');
} else if (quarter ==="4") {
minday = new Date('2015-10-01');
maxday = new Date('2015-12-31');
} else {
minday = new Date('2016-01-01');
maxday = new Date('2016-03-31');
}
for (var i = 0; i < docs.length; i++) {
// figure out the day in database
var today = new Date(docs[i].day);
// is it in the quarter and also a weekday?
if (today >= minday && today <= maxday && today.getDay() >=1 && today.getDay() <=5){
var thisSpace = docs[i].spaces;
for (var ii = 0; ii < thisSpace.length; ++ii) {
quarterlyResults.push({'day': docs[i].day, 'name': thisSpace[ii].name, 'stats': statsForUtils(thisSpace[ii].util)})
}
}
}
callback(null, quarterlyResults);
});
}
// example callback
function writeResultsToFile(err, results) {
if (err) return console.error("writeResultsToFile:", err);
require("fs").writeFile("quarterlyResults.dat", results, function (err) {
if (err) return console.error("writeFile:", err);
console.log("Wrote file successfully :)");
});
}
// and here's the actual function call, using the callback
callback_getSpecificNodeByQuarter(givenNodeID, quarter, writeResultsToFile);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment