Skip to content

Commit 8927ab0

Browse files
committed
Finished event chart render logic.
Added more type interfaces.
1 parent 5332403 commit 8927ab0

File tree

7 files changed

+64
-51
lines changed

7 files changed

+64
-51
lines changed

app/charts/EventChart.tsx

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,33 @@ import React, { useState } from 'react';
33
import Plot from 'react-plotly.js';
44
import { all, solo as soloStyle } from './sizeSwitch';
55

6-
interface ChartData {
7-
value: string[];
8-
time: string[];
9-
}
10-
116
interface EventChartProps {
127
key: string;
138
metricName: string;
14-
chartData: ChartData
9+
chartData: {
10+
value: string[],
11+
time: string[]
12+
}
13+
sizing: string;
14+
colourGenerator: Function;
1515
}
1616

1717
interface SoloStyles {
1818
height: number;
1919
width: number;
2020
}
2121

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

2635
setInterval(() => {
@@ -29,36 +38,39 @@ const EventChart: React.FC<EventChartProps> = React.memo(props => {
2938
}
3039
}, 20);
3140

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+
3252
const createChart = () => {
33-
const timeArr = timeList.map((el: any) => moment(el).format('kk:mm:ss'));
34-
const reverseTimeArr = timeArr.reverse()
35-
const hashedColour = colourGenerator(metric);
36-
const newMetricName = metric.replace("kubernetes-cadvisor/docker-desktop/", ""); // this will get rid of the long path
37-
const re = /_/g;
38-
let plotlyData: {
39-
name: any;
40-
x: any;
41-
y: any;
42-
type: any;
43-
mode: any;
44-
marker: { color: string };
45-
};
46-
plotlyData = {
47-
name: metric,
48-
x: reverseTimeArr,
49-
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,
5060
type: 'scattergl',
5161
mode: 'lines',
52-
marker: { color: hashedColour },
62+
marker: {
63+
colors: ['#fc4039', '#4b54ea', '#32b44f', '#3788fc', '#9c27b0', '#febc2c'],
64+
},
5365
};
5466
const sizeSwitch = sizing === 'all' ? all : solo;
5567

5668
return (
5769
<Plot
58-
data={[plotlyData]}
59-
config={{ displayModeBar: false }}
70+
data={[plotlyDataObject]}
71+
config={{ displayModeBar: true }}
6072
layout={{
61-
title: newMetricName.replace(re," "), // this will reaplce all the underscores in the graph titlke,
73+
title: prettyName,
6274
...sizeSwitch,
6375
font: {
6476
color: '#444d56',
@@ -69,10 +81,7 @@ const EventChart: React.FC<EventChartProps> = React.memo(props => {
6981
plot_bgcolor: 'white',
7082
showlegend: true,
7183
legend: {
72-
orientation: 'h',
73-
xanchor: 'center',
74-
x: 0.5,
75-
y: 5,
84+
orientation: 'v',
7685
},
7786
xaxis: {
7887
title: 'Time',
@@ -86,7 +95,7 @@ const EventChart: React.FC<EventChartProps> = React.memo(props => {
8695
},
8796
yaxis: {
8897
rangemode: 'nonnegative',
89-
title: metric,
98+
title: prettyName,
9099
},
91100
}}
92101
/>

app/charts/HealthChart.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
3535
};
3636

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

4242
// pulls the current service names to be shown in the graph title from chartData
@@ -118,7 +118,7 @@ const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
118118
},
119119
yaxis: {
120120
rangemode: 'nonnegative',
121-
title: `${dataType}`,
121+
title: dataType,
122122
},
123123
}}
124124
/>

app/components/TransferColumns.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,6 @@ const TransferColumns = React.memo(() => {
118118
return pool;
119119
};
120120

121-
// Justin's alternative idea to getCharts (b/c getCharts is just saving the user-selected metrics into QueryContext)
122-
// 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
123-
// the selectedMetrics from QueryContext is used in TransferColumns, EventContainer, GraphsContainer, and HealthContainer
124-
// const saveSelectedMetrics = () => {
125-
// // iterate over the targetKeys array
126-
// }
127-
128121
const createSelectedMetricsList = () => {
129122
const temp: any[] = [];
130123
const categorySet = new Set();
@@ -214,7 +207,6 @@ const TransferColumns = React.memo(() => {
214207
metricIndeces.forEach(el => {
215208
metrics.push(metricsPool[el].key);
216209
});
217-
console.log('targetKeys is set to: ', metrics);
218210
setTargetKeys(metrics);
219211
}}
220212
/>

app/containers/EventContainer.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,23 @@ interface EventContainerProps {
1212
interface Params {
1313
service: string;
1414
}
15+
interface MetricObject {
16+
[key: string]: {
17+
value: string[],
18+
time: string[]
19+
}
20+
21+
}
22+
23+
interface EventDataObject {
24+
[key: string]: MetricObject
25+
}
1526

1627
const EventContainer: React.FC<EventContainerProps> = React.memo(props => {
1728
const { eventData } = useContext(EventContext);
1829
const { selectedMetrics } = useContext(QueryContext);
1930
const { service } = useParams<keyof Params>() as Params;
31+
const { sizing, colourGenerator } = props;
2032
// eventChartsArr contains all charts of all metrics
2133
const [eventChartsArr, setEventChartsArr] = useState<JSX.Element[]>([]);
2234

@@ -28,7 +40,7 @@ const EventContainer: React.FC<EventContainerProps> = React.memo(props => {
2840
to an instance of Grafana, and integrate Grafana's dashboard into Chronos to visualize the data.
2941
*/
3042

31-
const filterSelectedEventMetricsandData = eventDataObj => {
43+
const filterSelectedEventMetricsandData = (eventDataObj: EventDataObject): EventDataObject => {
3244
const filteredEventData = {};
3345
// there's only one element in the selected metrics array for now...
3446
// selectedMetrics is... [{Event: ['metric', 'names', 'as', 'strings']}]
@@ -51,7 +63,7 @@ const EventContainer: React.FC<EventContainerProps> = React.memo(props => {
5163
};
5264

5365
// iterate over the filtered event data to build an array of charts, then set the event charts array state
54-
const generateEventCharts = filteredEventDataObj => {
66+
const generateEventCharts = (filteredEventDataObj: EventDataObject): void => {
5567
const chartsArray: JSX.Element[] = [];
5668
const keymaker = () => {
5769
return Math.floor(Math.random() * 1000);
@@ -65,6 +77,8 @@ const EventContainer: React.FC<EventContainerProps> = React.memo(props => {
6577
key={'E' + keymaker()}
6678
metricName={metricName}
6779
chartData={chartData}
80+
sizing={sizing}
81+
colourGenerator={colourGenerator}
6882
/>
6983
);
7084
}

app/containers/GraphsContainer.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ const GraphsContainer: React.FC = React.memo(props => {
146146
};
147147

148148
const HealthAndEventButtons: JSX.Element[] = getHealthAndEventComponents();
149-
console.log('selected metrics: ', selectedMetrics);
150149

151150
return (
152151
<>

app/containers/HealthContainer.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ const HealthContainer: React.FC<HealthContainerProps> = React.memo(props => {
3030
*/
3131
const { healthData } = useContext(HealthContext);
3232
const { selectedMetrics } = useContext(QueryContext);
33-
const { category } = props;
3433
const { service } = useParams<keyof Params>() as Params;
3534
const [healthChartsArr, setHealthChartsArr] = useState<JSX.Element[]>([]);
35+
const { category, sizing, colourGenerator } = props;
3636
/*
3737
This function filters the selectedMetrics array down to only metrics that match the category of this instance of HealthContainer.
3838
Once that has finished, it then filters the healthData down to the current category and the filteredMetrics.
@@ -160,8 +160,8 @@ const HealthContainer: React.FC<HealthContainerProps> = React.memo(props => {
160160
serviceName={serviceName}
161161
chartData={chartData}
162162
categoryName={category}
163-
sizing={props.sizing}
164-
colourGenerator={props.colourGenerator}
163+
sizing={sizing}
164+
colourGenerator={colourGenerator}
165165
/>
166166
);
167167
}

app/context/CommsContext.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ const CommsContextProvider: React.FC<Props> = React.memo((props) => {
5252
ipcRenderer.on('commsResponse', (event: Electron.Event, data: any) => {
5353
let result: any;
5454
if (tryParseJSON(data)) result = JSON.parse(data);
55-
console.log('communication data from fetch request is: ', result);
5655
// console.log(data);
5756
setCommsData(result);
5857
});

0 commit comments

Comments
 (0)