fix up notifications

This commit is contained in:
Josh Harvey 2017-05-16 18:06:49 +03:00
parent 87a4236313
commit 8ecc8ce510
2 changed files with 46 additions and 33 deletions

View file

@ -3,12 +3,13 @@ const _ = require('lodash/fp')
const prettyMs = require('pretty-ms') const prettyMs = require('pretty-ms')
const numeral = require('numeral') const numeral = require('numeral')
const db = require('./postgresql_interface') const dbm = require('./postgresql_interface')
const db = require('./db')
const T = require('./time') const T = require('./time')
const logger = require('./logger') const logger = require('./logger')
const STALE_STATE = 2 * T.minute const STALE_STATE = 2 * T.minute
const NETWORK_DOWN_TIME = T.minute const NETWORK_DOWN_TIME = 1 * T.minute
const ALERT_SEND_INTERVAL = T.hour const ALERT_SEND_INTERVAL = T.hour
const PING = Symbol('PING') const PING = Symbol('PING')
@ -87,23 +88,7 @@ function checkNotification (plugins) {
.catch(logger.error) .catch(logger.error)
} }
const getDeviceTime = _.flow(_.get('device_time'), _.unary(parseInt)) const getDeviceTime = _.flow(_.get('device_time'), Date.parse)
function checkPing (deviceEvents) {
const sortedEvents = _.sortBy(getDeviceTime, _.map(jsonParse, deviceEvents))
const lastEvent = _.last(sortedEvents)
if (!lastEvent) {
return [{code: PING}]
}
const age = Math.floor(lastEvent.age)
if (age > NETWORK_DOWN_TIME) {
return [{code: PING, age: age}]
}
return []
}
function dropRepeatsWith (comparator, arr) { function dropRepeatsWith (comparator, arr) {
const iteratee = (acc, val) => val === acc.last const iteratee = (acc, val) => val === acc.last
@ -137,26 +122,54 @@ function checkStuckScreen (deviceEvents) {
return [] return []
} }
function checkPing (deviceId) {
const sql = `select (EXTRACT(EPOCH FROM (now() - created))) * 1000 AS age from machine_pings
where device_id=$1
order by created desc
limit 1`
return db.oneOrNone(sql, [deviceId])
.then(row => {
if (!row) return [{code: PING}]
if (row.age > NETWORK_DOWN_TIME) return [{code: PING, age: row.age}]
return []
})
}
function checkPings (devices) {
const deviceIds = _.map('deviceId', devices)
const promises = _.map(checkPing, deviceIds)
return Promise.all(promises)
.then(_.zipObject(deviceIds))
}
function checkStatus (plugins) { function checkStatus (plugins) {
const alerts = {devices: {}, deviceNames: {}} const alerts = {devices: {}, deviceNames: {}}
return Promise.all([plugins.checkBalances(), db.machineEvents(), plugins.getMachineNames()]) return Promise.all([plugins.checkBalances(), dbm.machineEvents(), plugins.getMachineNames()])
.then(([balances, events, devices]) => { .then(([balances, events, devices]) => {
alerts.general = balances return checkPings(devices)
devices.forEach(function (device) { .then(pings => {
const deviceId = device.deviceId alerts.general = balances
const deviceName = device.name devices.forEach(function (device) {
const deviceEvents = events.filter(function (eventRow) { const deviceId = device.deviceId
return eventRow.device_id === deviceId const deviceName = device.name
const deviceEvents = events.filter(function (eventRow) {
return eventRow.device_id === deviceId
})
const ping = pings[deviceId] || []
const stuckScreen = checkStuckScreen(deviceEvents)
const deviceAlerts = _.isEmpty(ping) ? stuckScreen : ping
alerts.devices[deviceId] = deviceAlerts
alerts.deviceNames[deviceId] = deviceName
}) })
const deviceAlerts = checkStuckScreen(deviceEvents).concat(checkPing(deviceEvents)) return alerts
alerts.devices[deviceId] = deviceAlerts
alerts.deviceNames[deviceId] = deviceName
}) })
return alerts
}) })
} }

View file

@ -12,7 +12,7 @@ const SWEEP_HD_INTERVAL = T.minute
const TRADE_INTERVAL = 10 * T.seconds const TRADE_INTERVAL = 10 * T.seconds
const PONG_INTERVAL = 10 * T.seconds const PONG_INTERVAL = 10 * T.seconds
const PONG_CLEAR_INTERVAL = 1 * T.day const PONG_CLEAR_INTERVAL = 1 * T.day
const CHECK_NOTIFICATION_INTERVAL = 30 * T.seconds const CHECK_NOTIFICATION_INTERVAL = 20 * T.seconds
const PENDING_INTERVAL = 10 * T.seconds const PENDING_INTERVAL = 10 * T.seconds
let _pi, _settings let _pi, _settings