| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'dart:async'; |
| 6 import 'dart:html'; |
| 7 import 'package:observatory/models.dart' as M; |
| 8 import 'package:observatory/src/elements/helpers/rendering_scheduler.dart'; |
| 9 import 'package:observatory/src/elements/helpers/tag.dart'; |
| 10 import 'package:observatory/src/elements/helpers/uris.dart'; |
| 11 import 'package:observatory/utils.dart'; |
| 12 |
| 13 class SampleBufferControlChangedElement { |
| 14 final SampleBufferControlElement element; |
| 15 SampleBufferControlChangedElement(this.element); |
| 16 } |
| 17 |
| 18 class SampleBufferControlElement extends HtmlElement implements Renderable { |
| 19 static const tag = |
| 20 const Tag<SampleBufferControlElement>('sample-buffer-control'); |
| 21 |
| 22 RenderingScheduler<SampleBufferControlElement> _r; |
| 23 |
| 24 Stream<RenderedEvent<SampleBufferControlElement>> get onRendered => |
| 25 _r.onRendered; |
| 26 |
| 27 StreamController<SampleBufferControlChangedElement> _onTagChange = |
| 28 new StreamController<SampleBufferControlChangedElement>.broadcast(); |
| 29 Stream<SampleBufferControlChangedElement> get onTagChange => |
| 30 _onTagChange.stream; |
| 31 |
| 32 M.SampleProfileLoadingProgress _progress; |
| 33 M.SampleProfileTag _tag; |
| 34 bool _showTag = false; |
| 35 bool _profileVM = false; |
| 36 StreamSubscription _subscription; |
| 37 |
| 38 M.SampleProfileLoadingProgress get progress => _progress; |
| 39 M.SampleProfileTag get selectedTag => _tag; |
| 40 bool get showTag => _showTag; |
| 41 bool get profileVM => _profileVM; |
| 42 |
| 43 set selectedTag(M.SampleProfileTag value) => |
| 44 _tag = _r.checkAndReact(_tag, value); |
| 45 set showTag(bool value) => _showTag = _r.checkAndReact(_showTag, value); |
| 46 set profileVM(bool value) => _profileVM = _r.checkAndReact(_profileVM, value); |
| 47 |
| 48 |
| 49 factory SampleBufferControlElement(M.SampleProfileLoadingProgress progress, |
| 50 {M.SampleProfileTag selectedTag: M.SampleProfileTag.None, |
| 51 bool showTag: true, RenderingQueue queue}) { |
| 52 assert(progress != null); |
| 53 assert(selectedTag != null); |
| 54 assert(showTag != null); |
| 55 SampleBufferControlElement e = document.createElement(tag.name); |
| 56 e._r = new RenderingScheduler(e, queue: queue); |
| 57 e._progress = progress; |
| 58 e._tag = selectedTag; |
| 59 e._showTag = showTag; |
| 60 return e; |
| 61 } |
| 62 |
| 63 SampleBufferControlElement.created() : super.created(); |
| 64 |
| 65 @override |
| 66 void attached() { |
| 67 super.attached(); |
| 68 _r.enable(); |
| 69 _subscription = _progress.onProgress.listen((_) => _r.dirty()); |
| 70 } |
| 71 |
| 72 @override |
| 73 void detached() { |
| 74 super.detached(); _r.disable(notify: true); |
| 75 children = const []; |
| 76 _subscription.cancel(); |
| 77 } |
| 78 |
| 79 void render() { |
| 80 var content = <Element>[ |
| 81 new HeadingElement.h2()..text = 'Sample buffer', |
| 82 new HRElement() |
| 83 ]; |
| 84 switch (_progress.status) { |
| 85 case M.SampleProfileLoadingStatus.Fetching : |
| 86 content.addAll(_createStatusMessage('Fetching profile from VM...')); |
| 87 break; |
| 88 case M.SampleProfileLoadingStatus.Loading : |
| 89 content.addAll(_createStatusMessage('Loading profile...', |
| 90 progress: _progress.progress)); |
| 91 break; |
| 92 case M.SampleProfileLoadingStatus.Disabled : |
| 93 content.addAll(_createDisabledMessage()); |
| 94 break; |
| 95 case M.SampleProfileLoadingStatus.Loaded: |
| 96 content.addAll(_createStatusReport()); |
| 97 break; |
| 98 } |
| 99 children = [ |
| 100 new DivElement()..classes = ['content-centered-big'] |
| 101 ..children = content |
| 102 ]; |
| 103 } |
| 104 |
| 105 static List<Element> _createStatusMessage(String message, |
| 106 {double progress: 0.0}) { |
| 107 return [new DivElement()..classes = ['statusBox', 'shadow', 'center'] |
| 108 ..children = [ |
| 109 new DivElement()..classes = ['statusMessage'] |
| 110 ..text = message, |
| 111 new DivElement()..style.background = '#0489c3' |
| 112 ..style.width = '$progress%' |
| 113 ..style.height = '15px' |
| 114 ..style.borderRadius = '4px' |
| 115 ] |
| 116 ]; |
| 117 } |
| 118 |
| 119 static List<Element> _createDisabledMessage() { |
| 120 return [new DivElement()..classes = ['statusBox' 'shadow' 'center'] |
| 121 ..children = [ |
| 122 new DivElement() |
| 123 ..children = [ |
| 124 new HeadingElement.h1() |
| 125 ..text = 'Profiling is disabled', |
| 126 new BRElement(), |
| 127 new DivElement() |
| 128 ..innerHtml = 'Perhaps the <b>profile</b> ' |
| 129 'flag has been disabled for this VM.', |
| 130 new BRElement(), |
| 131 new SpanElement()..text = 'See all', |
| 132 new AnchorElement(href: Uris.flags())..text = 'vm flags' |
| 133 ] |
| 134 ] |
| 135 ]; |
| 136 } |
| 137 |
| 138 List<Element> _createStatusReport() { |
| 139 final fetchT = Utils.formatDurationInSeconds(_progress.fetchingTime); |
| 140 final loadT = Utils.formatDurationInSeconds(_progress.loadingTime); |
| 141 final sampleCount = _progress.profile.sampleCount; |
| 142 final refreshT = new DateTime.now(); |
| 143 final stackDepth = _progress.profile.stackDepth; |
| 144 final sampleRate = _progress.profile.sampleRate.toStringAsFixed(0); |
| 145 final timeSpan = _progress.profile.sampleCount == 0 ? '0s' |
| 146 : Utils.formatTimePrecise(_progress.profile.timeSpan); |
| 147 |
| 148 var content = <Element>[ |
| 149 new DivElement()..classes = ['memberItem'] |
| 150 ..children = [ |
| 151 new DivElement()..classes = ['memberName'] |
| 152 ..text = 'Refreshed at', |
| 153 new DivElement()..classes = ['memberValue'] |
| 154 ..text = '$refreshT (fetched in ${fetchT}s) (loaded in ${loadT}s)' |
| 155 ], |
| 156 new DivElement()..classes = ['memberItem'] |
| 157 ..children = [ |
| 158 new DivElement()..classes = ['memberName'] |
| 159 ..text = 'Profile contains ', |
| 160 new DivElement()..classes = ['memberValue'] |
| 161 ..text = '$sampleCount samples (spanning $timeSpan)' |
| 162 ], |
| 163 new DivElement()..classes = ['memberItem'] |
| 164 ..children = [ |
| 165 new DivElement()..classes = ['memberName'] |
| 166 ..text = 'Sampling', |
| 167 new DivElement()..classes = ['memberValue'] |
| 168 ..text = '$stackDepth stack frames @ ${sampleRate}Hz' |
| 169 ], |
| 170 ]; |
| 171 if (_showTag) { |
| 172 content.add( |
| 173 new DivElement()..classes = ['memberItem'] |
| 174 ..children = [ |
| 175 new DivElement()..classes = ['memberName'] |
| 176 ..text = 'Tag Order', |
| 177 new DivElement()..classes = ['memberValue'] |
| 178 ..children = _createTagSelect() |
| 179 ] |
| 180 ); |
| 181 } |
| 182 return [ |
| 183 new DivElement()..classes = ['memberList'] |
| 184 ..children = content |
| 185 ]; |
| 186 } |
| 187 |
| 188 List<Element> _createTagSelect() { |
| 189 var values = M.SampleProfileTag.values; |
| 190 if (!_profileVM) { |
| 191 values = const [M.SampleProfileTag.UserOnly, M.SampleProfileTag.None]; |
| 192 } |
| 193 var s; |
| 194 return [ |
| 195 s = new SelectElement()..value = tagToString(_tag) |
| 196 ..children = values.map((tag) { |
| 197 return new OptionElement(value : tagToString(tag), |
| 198 selected: _tag == tag) |
| 199 ..text = tagToString(tag); |
| 200 }).toList(growable: false) |
| 201 ..onChange.listen((_) { |
| 202 _tag = values[s.selectedIndex]; |
| 203 }) |
| 204 ..onChange.map(_toEvent).listen(_triggerModeChange), |
| 205 ]; |
| 206 } |
| 207 |
| 208 static String tagToString(M.SampleProfileTag tag) { |
| 209 switch (tag) { |
| 210 case M.SampleProfileTag.UserVM: return 'User > VM'; |
| 211 case M.SampleProfileTag.UserOnly: return 'User'; |
| 212 case M.SampleProfileTag.VMUser: return 'VM > User'; |
| 213 case M.SampleProfileTag.VMOnly: return 'VM'; |
| 214 case M.SampleProfileTag.None: return 'None'; |
| 215 } |
| 216 throw new Exception('Unknown tagToString'); |
| 217 } |
| 218 |
| 219 SampleBufferControlChangedElement _toEvent(_) { |
| 220 return new SampleBufferControlChangedElement(this); |
| 221 } |
| 222 |
| 223 void _triggerModeChange(e) => _onTagChange.add(e); |
| 224 } |
| OLD | NEW |