add pings table
This commit is contained in:
parent
31f7795fd7
commit
a123170622
4 changed files with 43 additions and 13 deletions
|
|
@ -165,14 +165,14 @@ function plugins (settings, deviceId) {
|
||||||
.then(row => row.id)
|
.then(row => row.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
function pollQueries (deviceTime, deviceRec) {
|
function pollQueries (serialNumber, deviceTime, deviceRec) {
|
||||||
const config = configManager.machineScoped(deviceId, settings.config)
|
const config = configManager.machineScoped(deviceId, settings.config)
|
||||||
const fiatCode = config.fiatCurrency
|
const fiatCode = config.fiatCurrency
|
||||||
const cryptoCodes = config.cryptoCurrencies
|
const cryptoCodes = config.cryptoCurrencies
|
||||||
|
|
||||||
const tickerPromises = cryptoCodes.map(c => ticker.getRates(settings, fiatCode, c))
|
const tickerPromises = cryptoCodes.map(c => ticker.getRates(settings, fiatCode, c))
|
||||||
const balancePromises = cryptoCodes.map(c => fiatBalance(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 currentConfigVersionPromise = fetchCurrentConfigVersion()
|
||||||
|
|
||||||
const promises = [
|
const promises = [
|
||||||
|
|
@ -201,15 +201,15 @@ function plugins (settings, deviceId) {
|
||||||
return wallet.sendCoins(settings, tx.toAddress, tx.cryptoAtoms, tx.cryptoCode)
|
return wallet.sendCoins(settings, tx.toAddress, tx.cryptoAtoms, tx.cryptoCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
function recordPing (deviceTime, rec) {
|
function recordPing (serialNumber, deviceTime, rec) {
|
||||||
const event = {
|
const r = {
|
||||||
id: uuid.v4(),
|
id: uuid.v4(),
|
||||||
deviceId,
|
device_id: deviceId,
|
||||||
eventType: 'ping',
|
serial_number: serialNumber,
|
||||||
note: JSON.stringify({state: rec.state, isIdle: rec.idle === 'true', txId: rec.txId}),
|
device_time: deviceTime
|
||||||
deviceTime
|
|
||||||
}
|
}
|
||||||
return dbm.machineEvent(event)
|
|
||||||
|
return db.none(pgp.helpers.insert(r, null, 'machine_pings'))
|
||||||
}
|
}
|
||||||
|
|
||||||
function isHd (tx) {
|
function isHd (tx) {
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ exports.machineEvent = function machineEvent (rec) {
|
||||||
const deleteSql = `delete from machine_events
|
const deleteSql = `delete from machine_events
|
||||||
where device_id=$1
|
where device_id=$1
|
||||||
and event_type=$2
|
and event_type=$2
|
||||||
and created < now() - interval '2 hours'`
|
and created < now() - interval '2 days'`
|
||||||
|
|
||||||
return db.none(sql, values)
|
return db.none(sql, values)
|
||||||
.then(() => db.none(deleteSql, [rec.deviceId, rec.eventType]))
|
.then(() => db.none(deleteSql, [rec.deviceId, rec.eventType]))
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ const devMode = argv.dev || options.http
|
||||||
function poll (req, res, next) {
|
function poll (req, res, next) {
|
||||||
const deviceId = req.deviceId
|
const deviceId = req.deviceId
|
||||||
const deviceTime = req.deviceTime
|
const deviceTime = req.deviceTime
|
||||||
|
const serialNumber = req.query.sn
|
||||||
const pid = req.query.pid
|
const pid = req.query.pid
|
||||||
const settings = req.settings
|
const settings = req.settings
|
||||||
const config = configManager.machineScoped(deviceId, settings.config)
|
const config = configManager.machineScoped(deviceId, settings.config)
|
||||||
|
|
@ -36,7 +37,7 @@ function poll (req, res, next) {
|
||||||
|
|
||||||
pids[deviceId] = {pid, ts: Date.now()}
|
pids[deviceId] = {pid, ts: Date.now()}
|
||||||
|
|
||||||
return pi.pollQueries(deviceTime, req.query)
|
return pi.pollQueries(serialNumber, deviceTime, req.query)
|
||||||
.then(results => {
|
.then(results => {
|
||||||
const cassettes = results.cassettes
|
const cassettes = results.cassettes
|
||||||
|
|
||||||
|
|
@ -203,7 +204,7 @@ function httpError (msg, code) {
|
||||||
|
|
||||||
function filterOldRequests (req, res, next) {
|
function filterOldRequests (req, res, next) {
|
||||||
const deviceTime = req.deviceTime
|
const deviceTime = req.deviceTime
|
||||||
const delta = Date.now() - deviceTime
|
const delta = Date.now() - Date.parse(deviceTime)
|
||||||
|
|
||||||
if (delta > CLOCK_SKEW) {
|
if (delta > CLOCK_SKEW) {
|
||||||
logger.error('Clock skew with lamassu-machine too high [%ss], adjust lamassu-machine clock', (delta / 1000).toFixed(2))
|
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
|
: null
|
||||||
|
|
||||||
req.deviceId = deviceId
|
req.deviceId = deviceId
|
||||||
req.deviceTime = Date.parse(req.get('date'))
|
req.deviceTime = req.get('date')
|
||||||
|
|
||||||
next()
|
next()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
29
migrations/032-create_machine_pings_table.js
Normal file
29
migrations/032-create_machine_pings_table.js
Normal 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()
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue