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 /// Abstract [Metric] class. | |
28 abstract class Metric { | |
29 final String _id; | |
30 /// [name] of this metric. | |
koda
2014/07/29 15:34:30
Consider describing a convention for hierarchical
Cutch
2014/07/29 18:27:01
Done.
| |
31 final String name; | |
32 /// [description] of this metric. | |
33 final String description; | |
34 | |
35 Metric(this.name, this.description) | |
36 : _id = '${Metrics._idCounter++}'; | |
37 | |
38 Map _toJSON(); | |
39 } | |
40 | |
41 /// A measured value with a min and max. Initial value is min. | |
42 class Gauge extends Metric { | |
43 final double min; | |
44 final double max; | |
45 | |
46 double _value; | |
47 double get value => _value; | |
48 set value(double v) { | |
49 _value = v; | |
koda
2014/07/29 15:34:30
Why no range checking here?
Cutch
2014/07/29 18:27:01
I clamp now. I do not want these code paths to be
| |
50 } | |
51 | |
52 Gauge(String name, String description, this.min, this.max) | |
53 : super(name, description) { | |
54 if (min is! double) { | |
55 throw new ArgumentError('min must be a double'); | |
56 } | |
57 if (max is! double) { | |
58 throw new ArgumentError('max must be a double'); | |
59 } | |
60 if (min >= max) { | |
koda
2014/07/29 15:34:29
If you change this to !(min < max) you also catch
Cutch
2014/07/29 18:27:01
Switched to !(min < max). I'm not overly concerned
| |
61 throw new ArgumentError('min must be less than max'); | |
62 } | |
63 _value = min; | |
64 } | |
65 | |
66 Map _toJSON() { | |
67 var map = { | |
68 'type': 'Gauge', | |
69 'id': 'metrics/$_id', | |
70 'name': name, | |
71 'description': description, | |
72 'value': value, | |
73 }; | |
74 map['min'] = min; | |
75 map['max'] = max; | |
76 return map; | |
77 } | |
78 } | |
79 | |
80 | |
81 /// A changing value. Initial value is 0.0. | |
koda
2014/07/29 15:34:29
There should somewhere be a comment noting the API
Cutch
2014/07/29 18:27:01
Don't the type annotations make it clear enough?
koda
2014/07/29 19:44:13
Sure, I guess. Actually, I guess what I meant was
Cutch
2014/07/30 00:01:23
I don't want to put that in the API documentation.
| |
82 class Counter extends Metric { | |
83 Counter(String name, String description) | |
84 : super(name, description); | |
85 | |
86 double _value = 0.0; | |
87 double get value => _value; | |
88 set value(double v) { | |
89 _value = v; | |
90 } | |
91 | |
92 Map _toJSON() { | |
93 var map = { | |
94 'type': 'Counter', | |
95 'id': 'metrics/$_id', | |
96 'name': name, | |
97 'description': description, | |
98 'value': value, | |
99 }; | |
100 return map; | |
101 } | |
102 } | |
103 | |
104 /// Register [Metric] to make them visible over the service. | |
105 class Metrics { | |
106 static int _idCounter = 0; | |
107 static final List<Metric> _metrics = new List<Metric>(); | |
108 | |
109 static void add(Metric metic) { | |
110 if (m == null) { | |
111 throw new ArgumentError('metric cannot be null'); | |
112 } | |
113 if (_metrics.contains(metric)) { | |
koda
2014/07/29 15:34:30
Since you didn't override operator==, this will ju
Cutch
2014/07/29 18:27:01
I like using the name as the id and requiring that
| |
114 throw new ArgumentError('metric already registered'); | |
115 } | |
116 _metrics.add(metric); | |
koda
2014/07/29 15:34:30
Assignment of the id happens in Metric, so if the
Cutch
2014/07/29 18:27:01
Acknowledged.
| |
117 } | |
118 | |
119 static void remove(Metric m) { | |
120 if (m == null) { | |
121 throw new ArgumentError('metric cannot be null'); | |
122 } | |
123 _metrics.remove(m); | |
124 } | |
125 | |
126 static String _printMetric(String id) { | |
127 for (var i = 0; i < _metrics.length; i++) { | |
128 var m = _metrics[i]; | |
129 if (m._id == id) { | |
130 return JSON.encode(m._toJSON()); | |
131 } | |
132 } | |
133 return null; | |
134 } | |
135 | |
136 static String _printMetrics() { | |
137 var l = []; | |
138 for (var i = 0; i < _metrics.length; i++) { | |
139 var m = _metrics[i]; | |
140 l.add(m._toJSON()); | |
141 } | |
142 var map = { | |
143 'type': 'MetricList', | |
144 'id': 'metrics', | |
145 'members': l, | |
koda
2014/07/29 15:34:29
I misread "l" as the digit one; please rename.
Cutch
2014/07/29 18:27:01
Done.
| |
146 }; | |
147 return JSON.encode(map); | |
148 } | |
149 } | |
OLD | NEW |