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'; | |
koda
2014/07/29 19:44:13
Inconsistent quotes.
Cutch
2014/07/30 00:01:24
Done.
| |
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. Metric names must be unique, are hierarchical, | |
28 /// and use periods as separators. For example, 'a.b.c'. Uniqueness is only | |
29 /// enforced when a Metric is registered. | |
30 abstract class Metric { | |
31 /// [name] of this metric. | |
32 final String name; | |
33 /// [description] of this metric. | |
34 final String description; | |
35 | |
36 Metric(this.name, this.description); | |
37 | |
38 Map _toJSON(); | |
39 } | |
40 | |
41 /// A measured value with a min and max. Initial value is min. Value will | |
42 /// be clamped to the interval [min, max]. | |
43 class Gauge extends Metric { | |
44 final double min; | |
45 final double max; | |
46 | |
47 double _value; | |
48 double get value => _value; | |
49 set value(double v) { | |
50 if (v < min) { | |
51 v = min; | |
52 } | |
53 if (v > max) { | |
koda
2014/07/29 19:44:13
else if
Cutch
2014/07/30 00:01:24
Done.
| |
54 v = max; | |
55 } | |
56 _value = v; | |
57 } | |
58 | |
59 Gauge(String name, String description, this.min, this.max) | |
60 : super(name, description) { | |
61 if (min is! double) { | |
62 throw new ArgumentError('min must be a double'); | |
63 } | |
64 if (max is! double) { | |
65 throw new ArgumentError('max must be a double'); | |
66 } | |
67 if (!(min < max)) { | |
68 throw new ArgumentError('min must be less than max'); | |
69 } | |
70 _value = min; | |
71 } | |
72 | |
73 Map _toJSON() { | |
74 var map = { | |
75 'type': 'Gauge', | |
76 'id': 'metrics/$name', | |
77 'name': name, | |
78 'description': description, | |
79 'value': value, | |
80 'min': min, | |
81 'max': max, | |
82 }; | |
83 return map; | |
84 } | |
85 } | |
86 | |
87 | |
88 /// A changing value. Initial value is 0.0. | |
89 class Counter extends Metric { | |
90 Counter(String name, String description) | |
91 : super(name, description); | |
92 | |
93 double _value = 0.0; | |
94 double get value => _value; | |
95 set value(double v) { | |
96 _value = v; | |
97 } | |
98 | |
99 Map _toJSON() { | |
100 var map = { | |
101 'type': 'Counter', | |
102 'id': 'metrics/$name', | |
koda
2014/07/29 19:44:13
I guess we should avoid having slashes in the name
Cutch
2014/07/30 00:01:24
We need to verify. Things will fall over when we p
| |
103 'name': name, | |
104 'description': description, | |
105 'value': value, | |
106 }; | |
107 return map; | |
108 } | |
109 } | |
110 | |
111 /// Register [Metric]s to make them visible to Observatory. | |
koda
2014/07/29 19:44:13
You use the term "register" here and above, but th
Cutch
2014/07/30 00:01:24
Done.
| |
112 class Metrics { | |
113 static final List<Metric> _metrics = new List<Metric>(); | |
114 | |
115 static bool _nameExists(String name) { | |
116 for (var i = 0; i < _metrics.length; i++) { | |
117 if (_metrics[i].name == name) { | |
118 return true; | |
119 } | |
120 } | |
121 return false; | |
122 } | |
123 | |
124 static void add(Metric metric) { | |
125 if (metric is! Metric) { | |
126 throw new ArgumentError('metric must be a Metric'); | |
127 } | |
128 if (_nameExists(metric.name)) { | |
129 throw new ArgumentError('Register metrics have unique names'); | |
130 } | |
131 _metrics.add(metric); | |
132 } | |
133 | |
134 static void remove(Metric metric) { | |
135 if (metric is! Metric) { | |
136 throw new ArgumentError('metric must be a Metric'); | |
137 } | |
138 _metrics.remove(metric); | |
139 } | |
140 | |
141 static String _printMetric(String id) { | |
142 for (var i = 0; i < _metrics.length; i++) { | |
143 var m = _metrics[i]; | |
144 if (m.name == id) { | |
145 return JSON.encode(m._toJSON()); | |
146 } | |
147 } | |
148 return null; | |
149 } | |
150 | |
151 static String _printMetrics() { | |
152 var members = []; | |
153 for (var i = 0; i < _metrics.length; i++) { | |
154 var m = _metrics[i]; | |
155 members.add(m._toJSON()); | |
156 } | |
157 var map = { | |
158 'type': 'MetricList', | |
159 'id': 'metrics', | |
160 'members': members, | |
161 }; | |
162 return JSON.encode(map); | |
163 } | |
164 } | |
OLD | NEW |