Add device names to sms alerts (#233)

* Add device names to sms alerts

* Write machine name on each alert

* Fixed alertSubject function (do not display machine name if null)

* Group machineNames by alertType

* Code review fixes. Filter empty machine names

* Use lodash complement instead of negate

* Revert "Use lodash complement instead of negate"

This reverts commit 385801624e432f03211867729281d7328493c30a.
This commit is contained in:
Davit Abulashvili 2019-01-04 20:21:57 +04:00 committed by Josh Harvey
parent 5c74e8d17c
commit fa69956963

View file

@ -104,7 +104,7 @@ function dropRepeatsWith (comparator, arr) {
return _.reduce(iteratee, { arr: [] }, arr).arr return _.reduce(iteratee, { arr: [] }, arr).arr
} }
function checkStuckScreen (deviceEvents) { function checkStuckScreen (deviceEvents, machineName) {
const sortedEvents = _.sortBy(getDeviceTime, _.map(jsonParse, deviceEvents)) const sortedEvents = _.sortBy(getDeviceTime, _.map(jsonParse, deviceEvents))
const noRepeatEvents = dropRepeatsWith(sameState, sortedEvents) const noRepeatEvents = dropRepeatsWith(sameState, sortedEvents)
const lastEvent = _.last(noRepeatEvents) const lastEvent = _.last(noRepeatEvents)
@ -122,27 +122,27 @@ function checkStuckScreen (deviceEvents) {
const age = Math.floor(lastEvent.age) const age = Math.floor(lastEvent.age)
if (age > STALE_STATE) { if (age > STALE_STATE) {
return [{ code: STALE, state, age }] return [{ code: STALE, state, age, machineName }]
} }
return [] return []
} }
function checkPing (deviceId) { function checkPing (device) {
const sql = `select (EXTRACT(EPOCH FROM (now() - created))) * 1000 AS age from machine_pings const sql = `select (EXTRACT(EPOCH FROM (now() - created))) * 1000 AS age from machine_pings
where device_id=$1` where device_id=$1`
const deviceId = device.deviceId
return db.oneOrNone(sql, [deviceId]) return db.oneOrNone(sql, [deviceId])
.then(row => { .then(row => {
if (!row) return [{ code: PING }] if (!row) return [{ code: PING }]
if (row.age > NETWORK_DOWN_TIME) return [{ code: PING, age: row.age }] if (row.age > NETWORK_DOWN_TIME) return [{ code: PING, age: row.age, machineName: device.name }]
return [] return []
}) })
} }
function checkPings (devices) { function checkPings (devices) {
const deviceIds = _.map('deviceId', devices) const deviceIds = _.map('deviceId', devices)
const promises = _.map(checkPing, deviceIds) const promises = _.map(checkPing, devices)
return Promise.all(promises) return Promise.all(promises)
.then(_.zipObject(deviceIds)) .then(_.zipObject(deviceIds))
@ -165,8 +165,7 @@ function checkStatus (plugins) {
const balanceAlerts = _.filter(['deviceId', deviceId], balances) const balanceAlerts = _.filter(['deviceId', deviceId], balances)
const ping = pings[deviceId] || [] const ping = pings[deviceId] || []
const stuckScreen = checkStuckScreen(deviceEvents) const stuckScreen = checkStuckScreen(deviceEvents, deviceName)
const deviceAlerts = _.isEmpty(ping) ? stuckScreen : ping const deviceAlerts = _.isEmpty(ping) ? stuckScreen : ping
alerts.devices[deviceId] = _.concat(deviceAlerts, balanceAlerts) alerts.devices[deviceId] = _.concat(deviceAlerts, balanceAlerts)
@ -226,15 +225,32 @@ function printEmailAlerts (alertRec) {
function alertSubject (alertRec) { function alertSubject (alertRec) {
let alerts = alertRec.general let alerts = alertRec.general
_.keys(alertRec.devices).forEach(function (device) { _.keys(alertRec.devices).forEach(function (device) {
alerts = _.concat(alerts, alertRec.devices[device]) alerts = _.concat(alerts, alertRec.devices[device])
}) })
if (alerts.length === 0) return null if (alerts.length === 0) return null
const alertTypes = _.map(codeDisplay, _.uniq(_.map('code', alerts))).sort() const alertsMap = _.groupBy('code', alerts)
return '[Lamassu] Errors reported: ' + alertTypes.join(', ')
const alertTypes = _.map(entry => {
const code = entry[0]
const machineNames = _.filter(_.negate(_.isEmpty), _.map('machineName', entry[1]))
return {
codeDisplay: codeDisplay(code),
machineNames
}
}, _.toPairs(alertsMap))
const sortedAlertTypes = _.sortBy('codeDisplay', alertTypes)
const displayAlertTypes = _.uniq(_.map(alertType => _.isEmpty(alertType.machineNames.length)
? alertType.codeDisplay
: `${alertType.codeDisplay} (${alertType.machineNames.join(', ')})`
, sortedAlertTypes))
return '[Lamassu] Errors reported: ' + displayAlertTypes.join(', ')
} }
function buildAlertFingerprint (alertRec) { function buildAlertFingerprint (alertRec) {