@@ -2,26 +2,20 @@ import moment from 'moment';
2
2
import React , { useState } from 'react' ;
3
3
import Plot from 'react-plotly.js' ;
4
4
import { all , solo as soloStyle } from './sizeSwitch' ;
5
- import { getTime } from '../context/helpers' ;
6
5
7
6
interface HealthChartProps {
8
7
key : string ;
9
- serviceName : string ;
10
- // metric: string ;
8
+ dataType : string ;
9
+ chartData : object ;
11
10
categoryName : string ;
12
- metrics : any [ ] ;
13
- timeList : any [ ] ;
14
- // valueList: any;
15
11
sizing : string ;
16
12
colourGenerator : Function ;
17
13
}
18
-
19
14
interface SoloStyles {
20
15
height : number ;
21
16
width : number ;
22
17
}
23
-
24
- type plotlyData = {
18
+ type PlotlyData = {
25
19
name : string ;
26
20
x : string [ ] ;
27
21
y : string [ ] ;
@@ -31,40 +25,61 @@ type plotlyData = {
31
25
} ;
32
26
33
27
const 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 ;
36
29
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 [ ] = [ ] ;
41
30
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 ;
68
83
} ;
69
84
70
85
setInterval ( ( ) => {
@@ -74,15 +89,16 @@ const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
74
89
} , 20 ) ;
75
90
76
91
const createChart = ( ) => {
77
- generatePlotlyDataObjects ( metrics , reverseTimeArr ) ;
92
+ const dataArray = generatePlotlyDataObjects ( chartData ) ;
93
+ const serviceNames = serviceNamesAsString ( chartData ) ;
78
94
const sizeSwitch = sizing === 'all' ? all : solo ;
79
95
80
96
return (
81
97
< Plot
82
- data = { plotlyDataObjectArray }
98
+ data = { dataArray }
83
99
config = { { displayModeBar : true } }
84
100
layout = { {
85
- title : `${ serviceName } | ${ categoryName } ` ,
101
+ title : `${ serviceNames } | ${ categoryName } ` ,
86
102
...sizeSwitch ,
87
103
font : {
88
104
color : '#444d56' ,
@@ -93,10 +109,7 @@ const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
93
109
plot_bgcolor : 'white' ,
94
110
showlegend : true ,
95
111
legend : {
96
- orientation : 'h' ,
97
- xanchor : 'center' ,
98
- x : 0.5 ,
99
- y : 5 ,
112
+ orientation : 'v' ,
100
113
} ,
101
114
xaxis : {
102
115
title : 'Time' ,
@@ -110,8 +123,7 @@ const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
110
123
} ,
111
124
yaxis : {
112
125
rangemode : 'nonnegative' ,
113
- //! change this later :^)
114
- title : 'Value' ,
126
+ title : `${ dataType } ` ,
115
127
} ,
116
128
} }
117
129
/>
0 commit comments