From 4e47a1ad3b188e82e2abae95aa6f2d6b26e002dd Mon Sep 17 00:00:00 2001 From: Johannes Zellner Date: Fri, 14 Oct 2022 11:15:27 +0200 Subject: [PATCH] Clean stats api response to have specific response types --- src/graphs.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/graphs.js b/src/graphs.js index c1ae6bbc2..4fb057e96 100644 --- a/src/graphs.js +++ b/src/graphs.js @@ -64,9 +64,10 @@ async function getContainerStats(name, fromMinutes, noNullPoints) { if (error) throw new BoxError(BoxError.NETWORK_ERROR, error.message); if (response.status !== 200) throw new BoxError(BoxError.EXTERNAL_ERROR, `Unknown error with ${target}: ${response.status} ${response.text}`); - results.push(response.body[0]); + results.push(response.body[0] && response.body[0].datapoints ? response.body[0].datapoints : []); } + // results are datapoints[[value, ts], [value, ts], ...]; return { cpu: results[0], memory: results[1], @@ -74,10 +75,10 @@ async function getContainerStats(name, fromMinutes, noNullPoints) { blockWrite: results[3], networkRead: results[4], networkWrite: results[5], - blockReadTotal: results[6].datapoints[0] ? results[6].datapoints[0][0] : 0, - blockWriteTotal: results[7].datapoints[0] ? results[6].datapoints[0][0] : 0, - networkReadTotal: results[8].datapoints[0] ? results[6].datapoints[0][0] : 0, - networkWriteTotal: results[9].datapoints[0] ? results[6].datapoints[0][0] : 0, + blockReadTotal: results[6][0] || 0, + blockWriteTotal: results[7][0] || 0, + networkReadTotal: results[8][0] || 0, + networkWriteTotal: results[9][0] || 0, cpuCount: os.cpus().length }; } @@ -114,8 +115,8 @@ async function getSystem(fromMinutes, noNullPoints) { } return { - cpu: memCpuResponse.body[0], - memory: memCpuResponse.body[1], + cpu: memCpuResponse.body[0] && memCpuResponse.body[0].datapoints ? memCpuResponse.body[0].datapoints : [], + memory: memCpuResponse.body[1] && memCpuResponse.body[1].datapoints ? memCpuResponse.body[1].datapoints : [], apps: appResponses, services: serviceResponses, cpuCount: os.cpus().length