Optimize machine pings query (#222)
* refactor recordPing method * optimize queries * migrate machine_pings remove id, serial_number add primary key on device_id add unique constraint on device_id * remove filelds from query, rename migration, delete duplicate migration * truncate machine_pings in migration
This commit is contained in:
parent
e7bb29341d
commit
13040f41a1
3 changed files with 22 additions and 12 deletions
|
|
@ -130,9 +130,7 @@ function checkStuckScreen (deviceEvents) {
|
||||||
|
|
||||||
function checkPing (deviceId) {
|
function checkPing (deviceId) {
|
||||||
const sql = `select (EXTRACT(EPOCH FROM (now() - created))) * 1000 AS age from machine_pings
|
const sql = `select (EXTRACT(EPOCH FROM (now() - created))) * 1000 AS age from machine_pings
|
||||||
where device_id=$1
|
where device_id=$1`
|
||||||
order by created desc
|
|
||||||
limit 1`
|
|
||||||
|
|
||||||
return db.oneOrNone(sql, [deviceId])
|
return db.oneOrNone(sql, [deviceId])
|
||||||
.then(row => {
|
.then(row => {
|
||||||
|
|
|
||||||
|
|
@ -203,7 +203,7 @@ function plugins (settings, deviceId) {
|
||||||
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 testnetPromises = cryptoCodes.map(c => wallet.cryptoNetwork(settings, c))
|
const testnetPromises = cryptoCodes.map(c => wallet.cryptoNetwork(settings, c))
|
||||||
const pingPromise = recordPing(serialNumber, deviceTime, deviceRec)
|
const pingPromise = recordPing(deviceTime)
|
||||||
const currentConfigVersionPromise = fetchCurrentConfigVersion()
|
const currentConfigVersionPromise = fetchCurrentConfigVersion()
|
||||||
|
|
||||||
const promises = [
|
const promises = [
|
||||||
|
|
@ -236,19 +236,14 @@ 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 (serialNumber, deviceTime, rec) {
|
function recordPing (deviceTime) {
|
||||||
const machinePings = {
|
|
||||||
id: uuid.v4(),
|
|
||||||
device_id: deviceId,
|
|
||||||
serial_number: serialNumber,
|
|
||||||
device_time: deviceTime
|
|
||||||
}
|
|
||||||
const devices = {
|
const devices = {
|
||||||
last_online: deviceTime
|
last_online: deviceTime
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.all([
|
return Promise.all([
|
||||||
db.none(pgp.helpers.insert(machinePings, null, 'machine_pings')),
|
db.none(`insert into machine_pings(device_id, device_time) values($1, $2)
|
||||||
|
ON CONFLICT (device_id) DO UPDATE SET device_time = $2`, [deviceId, deviceTime]),
|
||||||
db.none(pgp.helpers.update(devices, null, 'devices') + 'WHERE device_id = ${deviceId}', { deviceId })
|
db.none(pgp.helpers.update(devices, null, 'devices') + 'WHERE device_id = ${deviceId}', { deviceId })
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
|
||||||
17
migrations/1542638179228-alter-machine-pings.js
Normal file
17
migrations/1542638179228-alter-machine-pings.js
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
const db = require('./db')
|
||||||
|
|
||||||
|
exports.up = function(next) {
|
||||||
|
var sql = [
|
||||||
|
'TRUNCATE TABLE machine_pings',
|
||||||
|
'ALTER TABLE machine_pings DROP id',
|
||||||
|
'ALTER TABLE machine_pings DROP serial_number',
|
||||||
|
'ALTER TABLE machine_pings ADD CONSTRAINT PK_device_id PRIMARY KEY (device_id)',
|
||||||
|
'ALTER TABLE machine_pings ADD CONSTRAINT U_device_id UNIQUE(device_id)'
|
||||||
|
]
|
||||||
|
|
||||||
|
db.multi(sql, next)
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.down = function(next) {
|
||||||
|
next();
|
||||||
|
};
|
||||||
Loading…
Add table
Add a link
Reference in a new issue