Skip to content

Commit

Permalink
fix(api): calculate network speed in relation to time diff
Browse files Browse the repository at this point in the history
  • Loading branch information
MauriceNino committed Jun 11, 2022
1 parent 810e5a6 commit 55ec6b2
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions apps/api/src/dynamic-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export const getDynamicServerInfo = () => {
}
);

let [lastRx, lastTx] = [0, 0];
let [lastRx, lastTx, lastTs] = [0, 0, 0];

const networkObs = createBufferedInterval(
'Network',
Expand All @@ -102,14 +102,23 @@ export const getDynamicServerInfo = () => {
`cat /internal_mnt/host_sys/class/net/${NET_INTERFACE}/statistics/tx_bytes;`
);
const [rx, tx] = stdout.split('\n').map(Number);

const result = {
up: tx - lastTx,
down: rx - lastRx,
};
const thisTs = performance.now();
const dividend = (thisTs - lastTs) / 1000;

const result =
lastTs === 0
? {
up: 0,
down: 0,
}
: {
up: (tx - lastTx) / dividend,
down: (rx - lastRx) / dividend,
};

lastRx = rx;
lastTx = tx;
lastTs = thisTs;

return result;
} else {
Expand Down

0 comments on commit 55ec6b2

Please sign in to comment.