lots of notifier improvements
This commit is contained in:
parent
8a87c7579d
commit
8a4b447db3
7 changed files with 172 additions and 26 deletions
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
var _ = require('lodash')
|
||||
var async = require('async')
|
||||
var R = require('ramda')
|
||||
|
||||
var BigNumber = require('bignumber.js')
|
||||
|
||||
|
|
@ -15,6 +14,7 @@ var uuid = require('node-uuid')
|
|||
var tradeIntervals = {}
|
||||
|
||||
var CHECK_NOTIFICATION_INTERVAL = 60 * 1000
|
||||
var ALERT_SEND_INTERVAL = 60 * 60 * 1000
|
||||
var POLLING_RATE = 60 * 1000 // poll each minute
|
||||
var REAP_RATE = 2 * 1000
|
||||
var PENDING_TIMEOUT = 70 * 1000
|
||||
|
|
@ -51,6 +51,9 @@ var coins = {
|
|||
ETH: {unitScale: 18}
|
||||
}
|
||||
|
||||
var alertFingerprint = null
|
||||
var lastAlertTime = null
|
||||
|
||||
// that's basically a constructor
|
||||
exports.init = function init (databaseHandle) {
|
||||
if (!databaseHandle) {
|
||||
|
|
@ -428,7 +431,9 @@ exports.dispenseAck = function dispenseAck (session, rec) {
|
|||
}
|
||||
|
||||
exports.fiatBalance = function fiatBalance (cryptoCode) {
|
||||
var rawRate = exports.getDeviceRate(cryptoCode).rates.ask
|
||||
var deviceRate = exports.getDeviceRate(cryptoCode)
|
||||
if (!deviceRate) return null
|
||||
var rawRate = deviceRate.rates.ask
|
||||
var commission = cachedConfig.exchanges.settings.commission
|
||||
var lastBalance = lastBalances[cryptoCode]
|
||||
|
||||
|
|
@ -634,49 +639,95 @@ exports.getcryptoCodes = function getcryptoCodes () {
|
|||
return cryptoCodes
|
||||
}
|
||||
|
||||
function sendMessage (rec, cb) {
|
||||
console.log('DEBUG5')
|
||||
cb = cb || function () {}
|
||||
function sendMessage (rec) {
|
||||
console.log('DEBUG50')
|
||||
console.log('%j', rec)
|
||||
console.log(cachedConfig.exchanges.plugins.current.notify)
|
||||
var pluginTypes = JSON.parse(cachedConfig.exchanges.plugins.current.notify)
|
||||
console.log('DEBUG7')
|
||||
console.log(pluginTypes)
|
||||
var pluginPromises = pluginTypes.map(function (pluginType) {
|
||||
if (pluginType === 'email') return emailPlugin.sendMessage(rec, cb)
|
||||
if (pluginType === 'sms') return smsPlugin.sendMessage(rec, cb)
|
||||
if (pluginType === 'email') return emailPlugin.sendMessage(rec)
|
||||
if (pluginType === 'sms') return smsPlugin.sendMessage(rec)
|
||||
throw new Error('No such plugin type: ' + pluginType)
|
||||
})
|
||||
return Promise.all(pluginPromises)
|
||||
}
|
||||
exports.sendMessage = sendMessage
|
||||
|
||||
function sendNoAlerts () {
|
||||
var subject = '[Lamassu] All clear'
|
||||
var rec = {
|
||||
sms: {
|
||||
body: subject
|
||||
},
|
||||
email: {
|
||||
subject: subject,
|
||||
body: 'No errors are reported for your machines.'
|
||||
}
|
||||
}
|
||||
return sendMessage(rec)
|
||||
}
|
||||
|
||||
function checkNotification () {
|
||||
notifier.checkStatus()
|
||||
.then(function (alerts) {
|
||||
if (alerts.length === 0) return
|
||||
console.log('DEBUG39 ******************************')
|
||||
return notifier.checkStatus()
|
||||
.then(function (alertRec) {
|
||||
console.log('DEBUG41 ******************************')
|
||||
var fingerprint = notifier.alertFingerprint(alertRec)
|
||||
if (!fingerprint) {
|
||||
console.log('DEBUG40 ******************************')
|
||||
var inAlert = !!alertFingerprint
|
||||
alertFingerprint = null
|
||||
lastAlertTime = null
|
||||
if (inAlert) return sendNoAlerts()
|
||||
}
|
||||
|
||||
// var devices = R.keys(alerts.devices)
|
||||
var alertRecs = alerts.general
|
||||
R.keys(alerts.devices).forEach(function (device) {
|
||||
alertRecs = R.append(alerts.devices[device], alertRecs)
|
||||
})
|
||||
var alertTypes = alertRecs.pluck('code', alerts)
|
||||
var body = ''
|
||||
console.log('DEBUG42 ******************************')
|
||||
|
||||
var alertChanged = fingerprint === alertFingerprint &&
|
||||
lastAlertTime - Date.now() < ALERT_SEND_INTERVAL
|
||||
if (alertChanged) return
|
||||
|
||||
console.log('DEBUG43 ******************************')
|
||||
|
||||
var subject = notifier.alertSubject(alertRec)
|
||||
var rec = {
|
||||
sms: {
|
||||
body: '[Lamassu] Errors reported: ' + alertTypes.join(', ')
|
||||
body: subject
|
||||
},
|
||||
email: {
|
||||
subject: '[Lamassu] Errors reported: ' + alertTypes.join(', '),
|
||||
body: body
|
||||
subject: subject,
|
||||
body: notifier.printEmailAlerts(alertRec)
|
||||
}
|
||||
}
|
||||
sendMessage(rec)
|
||||
alertFingerprint = fingerprint
|
||||
lastAlertTime = Date.now()
|
||||
return sendMessage(rec)
|
||||
})
|
||||
.then(function () {
|
||||
logger.debug('Successfully sent alerts')
|
||||
})
|
||||
.catch(function (err) {
|
||||
logger.error(err)
|
||||
})
|
||||
}
|
||||
|
||||
function checkBalances () {
|
||||
var cryptoCodes = exports.getcryptoCodes()
|
||||
|
||||
var balances = []
|
||||
cryptoCodes.forEach(function (cryptoCode) {
|
||||
var balance = exports.fiatBalance(cryptoCode)
|
||||
if (!balance) return
|
||||
var rec = {fiatBalance: balance, cryptoCode: cryptoCode, fiatCode: deviceCurrency}
|
||||
balances.push(rec)
|
||||
})
|
||||
|
||||
return balances
|
||||
}
|
||||
|
||||
exports.startCheckingNotification = function startCheckingNotification () {
|
||||
notifier.init(db, checkBalances)
|
||||
checkNotification()
|
||||
setInterval(checkNotification, CHECK_NOTIFICATION_INTERVAL)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue