@@ -12,14 +12,11 @@ import '../ui/primer.dart';
1212import '../utils.dart' ;
1313import 'cpu_bottom_up.dart' ;
1414import 'cpu_call_tree.dart' ;
15- import 'cpu_flame_chart.dart' ;
1615import 'cpu_profile_protocol.dart' ;
16+ import 'flame_chart_canvas.dart' ;
1717import 'frame_flame_chart.dart' ;
1818import 'timeline_protocol.dart' ;
1919
20- // TODO(kenzie): this should be removed once the cpu flame chart is optimized.
21- const bool showCpuFlameChart = false ;
22-
2320class EventDetails extends CoreElement {
2421 EventDetails () : super ('div' ) {
2522 flex ();
@@ -146,10 +143,9 @@ class _UiEventDetails extends CoreElement {
146143 layoutVertical ();
147144 flex ();
148145
149- if (showCpuFlameChart) {
150- flameChart = CpuFlameChart ();
151- } else {
152- flameChart = div (c: 'ui-details-section' );
146+ flameChart = div (c: 'ui-details-section' );
147+
148+ if (! showCpuFlameChart) {
153149 flameChart.add (div (text: 'CPU flame chart coming soon' , c: 'message' ));
154150 }
155151
@@ -163,8 +159,6 @@ class _UiEventDetails extends CoreElement {
163159 if (showCpuFlameChart) {
164160 add (stackFrameDetails);
165161 }
166-
167- onSelectedCpuFlameChartItem.listen (updateStackFrameDetails);
168162 }
169163
170164 static const String stackFrameDetailsDefaultText = '[No function selected]' ;
@@ -174,15 +168,20 @@ class _UiEventDetails extends CoreElement {
174168 CpuCallTree callTree;
175169 CoreElement stackFrameDetails;
176170
171+ EventDetailsTabType selectedTab = EventDetailsTabType .flameChart;
172+
173+ bool showingFlameChartError = false ;
174+
177175 TimelineEvent event;
178176
179177 CpuProfileData cpuProfileData;
180178
181179 void showTab (EventDetailsTabType tabType) {
180+ selectedTab = tabType;
182181 switch (tabType) {
183182 case EventDetailsTabType .flameChart:
184183 flameChart.attribute ('hidden' , false );
185- stackFrameDetails.attribute ('hidden' , false );
184+ stackFrameDetails.attribute ('hidden' , showingFlameChartError );
186185 bottomUp.attribute ('hidden' , true );
187186 callTree.attribute ('hidden' , true );
188187 break ;
@@ -201,6 +200,45 @@ class _UiEventDetails extends CoreElement {
201200 }
202201 }
203202
203+ Future <void > _drawFlameChart () async {
204+ final Response response =
205+ await serviceManager.service.getCpuProfileTimeline (
206+ serviceManager.isolateManager.selectedIsolate.id,
207+ event.startTime,
208+ event.duration,
209+ );
210+
211+ cpuProfileData = CpuProfileData (response);
212+
213+ if (cpuProfileData.stackFrames.isEmpty) {
214+ _updateFlameChartForError (div (
215+ text: 'CPU profile unavailable for time range'
216+ ' [${event .startTime } - ${event .endTime }]' ,
217+ c: 'message' ,
218+ ));
219+ return ;
220+ }
221+
222+ final flameChartCanvas = FlameChartCanvas (
223+ data: cpuProfileData,
224+ flameChartWidth: flameChart.element.clientWidth,
225+ flameChartHeight:
226+ cpuProfileData.cpuProfileRoot.depth * rowHeightWithPadding,
227+ );
228+
229+ flameChartCanvas.onStackFrameSelected.listen ((CpuStackFrame stackFrame) {
230+ _updateStackFrameDetails (stackFrame);
231+ });
232+
233+ flameChart.add (flameChartCanvas.element);
234+ }
235+
236+ void _updateFlameChartForError (CoreElement errorDiv) {
237+ flameChart.add (errorDiv);
238+ showingFlameChartError = true ;
239+ stackFrameDetails.attribute ('hidden' , true );
240+ }
241+
204242 Future <void > update (TimelineEvent event) async {
205243 if (event == this .event) {
206244 return ;
@@ -212,17 +250,11 @@ class _UiEventDetails extends CoreElement {
212250 final Spinner spinner = Spinner ()..clazz ('cpu-profile-spinner' );
213251 add (spinner);
214252
215- final Response response =
216- await serviceManager.service.getCpuProfileTimeline (
217- serviceManager.isolateManager.selectedIsolate.id,
218- event.startTime,
219- event.duration,
220- );
221-
222- cpuProfileData = CpuProfileData (response);
223-
224- if (showCpuFlameChart) {
225- (flameChart as CpuFlameChart ).update (cpuProfileData);
253+ try {
254+ await _drawFlameChart ();
255+ } catch (e) {
256+ _updateFlameChartForError (div (
257+ text: 'Error retrieving CPU profile: ${e .toString ()}' , c: 'message' ));
226258 }
227259
228260 spinner.element.remove ();
@@ -233,11 +265,14 @@ class _UiEventDetails extends CoreElement {
233265 void reset () {
234266 flameChart.clear ();
235267 stackFrameDetails.clear ();
268+ showingFlameChartError = false ;
269+ stackFrameDetails.attribute (
270+ 'hidden' , selectedTab != EventDetailsTabType .flameChart);
236271 cpuProfileData = null ;
237272 }
238273
239- void updateStackFrameDetails ( CpuFlameChartItem item ) {
240- stackFrameDetails.text = item. stackFrame.toString ();
274+ void _updateStackFrameDetails ( CpuStackFrame stackFrame ) {
275+ stackFrameDetails.text = stackFrame.toString ();
241276 }
242277}
243278
0 commit comments