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 : {
@@ -96,10 +77,9 @@ const RouteLocations = React.memo(() => {
9677
9778 // ======Graphs logic =======//
9879 const nodeListObj = { } ;
99- const edgeList = [ ] ;
80+ const edgeListObj = { } ;
10081 for ( let route of tracePoints ) {
10182 for ( let i = 0 ; i < route . length ; i += 1 ) {
102- const colors = [ '#75b6d7' , '#cc000' , '#fce356' , '#888888' , '#ccd8e1' ] ;
10383 // check if node exists if not then add node
10484 let id = route [ i ] . microservice ;
10585 if ( nodeListObj [ id ] === undefined ) {
@@ -114,32 +94,34 @@ const RouteLocations = React.memo(() => {
11494 shape : 'circle' ,
11595 } ;
11696 }
117- // add edge from node 1 to node 2 (repeat til end)
97+ // add edge from node to node (repeat til end)
11898 if ( i !== 0 ) {
99+ let from = route [ i - 1 ] . microservice
100+ let to = id ;
101+ let edgeStr = JSON . stringify ( { from, to } )
119102 let duration = new Date ( route [ i ] . time ) - new Date ( route [ i - 1 ] . time ) ;
120- let edge = { from : route [ i - 1 ] . microservice , to : id , label : `${ duration * 100 } ms` } ;
121- edgeList . push ( edge ) ;
103+ // only want one edge per route with the average duration
104+ if ( edgeListObj [ edgeStr ] ) {
105+ duration = ( duration + edgeListObj [ edgeStr ] ) / 2
106+ }
107+ edgeListObj [ edgeStr ] = duration
122108 }
123109 }
124110 }
111+
112+ // turn objects into valid arrays to input into graph
125113 const nodeList = Object . values ( nodeListObj ) ;
126- console . log ( 'edgeList' , edgeList ) ;
127- console . log ( 'nodeList' , nodeList ) ;
114+ const edgeList = [ ]
115+ for ( let [ e , duration ] of Object . entries ( edgeListObj ) ) {
116+ const edge = JSON . parse ( e )
117+ edge . label = `${ ( duration * 10 ) . toFixed ( 0 ) } ms`
118+ edgeList . push ( edge )
119+ }
128120
129121 const graph = {
130122 nodes : nodeList ,
131123 edges : edgeList ,
132124 } ;
133- // const graph = {
134- // nodes: [
135- // { id: 'one', label: "Node 1", color: "#e04141" },
136- // { id: 2, label: "Node 2", color: "#e09c41" },
137- // { id: 3, label: "Node 3", color: "#e0df41" },
138- // { id: 4, label: "Node 4", color: "#7be041" },
139- // { id: 5, label: "Node 5", color: "#41e0c9" }
140- // ],
141- // edges: [{ from: 4, to: 2, label: 'hello' }, { from: 'one', to: 3 }, { from: 2, to: 4 }, { from: 2, to: 5 }]
142- // };
143125 const options = {
144126 height : '300px' ,
145127 width : '300px' ,
@@ -188,4 +170,4 @@ const RouteLocations = React.memo(() => {
188170 ) ;
189171} ) ;
190172
191- export default RouteLocations ;
173+ export default RouteChart ;
0 commit comments