Skip to content

Commit

Permalink
fix: prevent NaN/Infinity values
Browse files Browse the repository at this point in the history
  • Loading branch information
patrykcieszkowski committed Jun 15, 2022
1 parent 70d2285 commit 515dd1a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
30 changes: 19 additions & 11 deletions src/command/handlers/mtr/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,30 @@ const withSpacing = (string_: string | number, dSpacing: number, left = false):
return `${string_}${sSpacing}`;
};

const roundNumber = (value: number): number => {
if (!Number.isFinite(value)) {
return 0;
}

return Number.parseFloat(value.toFixed(1));
};

// eslint-disable-next-line @typescript-eslint/naming-convention
export const MtrParser = {
outputBuilder(hops: HopType[]): string {
const rawOutput = [];

const spacings = {
index: String(hops.length).length,
asn: 2 + Math.max(...hops.map(h => String(h?.asn ?? '').length)),
asn: 2 + Math.max(...hops.map(h => String(h?.asn ?? 0).length)),
hostname: (3
+ Math.max(...hops.map(h => String(h?.host ?? '').length))
+ Math.max(...hops.map(h => String(h?.resolvedHost ?? '').length))
+ Math.max(...hops.map(h => String(h?.host ?? 0).length))
+ Math.max(...hops.map(h => String(h?.resolvedHost ?? 0).length))
),
loss: 6,
drop: Math.max(4, ...hops.map(h => String(h?.stats?.drop ?? '').length)),
avg: Math.max(...hops.map(h => String(h?.stats?.avg ?? '').length)),
rcv: 2 + Math.max(...hops.map(h => String(h?.stats?.drop ?? '').length)),
drop: Math.max(4, ...hops.map(h => String(h?.stats?.drop ?? 0).length)),
avg: Math.max(...hops.map(h => String(h?.stats?.avg ?? 0).length)),
rcv: 2 + Math.max(...hops.map(h => String(h?.stats?.drop ?? 0).length)),
stDev: 6,
jAvg: 5,
};
Expand Down Expand Up @@ -192,9 +200,9 @@ export const MtrParser = {
if (timesArray.length > 0) {
stats.min = Math.min(...timesArray);
stats.max = Math.max(...timesArray);
stats.avg = Number.parseFloat((timesArray.reduce((a, b) => a + b, 0) / timesArray.length).toFixed(1));
stats.avg = roundNumber(timesArray.reduce((a, b) => a + b, 0) / timesArray.length);
stats.total = hop.times.length;
stats.stDev = Number.parseFloat((Math.sqrt(timesArray.map(x => (x - stats.avg) ** 2).reduce((a, b) => a + b, 0) / timesArray.length)).toFixed(1));
stats.stDev = roundNumber(Math.sqrt(timesArray.map(x => (x - stats.avg) ** 2).reduce((a, b) => a + b, 0) / timesArray.length));
}

stats.rcv = 0;
Expand Down Expand Up @@ -226,9 +234,9 @@ export const MtrParser = {
}

if (jitterArray.length > 0) {
stats.jMin = Number.parseFloat(Math.min(...jitterArray).toFixed(1));
stats.jMax = Number.parseFloat(Math.max(...jitterArray).toFixed(1));
stats.jAvg = Number.parseFloat((jitterArray.reduce((a, b) => a + b, 0) / jitterArray.length).toFixed(1));
stats.jMin = roundNumber(Math.min(...jitterArray));
stats.jMax = roundNumber(Math.max(...jitterArray));
stats.jAvg = roundNumber(jitterArray.reduce((a, b) => a + b, 0) / jitterArray.length);
}

return stats;
Expand Down
2 changes: 1 addition & 1 deletion test/mocks/mtr-success-raw-helper-final.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@
"resolvedHost": "lhr25s31-in-f14.1e100.net"
}
],
"rawOutput": "Host Loss% Drop Rcv Avg StDev Javg \n 1. AS??? _gateway (192.168.0.1) 66.7% 2 1 2.3 0.0 2.3\n 2. AS??? (waiting for reply) \n 3. AS??? 62.252.67.181 (62.252.67.181) 0.0% 0 3 10.3 0.8 6.2\n 4. AS??? (waiting for reply) \n 5. AS??? 62.254.59.130 (62.254.59.130) 0.0% 0 3 11.5 0.5 6.5\n 6. AS??? 142.250.160.116 (142.250.160.116) 0.0% 0 3 11.3 0.3 6.0\n 7. AS??? 216.239.41.193 (216.239.41.193) 0.0% 0 3 15.9 0.6 8.0\n 8. AS??? 142.251.54.27 (142.251.54.27) 0.0% 0 3 13.6 1.5 8.2\n 9. AS??? 142.250.179.238 (142.250.179.238) 0.0% 0 3 11.0 0.7 6.4\n10. AS??? lhr25s31-in-f14.1e100.net (142.250.179.238) 0.0% 0 1 11.4 0.0 11.4\n",
"rawOutput": "Host Loss% Drop Rcv Avg StDev Javg \n 1. AS??? _gateway (192.168.0.1) 66.7% 2 1 2.3 0.0 2.3\n 2. AS??? (waiting for reply) \n 3. AS??? 62.252.67.181 (62.252.67.181) 0.0% 0 3 10.3 0.8 6.2\n 4. AS??? (waiting for reply) \n 5. AS??? 62.254.59.130 (62.254.59.130) 0.0% 0 3 11.5 0.5 6.5\n 6. AS??? 142.250.160.116 (142.250.160.116) 0.0% 0 3 11.3 0.3 6.0\n 7. AS??? 216.239.41.193 (216.239.41.193) 0.0% 0 3 15.9 0.6 8.0\n 8. AS??? 142.251.54.27 (142.251.54.27) 0.0% 0 3 13.6 1.5 8.2\n 9. AS??? 142.250.179.238 (142.250.179.238) 0.0% 0 3 11.0 0.7 6.4\n10. AS??? lhr25s31-in-f14.1e100.net (142.250.179.238) 0.0% 0 1 11.4 0.0 11.4\n",
"data":
[
"x 0 33000",
Expand Down

0 comments on commit 515dd1a

Please sign in to comment.