1- import { Theme , makeStyles } from '@material-ui/core/styles' ;
2- import { BaseCSSProperties } from '@material-ui/core/styles/withStyles' ;
1+ import { makeStyles } from '@material-ui/core/styles' ;
32import React , { useContext } from 'react' ;
4- // import OverviewContext from '../context/OverviewContext';
53import { CommsContext } from '../context/CommsContext' ;
6- // import { log } from 'console';
74import Graph from 'react-graph-vis' ;
85
9- const RouteLocations = React . memo ( ( ) => {
6+ const RouteChart = React . memo ( ( ) => {
107 const communicationsData = useContext ( CommsContext ) . commsData ;
11- console . log ( 'commdata=======>' , communicationsData ) ;
12- console . log ( 'try again' ) ;
138
14- // initialize an empty object resObj.
15- // This object will store the microservice names as values and its corresponding correlatingid or correlatingid as keys.
16- // The microservice names will be stored in array within the order it was to the database.
9+ // gather all communicationsData and sort them using matching correlatingid.
10+ // resObj { key = correlatingid : value = array of objects{ microservice , time} }
1711 const resObj = { } ;
1812
1913 if ( communicationsData . length > 0 && communicationsData [ 0 ] . _id ) {
@@ -27,18 +21,13 @@ const RouteLocations = React.memo(() => {
2721
2822 // Iterate over sorted array to build up resObj.
2923 for ( let i = 0 ; i < communicationsData . length ; i += 1 ) {
30- // declare a constant element and initialize it as the object at index i of the communicationsData array
3124 const element = communicationsData [ i ] ;
32- // Pushes the microservice name & timeSent into the resObj.
33- // Data objects w/ same corrId will be grouped in a same array.
3425 if ( resObj [ element . correlatingid ] ) {
3526 resObj [ element . correlatingid ] . push ( {
3627 microservice : element . microservice ,
3728 time : element . time
3829 } ) ;
3930 } else {
40- // The value that corresp. to the correlationId key is an array of obj containing name and time data.
41- // Each obj is a data point.
4231 resObj [ element . correlatingid ] = [
4332 {
4433 microservice : element . microservice ,
@@ -56,28 +45,20 @@ const RouteLocations = React.memo(() => {
5645 time
5746 } ) ;
5847 } else {
59- // The value that corresp. to the correlationId key is an array of obj containing name and time data.
60- // Each obj is a data point.
6148 resObj [ element . correlatingid ] = [
6249 {
6350 microservice,
6451 time
6552 } ,
6653 ] ;
6754 }
68- // initializing the object with the first microservice
6955 }
70- console . log ( 'B' , resObj )
7156 }
72- console . log ( '+++RESOBJ+++' ) ;
73- console . log ( resObj ) ;
7457
75- // use Object.values to destructure locations
76- // Each elem in tracePoints is an array of arrays, which contain objects (each of which is a data point).
58+
7759 // Filter the array so that only subarrays w/ len > 1 are kept.
7860 // (len == 1 means there's only one point in the route. There's no meaningful data to be gained from those.)
7961 const tracePoints = Object . values ( resObj ) . filter ( subArray => subArray . length > 1 ) ;
80- console . log ( 'tracepoints =======>' , tracePoints ) ;
8162
8263 const useStyles = makeStyles ( theme => ( {
8364 paper : {
@@ -98,41 +79,42 @@ const RouteLocations = React.memo(() => {
9879
9980 // ======Graphs logic =======//
10081 const nodeListObj = { } ;
101- const edgeList = [ ] ;
82+ const edgeListObj = { } ;
10283 for ( let route of tracePoints ) {
10384 for ( let i = 0 ; i < route . length ; i += 1 ) {
104- const colors = [ '#75b6d7' , '#cc000' , '#fce356' , '#888888' , '#ccd8e1' ]
10585 // check if node exists if not then add node
10686 let id = route [ i ] . microservice
10787 if ( nodeListObj [ id ] === undefined ) {
10888 nodeListObj [ id ] = { id : id , label : id , color : { background : '#24d2f1' , border : 'white' , hover : { background : '#4d55ec' , border : 'white' } } , shape : 'circle' }
10989 }
110- // add edge from node 1 to node 2 (repeat til end)
90+ // add edge from node to node (repeat til end)
11191 if ( i !== 0 ) {
92+ let from = route [ i - 1 ] . microservice
93+ let to = id ;
94+ let edgeStr = JSON . stringify ( { from, to } )
11295 let duration = new Date ( route [ i ] . time ) - new Date ( route [ i - 1 ] . time ) ;
113- let edge = { from : route [ i - 1 ] . microservice , to : id , label : `${ duration * 100 } ms` }
114- edgeList . push ( edge )
96+ // only want one edge per route with the average duration
97+ if ( edgeListObj [ edgeStr ] ) {
98+ duration = ( duration + edgeListObj [ edgeStr ] ) / 2
99+ }
100+ edgeListObj [ edgeStr ] = duration
115101 }
116102 }
117103 }
104+
105+ // turn objects into valid arrays to input into graph
118106 const nodeList = Object . values ( nodeListObj ) ;
119- console . log ( edgeList ) ;
120- console . log ( nodeList ) ;
107+ const edgeList = [ ]
108+ for ( let [ e , duration ] of Object . entries ( edgeListObj ) ) {
109+ const edge = JSON . parse ( e )
110+ edge . label = `${ ( duration * 10 ) . toFixed ( 0 ) } ms`
111+ edgeList . push ( edge )
112+ }
121113
122114 const graph = {
123115 nodes : nodeList ,
124116 edges : edgeList
125117 } ;
126- // const graph = {
127- // nodes: [
128- // { id: 'one', label: "Node 1", color: "#e04141" },
129- // { id: 2, label: "Node 2", color: "#e09c41" },
130- // { id: 3, label: "Node 3", color: "#e0df41" },
131- // { id: 4, label: "Node 4", color: "#7be041" },
132- // { id: 5, label: "Node 5", color: "#41e0c9" }
133- // ],
134- // edges: [{ from: 4, to: 2, label: 'hello' }, { from: 'one', to: 3 }, { from: 2, to: 4 }, { from: 2, to: 5 }]
135- // };
136118 const options = {
137119 height : '300px' ,
138120 width : '300px' ,
@@ -169,4 +151,4 @@ const RouteLocations = React.memo(() => {
169151 ) ;
170152} ) ;
171153
172- export default RouteLocations ;
154+ export default RouteChart ;
0 commit comments