add pings table

This commit is contained in:
Josh Harvey 2017-05-15 02:02:43 +03:00
parent 31f7795fd7
commit a123170622
4 changed files with 43 additions and 13 deletions

View file

@ -165,14 +165,14 @@ function plugins (settings, deviceId) {
.then(row => row.id)
}
function pollQueries (deviceTime, deviceRec) {
function pollQueries (serialNumber, deviceTime, deviceRec) {
const config = configManager.machineScoped(deviceId, settings.config)
const fiatCode = config.fiatCurrency
const cryptoCodes = config.cryptoCurrencies
const tickerPromises = cryptoCodes.map(c => ticker.getRates(settings, fiatCode, c))
const balancePromises = cryptoCodes.map(c => fiatBalance(fiatCode, c))
const pingPromise = recordPing(deviceTime, deviceRec)
const pingPromise = recordPing(serialNumber, deviceTime, deviceRec)
const currentConfigVersionPromise = fetchCurrentConfigVersion()
const promises = [
@ -201,15 +201,15 @@ function plugins (settings, deviceId) {
return wallet.sendCoins(settings, tx.toAddress, tx.cryptoAtoms, tx.cryptoCode)
}
function recordPing (deviceTime, rec) {
const event = {
function recordPing (serialNumber, deviceTime, rec) {
const r = {
id: uuid.v4(),
deviceId,
eventType: 'ping',
note: JSON.stringify({state: rec.state, isIdle: rec.idle === 'true', txId: rec.txId}),
deviceTime
device_id: deviceId,
serial_number: serialNumber,
device_time: deviceTime
}
return dbm.machineEvent(event)
return db.none(pgp.helpers.insert(r, null, 'machine_pings'))
}
function isHd (tx) {

View file

@ -44,7 +44,7 @@ exports.machineEvent = function machineEvent (rec) {
const deleteSql = `delete from machine_events
where device_id=$1
and event_type=$2
and created < now() - interval '2 hours'`
and created < now() - interval '2 days'`
return db.none(sql, values)
.then(() => db.none(deleteSql, [rec.deviceId, rec.eventType]))

View file

@ -29,6 +29,7 @@ const devMode = argv.dev || options.http
function poll (req, res, next) {
const deviceId = req.deviceId
const deviceTime = req.deviceTime
const serialNumber = req.query.sn
const pid = req.query.pid
const settings = req.settings
const config = configManager.machineScoped(deviceId, settings.config)
@ -36,7 +37,7 @@ function poll (req, res, next) {
pids[deviceId] = {pid, ts: Date.now()}
return pi.pollQueries(deviceTime, req.query)
return pi.pollQueries(serialNumber, deviceTime, req.query)
.then(results => {
const cassettes = results.cassettes
@ -203,7 +204,7 @@ function httpError (msg, code) {
function filterOldRequests (req, res, next) {
const deviceTime = req.deviceTime
const delta = Date.now() - deviceTime
const delta = Date.now() - Date.parse(deviceTime)
if (delta > CLOCK_SKEW) {
logger.error('Clock skew with lamassu-machine too high [%ss], adjust lamassu-machine clock', (delta / 1000).toFixed(2))
@ -312,7 +313,7 @@ function populateDeviceId (req, res, next) {
: null
req.deviceId = deviceId
req.deviceTime = Date.parse(req.get('date'))
req.deviceTime = req.get('date')
next()
}

View file

@ -0,0 +1,29 @@
var db = require('./db')
exports.up = function (next) {
var sql = [
`create table machine_pings (
id uuid PRIMARY KEY,
device_id text not null,
serial_number integer not null,
device_time timestamptz not null,
created timestamptz not null default now())`,
`create table aggregated_machine_pings (
id uuid PRIMARY KEY,
device_id text not null,
dropped_pings integer not null,
total_pings integer not null,
lag_sd_ms integer not null,
lag_min_ms integer not null,
lag_max_ms integer not null,
lag_median_ms integer not null,
day date not null)`,
'alter table machine_events drop column device_time',
'alter table machine_events add column device_time timestamptz'
]
db.multi(sql, next)
}
exports.down = function (next) {
next()
}