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 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,11 +122,35 @@ 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]) => {
return checkPings(devices)
.then(pings => {
alerts.general = balances
devices.forEach(function (device) {
const deviceId = device.deviceId
@ -150,7 +159,10 @@ function checkStatus (plugins) {
return eventRow.device_id === deviceId
})
const deviceAlerts = checkStuckScreen(deviceEvents).concat(checkPing(deviceEvents))
const ping = pings[deviceId] || []
const stuckScreen = checkStuckScreen(deviceEvents)
const deviceAlerts = _.isEmpty(ping) ? stuckScreen : ping
alerts.devices[deviceId] = deviceAlerts
alerts.deviceNames[deviceId] = deviceName
@ -158,6 +170,7 @@ function checkStatus (plugins) {
return alerts
})
})
}
function formatCurrency (num, code) {

View file

@ -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