@@ -5,115 +5,118 @@ import CommunicationsContext from '../context/OverviewContext';
55const MicroServiceTraffic = ( props ) => {
66 const communicationsData = useContext ( CommunicationsContext ) . overviewData ;
77
8-
9- //initialize an empty object resObj. This object will store the microservice names as values and its corresponding correlatingId or correlatingid as keys. The microservice names will be stored in array within the order it was to the database.
8+ // initialize an empty object resObj. This object will store the microservice names as values and its corresponding correlatingId or correlatingid as keys. The microservice names will be stored in array within the order it was to the database.
109 const resObj = { } ;
1110
12- if ( communicationsData . length > 0 && communicationsData [ 0 ] [ " _id" ] ) {
13- //Sort the communication array from latest to earliest document
14- communicationsData . sort ( ( a , b ) => {
15- if ( new Date ( a . timeSent ) > new Date ( b . timeSent ) ) return 1 ;
16- if ( new Date ( a . timeSent ) < new Date ( b . timeSent ) ) return - 1 ;
11+ if ( communicationsData . length > 0 && communicationsData [ 0 ] . _id ) {
12+ // Sort the communication array from latest to earliest document
13+ communicationsData . sort ( ( a , b ) => {
14+ if ( new Date ( a . timeSent ) > new Date ( b . timeSent ) ) return 1 ;
15+ if ( new Date ( a . timeSent ) < new Date ( b . timeSent ) ) return - 1 ;
1716 return 0 ;
1817 } ) ;
19-
20- //Iterate over sorted communicationsData array from the end to the beginning
21- for ( let i = 0 ; i < communicationsData . length ; i += 1 ) {
22- //declare a constant element and initialize it as the object at index i of the communicationsData array
18+
19+ // Iterate over sorted communicationsData array from the end to the beginning
20+ for ( let i = 0 ; i < communicationsData . length ; i += 1 ) {
21+ // declare a constant element and initialize it as the object at index i of the communicationsData array
2322 const element = communicationsData [ i ] ;
24- //Pushes the microservice name into the object
23+ // Pushes the microservice name into the object
2524 if ( resObj [ element . correlatingId ] ) {
2625 resObj [ element . correlatingId ] . push ( element . currentMicroservice ) ;
27- }
28- else resObj [ element . correlatingId ] = [ element . currentMicroservice ] ;
26+ } else resObj [ element . correlatingId ] = [ element . currentMicroservice ] ;
2927 }
30- }
31-
32- else {
33- for ( let i = communicationsData . length - 1 ; i >= 0 ; i -- ) {
28+ } else {
29+ for ( let i = communicationsData . length - 1 ; i >= 0 ; i -- ) {
3430 const element = communicationsData [ i ] ;
35- if ( resObj [ element . correlatingid ] ) resObj [ element . correlatingid ] . push ( element . currentmicroservice ) ;
31+ if ( resObj [ element . correlatingid ] )
32+ resObj [ element . correlatingid ] . push ( element . currentmicroservice ) ;
3633 else resObj [ element . correlatingid ] = [ element . currentmicroservice ] ;
3734 // initializing the object with the first microservice
38- } ;
35+ }
3936 }
4037
41- //use object values to destructure locations
38+ // use object values to destructure locations
4239 const tracePoints = Object . values ( resObj ) ;
4340
4441 // Declare Micro-server-count dictinary to capture the amount of times a particular server is hit
45- const microServiceCountdictionary = { } ;
42+ const microServiceCountdictionary = { } ;
4643
47- //array logging every ping present in communications table ---> flat used to flatten multidimensional array and return 1d array
44+ // array logging every ping present in communications table ---> flat used to flatten multidimensional array and return 1d array
4845 const tracePointLog = tracePoints . flat ( Infinity ) ;
4946
5047 // iterate over Trace Points
51- for ( let i = 0 ; i < tracePointLog . length ; i += 1 ) {
52-
48+ for ( let i = 0 ; i < tracePointLog . length ; i += 1 ) {
5349 // populate Micro-count dictionary
5450 if ( ! microServiceCountdictionary [ tracePointLog [ i ] ] ) {
5551 microServiceCountdictionary [ tracePointLog [ i ] ] = 1 ;
5652 } else {
57- microServiceCountdictionary [ tracePointLog [ i ] ] += 1
53+ microServiceCountdictionary [ tracePointLog [ i ] ] += 1 ;
5854 }
59- } ;
55+ }
6056
6157 // capture values of microServiceCountdictionary to use as data to populate chart object
6258 const serverPingCount = Object . values ( microServiceCountdictionary ) ;
6359
6460 // variable 10 points higher than max number in microservicesDictionary aggregation --> variable allows for top level spacing on bar graph
65- const yAxisHeadRoom = ( Math . max ( ...serverPingCount ) + 10 ) ;
61+ const yAxisHeadRoom = Math . max ( ...serverPingCount ) + 10 ;
6662
67- // Create chart object data to feed into bar component
63+ // Create chart object data to feed into bar component
6864 const myChart = {
69- //spread dictionary keys inorder to properly label chart x axis
65+ // spread dictionary keys inorder to properly label chart x axis
7066 labels : [ ...Object . keys ( microServiceCountdictionary ) ] ,
7167 datasets : [
72- {
73- label : 'Times server Pinged' ,
74- backgroundColor : 'rgba(241, 207, 70,1)' ,
75- borderColor : 'rgba(0,0,0,1)' ,
76- borderWidth : 1 ,
77- data : [ ...serverPingCount , 0 , yAxisHeadRoom ] // spread ping count array into data array to have chart populate the Y axis
78- }
79- ] ,
80- }
68+ {
69+ label : 'Times server Pinged' ,
70+ backgroundColor : 'rgba(241, 207, 70,1)' ,
71+ borderColor : 'rgba(0,0,0,1)' ,
72+ borderWidth : 1 ,
73+ data : [ ...serverPingCount , 0 , yAxisHeadRoom ] , // spread ping count array into data array to have chart populate the Y axis
74+ } ,
75+ ] ,
76+ } ;
8177
8278 return (
83- < Plot
84- data = { [ {
85- type : 'bar' ,
86- x : [ 'Orders' , 'Customers' , 'Books' , 'Reverse-Proxy' , 'ReverseProxy' ] ,
87- y : [ ...serverPingCount , 0 , yAxisHeadRoom ] ,
88- fill : 'tozeroy' ,
89- marker : { 'color' : '#5C80FF' } ,
90- opacity : .7 ,
91- mode : 'none' ,
92- name : 'Times Server Pinged' ,
93- showlegend : true
94- } ] }
95- layout = {
96- {
79+ < div className = "trafficChart" >
80+ < Plot
81+ data = { [
82+ {
83+ type : 'bar' ,
84+ x : [
85+ 'Orders' ,
86+ 'Customers' ,
87+ 'Books' ,
88+ 'Reverse-Proxy' ,
89+ 'ReverseProxy' ,
90+ ] ,
91+ y : [ ...serverPingCount , 0 , yAxisHeadRoom ] ,
92+ fill : 'tozeroy' ,
93+ marker : { color : '#5C80FF' } ,
94+ opacity : 0.7 ,
95+ mode : 'none' ,
96+ name : 'Times Server Pinged' ,
97+ showlegend : true ,
98+ } ,
99+ ] }
100+ layout = { {
97101 height : 400 ,
98102 width : 400 ,
99103 font : {
100104 color : 'azure' ,
101- size : 15
105+ size : 15 ,
102106 } ,
103107 paper_bgcolor : '#8BA6B9' ,
104108 plot_bgcolor : '#8BA6B9' ,
105109 legend : {
106110 orientation : 'h' ,
107111 xanchor : 'center' ,
108- x : .5 ,
109- y : 5
112+ x : 0 .5,
113+ y : 5 ,
110114 } ,
111- yaxis : { rangemode : 'nonnegative' }
112- }
113- }
114- / >
115- )
115+ yaxis : { rangemode : 'nonnegative' } ,
116+ } }
117+ />
118+ </ div >
119+ ) ;
116120} ;
117121
118122export default MicroServiceTraffic ;
119-
0 commit comments