Skip to content

Commit 5f9565d

Browse files
author
Jeffrey Na
committed
merge
Merge branch 'dev' into jeff
2 parents ac2143b + e4f8623 commit 5f9565d

File tree

10 files changed

+323
-313
lines changed

10 files changed

+323
-313
lines changed

app/charts/HealthChart.tsx

Lines changed: 62 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,20 @@ import moment from 'moment';
22
import React, { useState } from 'react';
33
import Plot from 'react-plotly.js';
44
import { all, solo as soloStyle } from './sizeSwitch';
5-
import { getTime } from '../context/helpers';
65

76
interface HealthChartProps {
87
key: string;
9-
serviceName: string;
10-
// metric: string;
8+
dataType: string;
9+
chartData: object;
1110
categoryName: string;
12-
metrics: any[];
13-
timeList: any[];
14-
// valueList: any;
1511
sizing: string;
1612
colourGenerator: Function;
1713
}
18-
1914
interface SoloStyles {
2015
height: number;
2116
width: number;
2217
}
23-
24-
type plotlyData = {
18+
type PlotlyData = {
2519
name: string;
2620
x: string[];
2721
y: string[];
@@ -31,40 +25,61 @@ type plotlyData = {
3125
};
3226

3327
const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
34-
// 'metrics' is an array of the user-specified metrics as objects
35-
const { serviceName, categoryName, metrics, timeList, sizing, colourGenerator } = props;
28+
const { dataType, chartData, categoryName, sizing, colourGenerator } = props;
3629
const [solo, setSolo] = useState<SoloStyles | null>(null);
37-
const timeArr = timeList.map((el: any) => moment(el).format('kk:mm:ss'));
38-
const reverseTimeArr = timeArr.reverse();
39-
const re = /_/g;
40-
const plotlyDataObjectArray: plotlyData[] = [];
4130

42-
// generates an array plotly data objects to add to be passed into our plotly chart's data prop
43-
const generatePlotlyDataObjects = (metricsArray, timeArray) => {
44-
console.log('metricsArray: ', metricsArray);
45-
console.log('timeArray: ', timeArray);
46-
// initalize an array of objects for output
47-
// iterate through the metricsArray
48-
// each element is an array of num data (y-axis)
49-
metricsArray.forEach(el => {
50-
const originalMetricName = Object.keys(el)[0];
51-
const prettyMetricName = originalMetricName.replace(re, ' ');
52-
const newColor = colourGenerator(serviceName);
53-
console.log('prettyMetricName ', prettyMetricName);
54-
// plotly's data prop takes an array of objects that each have x, y, type, mode, marker
55-
const dataObject: plotlyData = {
56-
name: prettyMetricName,
57-
x: timeArray,
58-
y: el[originalMetricName],
59-
type: 'scattergl',
60-
mode: 'lines',
61-
marker: {
62-
colors: ['#fc4039', '#4b54ea', '#32b44f', '#3788fc', '#9c27b0', '#febc2c'],
63-
},
64-
};
65-
plotlyDataObjectArray.push(dataObject);
66-
});
67-
console.log('plotlydataObjectarray: ', plotlyDataObjectArray);
31+
// makes time data human-readable, and reverses it so it shows up correctly in the graph
32+
const prettyTimeInReverse = (timeArray: string[]): string[] => {
33+
return timeArray.map((el: any) => moment(el).format('kk:mm:ss')).reverse();
34+
};
35+
36+
// removes underscores from metric names to improve their look in the graph
37+
const prettyMetricName = metricName => {
38+
return metricName.replaceAll('_', ' ');
39+
};
40+
41+
// pulls the current service names to be shown in the graph title from chartData
42+
const serviceNamesAsString = (chartData: object): string => {
43+
let serviceNameString = '';
44+
for (const serviceName in chartData) {
45+
serviceNameString += `${serviceName} | `;
46+
}
47+
return serviceNameString;
48+
};
49+
50+
// generates an array of plotly data objects to be passed into our plotly chart's data prop
51+
const generatePlotlyDataObjects = (chartData: object): object[] => {
52+
const arrayOfPlotlyDataObjects: PlotlyData[] = [];
53+
// iterate through the chartData
54+
for (const serviceName in chartData) {
55+
// define the metrics for this service
56+
const metrics = chartData[serviceName];
57+
// loop through the list of metrics for the current service
58+
for (const metricName in metrics) {
59+
// define the value and time arrays; allow data to be reassignable in case we need to convert the bytes data into megabytes
60+
let dataArray = metrics[metricName].value;
61+
const timeArray = metrics[metricName].time;
62+
// specifically for `Megabyte` types, convert the original data of bytes into a value of megabytes before graphing
63+
if (dataType === 'Memory in Megabytes' || 'Cache in Megabytes') {
64+
dataArray = dataArray.map(value => (value / 1000000).toFixed(2));
65+
}
66+
// create the plotly object
67+
const plotlyDataObject: PlotlyData = {
68+
name: prettyMetricName(metricName),
69+
x: prettyTimeInReverse(timeArray),
70+
y: dataArray,
71+
type: 'scattergl',
72+
mode: 'lines',
73+
marker: {
74+
colors: ['#fc4039', '#4b54ea', '#32b44f', '#3788fc', '#9c27b0', '#febc2c'],
75+
},
76+
};
77+
// push the dataObject into the arrayOfPlotlyDataObjects
78+
arrayOfPlotlyDataObjects.push(plotlyDataObject);
79+
}
80+
}
81+
// return the array of plotlyDataObject
82+
return arrayOfPlotlyDataObjects;
6883
};
6984

7085
setInterval(() => {
@@ -74,15 +89,16 @@ const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
7489
}, 20);
7590

7691
const createChart = () => {
77-
generatePlotlyDataObjects(metrics, reverseTimeArr);
92+
const dataArray = generatePlotlyDataObjects(chartData);
93+
const serviceNames = serviceNamesAsString(chartData);
7894
const sizeSwitch = sizing === 'all' ? all : solo;
7995

8096
return (
8197
<Plot
82-
data={plotlyDataObjectArray}
98+
data={dataArray}
8399
config={{ displayModeBar: true }}
84100
layout={{
85-
title: `${serviceName} | ${categoryName}`,
101+
title: `${serviceNames}| ${categoryName}`,
86102
...sizeSwitch,
87103
font: {
88104
color: '#444d56',
@@ -93,10 +109,7 @@ const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
93109
plot_bgcolor: 'white',
94110
showlegend: true,
95111
legend: {
96-
orientation: 'h',
97-
xanchor: 'center',
98-
x: 0.5,
99-
y: 5,
112+
orientation: 'v',
100113
},
101114
xaxis: {
102115
title: 'Time',
@@ -110,8 +123,7 @@ const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
110123
},
111124
yaxis: {
112125
rangemode: 'nonnegative',
113-
//! change this later :^)
114-
title: 'Value',
126+
title: `${dataType}`,
115127
},
116128
}}
117129
/>

app/components/TransferColumns.tsx

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,21 @@ const TransferColumns = React.memo(() => {
2828
const { mode } = useContext(DashboardContext.DashboardContext);
2929

3030
const eventDataList = eventData.eventDataList;
31-
const healthDataList = healthData.healthDataList;
31+
const healthDataObject = healthData;
3232

3333
const currentMode = mode === 'light' ? lightAndDark.lightModeText : lightAndDark.darkModeText;
3434

35+
// console.log('healthMetrics: ', healthMetrics);
36+
// console.log('metricsPool: ', metricsPool);
37+
// console.log('targetKeys: ', targetKeys);
38+
// console.log('eventData: ', eventData);
39+
// console.log('eventDataList: ', eventDataList);
40+
3541
useEffect(() => {
36-
if (healthDataList && healthDataList.length > 0) {
42+
if (healthDataObject) {
3743
setHealthMetricsReady(true);
3844
}
39-
}, [healthDataList]);
45+
}, [healthDataObject]);
4046

4147
useEffect(() => {
4248
if (eventDataList && eventDataList.length > 0) {
@@ -45,7 +51,7 @@ const TransferColumns = React.memo(() => {
4551
}, [eventDataList]);
4652

4753
useEffect(() => {
48-
setHealthMetrics(getMetrics('health', healthDataList));
54+
setHealthMetrics(getMetrics('health', healthDataObject));
4955
}, [healthMetricsReady]);
5056

5157
useEffect(() => {
@@ -65,33 +71,29 @@ const TransferColumns = React.memo(() => {
6571
}
6672
// JJ-ADDITION (CAN ALSO JUST ADD OR OPERATOR TO ABOVE CONDITIONAL)
6773
else if (service === 'kubernetesmetrics') {
68-
if (healthDataList && healthDataList.length > 0) {
69-
setMetricsPool(getMetrics('health', healthDataList));
74+
if (healthDataObject) {
75+
setMetricsPool(getMetrics('health', healthDataObject));
7076
} else if (healthMetricsReady) {
7177
setMetricsPool(healthMetrics);
7278
}
7379
} else if (!service.includes('kafkametrics')) {
74-
if (healthDataList && healthDataList.length > 0) {
75-
setMetricsPool(getMetrics('health', healthDataList));
80+
if (healthDataObject) {
81+
setMetricsPool(getMetrics('health', healthDataObject));
7682
} else if (healthMetricsReady) {
7783
setMetricsPool(healthMetrics);
7884
}
7985
} else {
80-
if (
81-
healthDataList &&
82-
healthDataList.length > 0 &&
83-
eventDataList &&
84-
eventDataList.length > 0
85-
) {
86+
if (healthDataObject && eventDataList && eventDataList.length > 0) {
8687
setMetricsPool(
87-
getMetrics('event', eventDataList).concat(getMetrics('health', healthDataList))
88+
getMetrics('event', eventDataList).concat(getMetrics('health', healthDataObject))
8889
);
8990
} else if (healthMetricsReady && eventMetricsReady) {
9091
setMetricsPool(eventMetrics.concat(healthMetrics));
9192
}
9293
}
9394
}, [service, eventData, healthData]);
9495

96+
// responsible for parsing data and updating state via setMetricsPool
9597
const getMetrics = (type, datalist) => {
9698
let pool: any[] = [];
9799
if (type === 'event') {
@@ -105,25 +107,33 @@ const TransferColumns = React.memo(() => {
105107
pool.push(temp);
106108
});
107109
} else {
108-
datalist.forEach(category => {
109-
const tag: string = Object.keys(category)[0];
110-
const serviceObj: {} = category[tag][0];
111-
const valuesOfServiceObj: any[] = Object.values(serviceObj);
112-
const metricsArr: any[] = valuesOfServiceObj[0];
113-
metricsArr.forEach(element => {
114-
const temp = {};
115-
const metricName: string = Object.keys(element)[0];
116-
const key = tag + ' | ' + metricName;
117-
temp['key'] = key;
118-
temp['title'] = key;
119-
temp['tag'] = tag;
120-
pool.push(temp);
121-
});
122-
});
110+
// iterate throught the healthData object to populate the `Metrics Query` tab with metrics options
111+
// The pool array wants an object with a specific format in order to populate the selection table
112+
for (const service in healthData) {
113+
const categoryObjects = healthData[service];
114+
for (const category in categoryObjects) {
115+
const metricsObjects = categoryObjects[category];
116+
for (const metric in metricsObjects) {
117+
const key = category + ' | ' + metric;
118+
pool.push({
119+
key: key,
120+
title: key,
121+
tag: category,
122+
});
123+
}
124+
}
125+
}
123126
}
124127
return pool;
125128
};
126129

130+
// Justin's alternative to getCharts (b/c getCharts is just saving the user-selected metrics into QueryContext)
131+
// getCharts takes data that already exists in state as an array of strings, and makes it into an array of objects, just to save it to QueryContext state
132+
// the selectedMetrics from QueryContext is used in TransferColumns, EventContainer, GraphsContainer, and HealthContainer
133+
// const saveSelectedMetrics = () => {
134+
// // iterate over the targetKeys array
135+
// }
136+
127137
const getCharts = () => {
128138
const temp: any[] = [];
129139
const categorySet = new Set();
@@ -151,6 +161,7 @@ const TransferColumns = React.memo(() => {
151161
setSelectedMetrics(temp);
152162
};
153163

164+
// makes the column titles for the selection grid
154165
const columns = [
155166
{ field: 'id', headerName: 'ID', width: 100 },
156167
{
@@ -167,8 +178,8 @@ const TransferColumns = React.memo(() => {
167178
},
168179
];
169180

181+
// makes the rows needed for the selection grid
170182
const rows: any[] = [];
171-
172183
metricsPool.forEach((el, index) => {
173184
const row = {};
174185
row['id'] = index;
@@ -177,8 +188,8 @@ const TransferColumns = React.memo(() => {
177188
rows.push(row);
178189
});
179190

191+
// makes the Printed list of 'currently selected rows' on the page using targetKeys array
180192
const selectedRows: any[] = [];
181-
182193
targetKeys.forEach(el => {
183194
selectedRows.push(
184195
<li style={{ marginLeft: '30px', marginTop: '5px', color: currentMode.color }}>{el}</li>

app/containers/GraphsContainer.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const GraphsContainer: React.FC = React.memo(props => {
9191

9292
return () => {
9393
if (intervalID) clearInterval(intervalID);
94-
setHealthData({ healthDataList: [], healthTimeList: [] });
94+
setHealthData({});
9595
setDockerData({});
9696
setEventData({ eventDataList: [], eventTimeList: [] });
9797
};
@@ -156,9 +156,9 @@ const GraphsContainer: React.FC = React.memo(props => {
156156
};
157157

158158
const HealthAndEventButtons: JSX.Element[] = getHealthAndEventComponents();
159-
console.log('selected metrics: ', {GraphsContainer: selectedMetrics})
160-
console.log('chart: ', { GraphsContainer: chart });
161-
159+
console.log('selected metrics: ', selectedMetrics);
160+
console.log('chart: ', chart);
161+
162162
return (
163163
<>
164164
<nav id="navigationBar">

0 commit comments

Comments
 (0)