Skip to content

Commit b4c8c6a

Browse files
Merge pull request #5 from oslabs-beta/reactime7
Refactored Performance Barstack data
2 parents 625a64a + ee609c3 commit b4c8c6a

File tree

1 file changed

+95
-36
lines changed

1 file changed

+95
-36
lines changed

src/app/components/PerformanceVisx.tsx

Lines changed: 95 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -74,32 +74,52 @@ const tooltipStyles = {
7474

7575
/* DATA HANDLING HELPER FUNCTIONS */
7676

77-
// traverses a snapshot for data: rendering time, component type, or rtid
78-
const traverse = (snapshot, fetchData, data = {}) => {
79-
console.log("data in beg of traverse: ", data )
77+
const traverse = (snapshot, data = {}) => {
8078
if (!snapshot.children[0]) return;
8179
snapshot.children.forEach((child, idx) => {
8280
const componentName = child.name + -[idx + 1];
83-
// Get component Type
84-
if (fetchData === 'getComponentType') {
85-
if (child.state !== 'stateless') data[componentName] = 'stateful';
86-
else data[componentName] = child.state;
81+
if (!data.hasOwnProperty(componentName)) {
82+
data[componentName] = {};
8783
}
84+
// Get component Type
85+
if (child.state !== 'stateless') data[componentName].componentState = 'stateful';
86+
else data[componentName].componentState = child.state;
8887
// Get component Rendering Time
89-
else if (fetchData === 'getRenderTime') {
90-
const renderTime = Number(Number.parseFloat(child.componentData.actualDuration).toPrecision(5));
91-
data[componentName] = renderTime;
92-
}
93-
else if (fetchData === 'getRtid') {
94-
data[componentName] = child.rtid;
95-
}
96-
traverse(snapshot.children[idx], fetchData, data);
88+
const renderTime = Number(Number.parseFloat(child.componentData.actualDuration).toPrecision(5));
89+
data[componentName].renderTime = renderTime;
90+
// Get rtid
91+
data[componentName].rtid = child.rtid;
92+
traverse(snapshot.children[idx], data);
9793
})
98-
console.log("data in end of traverse: ", data )
9994
return data;
10095
};
10196

102-
const getSnapshotIds = (obj, snapshotIds = []):string[] => {
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+
122+
const getSnapshotIds = (obj, snapshotIds = []): string[] => {
103123
snapshotIds.push(`${obj.name}.${obj.branch}`);
104124
if (obj.children) {
105125
obj.children.forEach(child => {
@@ -110,15 +130,15 @@ const getSnapshotIds = (obj, snapshotIds = []):string[] => {
110130
};
111131

112132
// Returns array of snapshot objs each with components and corresponding render times
113-
const getPerfMetrics = (snapshots, snapshotsIds):any[] => {
133+
const getPerfMetrics = (snapshots, snapshotsIds): any[] => {
134+
console.log('snapshots: ', snapshots)
114135
return snapshots.reduce((perfSnapshots, curSnapshot, idx) => {
115-
return perfSnapshots.concat(traverse(curSnapshot, 'getRenderTime', { snapshotId: snapshotsIds[idx] }));
136+
return perfSnapshots.concat(traverse(curSnapshot, { snapshotId: snapshotsIds[idx] }));
116137
}, []);
117138
};
118139

119140
/* EXPORT COMPONENT */
120141
const PerformanceVisx = (props: BarStackProps) => {
121-
122142
// hook used to dispatch onhover action in rect
123143
const [{ tabs, currentTab }, dispatch] = useStoreContext();
124144

@@ -128,19 +148,40 @@ const PerformanceVisx = (props: BarStackProps) => {
128148
tooltipOpen, tooltipLeft, tooltipTop, tooltipData, hideTooltip, showTooltip,
129149
} = useTooltip<TooltipData>();
130150
let tooltipTimeout: number;
131-
151+
console.log('tooltipdata: ', tooltipData)
132152
const { containerRef, TooltipInPortal } = useTooltipInPortal();
133153

134154
// filter and structure incoming data for VISX
135155
const data = getPerfMetrics(snapshots, getSnapshotIds(hierarchy));
156+
console.log('data:', data);
136157
const keys = Object.keys(data[0]).filter(d => d !== 'snapshotId');
137-
const allComponentStates = traverse(snapshots[0], 'getComponentType');
138-
const allComponentRtids = traverse(snapshots[snapshots.length-1], 'getRtid');
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);
139178

140179
// create array of total render times for each snapshot
141180
const totalRenderArr = data.reduce((totalRender, curSnapshot) => {
181+
// console.log('curSnapshot: ', curSnapshot);
182+
// console.log('keys: ', keys);
142183
const curRenderTotal = keys.reduce((acc, cur) => {
143-
acc += Number(curSnapshot[cur]);
184+
acc += Number(curSnapshot[cur].renderTime);
144185
return acc;
145186
}, 0);
146187
totalRender.push(curRenderTotal);
@@ -149,11 +190,26 @@ const PerformanceVisx = (props: BarStackProps) => {
149190

150191
// data accessor (used to generate scales) and formatter (add units for on hover box)
151192
const getSnapshotId = (d: snapshot) => d.snapshotId;
152-
193+
153194
const formatSnapshotId = (id) => `Snapshot ID: ${id}`;
154-
195+
155196
const formatRenderTime = (time) => `${time} ms `;
156197

198+
const getTooltipStates = (data) => {
199+
const snapshotObj = {};
200+
data.forEach(snapshot => {
201+
snapshotObj[snapshot.snapshotId] = {};
202+
for (let key in snapshot) {
203+
if (key !== 'snapshotId') {
204+
snapshotObj[snapshot.snapshotId][key] = snapshot[key].componentState;
205+
}
206+
}
207+
});
208+
return snapshotObj;
209+
}
210+
const tooltipStates = getTooltipStates(data);
211+
console.log('tooltipStates: ', tooltipStates);
212+
157213
// create visualization SCALES with cleaned data
158214
const snapshotIdScale = scaleBand<string>({
159215
domain: data.map(getSnapshotId),
@@ -203,15 +259,15 @@ const PerformanceVisx = (props: BarStackProps) => {
203259
/>
204260
<Group top={margin.top} left={margin.left}>
205261
<BarStack
206-
data={data}
262+
data={barstackData}
207263
keys={keys}
208264
x={getSnapshotId}
209265
xScale={snapshotIdScale}
210266
yScale={renderingScale}
211267
color={colorScale}
212268
>
213-
{barStacks =>
214-
barStacks.map(barStack =>
269+
{barStacks =>
270+
barStacks.map(barStack =>
215271
barStack.bars.map(((bar, idx) => (
216272
<rect
217273
key={`bar-stack-${barStack.index}-${bar.index}`}
@@ -223,14 +279,17 @@ const PerformanceVisx = (props: BarStackProps) => {
223279
/* TIP TOOL EVENT HANDLERS */
224280
// Hides tool tip once cursor moves off the current rect
225281
onMouseLeave={() => {
226-
dispatch(onHoverExit(allComponentRtids[bar.key]),
227-
tooltipTimeout = window.setTimeout(() => {
228-
hideTooltip()
229-
}, 300))
282+
console.log('bar: ', bar);
283+
console.log('barstack', barStack);
284+
console.log('onHoverExit arg:', data[data.length-1][bar.key].rtid);
285+
dispatch(onHoverExit(data[data.length-1][bar.key]['rtid']),
286+
tooltipTimeout = window.setTimeout(() => {
287+
hideTooltip()
288+
}, 300))
230289
}}
231-
// Cursor position in window updates position of the tool tip
290+
// Cursor position in window updates position of the tool tip
232291
onMouseMove={event => {
233-
dispatch(onHover(allComponentRtids[bar.key]))
292+
dispatch(onHover(data[data.length-1][bar.key]['rtid']))
234293
if (tooltipTimeout) clearTimeout(tooltipTimeout);
235294
const top = event.clientY - margin.top - bar.height;
236295
const left = bar.x + bar.width / 2;
@@ -290,7 +349,7 @@ const PerformanceVisx = (props: BarStackProps) => {
290349
<strong>{tooltipData.key}</strong>
291350
{' '}
292351
</div>
293-
<div>{allComponentStates[tooltipData.key]}</div>
352+
<div>{tooltipStates[tooltipData.bar.data.snapshotId][tooltipData.key]}</div>
294353
<div>
295354
{' '}
296355
{formatRenderTime(tooltipData.bar.data[tooltipData.key])}

0 commit comments

Comments
 (0)