1+ import React , { useContext } from 'react' ;
2+ import HealthContext from '../context/DetailsContext' ;
3+ // import Plot from 'react-plotly.js';
4+
5+ // A table that displays real-time Docker container stats.
6+ // Just need to pull data from most recent (last) obj in healthData.
7+ 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.
10+ const healthData = useContext ( HealthContext ) . detailData ;
11+
12+ // console.log('healthData (in docker-stats-chart):', healthData);
13+
14+ // Declare a dockerStats obj to store extracted Docker stats.
15+ let dockerStats = { } ;
16+
17+ // 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).
19+ // 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:
23+ 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+ } ;
37+ break ;
38+ }
39+
40+ // If postgreSQL:
41+ 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+ } ;
54+ break ;
55+ }
56+ }
57+
58+
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+ )
68+ }
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+ )
76+ }
77+
78+ export default DockerStatsChart ;
0 commit comments