lbry-sdk/lbrynet/analytics/track.py

25 lines
625 B
Python
Raw Normal View History

2016-09-30 06:26:27 +02:00
import collections
class Track(object):
"""Track and summarize observations of metrics."""
def __init__(self):
self.data = collections.defaultdict(list)
def add_observation(self, metric, value):
self.data[metric].append(value)
def summarize(self, metric, op=sum):
"""Apply `op` on the current values for `metric`.
This operation also resets the metric.
2016-10-11 19:50:44 +02:00
Returns:
a tuple (should_send, value)
2016-09-30 06:26:27 +02:00
"""
2016-10-11 19:50:44 +02:00
try:
values = self.data.pop(metric)
return True, op(values)
except KeyError:
return False, None