Skip to content

Instantly share code, notes, and snippets.

@KarlHerler
Created June 9, 2011 05:22
Show Gist options
  • Save KarlHerler/1016111 to your computer and use it in GitHub Desktop.
Save KarlHerler/1016111 to your computer and use it in GitHub Desktop.
Loads some data from mongodb in node and parses it a bit
function loadData(req, res, next) {
/* Loads all data. */
/*var filter = {}
if(req.params.tag) {
console.log("fetching data for tag: "+req.params.tag)
filter = {"tag": req.params.tag}
}*/
WorkInstance.find({}, ['_id','tag', 'start', 'stop'], {sort:[['start', -1]]}, function(err, docs) {
var workDatas = [];
if (docs.length>0) {
var cWorkData = {workData: { _id: docs[0].doc._id,
tag: [docs[0].doc.tag],
start: new Date(docs[0].doc.start),
stop: new Date(docs[0].doc.stop),
isFinished: true
}};
for (i=1;i<docs.length;i++) {
if (docs[i].doc.start!=undefined && docs[i].doc.stop==undefined) {
cWorkData.workData.isFinished = false;
}
if (isEqual(cWorkData.workData.start, docs[i].doc.start) && isEqual(cWorkData.workData.stop, docs[i].doc.stop)
|| (!cWorkData.workData.isFinished && isEqual(cWorkData.workData.start, docs[i].doc.start))) {
cWorkData.workData.tag.push(docs[i].doc.tag);
} else {
workDatas.push(cWorkData);
var z = workDatas.length-1;
var time = (workDatas[z].workData.stop - workDatas[z].workData.start);
workDatas[z].workData.timeStr = timeStr(time);
workDatas[z].workData.time = time;
cWorkData = {workData: {_id: docs[i].doc._id,
tag: [docs[i].doc.tag],
start: new Date(docs[i].doc.start),
stop: new Date(docs[i].doc.stop),
isFinished: true}};
}
}
}
//needs to add remaining stuff in cWorkdata after loop
workDatas.push(cWorkData);
var z = workDatas.length-1;
var time = (workDatas[z].workData.stop - workDatas[z].workData.start);
workDatas[z].workData.timeStr = timeStr(time);
workDatas[z].workData.time = time;
req.workDatas = workDatas;
next();
});
}
exports.loadData = loadData;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment