@@ -5,26 +5,29 @@ import CommunicationsContext from '../context/OverviewContext';
55
66const RouteLocations = ( props ) => {
77 const communicationsData = useContext ( CommunicationsContext ) . overviewData ;
8- console . log ( 'commData (from overviewContxt):' , communicationsData ) ;
8+ // console.log('commData (from overviewContxt):', communicationsData);
99
10- // initialize an empty object resObj. This object will store the microservice names as values and its corresponding correlatingId or correlatingid as keys. The microservice names will be stored in array within the order it was to the database.
10+ // initialize an empty object resObj.
11+ // This object will store the microservice names as values and its corresponding correlatingId or correlatingid as keys.
12+ // The microservice names will be stored in array within the order it was to the database.
1113 const resObj = { } ;
1214
1315 if ( communicationsData . length > 0 && communicationsData [ 0 ] . _id ) {
14- // Sort the communication array from latest to earliest document
16+ // Sort the communication array from OLDEST to NEWEST documents.
1517 communicationsData . sort ( ( a , b ) => {
1618 // Note that a newer date obj IS GREATER THAN an older date obj.
17- // The following lines sort the array from OLDEST to NEWEST.
1819 if ( new Date ( a . timeSent ) > new Date ( b . timeSent ) ) return 1 ;
1920 if ( new Date ( a . timeSent ) < new Date ( b . timeSent ) ) return - 1 ;
2021 return 0 ;
2122 } ) ;
23+ console . log ( 'commData (AFTER sorting):' , communicationsData ) ;
2224
23- // Iterate over sorted communicationsData array from the end to the beginning
25+ // Iterate over sorted array to build up resObj.
2426 for ( let i = 0 ; i < communicationsData . length ; i += 1 ) {
2527 // declare a constant element and initialize it as the object at index i of the communicationsData array
2628 const element = communicationsData [ i ] ;
2729 // Pushes the microservice name & timeSent into the resObj.
30+ // Data objects w/ same corrId will be grouped in a same array.
2831 if ( resObj [ element . correlatingId ] ) {
2932 resObj [ element . correlatingId ] . push ( {
3033 microservice_name : element . currentMicroservice ,
@@ -59,13 +62,49 @@ const RouteLocations = (props) => {
5962 }
6063 }
6164
62- // use object values to destructure locations
65+ // use Object. values to destructure locations
6366 // Each elem in tracePoints is an array of arrays, which contain objects (each of which is a data point).
64- const tracePoints = Object . values ( resObj ) ;
67+ // Filter the array so that only subarrays w/ len > 1 are kept.
68+ // (len == 1 means there's only one point in the route. There's no meaningful data to be gained from those.)
69+ const tracePoints = Object . values ( resObj ) . filter ( subArray => subArray . length > 1 ) ;
6570 const position = communicationsData [ 0 ] . correlatingid ? 0 : tracePoints . length - 1 ;
6671 console . log ( 'tracePoints arr:' , tracePoints ) ;
6772 console . log ( 'position for tracePoints:' , position ) ;
6873
74+ // Construct an obj that stores data necessary for calculating avg speed of requests btw 2 pts.
75+ const avgDataObj = { } ;
76+ /****** Build the object here w/ nested loops ************/
77+ /****** WARNING: tracePoints arr can be very long (100+) ************/
78+ for ( let i = 0 ; i < tracePoints . length ; i += 1 ) {
79+ let subArr = tracePoints [ i ] ;
80+ for ( let j = 0 ; j < subArr . length ; j += 1 ) {
81+ let currDataObj = subArr [ j ] ;
82+ if ( j < subArr . length - 1 ) {
83+ let nextDataObj = subArr [ j + 1 ] ;
84+ let routeName = `${ currDataObj . microservice_name } -${ nextDataObj . microservice_name } ` ;
85+ // Key/value pair that keeps COUNT of two-point routes
86+ if ( ! avgDataObj [ `${ routeName } Count` ] ) avgDataObj [ `${ routeName } Count` ] = 1 ;
87+ else avgDataObj [ `${ routeName } Count` ] += 1 ;
88+
89+ // Key/value that accumulates TOTAL TIME a req travels btw 2 certain points
90+ let timeDiff = new Date ( nextDataObj . timeSent ) - new Date ( currDataObj . timeSent ) ;
91+ if ( ! avgDataObj [ `${ routeName } TotalTime` ] ) {
92+ avgDataObj [ `${ routeName } TotalTime` ] = timeDiff ;
93+ }
94+ else avgDataObj [ `${ routeName } TotalTime` ] += timeDiff ;
95+
96+ // Key/value that calculates AVG TIME of travel (dividing the 2 values above)
97+ let avgTime = avgDataObj [ `${ routeName } TotalTime` ] / avgDataObj [ `${ routeName } Count` ] ;
98+ avgDataObj [ `${ routeName } AvgTime` ] = avgTime ;
99+ }
100+ }
101+ }
102+ /** End of nested loops */
103+
104+ console . log ( 'avgDataObj:' , avgDataObj ) ;
105+
106+ /****************************************/
107+ // Array of <divs> to be rendered. Each <div> contains route name and time difference.
69108 const resArray = [ ] ;
70109
71110 // iterate over Trace Points, creating a <div> for every data obj.
0 commit comments