Skip to content

Commit 02c6d35

Browse files
authored
Merge pull request #23 from ajlee12/master
Fixed bug where app crashes when selected svc has no docker data. If …
2 parents 7d2de3a + 1dcd580 commit 02c6d35

File tree

1 file changed

+70
-50
lines changed

1 file changed

+70
-50
lines changed

app/charts/docker-stats-chart.jsx

Lines changed: 70 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,98 @@
1-
import React, { useContext } from 'react';
1+
import React, { useContext, useState, useEffect } from 'react';
22
import HealthContext from '../context/DetailsContext';
33
// import Plot from 'react-plotly.js';
44

55
// A table that displays real-time Docker container stats.
66
// Just need to pull data from most recent (last) obj in healthData.
77
const DockerStatsChart = (props) => {
8-
let length = useContext(HealthContext).detailData.length;
9-
// const healthData = useContext(HealthContext).detailData[length - 1]; // <== only used if only getting the last data pt.
108
const healthData = useContext(HealthContext).detailData;
119

1210
// console.log('healthData (in docker-stats-chart):', healthData);
1311

1412
// Declare a dockerStats obj to store extracted Docker stats.
1513
let dockerStats = {};
16-
14+
// This var will store the ind of the most recent data pt in healthData that matches currently selected microsvc.
15+
let index;
16+
1717
// Scan from the end of the data collection in heathData. (End = most recent)
18-
// Break the loop as soon as we find a data pt that matches current srvc (props.service).
18+
// Use the loop to find the index of the data pt that matches current microsrvc (props.service).
1919
// We are only extracting this most recent data pt.
20-
for (let i = length - 1; i >= 0; i -= 1) {
21-
// The difference btw MongoDB and pSQL is whether a stat title is camelCased (mongo = yes, psql = no).
22-
// If user-chosen DB is MongoDB:
20+
for (let i = healthData.length - 1; i >= 0; i -= 1) {
21+
// If user-chosen DB is NoSQL (Mongo):
2322
if (healthData[i].currentMicroservice === props.service) {
24-
// Extract Docker-related data (MongoDB) and save to dockerStats obj.
25-
dockerStats = {
26-
'Containerized service': healthData[i].currentMicroservice,
27-
'Container ID': healthData[i].containerId.slice(0, 7) + '[...]',
28-
'CPU usage %': parseFloat(healthData[i].containerCpuPercent).toFixed(2) + '%',
29-
'Mem usage %': parseFloat(healthData[i].containerMemPercent).toFixed(2) + '%',
30-
'Mem limit (Mb)': parseFloat(healthData[i].containerMemLimit / 1000000).toFixed(2),
31-
'Mem usage (Mb)': parseFloat(healthData[i].containerMemUsage / 1000000).toFixed(2),
32-
'Network I/O - Received (Kb)': parseFloat(healthData[i].networkReceived / 1000).toFixed(2),
33-
'Network I/O - Sent (Kb)': parseFloat(healthData[i].networkSent / 1000).toFixed(2),
34-
'Process count': healthData[i].containerProcessCount,
35-
'Restart count': healthData[i].containerRestartCount,
36-
};
23+
index = i;
24+
// db = 'mongo';
3725
break;
3826
}
39-
40-
// If postgreSQL:
27+
// If SQL:
4128
if (healthData[i].currentmicroservice === props.service) {
42-
dockerStats = {
43-
'Containerized service': healthData[i].currentmicroservice,
44-
'Container ID': healthData[i].containerid.slice(0, 7) + '[...]',
45-
'CPU usage %': parseFloat(healthData[i].containercpupercent).toFixed(2) + '%',
46-
'Mem usage %': parseFloat(healthData[i].containermempercent).toFixed(2) + '%',
47-
'Mem limit (Mb)': parseFloat(healthData[i].containermemlimit / 1000000).toFixed(2),
48-
'Mem usage (Mb)': parseFloat(healthData[i].containermemusage / 1000000).toFixed(2),
49-
'Network I/O - Received (Kb)': parseFloat(healthData[i].networkreceived / 1000).toFixed(2),
50-
'Network I/O - Sent (Kb)': parseFloat(healthData[i].networksent / 1000).toFixed(2),
51-
'Process count': healthData[i].containerprocesscount,
52-
'Restart count': healthData[i].containerrestartcount,
53-
};
29+
index = i;
30+
// db = 'sql';
5431
break;
5532
}
5633
}
5734

35+
// Note: The difference btw MongoDB and pSQL is whether a stat title is camelCased (mongo = yes, psql = no).
36+
// Extract Docker-related MongoDB data (if exists) and save to dockerStats obj.
37+
if (healthData[index].containerId) {
38+
dockerStats = {
39+
'Containerized service': healthData[index].currentMicroservice,
40+
'Container ID': healthData[index].containerId.slice(0, 7) + '[...]',
41+
'CPU usage %': parseFloat(healthData[index].containerCpuPercent).toFixed(2) + '%',
42+
'Mem usage %': parseFloat(healthData[index].containerMemPercent).toFixed(2) + '%',
43+
'Mem limit (Mb)': parseFloat(healthData[index].containerMemLimit / 1000000).toFixed(2),
44+
'Mem usage (Mb)': parseFloat(healthData[index].containerMemUsage / 1000000).toFixed(2),
45+
'Network I/O - Received (Kb)': parseFloat(healthData[index].networkReceived / 1000).toFixed(2),
46+
'Network I/O - Sent (Kb)': parseFloat(healthData[index].networkSent / 1000).toFixed(2),
47+
'Process count': healthData[index].containerProcessCount,
48+
'Restart count': healthData[index].containerRestartCount,
49+
};
50+
}
51+
// Extract Docker-related SQL data (if exists) and save to dockerStats obj.
52+
if (healthData[index].containerid) {
53+
dockerStats = {
54+
'Containerized service': healthData[index].currentmicroservice,
55+
'Container ID': healthData[index].containerid.slice(0, 7) + '[...]',
56+
'CPU usage %': parseFloat(healthData[index].containercpupercent).toFixed(2) + '%',
57+
'Mem usage %': parseFloat(healthData[index].containermempercent).toFixed(2) + '%',
58+
'Mem limit (Mb)': parseFloat(healthData[index].containermemlimit / 1000000).toFixed(2),
59+
'Mem usage (Mb)': parseFloat(healthData[index].containermemusage / 1000000).toFixed(2),
60+
'Network I/O - Received (Kb)': parseFloat(healthData[index].networkreceived / 1000).toFixed(2),
61+
'Network I/O - Sent (Kb)': parseFloat(healthData[index].networksent / 1000).toFixed(2),
62+
'Process count': healthData[index].containerprocesscount,
63+
'Restart count': healthData[index].containerrestartcount,
64+
};
65+
}
5866

59-
// Build an array of <span>s that'll be rendered. Each <span> contains one stat.
60-
const dockerStatsArr = [];
61-
62-
for (let stat in dockerStats) {
63-
dockerStatsArr.push(
64-
<span key={stat}>
65-
{stat}: {dockerStats[stat]}
66-
</span>
67+
// Conditional rendering, depending on if dockerStats obj contains anything.
68+
if (Object.keys(dockerStats).length > 0) {
69+
// Build an array of <span>s that'll be rendered. Each <span> contains one stat.
70+
const dockerStatsArr = [];
71+
72+
for (let stat in dockerStats) {
73+
dockerStatsArr.push(
74+
<span key={stat}>
75+
{stat}: {dockerStats[stat]}
76+
</span>
77+
)
78+
}
79+
console.log('dockerStatsArr (docker stats chart):', dockerStatsArr);
80+
return (
81+
<div id="docker-stats-chart">
82+
<header id="docker-stats-chart-header">Docker Container Stats</header>
83+
{dockerStatsArr}
84+
</div>
85+
)
86+
} else { // If no Docker data:
87+
return (
88+
<div id="docker-stats-chart">
89+
<header id="docker-stats-chart-header">
90+
No valid container ID for current microservice.
91+
Please ensure the microservice is running in a Docker container.
92+
</header>
93+
</div>
6794
)
6895
}
69-
70-
return (
71-
<div id="docker-stats-chart">
72-
<header id="docker-stats-chart-header">Docker Container Stats</header>
73-
{dockerStatsArr}
74-
</div>
75-
)
7696
}
7797

7898
export default DockerStatsChart;

0 commit comments

Comments
 (0)