From 5dca83ca577acdd0d951741736b0cb4eaf71ca79 Mon Sep 17 00:00:00 2001 From: Darren Date: Tue, 5 Aug 2014 18:41:52 +0100 Subject: [PATCH] Timers + gauges added to Stats --- server/stats.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 3 deletions(-) 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