-
-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathGraphsContainer.tsx
245 lines (232 loc) · 8.45 KB
/
GraphsContainer.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* eslint-disable no-bitwise */
import React, { useEffect, useState, useContext } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import { ApplicationContext } from '../context/ApplicationContext';
import { HealthContext } from '../context/HealthContext';
import { CommsContext } from '../context/CommsContext';
import { DockerContext } from '../context/DockerContext';
import { EventContext } from '../context/EventContext';
import { QueryContext } from '../context/QueryContext';
import Header from '../components/Header';
import RequestTypesChart from '../charts/RequestTypesChart';
import ResponseCodesChart from '../charts/ResponseCodesChart';
import TrafficChart from '../charts/TrafficChart';
import DockerChart from '../charts/DockerChart';
import RouteChart from '../charts/RouteChart';
import LogsTable from '../charts/LogsTable';
import EventContainer from './EventContainer';
import TransferColumns from '../components/TransferColumns';
import HealthContainer from './HealthContainer';
import ModifyMetrics from './ModifyMetricsContainer';
import * as DashboardContext from '../context/DashboardContext';
import lightAndDark from '../components/Styling';
import '../stylesheets/GraphsContainer.scss';
interface Params {
app: any;
service: string;
}
// interface GraphsContainerProps {
// match: {
// path: string;
// url: string;
// isExact: boolean;
// params: Params;
// };
// }
const GraphsContainer: React.FC = React.memo(props => {
const navigate = useNavigate();
const { app, service } = useParams<keyof Params>() as Params;
const [live, setLive] = useState<boolean>(false);
const { intervalID, setIntervalID } = useContext(ApplicationContext);
// const [intervalID, setIntervalID] = useState<NodeJS.Timeout | null>(null);
const { servicesData, getSavedMetrics } = useContext(ApplicationContext);
const { fetchHealthData, setHealthData, services } = useContext(HealthContext);
const { setDockerData, dockerData } = useContext(DockerContext);
const { fetchEventData, setEventData } = useContext(EventContext);
// const { fetchKafkaEventData, setKafkaEventData } = useContext(EventContext);
// const { fetchKubernetesEventData, setKubernetesEventData } = useContext(EventContext);
const { fetchCommsData, commsData } = useContext(CommsContext);
const { selectedMetrics } = useContext(QueryContext);
const [chart, setChart] = useState<string>('all');
const [prevRoute, setPrevRoute] = useState<string>('');
const { mode } = useContext(DashboardContext.DashboardContext);
useEffect(() => {
const serviceArray = service.split(' ');
// You would think you should add "kubernetesmetrics" into the below for consistency's sake but it makes it
// not work correctly, so it has been omitted
const healthServiceArray = serviceArray.filter((value: string) => value !== 'kafkametrics');
if (live) {
setIntervalID(
setInterval(() => {
//HERE fetching health data when on Live
fetchCommsData(app, live);
fetchHealthData(healthServiceArray);
if (service.includes('kafkametrics')) {
fetchEventData('kafkametrics');
}
// JJ-ADDITION
if (service.includes('kubernetesmetrics')) {
fetchEventData('kubernetesmetrics');
}
}, 10000)
);
} else {
if (intervalID) clearInterval(intervalID);
fetchCommsData(app, live);
fetchHealthData(healthServiceArray);
if (service.includes('kafkametrics')) {
fetchEventData('kafkametrics');
}
// JJ-ADDITION
if (service.includes('kubernetesmetrics')) {
fetchEventData('kubernetesmetrics');
}
getSavedMetrics();
}
return () => {
if (intervalID) clearInterval(intervalID);
setHealthData({ healthDataList: [], healthTimeList: [] });
setDockerData({});
setEventData({ eventDataList: [], eventTimeList: [] });
};
}, [service, live]);
const currentMode = mode === 'light' ? lightAndDark.lightModeText : lightAndDark.darkModeText;
const routing = (route: string) => {
if (location.href.includes('communications')) {
if (prevRoute === '') navigate(`${servicesData[0].microservice}`);
else navigate(prevRoute);
}
setChart(route);
};
const stringToColour = (string: string, recurses = 0) => {
if (recurses > 20) return string;
function hashString(str: string) {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
let colour = '#';
for (let i = 0; i < 3; i++) {
const value = (hash >> (i * 8)) & 0xff;
colour += `00${value.toString(16)}`.substring(-2);
}
return colour;
}
function contrastYiq(color: string) {
const num = parseInt(color.slice(1), 16);
const r = (num >>> 16) & 0xff;
const g = (num >>> 8) & 0xff;
const b = num & 0xff;
const yiq = (r * 299 + g * 587 + b * 114) / 1000;
return yiq <= 50 ? stringToColour(color, recurses + 1) : color;
}
for (let salt = 0; salt < 5; salt++) string = hashString(string);
return contrastYiq(string);
};
const getHealthAndEventComponents = () => {
const buttonList: JSX.Element[] = [];
if (selectedMetrics) {
selectedMetrics.forEach((element, id) => {
const categoryName = Object.keys(element)[0];
const prefix = categoryName === 'Event' ? 'event_' : 'health_';
buttonList.push(
<button
id={`${prefix}${categoryName}-button`}
className={chart === `${prefix}${categoryName}` ? 'selected' : undefined}
onClick={() => routing(`${prefix}${categoryName}`)}
key={`1-${id}`}
>
{categoryName}
</button>
);
});
}
return buttonList;
};
const HealthAndEventButtons: JSX.Element[] = getHealthAndEventComponents();
console.log({GraphsContainer: selectedMetrics})
console.log({GraphsContainer: chart});
return (
<>
<nav id="navigationBar">
<button
className={chart === 'all' ? 'selected' : undefined}
id="all-button"
onClick={() => routing('all')}
key="0"
>
Metrics Query
</button>
{HealthAndEventButtons}
{dockerData.containername && (
<button
id="docker-button"
className={chart === 'docker' ? 'selected' : undefined}
onClick={() => routing('docker')}
key="2"
>
Docker
</button>
)}
{commsData.length > 0 && (
<button
id="communication-button"
className={chart === 'communications' ? 'selected' : undefined}
onClick={() => {
if (!location.href.includes('communications')) setPrevRoute(services.join(' '));
setChart('communications');
}}
key="3"
>
Communication
</button>
)}
<button
id="modify-metrics-button"
className={chart === 'modifyMetrics' ? 'selected' : undefined}
onClick={() => {
routing('modifyMetrics');
}}
key="4"
>
Modify Metrics
</button>
</nav>
<Header app={app} service={service} live={live} setLive={setLive} />
<div className="graphs-container">
{chart === 'communications' ? (
<div className="graphs">
<RequestTypesChart />
<ResponseCodesChart />
<TrafficChart />
<RouteChart />
<LogsTable />
</div>
) : (
<div className="graphs">
{chart === 'all' && (
<div className="transferColumns">
<h2 style={currentMode}>Search Your Metrics to Display</h2>
<TransferColumns />
</div>
)}
{chart.startsWith('health_') && (
<HealthContainer
colourGenerator={stringToColour}
sizing="solo"
category={chart.substring(7)}
currentService={service}
/>
)}
{chart.startsWith('event_') && (
<EventContainer colourGenerator={stringToColour} sizing="solo" />
)}
{chart === 'docker' && <DockerChart />}
{chart === 'modifyMetrics' && <ModifyMetrics />}
</div>
)}
</div>
</>
);
});
export default GraphsContainer;