-
-
Save tanepiper/a5b494556e00b8b16ced to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'); | |
var fs = require('fs'); | |
module.exports = function(instance) { | |
return function(req, res, next) { | |
var now = Date.now(); | |
var chart_name = req.params.chart.toLowerCase(); | |
res.writeHead(200, { | |
'Content-Type': 'image/gif', | |
'Content-Length': instance.punchbag.IMG_DATA.length, | |
'Cache-Control': 'private, max-age=0' | |
}); | |
res.end(instance.punchbag.IMG_BUFFER, 'binary'); | |
instance.punchbag.mongodb.getCollection('point', function(err, collection) { | |
if (err) { | |
return instance.logger.error(error); | |
} | |
collection.update( | |
{ c: chart_name, p: now / 1000 }, | |
{ $inc: { n: 1 } }, | |
{ upsert: true }, | |
function(error, point) { | |
if (error) { | |
return instance.logger.error(error); | |
} | |
instance.punchbag.mongodb.getCollection('agg', function(err, aggrigate) { | |
function aggperiod( kind, period, offset ) { | |
var pval = Math.floor( ((now / 1000) + offset) / period ); | |
aggrigate.update( | |
{ c: chart_name, k: kind, p: pval }, | |
{ $inc: { n: 1 } }, | |
{ upsert: true }, function(error, p) { | |
if (error) { | |
return instance.logger.error(error); | |
} | |
}); | |
} | |
instance.punchbag.PERIODS.forEach(function(p){ | |
aggperiod(p[0], p[1], p[2]); | |
}); | |
function aggdirect( kind, pval ) { | |
aggrigate.update( | |
{ c: chart_name, k:kind, p:pval }, | |
{ $inc: { n: 1 } }, | |
{ upsert: true }, function(error, p) { | |
if (error) { | |
return instance.logger.error(error); | |
} | |
}); | |
} | |
var at = (new Date(now)); | |
var y = at.getUTCFullYear() - 1970; | |
var n = ( 1 + at.getUTCMonth()) + ( 12 * y ); | |
var q = y * 4 + (1 + Math.floor(at.getUTCMonth() / 3)); | |
[ | |
[ 'n', n ], | |
[ 'q', q], | |
[ 'y', y] | |
].forEach(function(p) { | |
aggdirect(p[0], p[1]); | |
}); | |
}); | |
} | |
); | |
}); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment