This commit is contained in:
Josh Harvey 2016-04-23 21:54:46 +03:00
parent 8a4b447db3
commit e930925818
5 changed files with 15 additions and 31 deletions

View file

@ -11,7 +11,7 @@ function getBalances () {
}
db.init('psql://lamassu:lamassu@localhost/lamassu')
notifier.init(db, getBalances)
notifier.init(db, getBalances, {lowBalanceThreshold: 10})
console.log('DEBUG0')
notifier.checkStatus()
.then(function (alertRec) {

View file

@ -5,9 +5,18 @@ var numeral = require('numeral')
var db = null
var getBalances = null
function init (_db, _getBalances) {
var LOW_BALANCE_THRESHOLD = 1000
var STALE_STATE = 2 * 60 * 1000
var NETWORK_DOWN_TIME = 60 * 1000
function init (_db, _getBalances, config) {
db = _db
getBalances = _getBalances
console.log(config)
if (config && config.lowBalanceThreshold) {
LOW_BALANCE_THRESHOLD = config.lowBalanceThreshold
}
}
exports.init = init
@ -22,7 +31,6 @@ function sameState (a, b) {
}
function checkBalance (rec) {
var LOW_BALANCE_THRESHOLD = 10
return rec.fiatBalance < LOW_BALANCE_THRESHOLD
? {code: 'lowBalance', cryptoCode: rec.cryptoCode, fiatBalance: rec.fiatBalance, fiatCode: rec.fiatCode}
: null
@ -36,7 +44,6 @@ function checkBalances () {
function checkPing (deviceEvents) {
var sortedEvents = R.sortBy(R.compose(toInt10, R.prop('device_time')), R.map(jsonParse, deviceEvents))
var lastEvent = R.last(sortedEvents)
var NETWORK_DOWN_TIME = 2 * 60 * 1000
if (!lastEvent) {
return [{code: 'ping'}]
@ -54,7 +61,6 @@ function checkStuckScreen (deviceEvents) {
var sortedEvents = R.sortBy(R.compose(toInt10, R.prop('device_time')), R.map(jsonParse, deviceEvents))
var noRepeatEvents = R.dropRepeatsWith(sameState, sortedEvents)
var lastEvent = R.last(noRepeatEvents)
var STALE_STATE = 60 * 1000
if (!lastEvent) {
return []

View file

@ -347,8 +347,6 @@ function reapTxs () {
// TODO: Run these in parallel and return success
exports.trade = function trade (session, rawTrade, cb) {
logger.debug('DEBUG2')
// TODO: move this to DB, too
// add bill to trader queue (if trader is enabled)
var cryptoCode = rawTrade.cryptoCode || 'BTC'
@ -363,16 +361,12 @@ exports.trade = function trade (session, rawTrade, cb) {
})
}
logger.debug('DEBUG3')
if (!rawTrade.toAddress) {
var newRawTrade = _.cloneDeep(rawTrade)
newRawTrade.toAddress = 'remit'
return db.recordBill(session, newRawTrade, cb)
}
logger.debug('DEBUG1')
async.parallel([
async.apply(db.addOutgoingPending, session, rawTrade.currency, rawTrade.cryptoCode, rawTrade.toAddress),
async.apply(db.recordBill, session, rawTrade)
@ -391,7 +385,6 @@ exports.stateChange = function stateChange (session, rec, cb) {
}
exports.recordPing = function recordPing (session, rec, cb) {
console.log('DEBUG4')
var event = {
id: uuid.v4(),
fingerprint: session.fingerprint,
@ -640,11 +633,7 @@ exports.getcryptoCodes = function getcryptoCodes () {
}
function sendMessage (rec) {
console.log('DEBUG50')
console.log('%j', rec)
console.log(cachedConfig.exchanges.plugins.current.notify)
var pluginTypes = JSON.parse(cachedConfig.exchanges.plugins.current.notify)
console.log(pluginTypes)
var pluginPromises = pluginTypes.map(function (pluginType) {
if (pluginType === 'email') return emailPlugin.sendMessage(rec)
if (pluginType === 'sms') return smsPlugin.sendMessage(rec)
@ -669,27 +658,20 @@ function sendNoAlerts () {
}
function checkNotification () {
console.log('DEBUG39 ******************************')
return notifier.checkStatus()
.then(function (alertRec) {
console.log('DEBUG41 ******************************')
var fingerprint = notifier.alertFingerprint(alertRec)
if (!fingerprint) {
console.log('DEBUG40 ******************************')
var inAlert = !!alertFingerprint
alertFingerprint = null
lastAlertTime = null
if (inAlert) return sendNoAlerts()
}
console.log('DEBUG42 ******************************')
var alertChanged = fingerprint === alertFingerprint &&
lastAlertTime - Date.now() < ALERT_SEND_INTERVAL
if (alertChanged) return
console.log('DEBUG43 ******************************')
var subject = notifier.alertSubject(alertRec)
var rec = {
sms: {
@ -702,6 +684,7 @@ function checkNotification () {
}
alertFingerprint = fingerprint
lastAlertTime = Date.now()
return sendMessage(rec)
})
.then(function () {
@ -727,7 +710,8 @@ function checkBalances () {
}
exports.startCheckingNotification = function startCheckingNotification () {
notifier.init(db, checkBalances)
var config = cachedConfig.exchanges.plugins.settings.notifier
notifier.init(db, checkBalances, config)
checkNotification()
setInterval(checkNotification, CHECK_NOTIFICATION_INTERVAL)
}

View file

@ -167,7 +167,6 @@ function dispenseAck (req, res) {
}
function deviceEvent (req, res) {
console.log('DEBUG5')
plugins.logEvent(session(req), req.body)
res.json({err: null})
}

View file

@ -1,6 +1 @@
- configure email, sms or both for notification
- integrate email and sms into notifications
- run notifications from server
- don't keep sending notifications when state persists -- keep variable in memory
- add toEmail, toNumber to db
- finish up notification emails
- configure LOW_BALANCE_THRESHOLD, STALE_STATE, NETWORK_DOWN_TIME