Skip to content

Commit b2663b9

Browse files
committed
Merge branch 'master' of https://github.com/oslabs-beta/Chronos into master
2 parents f695f9b + 429f595 commit b2663b9

File tree

1 file changed

+24
-42
lines changed

1 file changed

+24
-42
lines changed

app/charts/RouteChart.jsx

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
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';
32
import React, { useContext } from 'react';
4-
// import OverviewContext from '../context/OverviewContext';
53
import { CommsContext } from '../context/CommsContext';
6-
// import { log } from 'console';
74
import 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) {
@@ -108,32 +88,34 @@ const RouteLocations = React.memo(() => {
10888
label: id
10989
}
11090
}
111-
// add edge from node 1 to node 2 (repeat til end)
91+
// add edge from node to node (repeat til end)
11292
if (i !== 0) {
93+
let from = route[i - 1].microservice
94+
let to = id;
95+
let edgeStr = JSON.stringify({ from, to })
11396
let duration = new Date(route[i].time) - new Date(route[i - 1].time);
114-
let edge = { from: route[i - 1].microservice, to: id, label: `${duration * 100} ms` };
115-
edgeList.push(edge);
97+
// only want one edge per route with the average duration
98+
if (edgeListObj[edgeStr]) {
99+
duration = (duration + edgeListObj[edgeStr]) / 2
100+
}
101+
edgeListObj[edgeStr] = duration
116102
}
117103
}
118104
}
105+
106+
// turn objects into valid arrays to input into graph
119107
const nodeList = Object.values(nodeListObj);
120-
console.log('edgeList', edgeList);
121-
console.log('nodeList', nodeList);
108+
const edgeList = []
109+
for (let [e, duration] of Object.entries(edgeListObj)) {
110+
const edge = JSON.parse(e)
111+
edge.label = `${(duration * 10).toFixed(0)} ms`
112+
edgeList.push(edge)
113+
}
122114

123115
const graph = {
124116
nodes: nodeList,
125117
edges: edgeList,
126118
};
127-
// const graph = {
128-
// nodes: [
129-
// { id: 'one', label: "Node 1", color: "#e04141" },
130-
// { id: 2, label: "Node 2", color: "#e09c41" },
131-
// { id: 3, label: "Node 3", color: "#e0df41" },
132-
// { id: 4, label: "Node 4", color: "#7be041" },
133-
// { id: 5, label: "Node 5", color: "#41e0c9" }
134-
// ],
135-
// edges: [{ from: 4, to: 2, label: 'hello' }, { from: 'one', to: 3 }, { from: 2, to: 4 }, { from: 2, to: 5 }]
136-
// };
137119
const options = {
138120
height: '300px',
139121
width: '300px',
@@ -191,4 +173,4 @@ const RouteLocations = React.memo(() => {
191173
);
192174
});
193175

194-
export default RouteLocations;
176+
export default RouteChart;

0 commit comments

Comments
 (0)