From: Darren Date: Tue, 5 Aug 2014 17:41:52 +0000 (+0100) Subject: Timers + gauges added to Stats X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=5dca83ca577acdd0d951741736b0cb4eaf71ca79;p=KiwiIRC.git Timers + gauges added to Stats --- diff --git a/server/stats.js b/server/stats.js index 2898482..f790251 100644 --- a/server/stats.js +++ b/server/stats.js @@ -1,5 +1,59 @@ module.exports = { - incr: function incr(stat_name) { - global.modules.emit('stat counter', {name: stat_name}); + incr: function incr(stat_name, data) { + global.modules.emit('stat counter', {name: stat_name, data: data}); + }, + + gauge: function gauge(stat_name, value) { + global.modules.emit('stat gauge', {name: stat_name, value: value}); + }, + + + /** + * Send a timer value to the stats + * + * Usage: + * var timer = Stats.startTimer('stat_name', {some_data: 'value'}); + * // Do stuff + * timer.stop({other_data: 'value'}); + * + * The object passed into .startTimer() and .stop(); are optional. If + * given they will be shallow merged with .stop() overridding .startTimer() + */ + startTimer: function statsTimer(stat_name, data_start) { + var timer_started = new Date(); + + var timerStop = function timerStop(data_end) { + var time = (new Date()) - timer_started; + var data = shallowMergeObjects(data_start, data_end); + + global.modules.emit('stat timer', {name: stat_name, time: time, data: data}); + }; + + return { + stop: timerStop + }; } -}; \ No newline at end of file +}; + + + +function shallowMergeObjects(/** argn, ... **/) { + var arg_idx, arg, + data = {}; + + for(arg_idx=0; arg_idx