OLD | NEW |
---|---|
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import "dart:_internal"; | 5 import "dart:_internal"; |
6 import 'dart:convert'; | |
6 | 7 |
7 patch class UserTag { | 8 patch class UserTag { |
8 /* patch */ factory UserTag(String label) { | 9 /* patch */ factory UserTag(String label) { |
9 return new _UserTag(label); | 10 return new _UserTag(label); |
10 } | 11 } |
11 /* patch */ static UserTag get defaultTag => _getDefaultTag(); | 12 /* patch */ static UserTag get defaultTag => _getDefaultTag(); |
12 } | 13 } |
13 | 14 |
14 | 15 |
15 class _UserTag implements UserTag { | 16 class _UserTag implements UserTag { |
16 factory _UserTag(String label) native "UserTag_new"; | 17 factory _UserTag(String label) native "UserTag_new"; |
17 String get label native "UserTag_label"; | 18 String get label native "UserTag_label"; |
18 UserTag makeCurrent() native "UserTag_makeCurrent"; | 19 UserTag makeCurrent() native "UserTag_makeCurrent"; |
19 } | 20 } |
20 | 21 |
21 patch UserTag getCurrentTag() => _getCurrentTag(); | 22 patch UserTag getCurrentTag() => _getCurrentTag(); |
22 UserTag _getCurrentTag() native "Profiler_getCurrentTag"; | 23 UserTag _getCurrentTag() native "Profiler_getCurrentTag"; |
23 | 24 |
24 UserTag _getDefaultTag() native "UserTag_defaultTag"; | 25 UserTag _getDefaultTag() native "UserTag_defaultTag"; |
26 | |
27 /// A measurement. | |
28 abstract class Metric { | |
29 final String _id; | |
30 final String name; | |
31 final String description; | |
32 double _value = 0.0; | |
33 | |
34 double get value => _value; | |
35 | |
36 Metric(this.name, this.description) | |
37 : _id = '${Metrics._idCounter++}'; | |
38 | |
39 Map _toJSON(); | |
40 } | |
41 | |
42 /// A measured value, with an optional min and max. | |
43 class Gauge extends Metric { | |
44 final double min; | |
45 final double max; | |
46 | |
47 Gauge(String name, String description) | |
48 : min = null, | |
49 max = null, | |
srdjan
2014/07/29 00:30:01
Why setting it to null a second time (first at all
| |
50 super(name, description); | |
51 | |
52 Gauge.range(String name, String description, this.min, this.max) | |
53 : super(name, description) { | |
54 if ((min == null) || (max == null)) { | |
55 throw new ArgumentError('min and max cannot be null.'); | |
srdjan
2014/07/29 00:30:01
the min/max is! double should also cover min/max =
| |
56 } | |
57 if ((min is! double) || (max is! double)) { | |
58 throw new ArgumentError('min and max must be doubles.'); | |
59 } | |
60 if (min >= max) { | |
61 throw new ArgumentError('min must be less than max.'); | |
62 } | |
63 } | |
64 | |
65 set value(double v) { | |
66 _value = v; | |
67 } | |
68 | |
69 Map _toJSON() { | |
70 var map = { | |
71 'type': 'Gauge', | |
72 'id': 'metrics/$_id', | |
73 'name': name, | |
74 'description': description, | |
75 'value': _value, | |
76 }; | |
77 if (min != null) { | |
78 map['min'] = min; | |
79 map['max'] = max; | |
80 } | |
81 return map; | |
82 } | |
83 } | |
84 | |
85 /// A monotonically changing value. | |
86 class Counter extends Metric { | |
87 Counter(String name, String description) | |
88 : super(name, description); | |
89 | |
90 void add(double v) { | |
91 _value += v; | |
92 } | |
93 | |
94 Map _toJSON() { | |
95 var map = { | |
96 'type': 'Counter', | |
97 'id': 'metrics/$_id', | |
98 'name': name, | |
99 'description': description, | |
100 'value': _value, | |
101 }; | |
102 return map; | |
103 } | |
104 } | |
105 | |
106 /// Manager of register metrics. | |
107 class Metrics { | |
108 static int _idCounter = 0; | |
109 static final List<Metric> _metrics = new List<Metric>(); | |
110 | |
111 static void add(Metric m) { | |
112 if (m == null) { | |
113 return; | |
114 } | |
115 if (_metrics.contains(m)) { | |
116 return; | |
117 } | |
118 _metrics.add(m); | |
119 } | |
120 | |
121 static void remove(Metric m) { | |
122 _metrics.remove(m); | |
123 } | |
124 | |
125 static bool contains(Metric m) { | |
126 return _metrics.contains(m); | |
127 } | |
128 | |
129 static String _printMetric(String id) { | |
130 for (var i = 0; i < _metrics.length; i++) { | |
131 var m = _metrics[i]; | |
132 if (m._id == id) { | |
133 return JSON.encode(m._toJSON()); | |
134 } | |
135 } | |
136 return null; | |
137 } | |
138 | |
139 static String _printMetrics() { | |
140 var l = []; | |
141 for (var i = 0; i < _metrics.length; i++) { | |
142 var m = _metrics[i]; | |
143 l.add(m._toJSON()); | |
144 } | |
145 var map = { | |
146 'type': 'MetricList', | |
147 'id': 'metrics', | |
148 'members': l, | |
149 }; | |
150 return JSON.encode(map); | |
151 } | |
152 } | |
OLD | NEW |