Skip to content

Commit 1914cbf

Browse files
(added) algorithm to clean and structure incoming snapshots array
1 parent f09aaf6 commit 1914cbf

File tree

1 file changed

+54
-29
lines changed

1 file changed

+54
-29
lines changed

src/app/components/PerformanceVisx.tsx

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { AxisBottom } from "@visx/axis";
77
import { scaleBand, scaleLinear, scaleOrdinal } from "@visx/scale";
88
import { useTooltip, useTooltipInPortal, defaultStyles } from "@visx/tooltip";
99
import { LegendOrdinal } from "@visx/legend";
10-
import snapshots from "./snapshots";
10+
// import snapshots from "./snapshots";
1111

1212

1313
/* TYPESCRIPT */
@@ -29,6 +29,7 @@ export type BarStackProps = {
2929
height: number;
3030
margin?: { top: number; right: number; bottom: number; left: number };
3131
events?: boolean;
32+
snapshots?: any;
3233
};
3334

3435
/* DEFAULT STYLING */
@@ -45,10 +46,49 @@ const tooltipStyles = {
4546
color: "white"
4647
};
4748

48-
/* DATA PREP */
49-
const data = [...snapshots];
49+
/* DATA PREP FUNCTIONS */
50+
const getPerfMetrics = snapshots => {
51+
return snapshots.reduce((perfSnapshots, curSnapshot,idx)=> {
52+
return perfSnapshots.concat(traverse(curSnapshot, {snapshot:idx+1}))
53+
}, [])
54+
}
55+
56+
const traverse = (snapshot, perfSnapshot = {}) => {
57+
if (!snapshot.children[0]) return;
58+
perfSnapshot[snapshot.name] = snapshot.componentData.actualDuration;
59+
for (let i = 0; i < snapshot.children.length; i++){
60+
perfSnapshot[snapshot.children[i].name+i] = snapshot.children[i].componentData.actualDuration;
61+
traverse(snapshot.children[i], perfSnapshot);
62+
}
63+
return perfSnapshot;
64+
}
65+
66+
67+
/* EXPORT COMPONENT */
68+
export default function PerformanceVisx({
69+
width,
70+
height,
71+
events = false,
72+
margin = defaultMargin,
73+
snapshots
74+
}: BarStackProps)
75+
76+
{
77+
const {
78+
tooltipOpen,
79+
tooltipLeft,
80+
tooltipTop,
81+
tooltipData,
82+
hideTooltip,
83+
showTooltip
84+
} = useTooltip<TooltipData>();
85+
86+
/* DATA PREP */
87+
const data = getPerfMetrics(snapshots);
88+
console.log(data)
5089

51-
// array of all object keys
90+
91+
// array of all object keys
5292
const keys = Object.keys(data[0]).filter((d) => d !== "snapshot") as CityName[];
5393

5494
// ARRAY OF TOTAL VALUES PER SNAPSHOT
@@ -61,15 +101,6 @@ const temperatureTotals = data.reduce((allTotals, currentDate) => {
61101
return allTotals;
62102
}, [] as number[]);
63103

64-
65-
/* ACCESSORS */
66-
const getSnapshot = (d: snapshot) => d.snapshot;
67-
68-
/* SCALE */
69-
const dateScale = scaleBand<string>({
70-
domain: data.map(getSnapshot),
71-
padding: 0.2
72-
});
73104
const temperatureScale = scaleLinear<number>({
74105
domain: [0, Math.max(...temperatureTotals)],
75106
nice: true
@@ -81,21 +112,15 @@ const colorScale = scaleOrdinal<CityName, string>({
81112

82113
let tooltipTimeout: number;
83114

84-
/* EXPORT COMPONENT */
85-
export default function PerformanceVisx({
86-
width,
87-
height,
88-
events = false,
89-
margin = defaultMargin
90-
}: BarStackProps) {
91-
const {
92-
tooltipOpen,
93-
tooltipLeft,
94-
tooltipTop,
95-
tooltipData,
96-
hideTooltip,
97-
showTooltip
98-
} = useTooltip<TooltipData>();
115+
116+
/* ACCESSORS */
117+
const getSnapshot = (d: snapshot) => d.snapshot;
118+
119+
/* SCALE */
120+
const dateScale = scaleBand<string>({
121+
domain: data.map(getSnapshot),
122+
padding: 0.2
123+
});
99124

100125
const { containerRef, TooltipInPortal } = useTooltipInPortal();
101126

@@ -171,7 +196,7 @@ export default function PerformanceVisx({
171196
/>
172197
)),
173198
)
174-
}
199+
}
175200
</BarStack>
176201
</Group>
177202
<AxisBottom

0 commit comments

Comments
 (0)