diff --git a/lib/notifier.js b/lib/notifier.js index d93851a5..58be2b48 100644 --- a/lib/notifier.js +++ b/lib/notifier.js @@ -3,12 +3,13 @@ const _ = require('lodash/fp') const prettyMs = require('pretty-ms') const numeral = require('numeral') -const db = require('./postgresql_interface') +const dbm = require('./postgresql_interface') +const db = require('./db') const T = require('./time') const logger = require('./logger') 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 PING = Symbol('PING') @@ -87,23 +88,7 @@ function checkNotification (plugins) { .catch(logger.error) } -const getDeviceTime = _.flow(_.get('device_time'), _.unary(parseInt)) - -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 [] -} +const getDeviceTime = _.flow(_.get('device_time'), Date.parse) function dropRepeatsWith (comparator, arr) { const iteratee = (acc, val) => val === acc.last @@ -137,26 +122,54 @@ function checkStuckScreen (deviceEvents) { 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) { 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]) => { - alerts.general = balances - devices.forEach(function (device) { - const deviceId = device.deviceId - const deviceName = device.name - const deviceEvents = events.filter(function (eventRow) { - return eventRow.device_id === deviceId + return checkPings(devices) + .then(pings => { + alerts.general = balances + devices.forEach(function (device) { + const deviceId = device.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)) - - alerts.devices[deviceId] = deviceAlerts - alerts.deviceNames[deviceId] = deviceName + return alerts }) - - return alerts }) } diff --git a/lib/poller.js b/lib/poller.js index 32d59acb..5a639d6a 100644 --- a/lib/poller.js +++ b/lib/poller.js @@ -12,7 +12,7 @@ const SWEEP_HD_INTERVAL = T.minute const TRADE_INTERVAL = 10 * T.seconds const PONG_INTERVAL = 10 * T.seconds 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 let _pi, _settings