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:
parent
5c74e8d17c
commit
fa69956963
1 changed files with 27 additions and 11 deletions
|
|
@ -104,7 +104,7 @@ function dropRepeatsWith (comparator, arr) {
|
|||
return _.reduce(iteratee, { arr: [] }, arr).arr
|
||||
}
|
||||
|
||||
function checkStuckScreen (deviceEvents) {
|
||||
function checkStuckScreen (deviceEvents, machineName) {
|
||||
const sortedEvents = _.sortBy(getDeviceTime, _.map(jsonParse, deviceEvents))
|
||||
const noRepeatEvents = dropRepeatsWith(sameState, sortedEvents)
|
||||
const lastEvent = _.last(noRepeatEvents)
|
||||
|
|
@ -122,27 +122,27 @@ function checkStuckScreen (deviceEvents) {
|
|||
|
||||
const age = Math.floor(lastEvent.age)
|
||||
if (age > STALE_STATE) {
|
||||
return [{ code: STALE, state, age }]
|
||||
return [{ code: STALE, state, age, machineName }]
|
||||
}
|
||||
|
||||
return []
|
||||
}
|
||||
|
||||
function checkPing (deviceId) {
|
||||
function checkPing (device) {
|
||||
const sql = `select (EXTRACT(EPOCH FROM (now() - created))) * 1000 AS age from machine_pings
|
||||
where device_id=$1`
|
||||
|
||||
const deviceId = device.deviceId
|
||||
return db.oneOrNone(sql, [deviceId])
|
||||
.then(row => {
|
||||
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 []
|
||||
})
|
||||
}
|
||||
|
||||
function checkPings (devices) {
|
||||
const deviceIds = _.map('deviceId', devices)
|
||||
const promises = _.map(checkPing, deviceIds)
|
||||
const promises = _.map(checkPing, devices)
|
||||
|
||||
return Promise.all(promises)
|
||||
.then(_.zipObject(deviceIds))
|
||||
|
|
@ -165,8 +165,7 @@ function checkStatus (plugins) {
|
|||
|
||||
const balanceAlerts = _.filter(['deviceId', deviceId], balances)
|
||||
const ping = pings[deviceId] || []
|
||||
const stuckScreen = checkStuckScreen(deviceEvents)
|
||||
|
||||
const stuckScreen = checkStuckScreen(deviceEvents, deviceName)
|
||||
const deviceAlerts = _.isEmpty(ping) ? stuckScreen : ping
|
||||
|
||||
alerts.devices[deviceId] = _.concat(deviceAlerts, balanceAlerts)
|
||||
|
|
@ -226,15 +225,32 @@ function printEmailAlerts (alertRec) {
|
|||
|
||||
function alertSubject (alertRec) {
|
||||
let alerts = alertRec.general
|
||||
|
||||
_.keys(alertRec.devices).forEach(function (device) {
|
||||
alerts = _.concat(alerts, alertRec.devices[device])
|
||||
})
|
||||
|
||||
if (alerts.length === 0) return null
|
||||
|
||||
const alertTypes = _.map(codeDisplay, _.uniq(_.map('code', alerts))).sort()
|
||||
return '[Lamassu] Errors reported: ' + alertTypes.join(', ')
|
||||
const alertsMap = _.groupBy('code', alerts)
|
||||
|
||||
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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue