el-borracho-stats
Version:
REST and SSE API and worker producing daily and all-time totals for Bull queues
128 lines (94 loc) • 4.1 kB
text/coffeescript
errify = require "errify"
class ElBorrachoStats
completed: 0
failed: 0
incrementCompleted: => ++
incrementFailed: => ++
clearCompleted: => = 0
clearFailed: => = 0
prefixForQueue: (queue) -> "bull:#{@namespace}:#{queue}"
shortDate: (date) -> (date.toISOString().split "T")[0]
constructor: ({, , , , }) ->
throw new Error "redis client required" unless
or= "stat"
=
= "#{@prefix}:statistician"
or= 60
?= true
lock: (callback) =>
ideally = errify (err) -> throw err
await .exists , ideally defer key
throw new Error "key #{@statistician} exists, Stats may already be running for this queue" if key
await .set , true, ideally defer()
await ideally defer()
callback()
updateLock: (callback) =>
.expire , , callback
unlock: (callback = ->) =>
await .del , defer err
throw err if err
callback()
update: (callback = ->) =>
ideally = errify (err) -> throw err
today = new Date
all = "bull:#{@namespace}:all"
await ideally defer()
if
multi = .multi()
multi.incrby "#{@prefix}:completed",
multi.incrby "#{@prefix}:completed:#{today}",
if
multi.incrby "#{all}:completed",
multi.incrby "#{all}:completed:#{today}",
await multi.exec ideally defer()
()
if
multi = .multi()
multi.incrby "#{@prefix}:failed",
multi.incrby "#{@prefix}:failed:#{today}",
if
multi.incrby "#{all}:failed",
multi.incrby "#{all}:failed:#{today}",
await multi.exec ideally defer()
()
callback()
fetch: (callback) =>
, callback
fetchForAll: (callback) =>
"all", callback
fetchForQueue: (queue, callback) =>
ideally = errify callback
prefix = queue
await .get "#{prefix}:completed", ideally defer completed
await .get "#{prefix}:failed", ideally defer failed
callback null, {completed, failed}
fetchHistory: (startDate = new Date, daysPrevious = 30, callback) =>
, startDate, daysPrevious, callback
fetchHistoryForAll: (startDate = new Date, daysPrevious = 30, callback) =>
"all", startDate, daysPrevious, callback
fetchHistoryForQueue: (queue, startDate = new Date, daysPrevious = 30, callback) =>
ideally = errify callback
await queue, "completed", startDate, daysPrevious, ideally defer completed
await queue, "failed", startDate, daysPrevious, ideally defer failed
result = completed.concat failed
callback null, result
fetchStat: (type, startDate = new Date, daysPrevious = 30, callback) =>
, type, startDate, daysPrevious, callback
fetchStatForAll: (type, startDate = new Date, daysPrevious = 30, callback) =>
"all", type, startDate, daysPrevious, callback
fetchStatForQueue: (queue, type, startDate = new Date, daysPrevious = 30, callback) =>
ideally = errify callback
prefix = queue
startDate = new Date startDate unless startDate instanceof Date
statHash = {}
dates = []
keys = for i in [0..daysPrevious-1]
date = new Date startDate
date.setDate date.getDate() - i
datestr = date
dates.push datestr
"#{prefix}:#{type}:#{datestr}"
await .mget keys, ideally defer stats
stats = ({type, date: dates[i], value: stat} for stat, i in stats)
callback null, stats
module.exports = ElBorrachoStats