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+ console . log ( 'props.service:' , props . service ) ;
14+
15+ // Declare a dockerStats obj to store extracted Docker stats.
16+ let dockerStats = { } ;
17+
18+ // Scan from the end of the data collection in heathData. (End = most recent)
19+ // Break the loop as soon as we find a data pt that matches current srvc (props.service).
20+ // We are only extracting this most recent data pt.
21+ for ( let i = length - 1 ; i >= 0 ; i -= 1 ) {
22+ // The difference btw MongoDB and pSQL is whether a stat title is camelCased (mongo = yes, psql = no).
23+ // If user-chosen DB is MongoDB:
24+ if ( healthData [ i ] . currentMicroservice === props . service ) {
25+ // Extract Docker-related data (MongoDB) and save to dockerStats obj.
26+ dockerStats = {
27+ 'Containerized service' : healthData . currentMicroservice ,
28+ 'Container ID' : healthData . containerId . slice ( 0 , 7 ) + '[...]' ,
29+ 'CPU usage %' : parseFloat ( healthData . containerCpuPercent ) . toFixed ( 2 ) + '%' ,
30+ 'Mem usage %' : parseFloat ( healthData . containerMemPercent ) . toFixed ( 2 ) + '%' ,
31+ 'Mem limit (Mb)' : parseFloat ( healthData . containerMemLimit / 1000000 ) . toFixed ( 2 ) ,
32+ 'Mem usage (Mb)' : parseFloat ( healthData . containerMemUsage / 1000000 ) . toFixed ( 2 ) ,
33+ 'Network I/O - Received (Kb)' : parseFloat ( healthData . networkReceived / 1000 ) . toFixed ( 2 ) ,
34+ 'Network I/O - Sent (Kb)' : parseFloat ( healthData . networkSent / 1000 ) . toFixed ( 2 ) ,
35+ 'Process count' : healthData . containerProcessCount ,
36+ 'Restart count' : healthData . containerRestartCount ,
37+ } ;
38+ }
39+
40+ // If postgreSQL:
41+ if ( healthData [ i ] . currentmicroservice === props . service ) {
42+ dockerStats = {
43+ 'Containerized service' : healthData . currentmicroservice ,
44+ 'Container ID' : healthData . containerid . slice ( 0 , 7 ) + '[...]' ,
45+ 'CPU usage %' : parseFloat ( healthData . containercpupercent ) . toFixed ( 2 ) + '%' ,
46+ 'Mem usage %' : parseFloat ( healthData . containermempercent ) . toFixed ( 2 ) + '%' ,
47+ 'Mem limit (Mb)' : parseFloat ( healthData . containermemlimit / 1000000 ) . toFixed ( 2 ) ,
48+ 'Mem usage (Mb)' : parseFloat ( healthData . containermemusage / 1000000 ) . toFixed ( 2 ) ,
49+ 'Network I/O - Received (Kb)' : parseFloat ( healthData . networkreceived / 1000 ) . toFixed ( 2 ) ,
50+ 'Network I/O - Sent (Kb)' : parseFloat ( healthData . networksent / 1000 ) . toFixed ( 2 ) ,
51+ 'Process count' : healthData . containerprocesscount ,
52+ 'Restart count' : healthData . containerrestartcount ,
53+ } ;
54+ }
55+ }
56+
57+
58+ // Build an array of <span>s that'll be rendered. Each <span> contains one stat.
59+ const dockerStatsArr = [ ] ;
60+
61+ for ( let stat in dockerStats ) {
62+ dockerStatsArr . push (
63+ < span key = { stat } >
64+ { stat } : { dockerStats [ stat ] }
65+ </ span >
66+ )
67+ }
68+
69+ return (
70+ < div id = "docker-stats-chart" >
71+ < header id = "docker-stats-chart-header" > Docker Container Stats</ header >
72+ { dockerStatsArr }
73+ </ div >
74+ )
75+ }
76+
77+ export default DockerStatsChart ;
0 commit comments