feat: ping time internet quality measurement

This commit is contained in:
José Oliveira 2021-08-30 18:40:12 +01:00 committed by Josh Harvey
parent f2f146265e
commit 515d02dd21
7 changed files with 162 additions and 17 deletions

View file

@ -1,5 +1,7 @@
const _ = require('lodash/fp')
const pgp = require('pg-promise')()
const axios = require('axios')
const uuid = require('uuid')
const db = require('./db')
const pairing = require('./pairing')
@ -49,26 +51,28 @@ const getStatus = (ping, stuck) => {
function addName (pings, events, config) {
return machine => {
const cashOutConfig = configManager.getCashOut(machine.deviceId, config)
const cashOut = !!cashOutConfig.active
const statuses = [
getStatus(
_.first(pings[machine.deviceId]),
_.first(checkStuckScreen(events, machine.name))
)
]
)
]
return _.assign(machine, { cashOut, statuses })
}
}
function getMachineNames (config) {
return Promise.all([getMachines(), getConfig(config)])
.then(([machines, config]) => Promise.all(
[machines, checkPings(machines), dbm.machineEvents(), config]
return Promise.all([getMachines(), getConfig(config), getNetworkHeartbeat(), getNetworkPerformance()])
.then(([rawMachines, config, heartbeat, performance]) => Promise.all(
[rawMachines, checkPings(rawMachines), dbm.machineEvents(), config, heartbeat, performance]
))
.then(([machines, pings, events, config]) => {
.then(([rawMachines, pings, events, config, heartbeat, performance]) => {
const mergeByDeviceId = (x, y) => _.values(_.merge(_.keyBy('deviceId', x), _.keyBy('deviceId', y)))
const machines = mergeByDeviceId(mergeByDeviceId(rawMachines, heartbeat), performance)
return machines.map(addName(pings, events, config))
})
}
@ -105,9 +109,9 @@ function getMachine (machineId, config) {
}))
return Promise.all([queryMachine, dbm.machineEvents(), config])
.then(([machine, events, config]) => {
const pings = checkPings([machine])
.then(([machine, events, config]) => {
const pings = checkPings([machine])
return [machine].map(addName(pings, events, config))[0]
})
}
@ -163,4 +167,56 @@ function setMachine (rec) {
}
}
module.exports = { getMachineName, getMachines, getMachine, getMachineNames, setMachine }
function updateNetworkPerformance (deviceId, data) {
const downloadSpeed = _.head(data)
const dbData = {
device_id: deviceId,
download_speed: downloadSpeed.speed,
created: new Date()
}
const cs = new pgp.helpers.ColumnSet(['device_id', 'download_speed', 'created'],
{ table: 'machine_network_performance' })
const onConflict = ' ON CONFLICT (device_id) DO UPDATE SET ' +
cs.assignColumns({ from: 'EXCLUDED', skip: ['device_id'] })
const upsert = pgp.helpers.insert(dbData, cs) + onConflict
return db.none(upsert)
}
function updateNetworkHeartbeat (deviceId, data) {
const avgResponseTime = _.meanBy(e => _.toNumber(e.averageResponseTime), data)
const avgPacketLoss = _.meanBy(e => _.toNumber(e.packetLoss), data)
const dbData = {
id: uuid.v4(),
device_id: deviceId,
average_response_time: avgResponseTime,
average_packet_loss: avgPacketLoss
}
const sql = pgp.helpers.insert(dbData, null, 'machine_network_heartbeat')
return db.none(sql)
}
function getNetworkPerformance () {
const sql = `SELECT device_id, download_speed FROM machine_network_performance`
return db.manyOrNone(sql)
.then(res => _.map(_.mapKeys(_.camelCase))(res))
}
function getNetworkHeartbeat () {
const sql = `SELECT AVG(average_response_time) AS response_time, AVG(average_packet_loss) AS packet_loss, device_id
FROM machine_network_heartbeat
GROUP BY device_id`
return db.manyOrNone(sql)
.then(res => _.map(_.mapKeys(_.camelCase))(res))
}
module.exports = {
getMachineName,
getMachines,
getMachine,
getMachineNames,
setMachine,
updateNetworkPerformance,
updateNetworkHeartbeat,
getNetworkPerformance,
getNetworkHeartbeat
}