simple-stats-server
Version:
dead-simple resource stats for Node.js servers
72 lines (61 loc) • 1.98 kB
JavaScript
(function() {
var Collection, normalize,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
normalize = function(name) {
return (name || '').toLowerCase().replace(/\s+/g, '-');
};
Collection = (function() {
function Collection(_module) {
this._module = _module;
this.getAll = __bind(this.getAll, this);
this.get = __bind(this.get, this);
this.add = __bind(this.add, this);
this.getNames = __bind(this.getNames, this);
if (this._module == null) {
throw new Error('Collection expects a stats module');
}
this._stats = {};
}
Collection.prototype.getNames = function() {
return Object.keys(this._stats);
};
Collection.prototype.add = function(nm) {
var args, i, name, stat;
name = normalize(nm);
args = (function() {
var _i, _ref, _results;
_results = [];
for (i = _i = 1, _ref = arguments.length; 1 <= _ref ? _i < _ref : _i > _ref; i = 1 <= _ref ? ++_i : --_i) {
_results.push(arguments[i]);
}
return _results;
}).apply(this, arguments);
if (name in this._stats) {
throw new Error("Collection already has a stat named '" + name + "'");
}
stat = this._stats[name] = new this._module(args[0], args[1], args[2]);
return stat.frontend;
};
Collection.prototype.get = function(nm) {
var name, stat;
name = normalize(nm);
if (!(name in this._stats)) {
throw new Error("Collection has no stat named '" + name + "'");
}
stat = this._stats[name];
return stat.backend;
};
Collection.prototype.getAll = function() {
var all, name, stat, _ref;
all = {};
_ref = this._stats;
for (name in _ref) {
stat = _ref[name];
all[name] = stat.backend;
}
return all;
};
return Collection;
})();
module.exports = Collection;
}).call(this);