el-borracho-stats
Version:
REST and SSE API and worker producing daily and all-time totals for Bull queues
475 lines (446 loc) • 16.6 kB
JavaScript
// Generated by IcedCoffeeScript 108.0.11
var ElBorrachoStats, errify, iced, __iced_k, __iced_k_noop,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
iced = require('iced-runtime');
__iced_k = __iced_k_noop = function() {};
errify = require("errify");
ElBorrachoStats = (function() {
ElBorrachoStats.prototype.completed = 0;
ElBorrachoStats.prototype.failed = 0;
ElBorrachoStats.prototype.incrementCompleted = function() {
return this.completed++;
};
ElBorrachoStats.prototype.incrementFailed = function() {
return this.failed++;
};
ElBorrachoStats.prototype.clearCompleted = function() {
return this.completed = 0;
};
ElBorrachoStats.prototype.clearFailed = function() {
return this.failed = 0;
};
ElBorrachoStats.prototype.prefixForQueue = function(queue) {
return "bull:" + this.namespace + ":" + queue;
};
ElBorrachoStats.prototype.shortDate = function(date) {
return (date.toISOString().split("T"))[0];
};
function ElBorrachoStats(_arg) {
this.redis = _arg.redis, this.namespace = _arg.namespace, this.queuename = _arg.queuename, this.expire = _arg.expire, this.overallTotal = _arg.overallTotal;
this.fetchStatForQueue = __bind(this.fetchStatForQueue, this);
this.fetchStatForAll = __bind(this.fetchStatForAll, this);
this.fetchStat = __bind(this.fetchStat, this);
this.fetchHistoryForQueue = __bind(this.fetchHistoryForQueue, this);
this.fetchHistoryForAll = __bind(this.fetchHistoryForAll, this);
this.fetchHistory = __bind(this.fetchHistory, this);
this.fetchForQueue = __bind(this.fetchForQueue, this);
this.fetchForAll = __bind(this.fetchForAll, this);
this.fetch = __bind(this.fetch, this);
this.update = __bind(this.update, this);
this.unlock = __bind(this.unlock, this);
this.updateLock = __bind(this.updateLock, this);
this.lock = __bind(this.lock, this);
this.clearFailed = __bind(this.clearFailed, this);
this.clearCompleted = __bind(this.clearCompleted, this);
this.incrementFailed = __bind(this.incrementFailed, this);
this.incrementCompleted = __bind(this.incrementCompleted, this);
if (!this.redis) {
throw new Error("redis client required");
}
this.namespace || (this.namespace = "stat");
this.prefix = this.prefixForQueue(this.queuename);
this.statistician = "" + this.prefix + ":statistician";
this.expire || (this.expire = 60);
if (this.overallTotal == null) {
this.overallTotal = true;
}
}
ElBorrachoStats.prototype.lock = function(callback) {
var ideally, key, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
ideally = errify(function(err) {
throw err;
});
(function(_this) {
return (function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/home/charles/source/el-borracho-stats/src/models/stats.coffee",
funcname: "ElBorrachoStats.lock"
});
_this.redis.exists(_this.statistician, ideally(__iced_deferrals.defer({
assign_fn: (function() {
return function() {
return key = arguments[0];
};
})(),
lineno: 27
})));
__iced_deferrals._fulfill();
});
})(this)((function(_this) {
return function() {
if (key) {
throw new Error("key " + _this.statistician + " exists, Stats may already be running for this queue");
}
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/home/charles/source/el-borracho-stats/src/models/stats.coffee",
funcname: "ElBorrachoStats.lock"
});
_this.redis.set(_this.statistician, true, ideally(__iced_deferrals.defer({
lineno: 30
})));
__iced_deferrals._fulfill();
})(function() {
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/home/charles/source/el-borracho-stats/src/models/stats.coffee",
funcname: "ElBorrachoStats.lock"
});
_this.updateLock(ideally(__iced_deferrals.defer({
lineno: 31
})));
__iced_deferrals._fulfill();
})(function() {
return callback();
});
});
};
})(this));
};
ElBorrachoStats.prototype.updateLock = function(callback) {
return this.redis.expire(this.statistician, this.expire, callback);
};
ElBorrachoStats.prototype.unlock = function(callback) {
var err, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
if (callback == null) {
callback = function() {};
}
(function(_this) {
return (function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/home/charles/source/el-borracho-stats/src/models/stats.coffee",
funcname: "ElBorrachoStats.unlock"
});
_this.redis.del(_this.statistician, __iced_deferrals.defer({
assign_fn: (function() {
return function() {
return err = arguments[0];
};
})(),
lineno: 38
}));
__iced_deferrals._fulfill();
});
})(this)((function(_this) {
return function() {
if (err) {
throw err;
}
return callback();
};
})(this));
};
ElBorrachoStats.prototype.update = function(callback) {
var all, ideally, multi, today, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
if (callback == null) {
callback = function() {};
}
ideally = errify(function(err) {
throw err;
});
today = this.shortDate(new Date);
all = "bull:" + this.namespace + ":all";
(function(_this) {
return (function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/home/charles/source/el-borracho-stats/src/models/stats.coffee",
funcname: "ElBorrachoStats.update"
});
_this.updateLock(ideally(__iced_deferrals.defer({
lineno: 47
})));
__iced_deferrals._fulfill();
});
})(this)((function(_this) {
return function() {
(function(__iced_k) {
if (_this.completed) {
multi = _this.redis.multi();
multi.incrby("" + _this.prefix + ":completed", _this.completed);
multi.incrby("" + _this.prefix + ":completed:" + today, _this.completed);
if (_this.overallTotal) {
multi.incrby("" + all + ":completed", _this.completed);
multi.incrby("" + all + ":completed:" + today, _this.completed);
}
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/home/charles/source/el-borracho-stats/src/models/stats.coffee",
funcname: "ElBorrachoStats.update"
});
multi.exec(ideally(__iced_deferrals.defer({
lineno: 55
})));
__iced_deferrals._fulfill();
})(function() {
return __iced_k(_this.clearCompleted());
});
} else {
return __iced_k();
}
})(function() {
(function(__iced_k) {
if (_this.failed) {
multi = _this.redis.multi();
multi.incrby("" + _this.prefix + ":failed", _this.failed);
multi.incrby("" + _this.prefix + ":failed:" + today, _this.failed);
if (_this.overallTotal) {
multi.incrby("" + all + ":failed", _this.failed);
multi.incrby("" + all + ":failed:" + today, _this.failed);
}
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/home/charles/source/el-borracho-stats/src/models/stats.coffee",
funcname: "ElBorrachoStats.update"
});
multi.exec(ideally(__iced_deferrals.defer({
lineno: 65
})));
__iced_deferrals._fulfill();
})(function() {
return __iced_k(_this.clearFailed());
});
} else {
return __iced_k();
}
})(function() {
return callback();
});
});
};
})(this));
};
ElBorrachoStats.prototype.fetch = function(callback) {
return this.fetchForQueue(this.queuename, callback);
};
ElBorrachoStats.prototype.fetchForAll = function(callback) {
return this.fetchForQueue("all", callback);
};
ElBorrachoStats.prototype.fetchForQueue = function(queue, callback) {
var completed, failed, ideally, prefix, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
ideally = errify(callback);
prefix = this.prefixForQueue(queue);
(function(_this) {
return (function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/home/charles/source/el-borracho-stats/src/models/stats.coffee",
funcname: "ElBorrachoStats.fetchForQueue"
});
_this.redis.get("" + prefix + ":completed", ideally(__iced_deferrals.defer({
assign_fn: (function() {
return function() {
return completed = arguments[0];
};
})(),
lineno: 80
})));
__iced_deferrals._fulfill();
});
})(this)((function(_this) {
return function() {
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/home/charles/source/el-borracho-stats/src/models/stats.coffee",
funcname: "ElBorrachoStats.fetchForQueue"
});
_this.redis.get("" + prefix + ":failed", ideally(__iced_deferrals.defer({
assign_fn: (function() {
return function() {
return failed = arguments[0];
};
})(),
lineno: 81
})));
__iced_deferrals._fulfill();
})(function() {
return callback(null, {
completed: completed,
failed: failed
});
});
};
})(this));
};
ElBorrachoStats.prototype.fetchHistory = function(startDate, daysPrevious, callback) {
if (startDate == null) {
startDate = new Date;
}
if (daysPrevious == null) {
daysPrevious = 30;
}
return this.fetchHistoryForQueue(this.queuename, startDate, daysPrevious, callback);
};
ElBorrachoStats.prototype.fetchHistoryForAll = function(startDate, daysPrevious, callback) {
if (startDate == null) {
startDate = new Date;
}
if (daysPrevious == null) {
daysPrevious = 30;
}
return this.fetchHistoryForQueue("all", startDate, daysPrevious, callback);
};
ElBorrachoStats.prototype.fetchHistoryForQueue = function(queue, startDate, daysPrevious, callback) {
var completed, failed, ideally, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
if (startDate == null) {
startDate = new Date;
}
if (daysPrevious == null) {
daysPrevious = 30;
}
ideally = errify(callback);
(function(_this) {
return (function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/home/charles/source/el-borracho-stats/src/models/stats.coffee",
funcname: "ElBorrachoStats.fetchHistoryForQueue"
});
_this.fetchStatForQueue(queue, "completed", startDate, daysPrevious, ideally(__iced_deferrals.defer({
assign_fn: (function() {
return function() {
return completed = arguments[0];
};
})(),
lineno: 94
})));
__iced_deferrals._fulfill();
});
})(this)((function(_this) {
return function() {
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/home/charles/source/el-borracho-stats/src/models/stats.coffee",
funcname: "ElBorrachoStats.fetchHistoryForQueue"
});
_this.fetchStatForQueue(queue, "failed", startDate, daysPrevious, ideally(__iced_deferrals.defer({
assign_fn: (function() {
return function() {
return failed = arguments[0];
};
})(),
lineno: 95
})));
__iced_deferrals._fulfill();
})(function() {
result = completed.concat(failed);
return callback(null, result);
});
};
})(this));
};
ElBorrachoStats.prototype.fetchStat = function(type, startDate, daysPrevious, callback) {
if (startDate == null) {
startDate = new Date;
}
if (daysPrevious == null) {
daysPrevious = 30;
}
return this.fetchStatForQueue(this.queuename, type, startDate, daysPrevious, callback);
};
ElBorrachoStats.prototype.fetchStatForAll = function(type, startDate, daysPrevious, callback) {
if (startDate == null) {
startDate = new Date;
}
if (daysPrevious == null) {
daysPrevious = 30;
}
return this.fetchStatForQueue("all", type, startDate, daysPrevious, callback);
};
ElBorrachoStats.prototype.fetchStatForQueue = function(queue, type, startDate, daysPrevious, callback) {
var date, dates, datestr, i, ideally, keys, prefix, stat, statHash, stats, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
if (startDate == null) {
startDate = new Date;
}
if (daysPrevious == null) {
daysPrevious = 30;
}
ideally = errify(callback);
prefix = this.prefixForQueue(queue);
if (!(startDate instanceof Date)) {
startDate = new Date(startDate);
}
statHash = {};
dates = [];
keys = (function() {
var _i, _ref, _results;
_results = [];
for (i = _i = 0, _ref = daysPrevious - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
date = new Date(startDate);
date.setDate(date.getDate() - i);
datestr = this.shortDate(date);
dates.push(datestr);
_results.push("" + prefix + ":" + type + ":" + datestr);
}
return _results;
}).call(this);
(function(_this) {
return (function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/home/charles/source/el-borracho-stats/src/models/stats.coffee",
funcname: "ElBorrachoStats.fetchStatForQueue"
});
_this.redis.mget(keys, ideally(__iced_deferrals.defer({
assign_fn: (function() {
return function() {
return stats = arguments[0];
};
})(),
lineno: 120
})));
__iced_deferrals._fulfill();
});
})(this)((function(_this) {
return function() {
stats = (function() {
var _i, _len, _results;
_results = [];
for (i = _i = 0, _len = stats.length; _i < _len; i = ++_i) {
stat = stats[i];
_results.push({
type: type,
date: dates[i],
value: stat
});
}
return _results;
})();
return callback(null, stats);
};
})(this));
};
return ElBorrachoStats;
})();
module.exports = ElBorrachoStats;
//# sourceMappingURL=stats.js.map