From d794bca673dbcdc469ed4a53211cfce846347583 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Thu, 10 Feb 2022 16:05:30 +0000 Subject: [PATCH] fix: missing machine performance data on the dashboard machine overview page --- lib/machine-loader.js | 26 +++++++++++++++-- .../Machines/MachineComponents/Overview.js | 28 ++++++------------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/lib/machine-loader.js b/lib/machine-loader.js index ce62e05c..68748f5b 100644 --- a/lib/machine-loader.js +++ b/lib/machine-loader.js @@ -110,11 +110,17 @@ function getMachine (machineId, config) { else return toMachineObject(r) }) - return Promise.all([queryMachine, dbm.machineEvents(), config]) - .then(([machine, events, config]) => { + return Promise.all([queryMachine, dbm.machineEvents(), config, getNetworkHeartbeatByDevice(machineId), getNetworkPerformanceByDevice(machineId)]) + .then(([machine, events, config, heartbeat, performance]) => { const pings = checkPings([machine]) + const mergedMachine = { + ...machine, + responseTime: _.get('responseTime', heartbeat), + packetLoss: _.get('packetLoss', heartbeat), + downloadSpeed: _.get('downloadSpeed', performance), + } - return addName(pings, events, config)(machine) + return addName(pings, events, config)(mergedMachine) }) } @@ -234,6 +240,20 @@ function getNetworkHeartbeat () { .then(res => _.map(_.mapKeys(_.camelCase))(res)) } +function getNetworkPerformanceByDevice (deviceId) { + const sql = `SELECT device_id, download_speed FROM machine_network_performance WHERE device_id = $1` + return db.manyOrNone(sql, [deviceId]) + .then(res => _.mapKeys(_.camelCase, _.find(it => it.device_id === deviceId, res))) +} + +function getNetworkHeartbeatByDevice (deviceId) { + const sql = `SELECT AVG(average_response_time) AS response_time, AVG(average_packet_loss) AS packet_loss, device_id + FROM machine_network_heartbeat WHERE device_id = $1 + GROUP BY device_id` + return db.manyOrNone(sql, [deviceId]) + .then(res => _.mapKeys(_.camelCase, _.find(it => it.device_id === deviceId, res))) +} + module.exports = { getMachineName, getMachines, diff --git a/new-lamassu-admin/src/pages/Machines/MachineComponents/Overview.js b/new-lamassu-admin/src/pages/Machines/MachineComponents/Overview.js index 6c7c9f65..36b6b5df 100644 --- a/new-lamassu-admin/src/pages/Machines/MachineComponents/Overview.js +++ b/new-lamassu-admin/src/pages/Machines/MachineComponents/Overview.js @@ -1,6 +1,6 @@ import { makeStyles } from '@material-ui/core/styles' import BigNumber from 'bignumber.js' -import { differenceInSeconds } from 'date-fns/fp' +import { formatDistance } from 'date-fns' import React from 'react' import { Status } from 'src/components/Status' @@ -11,24 +11,6 @@ import CopyToClipboard from 'src/pages/Transactions/CopyToClipboard.js' import styles from '../Machines.styles' const useStyles = makeStyles(styles) -const makeLastPing = lastPing => { - if (!lastPing) return null - const secondsAgo = differenceInSeconds(lastPing, new Date()) - if (secondsAgo < 60) { - return `${secondsAgo} ${secondsAgo === 1 ? 'second' : 'seconds'} ago` - } - if (secondsAgo < 3600) { - const minutes = Math.round(secondsAgo / 60) - return `${minutes} ${minutes === 1 ? 'minute' : 'minutes'} ago` - } - if (secondsAgo < 3600 * 24) { - const hours = Math.round(secondsAgo / 3600) - return `${hours} ${hours === 1 ? 'hour' : 'hours'} ago` - } - const days = Math.round(secondsAgo / 3600 / 24) - return `${days} ${days === 1 ? 'day' : 'days'} ago` -} - const Overview = ({ data, onActionSuccess }) => { const classes = useStyles() @@ -48,7 +30,13 @@ const Overview = ({ data, onActionSuccess }) => {
Last ping -

{makeLastPing(data.lastPing)}

+

+ {data.lastPing + ? formatDistance(new Date(data.lastPing), new Date(), { + addSuffix: true + }) + : 'unknown'} +