@@ -21,34 +21,78 @@ const RouteLocations = (props) => {
2121 for ( let i = 0 ; i < communicationsData . length ; i += 1 ) {
2222 // declare a constant element and initialize it as the object at index i of the communicationsData array
2323 const element = communicationsData [ i ] ;
24- // Pushes the microservice name into the object
24+ // Pushes the microservice name & timeSent into the resObj.
2525 if ( resObj [ element . correlatingId ] ) {
26- resObj [ element . correlatingId ] . push ( element . currentMicroservice ) ;
27- } else resObj [ element . correlatingId ] = [ element . currentMicroservice ] ;
26+ resObj [ element . correlatingId ] . push ( {
27+ microservice_name : element . currentMicroservice ,
28+ timeSent : element . timeSent
29+ } ) ;
30+ } else {
31+ // The value that corresp. to the correlationId key is an array of obj containing name and time data.
32+ // Each obj is a data point.
33+ resObj [ element . correlatingId ] = [ {
34+ microservice_name : element . currentMicroservice ,
35+ timeSent : element . timeSent
36+ } ] ;
37+ }
2838 }
2939 } else {
3040 for ( let i = communicationsData . length - 1 ; i >= 0 ; i -- ) {
3141 const element = communicationsData [ i ] ;
32- if ( resObj [ element . correlatingid ] ) resObj [ element . correlatingid ] . push ( element . currentmicroservice ) ;
33- else resObj [ element . correlatingid ] = [ element . currentmicroservice ] ;
42+ if ( resObj [ element . correlatingId ] ) {
43+ resObj [ element . correlatingId ] . push ( {
44+ microservice_name : element . currentMicroservice ,
45+ timeSent : element . timeSent
46+ } ) ;
47+ } else {
48+ // The value that corresp. to the correlationId key is an array of obj containing name and time data.
49+ // Each obj is a data point.
50+ resObj [ element . correlatingId ] = [ {
51+ microservice_name : element . currentMicroservice ,
52+ timeSent : element . timeSent
53+ } ] ;
54+ }
3455 // initializing the object with the first microservice
3556 }
3657 }
3758
3859 // use object values to destructure locations
60+ // Each elem in tracePoints is an array of arrays, which contain objects (each of which is a data point).
3961 const tracePoints = Object . values ( resObj ) ;
4062 const position = communicationsData [ 0 ] . correlatingid ? 0 : tracePoints . length - 1 ;
4163
4264 const resArray = [ ] ;
4365
44- // iterate over Trace Points
66+ // iterate over Trace Points, creating a <div> for every data obj.
4567 for ( let i = 0 ; i < tracePoints [ position ] . length ; i += 1 ) {
46- // push into resulting array current tracepoint as div
47- resArray . push (
48- < div className = "RouteCircle" key = { i } >
49- < p id = "routeText" > Point { i + 1 } : { tracePoints [ position ] [ i ] } </ p >
50- </ div > ,
51- ) ;
68+ if ( i !== tracePoints [ position ] . length - 1 ) {
69+ // Calc time difference (when not at the end of array):
70+ // Using Date.parse() because timeSent's value is a string.
71+ const timeDiff = Date . parse ( tracePoints [ position ] [ i + 1 ] . timeSent ) - Date . parse ( tracePoints [ position ] [ i ] . timeSent ) ;
72+ resArray . push (
73+ < div className = "RouteCircle" key = { i } >
74+ { /* Altering this <p> so it displays only microsvc_name */ }
75+ < p id = "routeText" >
76+ Point { i + 1 } : { tracePoints [ position ] [ i ] . microservice_name }
77+ </ p >
78+ { /* Adding another <p> that displays time difference btw curr obj and next obj */ }
79+ < p id = "routeTimeDiff" >
80+ { /* Time: {tracePoints[position][i].timeSent} */ }
81+ { /* What datatype? {Date.parse(tracePoints[position][i].timeSent) - Date.parse(tracePoints[position][i + 1].timeSent)} ms */ }
82+ Time elapsed: { timeDiff } ms
83+ </ p >
84+ </ div > ,
85+ ) ;
86+ } else {
87+ // If at the end of array, don't push the timeDiff <p> to resArray (only push a <p> w/ name).
88+ resArray . push (
89+ < div className = "RouteCircle" key = { i } >
90+ < p id = "routeText" >
91+ Point { i + 1 } : { tracePoints [ position ] [ i ] . microservice_name }
92+ </ p >
93+ </ div > ,
94+ ) ;
95+ }
5296 }
5397
5498 console . log ( 'resArray: ' , resArray ) ;
0 commit comments