@@ -5,22 +5,17 @@ import { all, solo as soloStyle } from './sizeSwitch';
5
5
6
6
interface HealthChartProps {
7
7
key : string ;
8
- serviceName : string ;
9
- // metric: string ;
8
+ dataType : string ;
9
+ chartData : object ;
10
10
categoryName : string ;
11
- metrics : any [ ] ;
12
- timeList : any [ ] ;
13
- // valueList: any;
14
11
sizing : string ;
15
12
colourGenerator : Function ;
16
13
}
17
-
18
14
interface SoloStyles {
19
15
height : number ;
20
16
width : number ;
21
17
}
22
-
23
- type plotlyData = {
18
+ type PlotlyData = {
24
19
name : string ;
25
20
x : string [ ] ;
26
21
y : string [ ] ;
@@ -30,40 +25,62 @@ type plotlyData = {
30
25
} ;
31
26
32
27
const HealthChart : React . FC < HealthChartProps > = React . memo ( props => {
33
- // 'metrics' is an array of the user-specified metrics as objects => 'metric name': [metric values]
34
- const { serviceName, categoryName, metrics, timeList, sizing, colourGenerator } = props ;
28
+ const { dataType, chartData, categoryName, sizing, colourGenerator } = props ;
35
29
const [ solo , setSolo ] = useState < SoloStyles | null > ( null ) ;
36
- const timeArr = timeList . map ( ( el : any ) => moment ( el ) . format ( 'kk:mm:ss' ) ) ;
37
- const reverseTimeArr = timeArr . reverse ( ) ;
38
- const re = / _ / g;
39
30
40
- // this array gets populated once generatePlotlyDataObjects is invoked in `createChart`
41
- const plotlyDataObjectArray : plotlyData [ ] = [ ] ;
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
- // iterate through the metricsArray
47
- // each element is an array of num data (y-axis)
48
- metricsArray . forEach ( el => {
49
- const originalMetricName = Object . keys ( el ) [ 0 ] ;
50
- const prettyMetricName = originalMetricName . replace ( re , ' ' ) ;
51
- const newColor = colourGenerator ( serviceName ) ;
52
- console . log ( 'prettyMetricName ' , prettyMetricName ) ;
53
- // plotly's data prop takes an array of objects that each have x, y, type, mode, marker
54
- const dataObject : plotlyData = {
55
- name : prettyMetricName ,
56
- x : timeArray ,
57
- y : el [ originalMetricName ] ,
58
- type : 'scattergl' ,
59
- mode : 'lines' ,
60
- marker : {
61
- colors : [ '#fc4039' , '#4b54ea' , '#32b44f' , '#3788fc' , '#9c27b0' , '#febc2c' ] ,
62
- } ,
63
- } ;
64
- plotlyDataObjectArray . push ( dataObject ) ;
65
- } ) ;
66
- console . log ( 'plotlydataObjectarray: ' , plotlyDataObjectArray ) ;
31
+ // makes time data human-readable, and reverses it so it shows up correctly in the graph
32
+ const prettierTimeInReverse = ( 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 of metrics ) {
59
+ // define the value and time arrays
60
+ const dataArray = metrics [ metricName ] . value ;
61
+ const timeArray = metrics [ metricName ] . time ;
62
+ // specifically for `Megabyte` types, convert the data of bytes into a value of megabytes before graphing
63
+ if ( dataType === 'Memory in Megabytes' || 'Cache in Megabytes' ) {
64
+ dataArray . map ( value => ( value / 1000000 ) . toFixed ( 2 ) ) ;
65
+ }
66
+ // create the plotly object
67
+ const plotlyDataObject : PlotlyData = {
68
+ name : prettyMetricName ( metricName ) ,
69
+ x : prettierTimeInReverse ( 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
+ console . log ( 'plotlyObjectArray: ' , arrayOfPlotlyDataObjects ) ;
83
+ return arrayOfPlotlyDataObjects ;
67
84
} ;
68
85
69
86
setInterval ( ( ) => {
@@ -73,15 +90,16 @@ const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
73
90
} , 20 ) ;
74
91
75
92
const createChart = ( ) => {
76
- generatePlotlyDataObjects ( metrics , reverseTimeArr ) ;
93
+ const dataArray = generatePlotlyDataObjects ( chartData ) ;
94
+ const serviceNames = serviceNamesAsString ( chartData ) ;
77
95
const sizeSwitch = sizing === 'all' ? all : solo ;
78
96
79
97
return (
80
98
< Plot
81
- data = { plotlyDataObjectArray }
99
+ data = { dataArray }
82
100
config = { { displayModeBar : true } }
83
101
layout = { {
84
- title : `${ serviceName } | ${ categoryName } ` ,
102
+ title : `${ serviceNames } | ${ categoryName } ` ,
85
103
...sizeSwitch ,
86
104
font : {
87
105
color : '#444d56' ,
@@ -92,10 +110,7 @@ const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
92
110
plot_bgcolor : 'white' ,
93
111
showlegend : true ,
94
112
legend : {
95
- orientation : 'h' ,
96
- xanchor : 'center' ,
97
- x : 0.5 ,
98
- y : 5 ,
113
+ orientation : 'v' ,
99
114
} ,
100
115
xaxis : {
101
116
title : 'Time' ,
@@ -109,8 +124,7 @@ const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
109
124
} ,
110
125
yaxis : {
111
126
rangemode : 'nonnegative' ,
112
- //! change this later :^)
113
- title : 'Value' ,
127
+ title : `${ dataType } ` ,
114
128
} ,
115
129
} }
116
130
/>
0 commit comments