Skip to content

Commit b17e1ff

Browse files
Fixed typeSorting logic in HealthContainer.
Now properly sorts information by type, and renders graphs accordingly. Co-authored-by: jeffreyNa <[email protected]>
1 parent 9012561 commit b17e1ff

File tree

2 files changed

+47
-38
lines changed

2 files changed

+47
-38
lines changed

app/charts/HealthChart.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
2929
const [solo, setSolo] = useState<SoloStyles | null>(null);
3030

3131
// makes time data human-readable, and reverses it so it shows up correctly in the graph
32-
const prettierTimeInReverse = (timeArray: string[]): string[] => {
32+
const prettyTimeInReverse = (timeArray: string[]): string[] => {
3333
return timeArray.map((el: any) => moment(el).format('kk:mm:ss')).reverse();
3434
};
3535

@@ -55,7 +55,7 @@ const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
5555
// define the metrics for this service
5656
const metrics = chartData[serviceName];
5757
// loop through the list of metrics for the current service
58-
for (const metricName of metrics) {
58+
for (const metricName in metrics) {
5959
// define the value and time arrays
6060
const dataArray = metrics[metricName].value;
6161
const timeArray = metrics[metricName].time;
@@ -66,7 +66,7 @@ const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
6666
// create the plotly object
6767
const plotlyDataObject: PlotlyData = {
6868
name: prettyMetricName(metricName),
69-
x: prettierTimeInReverse(timeArray),
69+
x: prettyTimeInReverse(timeArray),
7070
y: dataArray,
7171
type: 'scattergl',
7272
mode: 'lines',

app/containers/HealthContainer.tsx

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,16 @@ const HealthContainer: React.FC<HealthContainerProps> = React.memo(props => {
3636

3737
/*
3838
This function filters the selectedMetrics array down to only metrics that match the category of this instance of HealthContainer.
39-
Once that has finished, it then filters the healthData down to the current category and only the filteredMetrics.
39+
Once that has finished, it then filters the healthData down to the current category and the filteredMetrics.
4040
*/
4141
const filterSelectedMetricsAndHealthData = (): DataObject => {
42-
// filtered health data for output
42+
// define a filtered health data object for output
43+
// define an array of filteredMetricNames
4344
const filteredHealthData = {};
4445
const filteredMetricNames: string[] = [];
46+
// iterate over the selectedMetrics from QueryContext
4547
selectedMetrics.forEach(metricObj => {
48+
// due to the way the data is stored, the metricObj only has one key on it; thus [0] is tacked onto the end
4649
const metricCategory = Object.keys(metricObj)[0];
4750
// if the current metric's category matches our category, add its array content to the filteredHealthData array
4851
if (metricCategory === category) {
@@ -51,30 +54,32 @@ const HealthContainer: React.FC<HealthContainerProps> = React.memo(props => {
5154
});
5255
}
5356
});
54-
// iterate over the healthData object
57+
/*
58+
Now that we've defined which of the user's selected metrics belong in this category, iterate over the healthData object
59+
and filter it down to the selected category and metrics.
60+
*/
5561
for (const service in healthData) {
62+
filteredHealthData[service] = {};
5663
const categoryObjects = healthData[service];
5764
for (const categoryName in categoryObjects) {
5865
// if the category in healthData matches the category passed down to this HealthContainer, iterate over the related metrics
5966
if (categoryName === category) {
6067
const metricObjects = categoryObjects[categoryName];
6168
for (const metric in metricObjects) {
62-
// if the metric title matches any element in the filtered metrics array, add the metric object to the filteredHealthData object
69+
// if the metric title matches any element in the filtered metrics array, add the metric serviceName to the filteredHealthData object, then add the metrics for that service
6370
if (filteredMetricNames.includes(metric)) {
64-
filteredHealthData[metric] = metricObjects[metric];
71+
filteredHealthData[service][metric] = metricObjects[metric];
6572
}
6673
}
6774
}
6875
}
6976
}
70-
// console.log('filteredMetricNames: ', filteredMetricNames);
71-
// console.log('filteredHealthData: ', filteredHealthData);
7277
return filteredHealthData;
7378
};
7479

7580
// function to create a version of the healthData based on the value type
81+
// current healthData value types: GHz, bytes, temperature, percent, ticks, processes, num, latency
7682
const defineDataValueType = (metricName: string): string => {
77-
// current healthData value types: GHz, bytes, temperature, percent, ticks, processes, num, latency
7883
/*
7984
define a dictionary of data types where the key is the expected chars to be found in the parameter
8085
and the value is the desired data type label
@@ -91,7 +96,7 @@ const HealthContainer: React.FC<HealthContainerProps> = React.memo(props => {
9196
latency: 'Latency',
9297
};
9398
// iterate through the dictionary and check if the key matches any part of the metricName
94-
// if true assign type to the value of the matching key and return
99+
// if they match, update type variable to the value of the matching key and return
95100
let type: string = ''; // type will store the result for returning
96101
for (const [key, value] of Object.entries(typeDictionary)) {
97102
if (metricName.includes(key)) {
@@ -104,32 +109,33 @@ const HealthContainer: React.FC<HealthContainerProps> = React.memo(props => {
104109
return type;
105110
};
106111

107-
// function to sort the filtered healthData by data type to use for building graphs with same data label
112+
// function to sort the filteredHealthData by data type to build graphs based on the same data label
108113
const healthDataGroupedByDataType = (filteredHealthData: object): DataObject => {
109-
const groupedObject: DataObject = {};
110-
// charts need to be separated first by service and then each metric needs to be separated by value type
114+
const typeGroupedObject = {};
111115
// iterate over the services in the healthData object
112116
for (const serviceName in filteredHealthData) {
113-
// save the metrics of the current service to a variable
114-
// note: 'category' allows direct access to the list of metrics since we've already filtered to the current category
115-
const metrics = healthData[serviceName][category];
116-
// iterate over the current service's metrics
117-
for (const metricName in metrics) {
118-
// define the data type of the current metric
119-
const type: string = defineDataValueType(metricName);
120-
// save the value type as a key on an object
121-
// save the value of that key as an empty object
122-
groupedObject[type] = {};
123-
// save the serviceName as a key and assign it an empty object as value
124-
groupedObject[type][serviceName] = {};
125-
// define the metric's value (an object containing two keys of `data` and `time`, whose values are arrays of data/timestamps)
126-
const metricValue: object = metrics[metricName];
127-
// assign the object at that type key a new entry with the key of our current metric object's name and its value as the values
128-
groupedObject[type][serviceName][metricName] = metricValue;
117+
// save the filtered metrics of the current service to a variable
118+
// define the types of each metric in the metrics object as an array
119+
const metrics: object = filteredHealthData[serviceName];
120+
const typesArray: string[] = Object.keys(metrics).map((metricName: string): string => {
121+
return defineDataValueType(metricName);
122+
});
123+
// iterate over the types array and assign the typeGroupedObject a key of type, with a value of an object
124+
// then assign that newly created object a key of the current service and a value of an object
125+
typesArray.forEach((metricType: string) => {
126+
typeGroupedObject[metricType] = {};
127+
typeGroupedObject[metricType][serviceName] = {};
128+
})
129+
// iterate over the metrics object
130+
for (const metric in metrics) {
131+
// define the current metric's type
132+
const metricType: string = defineDataValueType(metric);
133+
// store the current metric and its value in the typeGrouped object at the appropriate type and serviceName
134+
typeGroupedObject[metricType][serviceName][metric] = metrics[metric];
129135
}
130136
}
131-
132-
return groupedObject;
137+
console.log('typeGroupedObject: ', typeGroupedObject);
138+
return typeGroupedObject;
133139
};
134140

135141
// function to generate charts using the type-sorted data
@@ -139,9 +145,11 @@ const HealthContainer: React.FC<HealthContainerProps> = React.memo(props => {
139145
// iterate over the sortedData and create a chart for each data type
140146
for (const dataType in sortedData) {
141147
// pass down the value of the current data type object
148+
const chartData = sortedData[dataType];
149+
console.log('dataType: ', dataType, 'chartData: ', chartData);
142150
chartsArray.push(
143151
<HealthChart
144-
key={`Chart${keymaker}`}
152+
key={`Chart${keymaker++}`}
145153
dataType={dataType}
146154
chartData={sortedData[dataType]}
147155
categoryName={`${category}`}
@@ -152,17 +160,18 @@ const HealthContainer: React.FC<HealthContainerProps> = React.memo(props => {
152160
}
153161
setHealthChartsArr(chartsArray);
154162
};
155-
163+
156164
useEffect(() => {
157165
// returns an object containing only the healthData for the current category and the metrics the User selected
158166
const filteredHealthData = filterSelectedMetricsAndHealthData();
159-
console.log('filtered health data: ', filteredHealthData);
167+
console.log('filteredHealthData: ', filteredHealthData)
160168
// returns an object containing the filtered data sorted by data type
161169
const typeSortedHealthData = healthDataGroupedByDataType(filteredHealthData);
162-
console.log('metrics sorted by data type: ', typeSortedHealthData);
170+
console.log('typeSortedHealthData: ', typeSortedHealthData);
163171
// invoking generateCharts with the sorted data will update healthChartsArr in state with the list of charts to be rendered
164172
generateCharts(typeSortedHealthData);
165-
}, [healthData, category]);
173+
console.log(healthChartsArr)
174+
}, [category]);
166175

167176
// return <div>{service !== 'kafkametrics' ? healthChartsArr : []}</div>;
168177
// JJ-ADDITION

0 commit comments

Comments
 (0)