Skip to content

Commit 07a450a

Browse files
committed
Refactored getPerfMetrics and traverse functions to reduce time complexity and enhance readability
> > Co-authored-by: Nkmai <[email protected]> Co-authored-by: lind-tania <[email protected]> Co-authored-by: rtviner <[email protected]> Co-authored-by: caitlinchan23 <[email protected]>  
1 parent 49dc0ea commit 07a450a

File tree

1 file changed

+44
-96
lines changed

1 file changed

+44
-96
lines changed

src/app/components/PerformanceVisx.tsx

Lines changed: 44 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ interface BarStackProps {
4848
snapshots: [];
4949
hierarchy: any;
5050
}
51-
5251
interface snapshot {
5352
snapshotId?: string;
5453
children: [];
@@ -59,7 +58,6 @@ interface snapshot {
5958

6059
/* DEFAULTS */
6160
const margin = { top: 60, right: 30, bottom: 0, left: 50 };
62-
// const axisColor = '#679DCA';
6361
const axisColor = '#62d6fb';
6462
const background = '#242529';
6563
const tooltipStyles = {
@@ -73,52 +71,43 @@ const tooltipStyles = {
7371
};
7472

7573
/* DATA HANDLING HELPER FUNCTIONS */
76-
77-
const traverse = (snapshot, data = {}) => {
74+
const traverse = (snapshot, data) => {
7875
if (!snapshot.children[0]) return;
76+
77+
// loop through snapshots
7978
snapshot.children.forEach((child, idx) => {
8079
const componentName = child.name + -[idx + 1];
81-
if (!data.hasOwnProperty(componentName)) {
82-
data[componentName] = {};
83-
}
84-
// Get component Type
85-
if (child.state !== 'stateless') data[componentName].componentState = 'stateful';
86-
else data[componentName].componentState = child.state;
80+
8781
// Get component Rendering Time
8882
const renderTime = Number(Number.parseFloat(child.componentData.actualDuration).toPrecision(5));
89-
data[componentName].renderTime = renderTime;
90-
// Get rtid
91-
data[componentName].rtid = child.rtid;
83+
84+
// components as keys and set the value to their rendering time
85+
data['barStack'][data.barStack.length - 1][componentName] = renderTime;
86+
87+
// Get component stateType
88+
if (!data.componentData[componentName]) {
89+
data.componentData[componentName] = {
90+
stateType: 'stateless',
91+
renderFrequency: 0,
92+
totalRenderTime: 0,
93+
rtid: ''
94+
};
95+
if (child.state !== 'stateless') data.componentData[componentName].stateType = 'stateful';
96+
}
97+
// increment render frequencies
98+
if (renderTime > 0) {
99+
data.componentData[componentName].renderFrequency++;
100+
}
101+
102+
// add to total render time
103+
data.componentData[componentName].totalRenderTime += renderTime;
104+
// Get rtid for the hovering feature
105+
data.componentData[componentName].rtid = child.rtid;
92106
traverse(snapshot.children[idx], data);
93107
})
94108
return data;
95109
};
96110

97-
// traverses a snapshot for data: rendering time, component type, or rtid
98-
// const traverse = (snapshot, fetchData, data = {}) => {
99-
// // console.log("data in beg of traverse: ", data )
100-
// if (!snapshot.children[0]) return;
101-
// snapshot.children.forEach((child, idx) => {
102-
// const componentName = child.name + -[idx + 1];
103-
// // Get component Type
104-
// if (fetchData === 'getComponentType') {
105-
// if (child.state !== 'stateless') data[componentName] = 'stateful';
106-
// else data[componentName] = child.state;
107-
// }
108-
// // Get component Rendering Time
109-
// else if (fetchData === 'getRenderTime') {
110-
// const renderTime = Number(Number.parseFloat(child.componentData.actualDuration).toPrecision(5));
111-
// data[componentName] = renderTime;
112-
// }
113-
// else if (fetchData === 'getRtid') {
114-
// data[componentName] = child.rtid;
115-
// }
116-
// traverse(snapshot.children[idx], fetchData, data);
117-
// })
118-
// // console.log("data in end of traverse: ", data )
119-
// return data;
120-
// };
121-
122111
const getSnapshotIds = (obj, snapshotIds = []): string[] => {
123112
snapshotIds.push(`${obj.name}.${obj.branch}`);
124113
if (obj.children) {
@@ -131,58 +120,37 @@ const getSnapshotIds = (obj, snapshotIds = []): string[] => {
131120

132121
// Returns array of snapshot objs each with components and corresponding render times
133122
const getPerfMetrics = (snapshots, snapshotsIds): any[] => {
134-
console.log('snapshots: ', snapshots)
135-
return snapshots.reduce((perfSnapshots, curSnapshot, idx) => {
136-
return perfSnapshots.concat(traverse(curSnapshot, { snapshotId: snapshotsIds[idx] }));
137-
}, []);
123+
const perfData = {
124+
barStack: [],
125+
componentData: {},
126+
};
127+
snapshots.forEach((snapshot, i) => {
128+
perfData.barStack.push({snapshotId: snapshotsIds[i]});
129+
traverse(snapshot, perfData);
130+
});
131+
return perfData;
138132
};
139133

140134
/* EXPORT COMPONENT */
141135
const PerformanceVisx = (props: BarStackProps) => {
142136
// hook used to dispatch onhover action in rect
143137
const [{ tabs, currentTab }, dispatch] = useStoreContext();
144-
145138
const { width, height, snapshots, hierarchy } = props;
146139

147140
const {
148141
tooltipOpen, tooltipLeft, tooltipTop, tooltipData, hideTooltip, showTooltip,
149142
} = useTooltip<TooltipData>();
150143
let tooltipTimeout: number;
151-
console.log('tooltipdata: ', tooltipData)
152144
const { containerRef, TooltipInPortal } = useTooltipInPortal();
153145

154146
// filter and structure incoming data for VISX
155147
const data = getPerfMetrics(snapshots, getSnapshotIds(hierarchy));
156-
console.log('data:', data);
157-
const keys = Object.keys(data[0]).filter(d => d !== 'snapshotId');
158-
// const allComponentStates = traverse(snapshots[0], 'getComponentType');
159-
// const allComponentRtids = traverse(snapshots[snapshots.length - 1], 'getRtid');
160-
// console.log('allComponentRtids: ', allComponentRtids);
161-
// const allComponentRtids = data.filter()
162-
const getBarstackData = (data) => {
163-
const dataArr = [];
164-
data.forEach(snapshot => {
165-
const dataObj = {};
166-
for (let key in snapshot) {
167-
if (key !== 'snapshotId') {
168-
dataObj[key] = snapshot[key]['renderTime'];
169-
} else {
170-
dataObj[key] = snapshot[key];
171-
}
172-
}
173-
dataArr.push(dataObj);
174-
});
175-
return dataArr;
176-
};
177-
const barstackData = getBarstackData(data);
178-
console.log("barstackData : ", barstackData)
148+
const keys = Object.keys(data.componentData);
179149

180150
// create array of total render times for each snapshot
181-
const totalRenderArr = data.reduce((totalRender, curSnapshot) => {
182-
// console.log('curSnapshot: ', curSnapshot);
183-
// console.log('keys: ', keys);
151+
const totalRenderArr = data.barStack.reduce((totalRender, curSnapshot) => {
184152
const curRenderTotal = keys.reduce((acc, cur) => {
185-
acc += Number(curSnapshot[cur].renderTime);
153+
acc += Number(curSnapshot[cur]);
186154
return acc;
187155
}, 0);
188156
totalRender.push(curRenderTotal);
@@ -191,29 +159,12 @@ const PerformanceVisx = (props: BarStackProps) => {
191159

192160
// data accessor (used to generate scales) and formatter (add units for on hover box)
193161
const getSnapshotId = (d: snapshot) => d.snapshotId;
194-
195162
const formatSnapshotId = (id) => `Snapshot ID: ${id}`;
196-
197163
const formatRenderTime = (time) => `${time} ms `;
198164

199-
const getTooltipStates = (data) => {
200-
const snapshotObj = {};
201-
data.forEach(snapshot => {
202-
snapshotObj[snapshot.snapshotId] = {};
203-
for (let key in snapshot) {
204-
if (key !== 'snapshotId') {
205-
snapshotObj[snapshot.snapshotId][key] = snapshot[key].componentState;
206-
}
207-
}
208-
});
209-
return snapshotObj;
210-
}
211-
const tooltipStates = getTooltipStates(data);
212-
console.log('tooltipStates: ', tooltipStates);
213-
214165
// create visualization SCALES with cleaned data
215166
const snapshotIdScale = scaleBand<string>({
216-
domain: data.map(getSnapshotId),
167+
domain: data.barStack.map(getSnapshotId),
217168
padding: 0.2,
218169
});
219170

@@ -260,7 +211,7 @@ const PerformanceVisx = (props: BarStackProps) => {
260211
/>
261212
<Group top={margin.top} left={margin.left}>
262213
<BarStack
263-
data={barstackData}
214+
data={data.barStack}
264215
keys={keys}
265216
x={getSnapshotId}
266217
xScale={snapshotIdScale}
@@ -280,17 +231,14 @@ const PerformanceVisx = (props: BarStackProps) => {
280231
/* TIP TOOL EVENT HANDLERS */
281232
// Hides tool tip once cursor moves off the current rect
282233
onMouseLeave={() => {
283-
console.log('bar: ', bar);
284-
console.log('barstack', barStack);
285-
console.log('onHoverExit arg:', data[data.length-1][bar.key].rtid);
286-
dispatch(onHoverExit(data[data.length-1][bar.key]['rtid']),
234+
dispatch(onHoverExit(data.componentData[bar.key].rtid),
287235
tooltipTimeout = window.setTimeout(() => {
288236
hideTooltip()
289237
}, 300))
290238
}}
291239
// Cursor position in window updates position of the tool tip
292240
onMouseMove={event => {
293-
dispatch(onHover(data[data.length-1][bar.key]['rtid']))
241+
dispatch(onHover(data.componentData[bar.key].rtid))
294242
if (tooltipTimeout) clearTimeout(tooltipTimeout);
295243
const top = event.clientY - margin.top - bar.height;
296244
const left = bar.x + bar.width / 2;
@@ -350,7 +298,7 @@ const PerformanceVisx = (props: BarStackProps) => {
350298
<strong>{tooltipData.key}</strong>
351299
{' '}
352300
</div>
353-
<div>{tooltipStates[tooltipData.bar.data.snapshotId][tooltipData.key]}</div>
301+
<div>{data.componentData[tooltipData.key].stateType}</div>
354302
<div>
355303
{' '}
356304
{formatRenderTime(tooltipData.bar.data[tooltipData.key])}

0 commit comments

Comments
 (0)