|
| 1 | +import React, { useContext, useEffect } from 'react'; |
| 2 | +// import OverviewContext from '../context/OverviewContext'; |
| 3 | +import { CommsContext } from '../context/CommsContext'; |
| 4 | +// import { log } from 'console'; |
| 5 | +import Graph from 'react-graph-vis'; |
| 6 | + |
| 7 | +const RouteLocations = props => { |
| 8 | + const communicationsData = useContext(CommsContext).commsData; |
| 9 | + |
| 10 | + useEffect(() => { |
| 11 | + const parentNode = document.querySelector('div.chart'); |
| 12 | + const childNode = document.querySelectorAll('canvas')[0]; |
| 13 | + if (parentNode && childNode) { |
| 14 | + parentNode.append(childNode); |
| 15 | + childNode.id = `canvasGraph` |
| 16 | + } |
| 17 | + return childNode; |
| 18 | + }) |
| 19 | + |
| 20 | + console.log('commdata=======>', communicationsData); |
| 21 | + console.log('try again'); |
| 22 | + |
| 23 | + // initialize an empty object resObj. |
| 24 | + // This object will store the microservice names as values and its corresponding correlatingid or correlatingid as keys. |
| 25 | + // The microservice names will be stored in array within the order it was to the database. |
| 26 | + const resObj = {}; |
| 27 | + |
| 28 | + if (communicationsData.length > 0 && communicationsData[0]._id) { |
| 29 | + // Sort the communication array from OLDEST to NEWEST documents. |
| 30 | + communicationsData.sort((a, b) => { |
| 31 | + // Note that a newer date obj IS GREATER THAN an older date obj. |
| 32 | + if (new Date(a.time) > new Date(b.time)) return 1; |
| 33 | + if (new Date(a.time) < new Date(b.time)) return -1; |
| 34 | + return 0; |
| 35 | + }); |
| 36 | + |
| 37 | + // Iterate over sorted array to build up resObj. |
| 38 | + for (let i = 0; i < communicationsData.length; i += 1) { |
| 39 | + // declare a constant element and initialize it as the object at index i of the communicationsData array |
| 40 | + const element = communicationsData[i]; |
| 41 | + // Pushes the microservice name & timeSent into the resObj. |
| 42 | + // Data objects w/ same corrId will be grouped in a same array. |
| 43 | + if (resObj[element.correlatingid]) { |
| 44 | + resObj[element.correlatingid].push({ |
| 45 | + microservice: element.microservice, |
| 46 | + time: element.time |
| 47 | + }); |
| 48 | + } else { |
| 49 | + // The value that corresp. to the correlationId key is an array of obj containing name and time data. |
| 50 | + // Each obj is a data point. |
| 51 | + resObj[element.correlatingid] = [ |
| 52 | + { |
| 53 | + microservice: element.microservice, |
| 54 | + time: element.time |
| 55 | + }, |
| 56 | + ]; |
| 57 | + } |
| 58 | + } |
| 59 | + } else { |
| 60 | + for (let i = communicationsData.length - 1; i >= 0; i--) { |
| 61 | + const element = communicationsData[i]; |
| 62 | + if (resObj[element.correlatingid]) { |
| 63 | + resObj[element.correlatingid].push({ |
| 64 | + microservice, |
| 65 | + time |
| 66 | + }); |
| 67 | + } else { |
| 68 | + // The value that corresp. to the correlationId key is an array of obj containing name and time data. |
| 69 | + // Each obj is a data point. |
| 70 | + resObj[element.correlatingid] = [ |
| 71 | + { |
| 72 | + microservice, |
| 73 | + time |
| 74 | + }, |
| 75 | + ]; |
| 76 | + } |
| 77 | + // initializing the object with the first microservice |
| 78 | + } |
| 79 | + console.log('B', resObj) |
| 80 | + } |
| 81 | + console.log('+++RESOBJ+++'); |
| 82 | + console.log(resObj); |
| 83 | + |
| 84 | + // use Object.values to destructure locations |
| 85 | + // Each elem in tracePoints is an array of arrays, which contain objects (each of which is a data point). |
| 86 | + // Filter the array so that only subarrays w/ len > 1 are kept. |
| 87 | + // (len == 1 means there's only one point in the route. There's no meaningful data to be gained from those.) |
| 88 | + const tracePoints = Object.values(resObj).filter(subArray => subArray.length > 1); |
| 89 | + console.log('tracepoints =======>', tracePoints); |
| 90 | + |
| 91 | + const nodeListObj = {}; |
| 92 | + const edgeList = []; |
| 93 | + for (let route of tracePoints) { |
| 94 | + for (let i = 0; i < route.length; i += 1) { |
| 95 | + // check if node exists if not then add node |
| 96 | + let id = route[i].microservice |
| 97 | + if (nodeListObj[id] === undefined) { |
| 98 | + nodeListObj[id] = { id: id, label: id, color: '#e04141' } |
| 99 | + } |
| 100 | + // add edge from node 1 to node 2 (repeat til end) |
| 101 | + if (i !== 0) { |
| 102 | + let duration = new Date(route[i].time) - new Date(route[i-1].time); |
| 103 | + let edge = { from: route[i - 1].microservice, to: id, label: `${duration} ms`} |
| 104 | + edgeList.push(edge) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + const nodeList = Object.values(nodeListObj); |
| 109 | + console.log(edgeList); |
| 110 | + console.log(nodeList); |
| 111 | + |
| 112 | + const graph = { |
| 113 | + nodes: nodeList, |
| 114 | + edges: edgeList |
| 115 | + }; |
| 116 | + // const graph = { |
| 117 | + // nodes: [ |
| 118 | + // { id: 'one', label: "Node 1", color: "#e04141" }, |
| 119 | + // { id: 2, label: "Node 2", color: "#e09c41" }, |
| 120 | + // { id: 3, label: "Node 3", color: "#e0df41" }, |
| 121 | + // { id: 4, label: "Node 4", color: "#7be041" }, |
| 122 | + // { id: 5, label: "Node 5", color: "#41e0c9" } |
| 123 | + // ], |
| 124 | + // edges: [{ from: 4, to: 2, label: 'hello' }, { from: 'one', to: 3 }, { from: 2, to: 4 }, { from: 2, to: 5 }] |
| 125 | + // }; |
| 126 | + const options = { |
| 127 | + layout: { |
| 128 | + hierarchical: false |
| 129 | + }, |
| 130 | + edges: { |
| 131 | + color: "#000000", |
| 132 | + physics: false, |
| 133 | + smooth: { |
| 134 | + type: "curvedCCW", |
| 135 | + forceDirection: "none" |
| 136 | + } |
| 137 | + }, |
| 138 | + }; |
| 139 | + |
| 140 | + const events = { |
| 141 | + select: function (event) { |
| 142 | + var { nodes, edges } = event; |
| 143 | + console.log("Selected nodes:"); |
| 144 | + console.log(nodes); |
| 145 | + console.log("Selected edges:"); |
| 146 | + console.log(edges); |
| 147 | + } |
| 148 | + }; |
| 149 | + |
| 150 | + |
| 151 | + return ( |
| 152 | + <div className="chart"> |
| 153 | + <Graph id="visGraph" graph={graph} options={options} events={events} style={{ height: "640px" }} /> |
| 154 | + </div> |
| 155 | + ); |
| 156 | +}; |
| 157 | + |
| 158 | +export default RouteLocations; |
0 commit comments