UNPKG

librato-node

Version:

A node.js client for Librato Metrics (http://metrics.librato.com/)

91 lines (80 loc) 2.34 kB
// Generated by CoffeeScript 1.12.7 (function() { var Aggregator, assert, slice = [].slice; assert = require('assert'); Aggregator = (function() { function Aggregator() { this.cache = {}; } Aggregator.prototype.flushTo = function(queue) { var key, name, obj, ref, ref1, results, source, state; ref = this.cache; results = []; for (key in ref) { state = ref[key]; ref1 = key.split(';'), name = ref1[0], source = ref1[1]; if (state.count > 1) { obj = state; } else { obj = { value: state.sum }; } obj.name = name; if (source != null) { obj.source = source; } queue.push(obj); results.push(delete this.cache[key]); } return results; }; Aggregator.prototype.measure = function(key, value) { var base; assert(value != null, 'value is required'); if ((base = this.cache)[key] == null) { base[key] = { count: 0, sum: 0, min: value, max: value, sum_squares: 0 }; } this.cache[key].count++; this.cache[key].sum += value; this.cache[key].min = Math.min(this.cache[key].min, value); this.cache[key].max = Math.max(this.cache[key].max, value); return this.cache[key].sum_squares += Math.pow(value, 2); }; Aggregator.prototype.timing = function(key, fn, cb) { var done, retval, start; assert(fn != null, 'function is required'); start = process.hrtime(); done = (function(_this) { return function() { var msec, ref, sec, usec; ref = process.hrtime(start), sec = ref[0], usec = ref[1]; msec = (sec * 1000) + Math.max(usec / 1000 / 1000); return _this.measure(key, msec); }; })(this); if (fn.length) { return fn(function() { var args; args = 1 <= arguments.length ? slice.call(arguments, 0) : []; done(); return cb != null ? cb.apply(this, args) : void 0; }); } else { retval = fn(); done(); return retval; } }; return Aggregator; })(); module.exports = Aggregator; }).call(this); //# sourceMappingURL=aggregator.js.map