| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import collections | 5 import collections |
| 6 import copy | 6 import copy |
| 7 import itertools |
| 7 import threading | 8 import threading |
| 8 import time | 9 import time |
| 9 | 10 |
| 10 from infra_libs.ts_mon.common import errors | 11 from infra_libs.ts_mon.common import errors |
| 11 | 12 |
| 12 | 13 |
| 13 """A light-weight representation of a set or an incr. | 14 """A light-weight representation of a set or an incr. |
| 14 | 15 |
| 15 Args: | 16 Args: |
| 16 name: The metric name. | 17 name: The metric name. |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 def set_value(self, fields, target_fields, value): | 198 def set_value(self, fields, target_fields, value): |
| 198 self.get_target_values(target_fields).set_value(fields, value) | 199 self.get_target_values(target_fields).set_value(fields, value) |
| 199 | 200 |
| 200 def iter_targets(self): | 201 def iter_targets(self): |
| 201 # Make a copy of the values in case another thread (or this | 202 # Make a copy of the values in case another thread (or this |
| 202 # generator's consumer) modifies them while we're iterating. | 203 # generator's consumer) modifies them while we're iterating. |
| 203 with self._thread_lock: | 204 with self._thread_lock: |
| 204 values = copy.copy(self._values) | 205 values = copy.copy(self._values) |
| 205 for target_fields, fields_values in values.iteritems(): | 206 for target_fields, fields_values in values.iteritems(): |
| 206 target = copy.copy(self._store._state.target) | 207 target = copy.copy(self._store._state.target) |
| 207 target.update({k: v for k, v in target_fields}) | 208 if target_fields: |
| 209 target.update({k: v for k, v in target_fields}) |
| 208 yield target, fields_values | 210 yield target, fields_values |
| 209 | 211 |
| 210 | 212 |
| 211 class MetricValues(object): | 213 class MetricValues(object): |
| 212 def __init__(self, store, start_time): | 214 def __init__(self, store, start_time): |
| 213 self._start_time = start_time | 215 self._start_time = start_time |
| 214 self._values = TargetFieldsValues(store) | 216 self._values = TargetFieldsValues(store) |
| 215 | 217 |
| 216 @property | 218 @property |
| 217 def start_time(self): | 219 def start_time(self): |
| (...skipping 21 matching lines...) Expand all Loading... |
| 239 | 241 |
| 240 def _entry(self, name): | 242 def _entry(self, name): |
| 241 if name not in self._values: | 243 if name not in self._values: |
| 242 self._reset(name) | 244 self._reset(name) |
| 243 | 245 |
| 244 return self._values[name] | 246 return self._values[name] |
| 245 | 247 |
| 246 def get(self, name, fields, target_fields, default=None): | 248 def get(self, name, fields, target_fields, default=None): |
| 247 return self._entry(name).get_value(fields, target_fields, default) | 249 return self._entry(name).get_value(fields, target_fields, default) |
| 248 | 250 |
| 251 def iter_field_values(self, name): |
| 252 return itertools.chain.from_iterable( |
| 253 x.iteritems() for _, x in self._entry(name).values.iter_targets()) |
| 254 |
| 249 def get_all(self): | 255 def get_all(self): |
| 250 # Make a copy of the metric values in case another thread (or this | 256 # Make a copy of the metric values in case another thread (or this |
| 251 # generator's consumer) modifies them while we're iterating. | 257 # generator's consumer) modifies them while we're iterating. |
| 252 with self._thread_lock: | 258 with self._thread_lock: |
| 253 values = copy.copy(self._values) | 259 values = copy.copy(self._values) |
| 254 | 260 |
| 255 for name, metric_values in values.iteritems(): | 261 for name, metric_values in values.iteritems(): |
| 256 if name not in self._state.metrics: | 262 if name not in self._state.metrics: |
| 257 continue | 263 continue |
| 258 start_time = metric_values.start_time | 264 start_time = metric_values.start_time |
| (...skipping 27 matching lines...) Expand all Loading... |
| 286 | 292 |
| 287 def reset_for_unittest(self, name=None): | 293 def reset_for_unittest(self, name=None): |
| 288 if name is not None: | 294 if name is not None: |
| 289 self._reset(name) | 295 self._reset(name) |
| 290 else: | 296 else: |
| 291 for name in self._values.keys(): | 297 for name in self._values.keys(): |
| 292 self._reset(name) | 298 self._reset(name) |
| 293 | 299 |
| 294 def _reset(self, name): | 300 def _reset(self, name): |
| 295 self._values[name] = MetricValues(self, self._start_time(name)) | 301 self._values[name] = MetricValues(self, self._start_time(name)) |
| OLD | NEW |