cleanup
This commit is contained in:
parent
8a4b447db3
commit
e930925818
5 changed files with 15 additions and 31 deletions
|
|
@ -11,7 +11,7 @@ function getBalances () {
|
||||||
}
|
}
|
||||||
|
|
||||||
db.init('psql://lamassu:lamassu@localhost/lamassu')
|
db.init('psql://lamassu:lamassu@localhost/lamassu')
|
||||||
notifier.init(db, getBalances)
|
notifier.init(db, getBalances, {lowBalanceThreshold: 10})
|
||||||
console.log('DEBUG0')
|
console.log('DEBUG0')
|
||||||
notifier.checkStatus()
|
notifier.checkStatus()
|
||||||
.then(function (alertRec) {
|
.then(function (alertRec) {
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,18 @@ var numeral = require('numeral')
|
||||||
var db = null
|
var db = null
|
||||||
var getBalances = 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
|
db = _db
|
||||||
getBalances = _getBalances
|
getBalances = _getBalances
|
||||||
|
|
||||||
|
console.log(config)
|
||||||
|
if (config && config.lowBalanceThreshold) {
|
||||||
|
LOW_BALANCE_THRESHOLD = config.lowBalanceThreshold
|
||||||
|
}
|
||||||
}
|
}
|
||||||
exports.init = init
|
exports.init = init
|
||||||
|
|
||||||
|
|
@ -22,7 +31,6 @@ function sameState (a, b) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkBalance (rec) {
|
function checkBalance (rec) {
|
||||||
var LOW_BALANCE_THRESHOLD = 10
|
|
||||||
return rec.fiatBalance < LOW_BALANCE_THRESHOLD
|
return rec.fiatBalance < LOW_BALANCE_THRESHOLD
|
||||||
? {code: 'lowBalance', cryptoCode: rec.cryptoCode, fiatBalance: rec.fiatBalance, fiatCode: rec.fiatCode}
|
? {code: 'lowBalance', cryptoCode: rec.cryptoCode, fiatBalance: rec.fiatBalance, fiatCode: rec.fiatCode}
|
||||||
: null
|
: null
|
||||||
|
|
@ -36,7 +44,6 @@ function checkBalances () {
|
||||||
function checkPing (deviceEvents) {
|
function checkPing (deviceEvents) {
|
||||||
var sortedEvents = R.sortBy(R.compose(toInt10, R.prop('device_time')), R.map(jsonParse, deviceEvents))
|
var sortedEvents = R.sortBy(R.compose(toInt10, R.prop('device_time')), R.map(jsonParse, deviceEvents))
|
||||||
var lastEvent = R.last(sortedEvents)
|
var lastEvent = R.last(sortedEvents)
|
||||||
var NETWORK_DOWN_TIME = 2 * 60 * 1000
|
|
||||||
|
|
||||||
if (!lastEvent) {
|
if (!lastEvent) {
|
||||||
return [{code: 'ping'}]
|
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 sortedEvents = R.sortBy(R.compose(toInt10, R.prop('device_time')), R.map(jsonParse, deviceEvents))
|
||||||
var noRepeatEvents = R.dropRepeatsWith(sameState, sortedEvents)
|
var noRepeatEvents = R.dropRepeatsWith(sameState, sortedEvents)
|
||||||
var lastEvent = R.last(noRepeatEvents)
|
var lastEvent = R.last(noRepeatEvents)
|
||||||
var STALE_STATE = 60 * 1000
|
|
||||||
|
|
||||||
if (!lastEvent) {
|
if (!lastEvent) {
|
||||||
return []
|
return []
|
||||||
|
|
|
||||||
|
|
@ -347,8 +347,6 @@ function reapTxs () {
|
||||||
|
|
||||||
// TODO: Run these in parallel and return success
|
// TODO: Run these in parallel and return success
|
||||||
exports.trade = function trade (session, rawTrade, cb) {
|
exports.trade = function trade (session, rawTrade, cb) {
|
||||||
logger.debug('DEBUG2')
|
|
||||||
|
|
||||||
// TODO: move this to DB, too
|
// TODO: move this to DB, too
|
||||||
// add bill to trader queue (if trader is enabled)
|
// add bill to trader queue (if trader is enabled)
|
||||||
var cryptoCode = rawTrade.cryptoCode || 'BTC'
|
var cryptoCode = rawTrade.cryptoCode || 'BTC'
|
||||||
|
|
@ -363,16 +361,12 @@ exports.trade = function trade (session, rawTrade, cb) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug('DEBUG3')
|
|
||||||
|
|
||||||
if (!rawTrade.toAddress) {
|
if (!rawTrade.toAddress) {
|
||||||
var newRawTrade = _.cloneDeep(rawTrade)
|
var newRawTrade = _.cloneDeep(rawTrade)
|
||||||
newRawTrade.toAddress = 'remit'
|
newRawTrade.toAddress = 'remit'
|
||||||
return db.recordBill(session, newRawTrade, cb)
|
return db.recordBill(session, newRawTrade, cb)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug('DEBUG1')
|
|
||||||
|
|
||||||
async.parallel([
|
async.parallel([
|
||||||
async.apply(db.addOutgoingPending, session, rawTrade.currency, rawTrade.cryptoCode, rawTrade.toAddress),
|
async.apply(db.addOutgoingPending, session, rawTrade.currency, rawTrade.cryptoCode, rawTrade.toAddress),
|
||||||
async.apply(db.recordBill, session, rawTrade)
|
async.apply(db.recordBill, session, rawTrade)
|
||||||
|
|
@ -391,7 +385,6 @@ exports.stateChange = function stateChange (session, rec, cb) {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.recordPing = function recordPing (session, rec, cb) {
|
exports.recordPing = function recordPing (session, rec, cb) {
|
||||||
console.log('DEBUG4')
|
|
||||||
var event = {
|
var event = {
|
||||||
id: uuid.v4(),
|
id: uuid.v4(),
|
||||||
fingerprint: session.fingerprint,
|
fingerprint: session.fingerprint,
|
||||||
|
|
@ -640,11 +633,7 @@ exports.getcryptoCodes = function getcryptoCodes () {
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendMessage (rec) {
|
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)
|
var pluginTypes = JSON.parse(cachedConfig.exchanges.plugins.current.notify)
|
||||||
console.log(pluginTypes)
|
|
||||||
var pluginPromises = pluginTypes.map(function (pluginType) {
|
var pluginPromises = pluginTypes.map(function (pluginType) {
|
||||||
if (pluginType === 'email') return emailPlugin.sendMessage(rec)
|
if (pluginType === 'email') return emailPlugin.sendMessage(rec)
|
||||||
if (pluginType === 'sms') return smsPlugin.sendMessage(rec)
|
if (pluginType === 'sms') return smsPlugin.sendMessage(rec)
|
||||||
|
|
@ -669,27 +658,20 @@ function sendNoAlerts () {
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkNotification () {
|
function checkNotification () {
|
||||||
console.log('DEBUG39 ******************************')
|
|
||||||
return notifier.checkStatus()
|
return notifier.checkStatus()
|
||||||
.then(function (alertRec) {
|
.then(function (alertRec) {
|
||||||
console.log('DEBUG41 ******************************')
|
|
||||||
var fingerprint = notifier.alertFingerprint(alertRec)
|
var fingerprint = notifier.alertFingerprint(alertRec)
|
||||||
if (!fingerprint) {
|
if (!fingerprint) {
|
||||||
console.log('DEBUG40 ******************************')
|
|
||||||
var inAlert = !!alertFingerprint
|
var inAlert = !!alertFingerprint
|
||||||
alertFingerprint = null
|
alertFingerprint = null
|
||||||
lastAlertTime = null
|
lastAlertTime = null
|
||||||
if (inAlert) return sendNoAlerts()
|
if (inAlert) return sendNoAlerts()
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('DEBUG42 ******************************')
|
|
||||||
|
|
||||||
var alertChanged = fingerprint === alertFingerprint &&
|
var alertChanged = fingerprint === alertFingerprint &&
|
||||||
lastAlertTime - Date.now() < ALERT_SEND_INTERVAL
|
lastAlertTime - Date.now() < ALERT_SEND_INTERVAL
|
||||||
if (alertChanged) return
|
if (alertChanged) return
|
||||||
|
|
||||||
console.log('DEBUG43 ******************************')
|
|
||||||
|
|
||||||
var subject = notifier.alertSubject(alertRec)
|
var subject = notifier.alertSubject(alertRec)
|
||||||
var rec = {
|
var rec = {
|
||||||
sms: {
|
sms: {
|
||||||
|
|
@ -702,6 +684,7 @@ function checkNotification () {
|
||||||
}
|
}
|
||||||
alertFingerprint = fingerprint
|
alertFingerprint = fingerprint
|
||||||
lastAlertTime = Date.now()
|
lastAlertTime = Date.now()
|
||||||
|
|
||||||
return sendMessage(rec)
|
return sendMessage(rec)
|
||||||
})
|
})
|
||||||
.then(function () {
|
.then(function () {
|
||||||
|
|
@ -727,7 +710,8 @@ function checkBalances () {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.startCheckingNotification = function startCheckingNotification () {
|
exports.startCheckingNotification = function startCheckingNotification () {
|
||||||
notifier.init(db, checkBalances)
|
var config = cachedConfig.exchanges.plugins.settings.notifier
|
||||||
|
notifier.init(db, checkBalances, config)
|
||||||
checkNotification()
|
checkNotification()
|
||||||
setInterval(checkNotification, CHECK_NOTIFICATION_INTERVAL)
|
setInterval(checkNotification, CHECK_NOTIFICATION_INTERVAL)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -167,7 +167,6 @@ function dispenseAck (req, res) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function deviceEvent (req, res) {
|
function deviceEvent (req, res) {
|
||||||
console.log('DEBUG5')
|
|
||||||
plugins.logEvent(session(req), req.body)
|
plugins.logEvent(session(req), req.body)
|
||||||
res.json({err: null})
|
res.json({err: null})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
7
todo.txt
7
todo.txt
|
|
@ -1,6 +1 @@
|
||||||
- configure email, sms or both for notification
|
- configure LOW_BALANCE_THRESHOLD, STALE_STATE, NETWORK_DOWN_TIME
|
||||||
- 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
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue