Skip to content

Commit e89e238

Browse files
committed
Remade transformData to be in a nested object format.
1 parent 135e28e commit e89e238

File tree

6 files changed

+111
-153
lines changed

6 files changed

+111
-153
lines changed

app/charts/HealthChart.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ 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;
@@ -31,19 +30,19 @@ type plotlyData = {
3130
};
3231

3332
const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
34-
// 'metrics' is an array of the user-specified metrics as objects
33+
// 'metrics' is an array of the user-specified metrics as objects => 'metric name': [metric values]
3534
const { serviceName, categoryName, metrics, timeList, sizing, colourGenerator } = props;
3635
const [solo, setSolo] = useState<SoloStyles | null>(null);
3736
const timeArr = timeList.map((el: any) => moment(el).format('kk:mm:ss'));
3837
const reverseTimeArr = timeArr.reverse();
3938
const re = /_/g;
40-
const plotlyDataObjectArray: plotlyData[] = [];
4139

40+
// this array gets populated once generatePlotlyDataObjects is invoked in `createChart`
41+
const plotlyDataObjectArray: plotlyData[] = [];
4242
// generates an array plotly data objects to add to be passed into our plotly chart's data prop
4343
const generatePlotlyDataObjects = (metricsArray, timeArray) => {
4444
console.log('metricsArray: ', metricsArray);
4545
console.log('timeArray: ', timeArray);
46-
// initalize an array of objects for output
4746
// iterate through the metricsArray
4847
// each element is an array of num data (y-axis)
4948
metricsArray.forEach(el => {

app/components/TransferColumns.tsx

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ 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

3535
useEffect(() => {
36-
if (healthDataList && healthDataList.length > 0) {
36+
if (healthDataObject) {
3737
setHealthMetricsReady(true);
3838
}
39-
}, [healthDataList]);
39+
}, [healthDataObject]);
4040

4141
useEffect(() => {
4242
if (eventDataList && eventDataList.length > 0) {
@@ -45,7 +45,7 @@ const TransferColumns = React.memo(() => {
4545
}, [eventDataList]);
4646

4747
useEffect(() => {
48-
setHealthMetrics(getMetrics('health', healthDataList));
48+
setHealthMetrics(getMetrics('health', healthDataObject));
4949
}, [healthMetricsReady]);
5050

5151
useEffect(() => {
@@ -65,26 +65,21 @@ const TransferColumns = React.memo(() => {
6565
}
6666
// JJ-ADDITION (CAN ALSO JUST ADD OR OPERATOR TO ABOVE CONDITIONAL)
6767
else if (service === 'kubernetesmetrics') {
68-
if (healthDataList && healthDataList.length > 0) {
69-
setMetricsPool(getMetrics('health', healthDataList));
68+
if (healthDataObject) {
69+
setMetricsPool(getMetrics('health', healthDataObject));
7070
} else if (healthMetricsReady) {
7171
setMetricsPool(healthMetrics);
7272
}
7373
} else if (!service.includes('kafkametrics')) {
74-
if (healthDataList && healthDataList.length > 0) {
75-
setMetricsPool(getMetrics('health', healthDataList));
74+
if (healthDataObject) {
75+
setMetricsPool(getMetrics('health', healthDataObject));
7676
} else if (healthMetricsReady) {
7777
setMetricsPool(healthMetrics);
7878
}
7979
} else {
80-
if (
81-
healthDataList &&
82-
healthDataList.length > 0 &&
83-
eventDataList &&
84-
eventDataList.length > 0
85-
) {
80+
if (healthDataObject && eventDataList && eventDataList.length > 0) {
8681
setMetricsPool(
87-
getMetrics('event', eventDataList).concat(getMetrics('health', healthDataList))
82+
getMetrics('event', eventDataList).concat(getMetrics('health', healthDataObject))
8883
);
8984
} else if (healthMetricsReady && eventMetricsReady) {
9085
setMetricsPool(eventMetrics.concat(healthMetrics));

app/containers/HealthContainer.tsx

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React, { useEffect, useState, useContext } from 'react';
22
import { HealthContext } from '../context/HealthContext';
33
import { QueryContext } from '../context/QueryContext';
44
import HealthChart from '../charts/HealthChart';
5-
import { getTime } from '../context/helpers';
65
import { useParams } from 'react-router-dom';
76

87
interface HealthContainerProps {
@@ -17,24 +16,70 @@ interface Params {
1716
}
1817

1918
const HealthContainer: React.FC<HealthContainerProps> = React.memo(props => {
19+
/*
20+
Pull in all the health data via HealthContext (an object with data and timestamps).
21+
Pull in the list of ALL user-selected metrics via QueryContext as strings, even if they don't pertain to this category.
22+
Destructure category from props. This category was passed down from the GraphsContainer and creates a new tab in Chronos to view charts pertaining only to the category.
23+
Think of each <HealthContainer /> as the new tab in Chronos. It will only create charts pertaining to that category.
24+
healthChartsArr is local state that gets updated here with an array of <HealthChart />'s that display user-selected data for the category.
25+
`service` is used to determine if we should display our health charts (because kafka and kubernetes specifically don't use HealthChart to display data).
26+
*/
2027
const { healthData } = useContext(HealthContext);
2128
const { selectedMetrics } = useContext(QueryContext);
22-
const [healthChartsArr, setHealthChartsArr] = useState<JSX.Element[]>([]);
2329
const { category } = props;
2430
const { service } = useParams<keyof Params>() as Params;
31+
const [healthChartsArr, setHealthChartsArr] = useState<JSX.Element[]>([]);
32+
33+
const filteredHealthDataList: any[] = [];
34+
const filteredHealthTimeList: any[] = [];
35+
36+
// function that filters healthData's list of metric data down to only the data pertaining to the passed in category
37+
function filterHealthDataAndTimeByCategory() {
38+
const originalHealthDataList = healthData.healthDataList;
39+
const originalHealthTimeList = healthData.healthTimeList;
40+
41+
originalHealthDataList.forEach(categoryObject => {
42+
// categoryObject is an object with a key of "Memory", "CPU", "Processes", or "Latency" and a value of an array with one element in it.
43+
// each value is another object with a key of whichever services the user has selected to monitor (ex: "books", "customers", "reverse proxy")
44+
const dataCategory = Object.keys(categoryObject)[0];
45+
if (dataCategory === category) {
46+
// if the categories match, iterate over the categoryObject's values which will be an array of services that have metric data for that category
47+
// push the services in the matched category to the filteredHealthDataList for use in graph construction
48+
const services = categoryObject[dataCategory];
49+
services.forEach((service: object) => {
50+
filteredHealthDataList.push(service);
51+
})
52+
}
53+
});
54+
55+
originalHealthTimeList.forEach(categoryObject => {
56+
const dataCategory: string = Object.keys(categoryObject)[0];
57+
if (dataCategory === category) {
58+
const services = Object.values(categoryObject);
59+
services.forEach((service) => {
60+
filteredHealthTimeList.push(service);
61+
})
62+
}
63+
})
64+
};
65+
66+
// function that filters ALL user-selected metric titles down to ones that pertain to this category
67+
68+
// function that filters all the healthData (health and timestamp data) pertaining to this category down to only the data matching the metric titles selected by the user
2569

2670
useEffect(() => {
2771
const temp: JSX.Element[] = [];
2872
let counter: number = 0;
2973
const dataList: any[] = healthData.healthDataList;
3074
const timeList: any[] = healthData.healthTimeList;
31-
// dataList and timeList are structured the same, but time holds timestamps. An array of 4 objects. [Memory, CPU, Processes, Latency]
32-
// Each element has all its metrics.
33-
// console.log('healthData object in state: ', healthData);
75+
// dataList and timeList are structured the same, but timeList holds timestamps. An array of 4 objects: [Memory, CPU, Processes, Latency]
3476
// console.log('dataList in healthcontainer is:', dataList);
3577
// console.log('timelist in healthcontainer is:', timeList);
3678

79+
// conditional that verifies data exists before any charts are created
3780
if (healthData && dataList && timeList && dataList.length > 0 && timeList.length > 0) {
81+
// initalize an array to store user-selected metrics
82+
// iterates through ALL user-selected metrics and filters it down to metrics that pertain to this category
3883
let selectedMetricsList: string[] = [];
3984
selectedMetrics.forEach(element => {
4085
if (Object.keys(element)[0] === category) {
@@ -52,19 +97,14 @@ const HealthContainer: React.FC<HealthContainerProps> = React.memo(props => {
5297
The 'category' variable is the specific category passed in to HealthContainer via prop drilling.
5398
The 'category' is the string Memory, CPU, or others that are in the Category column of the Query Selector interface.
5499
The 'categoryName' is the string that is Memory/CPU/other inside the metrics data response ('dataList' or 'timelist').
55-
When the 'element'/'categoryName' matches the 'category' selected in the Query Selection interface...
56-
... it will dive into that Category object to pull out a chart for each metric selected in the selection interface.
57-
selectedMetricsList is derived from the selectedMetrics that were in the QueryContext.
58-
selectedMetricsList is how we know which metrics should be made into a chart.
59-
selectedMetricsList is the way we can give one chart the multiple metrics being requested.
60100
*/
61101
if (category === categoryName) {
62102
const categoryObj: [] = element[categoryName];
63103
const filteredMetrics: any[] = [];
64104
for (const metricObj of categoryObj) {
65105
// serviceName = category (ex. books)
66-
const serviceName: string = Object.keys(metricObj)[0];
67-
console.log('metricObj: ', metricObj)
106+
const serviceName: string = Object.keys(metricObj)[0];
107+
console.log('metricObj: ', metricObj);
68108
const serviceMetricsArr: any[] = Object.values(metricObj).flat();
69109
console.log('serviceMetricsArr: ', serviceMetricsArr); // -> array of arrays containing numerical data.
70110
/* serviceMetricsArr = array of all metric objects

app/context/AwsContext.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { useCallback, useState, useEffect, useContext } from 'react';
22
import Electron from 'electron';
3-
import { transformData } from './helpers';
43
const { ipcRenderer } = window.require('electron');
54
import { ApplicationContext } from './ApplicationContext';
65

app/context/HealthContext.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useCallback, useState } from 'react';
22
import Electron from 'electron';
3-
import { transformData } from './helpers';
3+
import { transformer } from './helpers';
44
const { ipcRenderer } = window.require('electron');
55

66
export const HealthContext = React.createContext<any>(null);
@@ -53,7 +53,6 @@ const HealthContextProvider: React.FC<Props> = React.memo(({ children }) => {
5353
let result: any[];
5454
if (JSON.stringify(data) !== '{}' && tryParseJSON(data)) {
5555
result = JSON.parse(data);
56-
console.log('the health results before transformation: ', result)
5756
if (result && result.length && service === Object.keys(result[0])[0]) {
5857
resolve(result[0]);
5958
}
@@ -64,8 +63,9 @@ const HealthContextProvider: React.FC<Props> = React.memo(({ children }) => {
6463
if (checkServicesComplete(temp, serv)) {
6564
setServices(serv);
6665
let transformedData: any = {};
67-
transformedData = transformData(temp);
68-
console.log('results from fetch health data: ', transformedData);
66+
console.log('original data before transformation: ', temp);
67+
transformedData = transformer(temp);
68+
console.log('data after tranformation: ', transformedData);
6969
setHealthData(transformedData);
7070
}
7171
});

0 commit comments

Comments
 (0)