From e42bde7b636557af05d72ffbc3007153084bf887 Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Thu, 21 Apr 2016 22:42:47 +0300 Subject: [PATCH] WIP --- lib/notifier.js | 127 ++++++++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 68 deletions(-) diff --git a/lib/notifier.js b/lib/notifier.js index 935a73db..f7db3bc5 100644 --- a/lib/notifier.js +++ b/lib/notifier.js @@ -1,13 +1,14 @@ 'use strict' +require('es6-promise').polyfill() + var R = require('ramda') - var db = null -var getBalance = null +var getBalances = null -function init (_db, _getBalance) { +function init (_db, _getBalances) { db = _db - getBalance = _getBalance + getBalances = _getBalances } 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 } -function checkBalance () { +function checkBalance (rec) { var LOW_BALANCE_THRESHOLD = 10 - if (getBalance() < LOW_BALANCE_THRESHOLD) { - return [{code: 'lowBitcoinBalance'}] - } + return rec.fiatBalance < LOW_BALANCE_THRESHOLD + ? {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) { @@ -38,11 +42,9 @@ function checkPing (deviceEvents) { return [{code: 'ping'}] } - console.log('DEBUG6') - console.log(lastEvent) - - if (lastEvent.age > NETWORK_DOWN_TIME) { - return [{code: 'ping'}] + var age = Math.floor(lastEvent.age) + if (age > NETWORK_DOWN_TIME) { + return [{code: 'ping', age: age}] } return [] @@ -65,78 +67,67 @@ function checkStuckScreen (deviceEvents) { return [] } - console.log(lastEvent.age) - if (lastEvent.age > STALE_STATE) { - return [{code: 'stale', state: state}] + var age = Math.floor(lastEvent.age) + if (age > STALE_STATE) { + return [{code: 'stale', state: state, age: age}] } return [] } -function checkStatus (cb) { -/* - - 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 = [] +function devicesAndEvents () { + return new Promise(function (resolve, reject) { + db.devices(function (err, devices) { + if (err) return reject(err) - alerts = R.concat(alerts, checkBalance()) + db.machineEvents(function (err, events) { + if (err) return reject(err) - db.devices(function (err, devices) { - if (err) return cb(err) - - db.machineEvents(function (err, events) { - if (err) return cb(err) - - devices.rows.forEach(function (deviceRow) { - var deviceFingerprint = deviceRow.fingerprint - var deviceEvents = events.rows.filter(function (eventRow) { - return eventRow.device_fingerprint === deviceFingerprint - }) - - console.log('DEVICE: ' + deviceRow.fingerprint) - alerts = R.concat(alerts, checkStuckScreen(deviceEvents)) - alerts = R.concat(alerts, checkPing(deviceEvents)) - - console.log(deviceFingerprint) - console.log(alerts) - - // TODO: Use promises to do this right + 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) { + var deviceFingerprint = deviceRow.fingerprint + var deviceEvents = events.rows.filter(function (eventRow) { + return eventRow.device_fingerprint === deviceFingerprint + }) + + var deviceAlerts = [] + deviceAlerts = R.concat(deviceAlerts, checkStuckScreen(deviceEvents)) + deviceAlerts = R.concat(deviceAlerts, checkPing(deviceEvents)) + + alerts.devices[deviceRow.fingerprint] = deviceAlerts + }) + + return alerts + }) +} + var _db = require('./postgresql_interface') var connectionString = 'postgres://lamassu:lamassu@localhost/lamassu' -var _getBalance = function () { - return 12 +var _getBalances = function () { + return [{cryptoCode: 'BTC', fiatBalance: 12}, {cryptoCode: 'ETH', fiatBalance: 8}] } _db.init(connectionString) -init(_db, _getBalance) +init(_db, _getBalances) -checkStatus(function (err, alerts) { - if (err) console.log(err) +checkStatus() +.then(function (alerts) { 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 -*/