This commit is contained in:
Josh Harvey 2016-04-21 22:42:47 +03:00
parent 4b5b460d12
commit e42bde7b63

View file

@ -1,13 +1,14 @@
'use strict' 'use strict'
require('es6-promise').polyfill()
var R = require('ramda') var R = require('ramda')
var db = null var db = null
var getBalance = null var getBalances = null
function init (_db, _getBalance) { function init (_db, _getBalances) {
db = _db db = _db
getBalance = _getBalance getBalances = _getBalances
} }
function toInt10 (str) { return parseInt(str, 10) } function toInt10 (str) { return parseInt(str, 10) }
@ -20,13 +21,16 @@ function sameState (a, b) {
return a.note.sessionId === b.note.sessionId && a.note.state === b.note.state return a.note.sessionId === b.note.sessionId && a.note.state === b.note.state
} }
function checkBalance () { function checkBalance (rec) {
var LOW_BALANCE_THRESHOLD = 10 var LOW_BALANCE_THRESHOLD = 10
if (getBalance() < LOW_BALANCE_THRESHOLD) { return rec.fiatBalance < LOW_BALANCE_THRESHOLD
return [{code: 'lowBitcoinBalance'}] ? {code: 'lowBalance', cryptoCode: rec.cryptoCode, fiatBalance: rec.fiatBalance}
: null
} }
return [] function checkBalances () {
var balances = getBalances()
return R.reject(R.isNil, balances.map(checkBalance))
} }
function checkPing (deviceEvents) { function checkPing (deviceEvents) {
@ -38,11 +42,9 @@ function checkPing (deviceEvents) {
return [{code: 'ping'}] return [{code: 'ping'}]
} }
console.log('DEBUG6') var age = Math.floor(lastEvent.age)
console.log(lastEvent) if (age > NETWORK_DOWN_TIME) {
return [{code: 'ping', age: age}]
if (lastEvent.age > NETWORK_DOWN_TIME) {
return [{code: 'ping'}]
} }
return [] return []
@ -65,37 +67,36 @@ function checkStuckScreen (deviceEvents) {
return [] return []
} }
console.log(lastEvent.age) var age = Math.floor(lastEvent.age)
if (lastEvent.age > STALE_STATE) { if (age > STALE_STATE) {
return [{code: 'stale', state: state}] return [{code: 'stale', state: state, age: age}]
} }
return [] return []
} }
function checkStatus (cb) { function devicesAndEvents () {
/* return new Promise(function (resolve, reject) {
- Fetch devices from devices table
- Fetch all machine_events into memory
- For each device, verify the following:
v stuck on screen
- last screen is >5m stale and is not idle screen
- report stuck on screen and name of screen
- not scanning qr codes?
v low bitcoins -- need a separate strategy, but server has this info
- var fiatBalance = plugins.fiatBalance();
v machine isn't pinging server
- jam checking, need report from l-m [next release]
*/
var alerts = []
alerts = R.concat(alerts, checkBalance())
db.devices(function (err, devices) { db.devices(function (err, devices) {
if (err) return cb(err) if (err) return reject(err)
db.machineEvents(function (err, events) { db.machineEvents(function (err, events) {
if (err) return cb(err) if (err) return reject(err)
return resolve({devices: devices, events: events})
})
})
})
}
function checkStatus () {
var alerts = {devices: {}}
alerts.general = checkBalances()
return devicesAndEvents()
.then(function (rec) {
var devices = rec.devices
var events = rec.events
devices.rows.forEach(function (deviceRow) { devices.rows.forEach(function (deviceRow) {
var deviceFingerprint = deviceRow.fingerprint var deviceFingerprint = deviceRow.fingerprint
@ -103,40 +104,30 @@ function checkStatus (cb) {
return eventRow.device_fingerprint === deviceFingerprint return eventRow.device_fingerprint === deviceFingerprint
}) })
console.log('DEVICE: ' + deviceRow.fingerprint) var deviceAlerts = []
alerts = R.concat(alerts, checkStuckScreen(deviceEvents)) deviceAlerts = R.concat(deviceAlerts, checkStuckScreen(deviceEvents))
alerts = R.concat(alerts, checkPing(deviceEvents)) deviceAlerts = R.concat(deviceAlerts, checkPing(deviceEvents))
console.log(deviceFingerprint) alerts.devices[deviceRow.fingerprint] = deviceAlerts
console.log(alerts) })
// TODO: Use promises to do this right return alerts
})
})
}) })
} }
var _db = require('./postgresql_interface') var _db = require('./postgresql_interface')
var connectionString = 'postgres://lamassu:lamassu@localhost/lamassu' var connectionString = 'postgres://lamassu:lamassu@localhost/lamassu'
var _getBalance = function () { var _getBalances = function () {
return 12 return [{cryptoCode: 'BTC', fiatBalance: 12}, {cryptoCode: 'ETH', fiatBalance: 8}]
} }
_db.init(connectionString) _db.init(connectionString)
init(_db, _getBalance) init(_db, _getBalances)
checkStatus(function (err, alerts) { checkStatus()
if (err) console.log(err) .then(function (alerts) {
console.log('DEBUG1') console.log('DEBUG1')
console.log(alerts) console.log('%j', alerts)
process.exit(0)
}) })
// TODO: How to know which alerts have been sent?
// Send alert every 10m while alert state
// Remember last sent alert in memory
/*
- check balances for all cryptos
- update idle screens
*/