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 : {
@@ -96,10 +77,9 @@ const RouteLocations = React.memo(() => {
96
77
97
78
// ======Graphs logic =======//
98
79
const nodeListObj = { } ;
99
- const edgeList = [ ] ;
80
+ const edgeListObj = { } ;
100
81
for ( let route of tracePoints ) {
101
82
for ( let i = 0 ; i < route . length ; i += 1 ) {
102
- const colors = [ '#75b6d7' , '#cc000' , '#fce356' , '#888888' , '#ccd8e1' ] ;
103
83
// check if node exists if not then add node
104
84
let id = route [ i ] . microservice ;
105
85
if ( nodeListObj [ id ] === undefined ) {
@@ -114,32 +94,34 @@ const RouteLocations = React.memo(() => {
114
94
shape : 'circle' ,
115
95
} ;
116
96
}
117
- // add edge from node 1 to node 2 (repeat til end)
97
+ // add edge from node to node (repeat til end)
118
98
if ( i !== 0 ) {
99
+ let from = route [ i - 1 ] . microservice
100
+ let to = id ;
101
+ let edgeStr = JSON . stringify ( { from, to } )
119
102
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
122
108
}
123
109
}
124
110
}
111
+
112
+ // turn objects into valid arrays to input into graph
125
113
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
+ }
128
120
129
121
const graph = {
130
122
nodes : nodeList ,
131
123
edges : edgeList ,
132
124
} ;
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
- // };
143
125
const options = {
144
126
height : '300px' ,
145
127
width : '300px' ,
@@ -188,4 +170,4 @@ const RouteLocations = React.memo(() => {
188
170
) ;
189
171
} ) ;
190
172
191
- export default RouteLocations ;
173
+ export default RouteChart ;
0 commit comments