|
| 1 | +import React, { useContext } from 'react'; |
| 2 | +import { Bar } from 'react-chartjs-2'; |
| 3 | +import CommunicationsContext from '../context/OverviewContext'; |
| 4 | + |
| 5 | +const MicroServiceTraffic = (props) => { |
| 6 | + const communicationsData = useContext(CommunicationsContext).overviewData; |
| 7 | + |
| 8 | + //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. |
| 9 | + const resObj = {}; |
| 10 | + |
| 11 | + if(communicationsData.length>0 && communicationsData[0]["_id"]){ |
| 12 | + //Sort the communication array from latest to earliest document |
| 13 | + communicationsData.sort((a,b)=>{ |
| 14 | + if(new Date(a.timeSent) > new Date(b.timeSent)) return 1; |
| 15 | + if(new Date(a.timeSent) < new Date(b.timeSent)) return -1; |
| 16 | + return 0; |
| 17 | + }); |
| 18 | + |
| 19 | + //Iterate over sorted communicationsData array from the end to the beginning |
| 20 | + for (let i = 0; i < communicationsData.length; i+=1) { |
| 21 | + //declare a constant element and initialize it as the object at index i of the communicationsData array |
| 22 | + const element = communicationsData[i]; |
| 23 | + //Pushes the microservice name into the object |
| 24 | + if (resObj[element.correlatingId]) { |
| 25 | + resObj[element.correlatingId].push(element.currentMicroservice); |
| 26 | + } |
| 27 | + else resObj[element.correlatingId] = [element.currentMicroservice]; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + else { |
| 32 | + |
| 33 | + for (let i = communicationsData.length-1; i >= 0; i--) { |
| 34 | + const element = communicationsData[i]; |
| 35 | + if (resObj[element.correlatingid]) resObj[element.correlatingid].push(element.currentmicroservice); |
| 36 | + else resObj[element.correlatingid] = [element.currentmicroservice]; |
| 37 | + // initializing the object with the first microservice |
| 38 | + }; |
| 39 | + } |
| 40 | + |
| 41 | + //use object values to destructure locations |
| 42 | + const tracePoints = Object.values(resObj); |
| 43 | + let position = communicationsData[0].correlatingid ? 0 : tracePoints.length-1; |
| 44 | + |
| 45 | + // Declare Micro-server-count dictinary to capture the amount of times a particular server is hit |
| 46 | + const microServiceCountdictionary = {}; |
| 47 | + |
| 48 | + // iterate over Trace Points |
| 49 | + for (let i = 0; i < tracePoints[position].length; i+=1) { |
| 50 | + |
| 51 | + // populate Micro-count dictionary |
| 52 | + if (!microServiceCountdictionary[tracePoints[position][i]]) { |
| 53 | + microServiceCountdictionary[tracePoints[position][i]] = 1; |
| 54 | + } else { |
| 55 | + microServiceCountdictionary[tracePoints[position][i]]+=1 |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + // capture values of microServiceCountdictionary to use as data to populate chart object |
| 60 | + const serverPingCount = Object.values(microServiceCountdictionary); |
| 61 | + |
| 62 | + |
| 63 | + // Create chart object data to feed into bar component |
| 64 | + const myChart = { |
| 65 | + //spread dictionary keys inorder to properly label chart x axis |
| 66 | + labels: [...Object.keys(microServiceCountdictionary)], |
| 67 | + datasets: [ |
| 68 | + { |
| 69 | + label: 'Times server Pinged', |
| 70 | + backgroundColor: 'rgba(241, 207, 70,1)', |
| 71 | + borderColor: 'rgba(0,0,0,1)', |
| 72 | + borderWidth: 1, |
| 73 | + data: [...serverPingCount, 0] // spread ping count array into data array to have chart populate the Y axis |
| 74 | + } |
| 75 | + ] |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + // return div with Bar component and Trace Points data |
| 80 | + return ( |
| 81 | + <div> |
| 82 | + <h1>THIS IS FROM MS TRAFFIC</h1> |
| 83 | + <Bar |
| 84 | + data={myChart} |
| 85 | + width={100} |
| 86 | + height={50} |
| 87 | + options={{ |
| 88 | + title:{ |
| 89 | + display:true, |
| 90 | + text:'Microservices Overview', |
| 91 | + fontSize:20 |
| 92 | + }, |
| 93 | + legend:{ |
| 94 | + display:true, |
| 95 | + position:'right' |
| 96 | + } |
| 97 | + }} |
| 98 | + /> |
| 99 | + </div> |
| 100 | + ) |
| 101 | +}; |
| 102 | + |
| 103 | +export default MicroServiceTraffic; |
| 104 | + |
0 commit comments