| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2014, 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 library metrics; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:html'; |
| 9 import 'observatory_element.dart'; |
| 10 import 'package:observatory/app.dart'; |
| 11 import 'package:observatory/service.dart'; |
| 12 import 'package:polymer/polymer.dart'; |
| 13 |
| 14 @CustomTag('metrics-page') |
| 15 class MetricsPageElement extends ObservatoryElement { |
| 16 MetricsPageElement.created() : super.created(); |
| 17 |
| 18 @observable MetricsPage page; |
| 19 @observable Isolate isolate; |
| 20 @observable ServiceMetric selectedMetric; |
| 21 |
| 22 void _autoPickSelectedMetric() { |
| 23 if (selectedMetric != null) { |
| 24 return; |
| 25 } |
| 26 // Attempt to pick the last selected metric. |
| 27 if ((isolate != null) && (page != null) && |
| 28 (page.selectedMetricId != null)) { |
| 29 selectedMetric = isolate.dartMetrics[page.selectedMetricId]; |
| 30 if (selectedMetric == null) { |
| 31 selectedMetric = isolate.vmMetrics[page.selectedMetricId]; |
| 32 } |
| 33 } |
| 34 if ((selectedMetric == null) && (isolate != null)) { |
| 35 var values = isolate.dartMetrics.values; |
| 36 if (values != null) { |
| 37 // Fall back and pick the first isolate metric. |
| 38 selectedMetric = values.first; |
| 39 } |
| 40 } |
| 41 } |
| 42 |
| 43 void attached() { |
| 44 _autoPickSelectedMetric(); |
| 45 } |
| 46 |
| 47 void isolateChanged(oldValue) { |
| 48 if (isolate != null) { |
| 49 isolate.refreshMetrics().then((_) { |
| 50 _autoPickSelectedMetric(); |
| 51 }); |
| 52 } |
| 53 } |
| 54 |
| 55 void refresh(var done) { |
| 56 isolate.refreshMetrics().whenComplete(done); |
| 57 } |
| 58 |
| 59 void selectMetric(Event e, var detail, Element target) { |
| 60 String id = target.attributes['data-id']; |
| 61 selectedMetric = isolate.dartMetrics[id]; |
| 62 if (selectedMetric == null) { |
| 63 // Check VM metrics. |
| 64 selectedMetric = isolate.vmMetrics[id]; |
| 65 } |
| 66 if (selectedMetric != null) { |
| 67 page.selectedMetricId = id; |
| 68 } else { |
| 69 page.selectedMetricId = null; |
| 70 } |
| 71 } |
| 72 } |
| 73 |
| 74 @CustomTag('metric-details') |
| 75 class MetricDetailsElement extends ObservatoryElement { |
| 76 MetricDetailsElement.created() : super.created(); |
| 77 @published MetricsPage page; |
| 78 @published ServiceMetric metric; |
| 79 |
| 80 int _findIndex(SelectElement element, int value) { |
| 81 if (element == null) { |
| 82 return null; |
| 83 } |
| 84 for (var i = 0; i < element.options.length; i++) { |
| 85 var optionElement = element.options[i]; |
| 86 int optionValue = int.parse(optionElement.value); |
| 87 if (optionValue == value) { |
| 88 return i; |
| 89 } |
| 90 } |
| 91 return null; |
| 92 } |
| 93 |
| 94 void attached() { |
| 95 super.attached(); |
| 96 _updateSelectedIndexes(); |
| 97 } |
| 98 |
| 99 void _updateSelectedIndexes() { |
| 100 if (metric == null) { |
| 101 return; |
| 102 } |
| 103 SelectElement refreshRateElement = shadowRoot.querySelector('#refreshrate'); |
| 104 if (refreshRateElement == null) { |
| 105 // Race between shadowRoot setup and events. |
| 106 return; |
| 107 } |
| 108 int period = 0; |
| 109 if (metric.poller != null) { |
| 110 period = metric.poller.pollPeriod.inMilliseconds; |
| 111 } |
| 112 var index = _findIndex(refreshRateElement, period); |
| 113 assert(index != null); |
| 114 refreshRateElement.selectedIndex = index; |
| 115 SelectElement bufferSizeElement = shadowRoot.querySelector('#buffersize'); |
| 116 index = _findIndex(bufferSizeElement, metric.sampleBufferSize); |
| 117 assert(index != null); |
| 118 bufferSizeElement.selectedIndex = index; |
| 119 } |
| 120 |
| 121 metricChanged(oldValue) { |
| 122 _updateSelectedIndexes(); |
| 123 } |
| 124 |
| 125 void refreshRateChange(Event e, var detail, Element target) { |
| 126 var value = int.parse((target as SelectElement).value); |
| 127 if (metric == null) { |
| 128 return; |
| 129 } |
| 130 page.setRefreshPeriod(value, metric); |
| 131 } |
| 132 |
| 133 void sampleBufferSizeChange(Event e, var detail, Element target) { |
| 134 var value = int.parse((target as SelectElement).value); |
| 135 if (metric == null) { |
| 136 return; |
| 137 } |
| 138 metric.sampleBufferSize = value; |
| 139 } |
| 140 } |
| 141 |
| 142 @CustomTag('metrics-graph') |
| 143 class MetricsGraphElement extends ObservatoryElement { |
| 144 MetricsGraphElement.created() : super.created(); |
| 145 |
| 146 final DataTable _table = new DataTable(); |
| 147 Chart _chart; |
| 148 |
| 149 @published ServiceMetric metric; |
| 150 @observable Isolate isolate; |
| 151 |
| 152 void attached() { |
| 153 // Redraw once a second. |
| 154 pollPeriod = new Duration(seconds: 1); |
| 155 super.attached(); |
| 156 } |
| 157 |
| 158 void onPoll() { |
| 159 draw(); |
| 160 } |
| 161 |
| 162 void draw() { |
| 163 if (_chart == null) { |
| 164 // Construct chart. |
| 165 var element = shadowRoot.querySelector('#graph'); |
| 166 if (element == null) { |
| 167 // Bail. |
| 168 return; |
| 169 } |
| 170 _chart = new Chart('LineChart', element); |
| 171 } |
| 172 if (metric == null) { |
| 173 return; |
| 174 } |
| 175 _update(); |
| 176 _chart.draw(_table); |
| 177 } |
| 178 |
| 179 void _setupInitialDataTable() { |
| 180 _table.clearColumns(); |
| 181 // Only one metric right now. |
| 182 _table.addColumn('timeofday', 'time'); |
| 183 _table.addColumn('number', metric.name); |
| 184 } |
| 185 |
| 186 void _update() { |
| 187 _table.clearRows(); |
| 188 for (var i = 0; i < metric.samples.length; i++) { |
| 189 var sample = metric.samples[i]; |
| 190 _table.addTimeOfDayValue(sample.time, sample.value); |
| 191 } |
| 192 } |
| 193 |
| 194 metricChanged(oldValue) { |
| 195 if (oldValue != metric) { |
| 196 _setupInitialDataTable(); |
| 197 } |
| 198 } |
| 199 } |
| OLD | NEW |