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' ;
3
2
import React , { useContext } from 'react' ;
4
- // import OverviewContext from '../context/OverviewContext';
5
3
import { CommsContext } from '../context/CommsContext' ;
6
- // import { log } from 'console';
7
4
import Graph from 'react-graph-vis' ;
8
5
9
- const RouteLocations = React . memo ( ( ) => {
6
+ const RouteChart = React . memo ( ( ) => {
10
7
const communicationsData = useContext ( CommsContext ) . commsData ;
11
- console . log ( 'commdata=======>' , communicationsData ) ;
12
- console . log ( 'try again' ) ;
13
8
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} }
17
11
const resObj = { } ;
18
12
19
13
if ( communicationsData . length > 0 && communicationsData [ 0 ] . _id ) {
@@ -27,18 +21,13 @@ const RouteLocations = React.memo(() => {
27
21
28
22
// Iterate over sorted array to build up resObj.
29
23
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
31
24
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.
34
25
if ( resObj [ element . correlatingid ] ) {
35
26
resObj [ element . correlatingid ] . push ( {
36
27
microservice : element . microservice ,
37
28
time : element . time
38
29
} ) ;
39
30
} 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.
42
31
resObj [ element . correlatingid ] = [
43
32
{
44
33
microservice : element . microservice ,
@@ -56,28 +45,20 @@ const RouteLocations = React.memo(() => {
56
45
time
57
46
} ) ;
58
47
} 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.
61
48
resObj [ element . correlatingid ] = [
62
49
{
63
50
microservice,
64
51
time
65
52
} ,
66
53
] ;
67
54
}
68
- // initializing the object with the first microservice
69
55
}
70
- console . log ( 'B' , resObj )
71
56
}
72
- console . log ( '+++RESOBJ+++' ) ;
73
- console . log ( resObj ) ;
74
57
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
+
77
59
// Filter the array so that only subarrays w/ len > 1 are kept.
78
60
// (len == 1 means there's only one point in the route. There's no meaningful data to be gained from those.)
79
61
const tracePoints = Object . values ( resObj ) . filter ( subArray => subArray . length > 1 ) ;
80
- console . log ( 'tracepoints =======>' , tracePoints ) ;
81
62
82
63
const useStyles = makeStyles ( theme => ( {
83
64
paper : {
@@ -98,41 +79,42 @@ const RouteLocations = React.memo(() => {
98
79
99
80
// ======Graphs logic =======//
100
81
const nodeListObj = { } ;
101
- const edgeList = [ ] ;
82
+ const edgeListObj = { } ;
102
83
for ( let route of tracePoints ) {
103
84
for ( let i = 0 ; i < route . length ; i += 1 ) {
104
- const colors = [ '#75b6d7' , '#cc000' , '#fce356' , '#888888' , '#ccd8e1' ]
105
85
// check if node exists if not then add node
106
86
let id = route [ i ] . microservice
107
87
if ( nodeListObj [ id ] === undefined ) {
108
88
nodeListObj [ id ] = { id : id , label : id , color : { background : '#24d2f1' , border : 'white' , hover : { background : '#4d55ec' , border : 'white' } } , shape : 'circle' }
109
89
}
110
- // add edge from node 1 to node 2 (repeat til end)
90
+ // add edge from node to node (repeat til end)
111
91
if ( i !== 0 ) {
92
+ let from = route [ i - 1 ] . microservice
93
+ let to = id ;
94
+ let edgeStr = JSON . stringify ( { from, to } )
112
95
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
115
101
}
116
102
}
117
103
}
104
+
105
+ // turn objects into valid arrays to input into graph
118
106
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
+ }
121
113
122
114
const graph = {
123
115
nodes : nodeList ,
124
116
edges : edgeList
125
117
} ;
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
- // };
136
118
const options = {
137
119
height : '300px' ,
138
120
width : '300px' ,
@@ -169,4 +151,4 @@ const RouteLocations = React.memo(() => {
169
151
) ;
170
152
} ) ;
171
153
172
- export default RouteLocations ;
154
+ export default RouteChart ;
0 commit comments