@@ -48,7 +48,6 @@ interface BarStackProps {
48
48
snapshots : [ ] ;
49
49
hierarchy : any ;
50
50
}
51
-
52
51
interface snapshot {
53
52
snapshotId ?: string ;
54
53
children : [ ] ;
@@ -59,7 +58,6 @@ interface snapshot {
59
58
60
59
/* DEFAULTS */
61
60
const margin = { top : 60 , right : 30 , bottom : 0 , left : 50 } ;
62
- // const axisColor = '#679DCA';
63
61
const axisColor = '#62d6fb' ;
64
62
const background = '#242529' ;
65
63
const tooltipStyles = {
@@ -73,52 +71,43 @@ const tooltipStyles = {
73
71
} ;
74
72
75
73
/* DATA HANDLING HELPER FUNCTIONS */
76
-
77
- const traverse = ( snapshot , data = { } ) => {
74
+ const traverse = ( snapshot , data ) => {
78
75
if ( ! snapshot . children [ 0 ] ) return ;
76
+
77
+ // loop through snapshots
79
78
snapshot . children . forEach ( ( child , idx ) => {
80
79
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
+
87
81
// Get component Rendering Time
88
82
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 ;
92
106
traverse ( snapshot . children [ idx ] , data ) ;
93
107
} )
94
108
return data ;
95
109
} ;
96
110
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
111
const getSnapshotIds = ( obj , snapshotIds = [ ] ) : string [ ] => {
123
112
snapshotIds . push ( `${ obj . name } .${ obj . branch } ` ) ;
124
113
if ( obj . children ) {
@@ -131,58 +120,37 @@ const getSnapshotIds = (obj, snapshotIds = []): string[] => {
131
120
132
121
// Returns array of snapshot objs each with components and corresponding render times
133
122
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 ;
138
132
} ;
139
133
140
134
/* EXPORT COMPONENT */
141
135
const PerformanceVisx = ( props : BarStackProps ) => {
142
136
// hook used to dispatch onhover action in rect
143
137
const [ { tabs, currentTab } , dispatch ] = useStoreContext ( ) ;
144
-
145
138
const { width, height, snapshots, hierarchy } = props ;
146
139
147
140
const {
148
141
tooltipOpen, tooltipLeft, tooltipTop, tooltipData, hideTooltip, showTooltip,
149
142
} = useTooltip < TooltipData > ( ) ;
150
143
let tooltipTimeout : number ;
151
- console . log ( 'tooltipdata: ' , tooltipData )
152
144
const { containerRef, TooltipInPortal } = useTooltipInPortal ( ) ;
153
145
154
146
// filter and structure incoming data for VISX
155
147
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 ) ;
179
149
180
150
// 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 ) => {
184
152
const curRenderTotal = keys . reduce ( ( acc , cur ) => {
185
- acc += Number ( curSnapshot [ cur ] . renderTime ) ;
153
+ acc += Number ( curSnapshot [ cur ] ) ;
186
154
return acc ;
187
155
} , 0 ) ;
188
156
totalRender . push ( curRenderTotal ) ;
@@ -191,29 +159,12 @@ const PerformanceVisx = (props: BarStackProps) => {
191
159
192
160
// data accessor (used to generate scales) and formatter (add units for on hover box)
193
161
const getSnapshotId = ( d : snapshot ) => d . snapshotId ;
194
-
195
162
const formatSnapshotId = ( id ) => `Snapshot ID: ${ id } ` ;
196
-
197
163
const formatRenderTime = ( time ) => `${ time } ms ` ;
198
164
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
-
214
165
// create visualization SCALES with cleaned data
215
166
const snapshotIdScale = scaleBand < string > ( {
216
- domain : data . map ( getSnapshotId ) ,
167
+ domain : data . barStack . map ( getSnapshotId ) ,
217
168
padding : 0.2 ,
218
169
} ) ;
219
170
@@ -260,7 +211,7 @@ const PerformanceVisx = (props: BarStackProps) => {
260
211
/>
261
212
< Group top = { margin . top } left = { margin . left } >
262
213
< BarStack
263
- data = { barstackData }
214
+ data = { data . barStack }
264
215
keys = { keys }
265
216
x = { getSnapshotId }
266
217
xScale = { snapshotIdScale }
@@ -280,17 +231,14 @@ const PerformanceVisx = (props: BarStackProps) => {
280
231
/* TIP TOOL EVENT HANDLERS */
281
232
// Hides tool tip once cursor moves off the current rect
282
233
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 ) ,
287
235
tooltipTimeout = window . setTimeout ( ( ) => {
288
236
hideTooltip ( )
289
237
} , 300 ) )
290
238
} }
291
239
// Cursor position in window updates position of the tool tip
292
240
onMouseMove = { event => {
293
- dispatch ( onHover ( data [ data . length - 1 ] [ bar . key ] [ ' rtid' ] ) )
241
+ dispatch ( onHover ( data . componentData [ bar . key ] . rtid ) )
294
242
if ( tooltipTimeout ) clearTimeout ( tooltipTimeout ) ;
295
243
const top = event . clientY - margin . top - bar . height ;
296
244
const left = bar . x + bar . width / 2 ;
@@ -350,7 +298,7 @@ const PerformanceVisx = (props: BarStackProps) => {
350
298
< strong > { tooltipData . key } </ strong >
351
299
{ ' ' }
352
300
</ div >
353
- < div > { tooltipStates [ tooltipData . bar . data . snapshotId ] [ tooltipData . key ] } </ div >
301
+ < div > { data . componentData [ tooltipData . key ] . stateType } </ div >
354
302
< div >
355
303
{ ' ' }
356
304
{ formatRenderTime ( tooltipData . bar . data [ tooltipData . key ] ) }
0 commit comments