lots of notifier improvements

This commit is contained in:
Josh Harvey 2016-04-23 19:27:53 +03:00
parent 8a87c7579d
commit 8a4b447db3
7 changed files with 172 additions and 26 deletions

View file

@ -1,4 +1,7 @@
var crypto = require('crypto')
var R = require('ramda')
var prettyMs = require('pretty-ms')
var numeral = require('numeral')
var db = null
var getBalances = null
@ -21,7 +24,7 @@ function sameState (a, b) {
function checkBalance (rec) {
var LOW_BALANCE_THRESHOLD = 10
return rec.fiatBalance < LOW_BALANCE_THRESHOLD
? {code: 'lowBalance', cryptoCode: rec.cryptoCode, fiatBalance: rec.fiatBalance}
? {code: 'lowBalance', cryptoCode: rec.cryptoCode, fiatBalance: rec.fiatBalance, fiatCode: rec.fiatCode}
: null
}
@ -110,5 +113,65 @@ function checkStatus () {
return alerts
})
.catch(function (err) {
console.log(err.stack)
})
}
exports.checkStatus = checkStatus
function formatCurrency (num, code) {
return numeral(num).format('0,0.00') + ' ' + code
}
function emailAlert (alert) {
switch (alert.code) {
case 'ping':
var pingAge = prettyMs(alert.age, {compact: true, verbose: true})
return 'Connection to machine down for ' + pingAge
case 'stale':
var stuckAge = prettyMs(alert.age, {compact: true, verbose: true})
return 'Machine is stuck on ' + alert.state + 'screen for ' + stuckAge
case 'lowBalance':
var balance = formatCurrency(alert.fiatBalance, alert.fiatCode)
return 'Low balance of ' + balance + ' in ' + alert.cryptoCode + ' wallet'
}
}
function emailAlerts (alerts) {
return alerts.map(emailAlert).join('\n') + '\n'
}
function printEmailAlerts (alertRec) {
var body = 'Errors were reported by your Lamassu Machines.\n'
if (alertRec.general.length !== 0) {
body = body + '\nGeneral errors:\n'
body = body + emailAlerts(alertRec.general)
}
R.keys(alertRec.devices).forEach(function (device) {
body = body + '\nErrors for ' + device + ':\n'
body = body + emailAlerts(alertRec.devices[device])
})
return body
}
exports.printEmailAlerts = printEmailAlerts
function alertSubject (alertRec) {
var alerts = alertRec.general
R.keys(alertRec.devices).forEach(function (device) {
alerts = R.concat(alerts, alertRec.devices[device])
})
if (alerts.length === 0) return null
var alertTypes = R.uniq(R.pluck('code', alerts)).sort()
return '[Lamassu] Errors reported: ' + alertTypes.join(', ')
}
exports.alertSubject = alertSubject
function alertFingerprint (alertRec) {
var subject = alertSubject(alertRec)
if (!subject) return null
return crypto.createHash('sha256').update(subject).digest('hex')
}
exports.alertFingerprint = alertFingerprint