@@ -2,26 +2,20 @@ import moment from 'moment';
22import React , { useState } from 'react' ;
33import Plot from 'react-plotly.js' ;
44import { all , solo as soloStyle } from './sizeSwitch' ;
5- import { getTime } from '../context/helpers' ;
65
76interface HealthChartProps {
87 key : string ;
9- serviceName : string ;
10- // metric: string ;
8+ dataType : string ;
9+ chartData : object ;
1110 categoryName : string ;
12- metrics : any [ ] ;
13- timeList : any [ ] ;
14- // valueList: any;
1511 sizing : string ;
1612 colourGenerator : Function ;
1713}
18-
1914interface SoloStyles {
2015 height : number ;
2116 width : number ;
2217}
23-
24- type plotlyData = {
18+ type PlotlyData = {
2519 name : string ;
2620 x : string [ ] ;
2721 y : string [ ] ;
@@ -31,40 +25,61 @@ type plotlyData = {
3125} ;
3226
3327const HealthChart : React . FC < HealthChartProps > = React . memo ( props => {
34- // 'metrics' is an array of the user-specified metrics as objects
35- const { serviceName, categoryName, metrics, timeList, sizing, colourGenerator } = props ;
28+ const { dataType, chartData, categoryName, sizing, colourGenerator } = props ;
3629 const [ solo , setSolo ] = useState < SoloStyles | null > ( null ) ;
37- const timeArr = timeList . map ( ( el : any ) => moment ( el ) . format ( 'kk:mm:ss' ) ) ;
38- const reverseTimeArr = timeArr . reverse ( ) ;
39- const re = / _ / g;
40- const plotlyDataObjectArray : plotlyData [ ] = [ ] ;
4130
42- // generates an array plotly data objects to add to be passed into our plotly chart's data prop
43- const generatePlotlyDataObjects = ( metricsArray , timeArray ) => {
44- console . log ( 'metricsArray: ' , metricsArray ) ;
45- console . log ( 'timeArray: ' , timeArray ) ;
46- // initalize an array of objects for output
47- // iterate through the metricsArray
48- // each element is an array of num data (y-axis)
49- metricsArray . forEach ( el => {
50- const originalMetricName = Object . keys ( el ) [ 0 ] ;
51- const prettyMetricName = originalMetricName . replace ( re , ' ' ) ;
52- const newColor = colourGenerator ( serviceName ) ;
53- console . log ( 'prettyMetricName ' , prettyMetricName ) ;
54- // plotly's data prop takes an array of objects that each have x, y, type, mode, marker
55- const dataObject : plotlyData = {
56- name : prettyMetricName ,
57- x : timeArray ,
58- y : el [ originalMetricName ] ,
59- type : 'scattergl' ,
60- mode : 'lines' ,
61- marker : {
62- colors : [ '#fc4039' , '#4b54ea' , '#32b44f' , '#3788fc' , '#9c27b0' , '#febc2c' ] ,
63- } ,
64- } ;
65- plotlyDataObjectArray . push ( dataObject ) ;
66- } ) ;
67- console . log ( 'plotlydataObjectarray: ' , plotlyDataObjectArray ) ;
31+ // makes time data human-readable, and reverses it so it shows up correctly in the graph
32+ const prettyTimeInReverse = ( timeArray : string [ ] ) : string [ ] => {
33+ return timeArray . map ( ( el : any ) => moment ( el ) . format ( 'kk:mm:ss' ) ) . reverse ( ) ;
34+ } ;
35+
36+ // removes underscores from metric names to improve their look in the graph
37+ const prettyMetricName = metricName => {
38+ return metricName . replaceAll ( '_' , ' ' ) ;
39+ } ;
40+
41+ // pulls the current service names to be shown in the graph title from chartData
42+ const serviceNamesAsString = ( chartData : object ) : string => {
43+ let serviceNameString = '' ;
44+ for ( const serviceName in chartData ) {
45+ serviceNameString += `${ serviceName } | ` ;
46+ }
47+ return serviceNameString ;
48+ } ;
49+
50+ // generates an array of plotly data objects to be passed into our plotly chart's data prop
51+ const generatePlotlyDataObjects = ( chartData : object ) : object [ ] => {
52+ const arrayOfPlotlyDataObjects : PlotlyData [ ] = [ ] ;
53+ // iterate through the chartData
54+ for ( const serviceName in chartData ) {
55+ // define the metrics for this service
56+ const metrics = chartData [ serviceName ] ;
57+ // loop through the list of metrics for the current service
58+ for ( const metricName in metrics ) {
59+ // define the value and time arrays; allow data to be reassignable in case we need to convert the bytes data into megabytes
60+ let dataArray = metrics [ metricName ] . value ;
61+ const timeArray = metrics [ metricName ] . time ;
62+ // specifically for `Megabyte` types, convert the original data of bytes into a value of megabytes before graphing
63+ if ( dataType === 'Memory in Megabytes' || 'Cache in Megabytes' ) {
64+ dataArray = dataArray . map ( value => ( value / 1000000 ) . toFixed ( 2 ) ) ;
65+ }
66+ // create the plotly object
67+ const plotlyDataObject : PlotlyData = {
68+ name : prettyMetricName ( metricName ) ,
69+ x : prettyTimeInReverse ( timeArray ) ,
70+ y : dataArray ,
71+ type : 'scattergl' ,
72+ mode : 'lines' ,
73+ marker : {
74+ colors : [ '#fc4039' , '#4b54ea' , '#32b44f' , '#3788fc' , '#9c27b0' , '#febc2c' ] ,
75+ } ,
76+ } ;
77+ // push the dataObject into the arrayOfPlotlyDataObjects
78+ arrayOfPlotlyDataObjects . push ( plotlyDataObject ) ;
79+ }
80+ }
81+ // return the array of plotlyDataObject
82+ return arrayOfPlotlyDataObjects ;
6883 } ;
6984
7085 setInterval ( ( ) => {
@@ -74,15 +89,16 @@ const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
7489 } , 20 ) ;
7590
7691 const createChart = ( ) => {
77- generatePlotlyDataObjects ( metrics , reverseTimeArr ) ;
92+ const dataArray = generatePlotlyDataObjects ( chartData ) ;
93+ const serviceNames = serviceNamesAsString ( chartData ) ;
7894 const sizeSwitch = sizing === 'all' ? all : solo ;
7995
8096 return (
8197 < Plot
82- data = { plotlyDataObjectArray }
98+ data = { dataArray }
8399 config = { { displayModeBar : true } }
84100 layout = { {
85- title : `${ serviceName } | ${ categoryName } ` ,
101+ title : `${ serviceNames } | ${ categoryName } ` ,
86102 ...sizeSwitch ,
87103 font : {
88104 color : '#444d56' ,
@@ -93,10 +109,7 @@ const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
93109 plot_bgcolor : 'white' ,
94110 showlegend : true ,
95111 legend : {
96- orientation : 'h' ,
97- xanchor : 'center' ,
98- x : 0.5 ,
99- y : 5 ,
112+ orientation : 'v' ,
100113 } ,
101114 xaxis : {
102115 title : 'Time' ,
@@ -110,8 +123,7 @@ const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
110123 } ,
111124 yaxis : {
112125 rangemode : 'nonnegative' ,
113- //! change this later :^)
114- title : 'Value' ,
126+ title : `${ dataType } ` ,
115127 } ,
116128 } }
117129 />
0 commit comments