| 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 Stream<M.SampleProfileLoadingProgressEvent> _progressStream; |
| 33 M.SampleProfileLoadingProgress _progress; |
| 34 M.SampleProfileTag _tag; |
| 35 bool _showTag = false; |
| 36 bool _profileVM = false; |
| 37 StreamSubscription _subscription; |
| 38 |
| 39 M.SampleProfileLoadingProgress get progress => _progress; |
| 40 M.SampleProfileTag get selectedTag => _tag; |
| 41 bool get showTag => _showTag; |
| 42 bool get profileVM => _profileVM; |
| 43 |
| 44 set selectedTag(M.SampleProfileTag value) => |
| 45 _tag = _r.checkAndReact(_tag, value); |
| 46 set showTag(bool value) => _showTag = _r.checkAndReact(_showTag, value); |
| 47 set profileVM(bool value) => _profileVM = _r.checkAndReact(_profileVM, value); |
| 48 |
| 49 |
| 50 factory SampleBufferControlElement(M.SampleProfileLoadingProgress progress, |
| 51 Stream<M.SampleProfileLoadingProgressEvent> progressStream, |
| 52 {M.SampleProfileTag selectedTag: M.SampleProfileTag.none, |
| 53 bool showTag: true, RenderingQueue queue}) { |
| 54 assert(progress != null); |
| 55 assert(progressStream != null); |
| 56 assert(selectedTag != null); |
| 57 assert(showTag != null); |
| 58 SampleBufferControlElement e = document.createElement(tag.name); |
| 59 e._r = new RenderingScheduler(e, queue: queue); |
| 60 e._progress = progress; |
| 61 e._progressStream = progressStream; |
| 62 e._tag = selectedTag; |
| 63 e._showTag = showTag; |
| 64 return e; |
| 65 } |
| 66 |
| 67 SampleBufferControlElement.created() : super.created(); |
| 68 |
| 69 @override |
| 70 void attached() { |
| 71 super.attached(); |
| 72 _r.enable(); |
| 73 _subscription = _progressStream.listen((e) { |
| 74 _progress = e.progress; |
| 75 _r.dirty(); |
| 76 }); |
| 77 } |
| 78 |
| 79 @override |
| 80 void detached() { |
| 81 super.detached(); _r.disable(notify: true); |
| 82 children = const []; |
| 83 _subscription.cancel(); |
| 84 } |
| 85 |
| 86 void render() { |
| 87 var content = <Element>[ |
| 88 new HeadingElement.h2()..text = 'Sample buffer', |
| 89 new HRElement() |
| 90 ]; |
| 91 switch (_progress.status) { |
| 92 case M.SampleProfileLoadingStatus.fetching : |
| 93 content.addAll(_createStatusMessage('Fetching profile from VM...')); |
| 94 break; |
| 95 case M.SampleProfileLoadingStatus.loading : |
| 96 content.addAll(_createStatusMessage('Loading profile...', |
| 97 progress: _progress.progress)); |
| 98 break; |
| 99 case M.SampleProfileLoadingStatus.disabled : |
| 100 content.addAll(_createDisabledMessage()); |
| 101 break; |
| 102 case M.SampleProfileLoadingStatus.loaded: |
| 103 content.addAll(_createStatusReport()); |
| 104 break; |
| 105 } |
| 106 children = [ |
| 107 new DivElement()..classes = ['content-centered-big'] |
| 108 ..children = content |
| 109 ]; |
| 110 } |
| 111 |
| 112 static List<Element> _createStatusMessage(String message, |
| 113 {double progress: 0.0}) { |
| 114 return [new DivElement()..classes = ['statusBox', 'shadow', 'center'] |
| 115 ..children = [ |
| 116 new DivElement()..classes = ['statusMessage'] |
| 117 ..text = message, |
| 118 new DivElement()..style.background = '#0489c3' |
| 119 ..style.width = '$progress%' |
| 120 ..style.height = '15px' |
| 121 ..style.borderRadius = '4px' |
| 122 ] |
| 123 ]; |
| 124 } |
| 125 |
| 126 static List<Element> _createDisabledMessage() { |
| 127 return [new DivElement()..classes = ['statusBox' 'shadow' 'center'] |
| 128 ..children = [ |
| 129 new DivElement() |
| 130 ..children = [ |
| 131 new HeadingElement.h1() |
| 132 ..text = 'Profiling is disabled', |
| 133 new BRElement(), |
| 134 new DivElement() |
| 135 ..innerHtml = 'Perhaps the <b>profile</b> ' |
| 136 'flag has been disabled for this VM.', |
| 137 new BRElement(), |
| 138 new SpanElement()..text = 'See all', |
| 139 new AnchorElement(href: Uris.flags())..text = 'vm flags' |
| 140 ] |
| 141 ] |
| 142 ]; |
| 143 } |
| 144 |
| 145 List<Element> _createStatusReport() { |
| 146 final fetchT = Utils.formatDurationInSeconds(_progress.fetchingTime); |
| 147 final loadT = Utils.formatDurationInSeconds(_progress.loadingTime); |
| 148 final sampleCount = _progress.profile.sampleCount; |
| 149 final refreshT = new DateTime.now(); |
| 150 final stackDepth = _progress.profile.stackDepth; |
| 151 final sampleRate = _progress.profile.sampleRate.toStringAsFixed(0); |
| 152 final timeSpan = _progress.profile.sampleCount == 0 ? '0s' |
| 153 : Utils.formatTimePrecise(_progress.profile.timeSpan); |
| 154 |
| 155 var content = <Element>[ |
| 156 new DivElement()..classes = ['memberItem'] |
| 157 ..children = [ |
| 158 new DivElement()..classes = ['memberName'] |
| 159 ..text = 'Refreshed at', |
| 160 new DivElement()..classes = ['memberValue'] |
| 161 ..text = '$refreshT (fetched in ${fetchT}s) (loaded in ${loadT}s)' |
| 162 ], |
| 163 new DivElement()..classes = ['memberItem'] |
| 164 ..children = [ |
| 165 new DivElement()..classes = ['memberName'] |
| 166 ..text = 'Profile contains ', |
| 167 new DivElement()..classes = ['memberValue'] |
| 168 ..text = '$sampleCount samples (spanning $timeSpan)' |
| 169 ], |
| 170 new DivElement()..classes = ['memberItem'] |
| 171 ..children = [ |
| 172 new DivElement()..classes = ['memberName'] |
| 173 ..text = 'Sampling', |
| 174 new DivElement()..classes = ['memberValue'] |
| 175 ..text = '$stackDepth stack frames @ ${sampleRate}Hz' |
| 176 ], |
| 177 ]; |
| 178 if (_showTag) { |
| 179 content.add( |
| 180 new DivElement()..classes = ['memberItem'] |
| 181 ..children = [ |
| 182 new DivElement()..classes = ['memberName'] |
| 183 ..text = 'Tag Order', |
| 184 new DivElement()..classes = ['memberValue'] |
| 185 ..children = _createTagSelect() |
| 186 ] |
| 187 ); |
| 188 } |
| 189 return [ |
| 190 new DivElement()..classes = ['memberList'] |
| 191 ..children = content |
| 192 ]; |
| 193 } |
| 194 |
| 195 List<Element> _createTagSelect() { |
| 196 var values = M.SampleProfileTag.values; |
| 197 if (!_profileVM) { |
| 198 values = const [M.SampleProfileTag.userOnly, M.SampleProfileTag.none]; |
| 199 } |
| 200 var s; |
| 201 return [ |
| 202 s = new SelectElement()..classes = ['tag-select'] |
| 203 ..value = tagToString(_tag) |
| 204 ..children = values.map((tag) { |
| 205 return new OptionElement(value : tagToString(tag), |
| 206 selected: _tag == tag) |
| 207 ..text = tagToString(tag); |
| 208 }).toList(growable: false) |
| 209 ..onChange.listen((_) { |
| 210 _tag = values[s.selectedIndex]; |
| 211 }) |
| 212 ..onChange.map(_toEvent).listen(_triggerModeChange), |
| 213 ]; |
| 214 } |
| 215 |
| 216 static String tagToString(M.SampleProfileTag tag) { |
| 217 switch (tag) { |
| 218 case M.SampleProfileTag.userVM: return 'User > VM'; |
| 219 case M.SampleProfileTag.userOnly: return 'User'; |
| 220 case M.SampleProfileTag.vmUser: return 'VM > User'; |
| 221 case M.SampleProfileTag.vmOnly: return 'VM'; |
| 222 case M.SampleProfileTag.none: return 'None'; |
| 223 } |
| 224 throw new Exception('Unknown tagToString'); |
| 225 } |
| 226 |
| 227 SampleBufferControlChangedElement _toEvent(_) { |
| 228 return new SampleBufferControlChangedElement(this); |
| 229 } |
| 230 |
| 231 void _triggerModeChange(e) => _onTagChange.add(e); |
| 232 } |
| OLD | NEW |