WIPP
This commit is contained in:
parent
a375adb8b9
commit
c3261bc61a
5 changed files with 101 additions and 106 deletions
|
|
@ -6,15 +6,15 @@ const numeral = require('numeral')
|
|||
const configManager = require('./config-manager')
|
||||
const settingsLoader = require('./settings-loader')
|
||||
const db = require('./postgresql_interface')
|
||||
const T = require('./time')
|
||||
const logger = require('./logger')
|
||||
|
||||
const STALE_STATE = 2 * 60 * 1000
|
||||
const NETWORK_DOWN_TIME = 60 * 1000
|
||||
const STALE_STATE = 2 * T.minute
|
||||
const NETWORK_DOWN_TIME = T.minute
|
||||
const ALERT_SEND_INTERVAL = T.hour
|
||||
|
||||
let getBalances
|
||||
|
||||
function init (_getBalances) {
|
||||
getBalances = _getBalances
|
||||
}
|
||||
let alertFingerprint
|
||||
let lastAlertTime
|
||||
|
||||
function toInt10 (str) { return parseInt(str, 10) }
|
||||
|
||||
|
|
@ -26,6 +26,57 @@ function sameState (a, b) {
|
|||
return a.note.txId === b.note.txId && a.note.state === b.note.state
|
||||
}
|
||||
|
||||
function sendNoAlerts (plugins) {
|
||||
const subject = '[Lamassu] All clear'
|
||||
const rec = {
|
||||
sms: {
|
||||
body: subject
|
||||
},
|
||||
email: {
|
||||
subject,
|
||||
body: 'No errors are reported for your machines.'
|
||||
}
|
||||
}
|
||||
|
||||
return plugins.sendMessage(rec)
|
||||
}
|
||||
|
||||
function checkNotification (plugins) {
|
||||
return checkStatus(plugins)
|
||||
.then(alertRec => {
|
||||
const currentAlertFingerprint = buildAlertFingerprint(alertRec)
|
||||
if (!currentAlertFingerprint) {
|
||||
const inAlert = !!alertFingerprint
|
||||
alertFingerprint = null
|
||||
lastAlertTime = null
|
||||
if (inAlert) return sendNoAlerts(plugins)
|
||||
}
|
||||
|
||||
const alertChanged = currentAlertFingerprint === alertFingerprint &&
|
||||
lastAlertTime - Date.now() < ALERT_SEND_INTERVAL
|
||||
if (alertChanged) return
|
||||
|
||||
const subject = alertSubject(alertRec)
|
||||
const rec = {
|
||||
sms: {
|
||||
body: subject
|
||||
},
|
||||
email: {
|
||||
subject,
|
||||
body: printEmailAlerts(alertRec)
|
||||
}
|
||||
}
|
||||
alertFingerprint = currentAlertFingerprint
|
||||
lastAlertTime = Date.now()
|
||||
|
||||
return plugins.sendMessage(rec)
|
||||
})
|
||||
.then(results => {
|
||||
if (results && results.length > 0) logger.debug('Successfully sent alerts')
|
||||
})
|
||||
.catch(logger.error)
|
||||
}
|
||||
|
||||
function checkBalance (rec) {
|
||||
const settings = settingsLoader.settings()
|
||||
const config = configManager.unscoped(settings.config)
|
||||
|
|
@ -35,8 +86,8 @@ function checkBalance (rec) {
|
|||
: null
|
||||
}
|
||||
|
||||
function checkBalances () {
|
||||
return getBalances()
|
||||
function checkBalances (plugins) {
|
||||
return plugins.checkBalances()
|
||||
.then(balances => R.reject(R.isNil, balances.map(checkBalance)))
|
||||
}
|
||||
|
||||
|
|
@ -85,10 +136,10 @@ function devicesAndEvents () {
|
|||
.then(arr => ({devices: arr[0], events: arr[1]}))
|
||||
}
|
||||
|
||||
function checkStatus () {
|
||||
function checkStatus (plugins) {
|
||||
const alerts = {devices: {}, deviceNames: {}}
|
||||
|
||||
return Promise.all([checkBalances(), devicesAndEvents()])
|
||||
return Promise.all([checkBalances(plugins), devicesAndEvents()])
|
||||
.then(([balances, rec]) => {
|
||||
const devices = rec.devices
|
||||
const events = rec.events
|
||||
|
|
@ -163,16 +214,10 @@ function alertSubject (alertRec) {
|
|||
return '[Lamassu] Errors reported: ' + alertTypes.join(', ')
|
||||
}
|
||||
|
||||
function alertFingerprint (alertRec) {
|
||||
function buildAlertFingerprint (alertRec) {
|
||||
const subject = alertSubject(alertRec)
|
||||
if (!subject) return null
|
||||
return crypto.createHash('sha256').update(subject).digest('hex')
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
init,
|
||||
checkStatus,
|
||||
printEmailAlerts,
|
||||
alertFingerprint,
|
||||
alertSubject
|
||||
}
|
||||
module.exports = {checkNotification}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue