Skip to content

Commit 2964159

Browse files
authored
Merge pull request #10 from oslabs-beta/justin/event-metrics
Justin/event metrics
2 parents a7c119c + 54bd670 commit 2964159

File tree

13 files changed

+297
-296
lines changed

13 files changed

+297
-296
lines changed

app/charts/EventChart.tsx

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import { all, solo as soloStyle } from './sizeSwitch';
55

66
interface EventChartProps {
77
key: string;
8-
metric: string;
9-
timeList: any;
10-
valueList: any;
8+
metricName: string;
9+
chartData: {
10+
value: string[],
11+
time: string[]
12+
}
1113
sizing: string;
1214
colourGenerator: Function;
1315
}
@@ -17,8 +19,17 @@ interface SoloStyles {
1719
width: number;
1820
}
1921

22+
type PlotlyData = {
23+
name: string;
24+
x: string[];
25+
y: string[];
26+
type: string;
27+
mode: string;
28+
marker: { colors: string[] };
29+
};
30+
2031
const EventChart: React.FC<EventChartProps> = React.memo(props => {
21-
const { metric, timeList, valueList, sizing, colourGenerator } = props;
32+
const { metricName, chartData, sizing, colourGenerator } = props;
2233
const [solo, setSolo] = useState<SoloStyles | null>(null);
2334

2435
setInterval(() => {
@@ -27,36 +38,39 @@ const EventChart: React.FC<EventChartProps> = React.memo(props => {
2738
}
2839
}, 20);
2940

41+
// makes time data human-readable, and reverses it so it shows up correctly in the graph
42+
const prettyTimeInReverse = (timeArray: string[]): string[] => {
43+
return timeArray.map((el: any) => moment(el).format('kk:mm:ss')).reverse();
44+
};
45+
46+
// removes underscores from metric names to improve their look in the graph
47+
const prettyMetricName = (metricName: string): string => {
48+
const newName = metricName.replace(/kubernetes-cadvisor\/docker-desktop\//g, '');
49+
return newName.replace(/_/g, ' ');
50+
};
51+
3052
const createChart = () => {
31-
const timeArr = timeList.map((el: any) => moment(el).format('kk:mm:ss'));
32-
const reverseTimeArr = timeArr.reverse()
33-
const hashedColour = colourGenerator(metric);
34-
const newMetricName = metric.replace("kubernetes-cadvisor/docker-desktop/", ""); // this will get rid of the long path
35-
const re = /_/g;
36-
let plotlyData: {
37-
name: any;
38-
x: any;
39-
y: any;
40-
type: any;
41-
mode: any;
42-
marker: { color: string };
43-
};
44-
plotlyData = {
45-
name: metric,
46-
x: reverseTimeArr,
47-
y: valueList,
53+
const prettyName = prettyMetricName(metricName);
54+
const prettyTime = prettyTimeInReverse(chartData.time);
55+
56+
const plotlyDataObject: PlotlyData = {
57+
name: prettyName,
58+
x: prettyTime,
59+
y: chartData.value,
4860
type: 'scattergl',
4961
mode: 'lines',
50-
marker: { color: hashedColour },
62+
marker: {
63+
colors: ['#fc4039', '#4b54ea', '#32b44f', '#3788fc', '#9c27b0', '#febc2c'],
64+
},
5165
};
5266
const sizeSwitch = sizing === 'all' ? all : solo;
5367

5468
return (
5569
<Plot
56-
data={[plotlyData]}
57-
config={{ displayModeBar: false }}
70+
data={[plotlyDataObject]}
71+
config={{ displayModeBar: true }}
5872
layout={{
59-
title: newMetricName.replace(re," "), // this will reaplce all the underscores in the graph titlke,
73+
title: prettyName,
6074
...sizeSwitch,
6175
font: {
6276
color: '#444d56',
@@ -67,10 +81,7 @@ const EventChart: React.FC<EventChartProps> = React.memo(props => {
6781
plot_bgcolor: 'white',
6882
showlegend: true,
6983
legend: {
70-
orientation: 'h',
71-
xanchor: 'center',
72-
x: 0.5,
73-
y: 5,
84+
orientation: 'v',
7485
},
7586
xaxis: {
7687
title: 'Time',
@@ -84,7 +95,7 @@ const EventChart: React.FC<EventChartProps> = React.memo(props => {
8495
},
8596
yaxis: {
8697
rangemode: 'nonnegative',
87-
title: metric,
98+
title: prettyName,
8899
},
89100
}}
90101
/>

app/charts/HealthChart.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { all, solo as soloStyle } from './sizeSwitch';
66
interface HealthChartProps {
77
key: string;
88
dataType: string;
9+
serviceName: string;
910
chartData: object;
1011
categoryName: string;
1112
sizing: string;
@@ -25,7 +26,7 @@ type PlotlyData = {
2526
};
2627

2728
const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
28-
const { dataType, chartData, categoryName, sizing, colourGenerator } = props;
29+
const { dataType, serviceName, chartData, categoryName, sizing, colourGenerator } = props;
2930
const [solo, setSolo] = useState<SoloStyles | null>(null);
3031

3132
// makes time data human-readable, and reverses it so it shows up correctly in the graph
@@ -34,8 +35,8 @@ const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
3435
};
3536

3637
// removes underscores from metric names to improve their look in the graph
37-
const prettyMetricName = metricName => {
38-
return metricName.replaceAll('_', ' ');
38+
const prettyMetricName = (metricName: string): string => {
39+
return metricName.replace(/_/g, ' ');
3940
};
4041

4142
// pulls the current service names to be shown in the graph title from chartData
@@ -90,15 +91,14 @@ const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
9091

9192
const createChart = () => {
9293
const dataArray = generatePlotlyDataObjects(chartData);
93-
const serviceNames = serviceNamesAsString(chartData);
9494
const sizeSwitch = sizing === 'all' ? all : solo;
9595

9696
return (
9797
<Plot
9898
data={dataArray}
9999
config={{ displayModeBar: true }}
100100
layout={{
101-
title: `${serviceNames}| ${categoryName}`,
101+
title: `${serviceName} || ${categoryName}`,
102102
...sizeSwitch,
103103
font: {
104104
color: '#444d56',
@@ -123,7 +123,7 @@ const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
123123
},
124124
yaxis: {
125125
rangemode: 'nonnegative',
126-
title: `${dataType}`,
126+
title: dataType,
127127
},
128128
}}
129129
/>

app/components/Occupied.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ type ClickEvent = React.MouseEvent<HTMLElement>;
6262

6363
//v10: Memoized function, witouth any props. Should theoretically be called only once.
6464
const Occupied = React.memo(() => {
65-
console.log('Hi, inside Occupied. Memoize function invoked');
66-
6765
const { awsData, fetchAwsData, fetchAwsAppInfo, setLoadingState } = useContext(AwsContext);
6866
const { setServicesData, app, setApp } = useContext(ApplicationContext);
6967
// const { user, getApplications, deleteApp, mode } = useContext(DashboardContext);
@@ -88,7 +86,6 @@ const Occupied = React.memo(() => {
8886
// Grab services and applications whenever the user changes
8987
// v10: Runs once when Occupied is memoized, and subsequently when user is updated.
9088
useEffect(() => {
91-
console.log('Hi, inside Occupied.ts useEffect function.');
9289
setServicesData([]);
9390
getApplications();
9491
}, [user]);

app/components/TransferColumns.tsx

Lines changed: 44 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -27,94 +27,85 @@ const TransferColumns = React.memo(() => {
2727
const { service } = useParams<keyof Params>() as Params;
2828
const { mode } = useContext(DashboardContext.DashboardContext);
2929

30-
const eventDataList = eventData.eventDataList;
31-
const healthDataObject = healthData;
32-
3330
const currentMode = mode === 'light' ? lightAndDark.lightModeText : lightAndDark.darkModeText;
3431

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-
4132
useEffect(() => {
42-
if (healthDataObject) {
33+
if (healthData) {
4334
setHealthMetricsReady(true);
4435
}
45-
}, [healthDataObject]);
36+
}, [healthData]);
4637

4738
useEffect(() => {
48-
if (eventDataList && eventDataList.length > 0) {
39+
if (eventData) {
4940
setEventMetricsReady(true);
5041
}
51-
}, [eventDataList]);
42+
}, [eventData]);
5243

5344
useEffect(() => {
54-
setHealthMetrics(getMetrics('health', healthDataObject));
45+
setHealthMetrics(getMetrics('health', healthData));
5546
}, [healthMetricsReady]);
5647

5748
useEffect(() => {
58-
setEventMetrics(getMetrics('event', eventDataList));
49+
setEventMetrics(getMetrics('event', eventData));
5950
}, [eventMetricsReady]);
6051

6152
useEffect(() => {
6253
if (service === '') {
6354
return;
6455
}
6556
if (service === 'kafkametrics') {
66-
if (eventDataList && eventDataList.length > 0) {
67-
setMetricsPool(getMetrics('event', eventDataList));
57+
if (eventData) {
58+
setMetricsPool(getMetrics('event', eventData));
6859
} else if (eventMetricsReady) {
6960
setMetricsPool(eventMetrics);
7061
}
7162
}
7263
// JJ-ADDITION (CAN ALSO JUST ADD OR OPERATOR TO ABOVE CONDITIONAL)
7364
else if (service === 'kubernetesmetrics') {
74-
if (healthDataObject) {
75-
setMetricsPool(getMetrics('health', healthDataObject));
76-
} else if (healthMetricsReady) {
77-
setMetricsPool(healthMetrics);
65+
if (eventData) {
66+
setMetricsPool(getMetrics('event', eventData));
67+
} else if (eventMetricsReady) {
68+
setMetricsPool(eventMetrics);
7869
}
7970
} else if (!service.includes('kafkametrics')) {
80-
if (healthDataObject) {
81-
setMetricsPool(getMetrics('health', healthDataObject));
71+
if (healthData) {
72+
setMetricsPool(getMetrics('health', healthData));
8273
} else if (healthMetricsReady) {
8374
setMetricsPool(healthMetrics);
8475
}
8576
} else {
86-
if (healthDataObject && eventDataList && eventDataList.length > 0) {
87-
setMetricsPool(
88-
getMetrics('event', eventDataList).concat(getMetrics('health', healthDataObject))
89-
);
77+
if (healthData && eventData) {
78+
setMetricsPool(getMetrics('event', eventData).concat(getMetrics('health', healthData)));
9079
} else if (healthMetricsReady && eventMetricsReady) {
9180
setMetricsPool(eventMetrics.concat(healthMetrics));
9281
}
9382
}
9483
}, [service, eventData, healthData]);
9584

9685
// responsible for parsing data and updating state via setMetricsPool
97-
const getMetrics = (type, datalist) => {
86+
const getMetrics = (type, dataObject) => {
9887
let pool: any[] = [];
9988
if (type === 'event') {
100-
datalist.forEach(metric => {
101-
const temp = {};
102-
const metricName: string = Object.keys(metric)[0];
103-
const key = 'Event | ' + metricName;
104-
temp['key'] = key;
105-
temp['title'] = key;
106-
temp['tag'] = 'Event';
107-
pool.push(temp);
108-
});
89+
for (const category in dataObject) {
90+
const metricsObjects = dataObject[category];
91+
for (const metricName in metricsObjects) {
92+
const key = category + ' | ' + metricName;
93+
pool.push({
94+
key: key,
95+
title: key,
96+
tag: category,
97+
});
98+
}
99+
}
109100
} else {
110101
// iterate throught the healthData object to populate the `Metrics Query` tab with metrics options
111102
// 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];
103+
for (const service in dataObject) {
104+
const categoryObjects = dataObject[service];
114105
for (const category in categoryObjects) {
115106
const metricsObjects = categoryObjects[category];
116-
for (const metric in metricsObjects) {
117-
const key = category + ' | ' + metric;
107+
for (const metricName in metricsObjects) {
108+
const key = category + ' | ' + metricName;
118109
pool.push({
119110
key: key,
120111
title: key,
@@ -127,17 +118,9 @@ const TransferColumns = React.memo(() => {
127118
return pool;
128119
};
129120

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-
137-
const getCharts = () => {
121+
const createSelectedMetricsList = () => {
138122
const temp: any[] = [];
139123
const categorySet = new Set();
140-
console.log('targetKeys is: ', targetKeys);
141124
for (let i = 0; i < targetKeys.length; i++) {
142125
const str: string = targetKeys[i];
143126
const strArr: string[] = str.split(' | ');
@@ -157,7 +140,6 @@ const TransferColumns = React.memo(() => {
157140
temp.push(newCategory);
158141
}
159142
}
160-
console.log('temp array with requested graphs is: ', temp);
161143
setSelectedMetrics(temp);
162144
};
163145

@@ -181,10 +163,11 @@ const TransferColumns = React.memo(() => {
181163
// makes the rows needed for the selection grid
182164
const rows: any[] = [];
183165
metricsPool.forEach((el, index) => {
184-
const row = {};
185-
row['id'] = index;
186-
row['tag'] = el.tag;
187-
row['title'] = el.title.split(' | ')[1].replace('kubernetes-cadvisor/docker-desktop/', ''); // gets rid of the full path
166+
const row = {
167+
id: index,
168+
tag: el.tag,
169+
title: el.title.split(' | ')[1].replace('kubernetes-cadvisor/docker-desktop/', ''),
170+
}; // gets rid of the full path
188171
rows.push(row);
189172
});
190173

@@ -201,7 +184,12 @@ const TransferColumns = React.memo(() => {
201184
return (
202185
<>
203186
<div id="getChartsContainer">
204-
<Button id="getCharts" onClick={getCharts} variant="contained" color="primary">
187+
<Button
188+
id="getCharts"
189+
onClick={createSelectedMetricsList}
190+
variant="contained"
191+
color="primary"
192+
>
205193
Get Charts
206194
</Button>
207195
</div>

0 commit comments

Comments
 (0)