# Path: metrology/instruments/meter.py
# class Meter(object):
#     """A meter measures the rate of events over time
#     (e.g., "requests per second").
# 
#     In addition to the mean rate, you can also track 1, 5 and 15 minutes moving
#     averages ::
# 
#       meter = Metrology.meter('requests')
#       meter.mark()
#       meter.count
# 
#     """
#     def __init__(self, average_class=EWMA):
#         self.counter = AtomicLong(0)
#         self.start_time = now()
#         self.last_tick = AtomicLong(self.start_time)
# 
#         self.interval = EWMA.INTERVAL
#         self.m1_rate = EWMA.m1()
#         self.m5_rate = EWMA.m5()
#         self.m15_rate = EWMA.m15()
# 
#     def _tick(self):
#         old_tick, new_tick = self.last_tick.value, time()
#         age = new_tick - old_tick
#         ticks = int(age / self.interval)
#         new_tick = old_tick + int(ticks * self.interval)
#         if ticks and self.last_tick.compare_and_swap(old_tick, new_tick):
#             for _ in range(ticks):
#                 self.tick()
# 
#     def __call__(self, *args, **kwargs):
#         if args and hasattr(args[0], '__call__'):
#             _orig_func = args[0]
# 
#             def _decorator(*args, **kwargs):
#                 with self:
#                     return _orig_func(*args, **kwargs)
#             return _decorator
# 
#     def __enter__(self):
#         pass
# 
#     def __exit__(self, exc, exv, trace):
#         self.mark()
# 
#     @property
#     def count(self):
#         """Returns the total number of events that have been recorded."""
#         return self.counter.value
# 
#     def clear(self):
#         self.counter.value = 0
#         self.start_time = time()
# 
#         self.m1_rate.clear()
#         self.m5_rate.clear()
#         self.m15_rate.clear()
# 
#     @ticker
#     def mark(self, value=1):
#         """Record an event with the meter. By default it will record one event.
# 
#         :param value: number of event to record
#         """
#         self.counter += value
#         self.m1_rate.update(value)
#         self.m5_rate.update(value)
#         self.m15_rate.update(value)
# 
#     def tick(self):
#         self.m1_rate.tick()
#         self.m5_rate.tick()
#         self.m15_rate.tick()
# 
#     @property
#     @ticker
#     def one_minute_rate(self):
#         """Returns the one-minute average rate."""
#         return self.m1_rate.rate
# 
#     @property
#     @ticker
#     def five_minute_rate(self):
#         """Returns the five-minute average rate."""
#         return self.m5_rate.rate
# 
#     @property
#     @ticker
#     def fifteen_minute_rate(self):
#         """Returns the fifteen-minute average rate."""
#         return self.m15_rate.rate
# 
#     @property
#     def mean_rate(self):
#         """
#         Returns the mean rate of the events since the start of the process.
#         """
#         if self.counter.value == 0:
#             return 0.0
#         else:
#             elapsed = time() - self.start_time
#             return self.counter.value / elapsed
# 
#     def stop(self):
#         pass
# 
# Path: metrology/stats/ewma.py
# class EWMA(object):
#     INTERVAL = 5.0
#     SECONDS_PER_MINUTE = 60.0
# 
#     ONE_MINUTE = 1
#     FIVE_MINUTES = 5
#     FIFTEEN_MINUTES = 15
# 
#     M1_ALPHA = 1 - math.exp(-INTERVAL / SECONDS_PER_MINUTE / ONE_MINUTE)
#     M5_ALPHA = 1 - math.exp(-INTERVAL / SECONDS_PER_MINUTE / FIVE_MINUTES)
#     M15_ALPHA = 1 - math.exp(-INTERVAL / SECONDS_PER_MINUTE / FIFTEEN_MINUTES)
# 
#     @classmethod
#     def m1(cls):
#         return EWMA(cls.M1_ALPHA, cls.INTERVAL)
# 
#     @classmethod
#     def m5(cls):
#         return EWMA(cls.M5_ALPHA, cls.INTERVAL)
# 
#     @classmethod
#     def m15(cls):
#         return EWMA(cls.M15_ALPHA, cls.INTERVAL)
# 
#     def __init__(self, alpha, interval):
#         self.alpha = alpha
#         self.interval = interval
# 
#         self.initialized = False
#         self._rate = 0.0
#         self._uncounted = AtomicLong(0)
# 
#     def clear(self):
#         self.initialized = False
#         self._rate = 0.0
#         self._uncounted.value = 0
# 
#     def update(self, value):
#         self._uncounted += value
# 
#     def tick(self):
#         count = self._uncounted.swap(0)
#         instant_rate = count / self.interval
# 
#         if self.initialized:
#             self._rate += self.alpha * (instant_rate - self._rate)
#         else:
#             self._rate = instant_rate
#             self.initialized = True
# 
#     @property
#     def rate(self):
#         return self._rate


from atomic import AtomicLong
from metrology.instruments.meter import Meter
from metrology.stats import EWMA



class Derive(Meter):
    """
    A derive is like a meter but accepts an absolute counter as input.

      derive = Metrology.derive('network.io')
      derive.mark()
      derive.count

    """

# Path: metrology/instruments/meter.py
# class Meter(object):
#     """A meter measures the rate of events over time
#     (e.g., "requests per second").
# 
#     In addition to the mean rate, you can also track 1, 5 and 15 minutes moving
#     averages ::
# 
#       meter = Metrology.meter('requests')
#       meter.mark()
#       meter.count
# 
#     """
#     def __init__(self, average_class=EWMA):
#         self.counter = AtomicLong(0)
#         self.start_time = now()
#         self.last_tick = AtomicLong(self.start_time)
# 
#         self.interval = EWMA.INTERVAL
#         self.m1_rate = EWMA.m1()
#         self.m5_rate = EWMA.m5()
#         self.m15_rate = EWMA.m15()
# 
#     def _tick(self):
#         old_tick, new_tick = self.last_tick.value, time()
#         age = new_tick - old_tick
#         ticks = int(age / self.interval)
#         new_tick = old_tick + int(ticks * self.interval)
#         if ticks and self.last_tick.compare_and_swap(old_tick, new_tick):
#             for _ in range(ticks):
#                 self.tick()
# 
#     def __call__(self, *args, **kwargs):
#         if args and hasattr(args[0], '__call__'):
#             _orig_func = args[0]
# 
#             def _decorator(*args, **kwargs):
#                 with self:
#                     return _orig_func(*args, **kwargs)
#             return _decorator
# 
#     def __enter__(self):
#         pass
# 
#     def __exit__(self, exc, exv, trace):
#         self.mark()
# 
#     @property
#     def count(self):
#         """Returns the total number of events that have been recorded."""
#         return self.counter.value
# 
#     def clear(self):
#         self.counter.value = 0
#         self.start_time = time()
# 
#         self.m1_rate.clear()
#         self.m5_rate.clear()
#         self.m15_rate.clear()
# 
#     @ticker
#     def mark(self, value=1):
#         """Record an event with the meter. By default it will record one event.
# 
#         :param value: number of event to record
#         """
#         self.counter += value
#         self.m1_rate.update(value)
#         self.m5_rate.update(value)
#         self.m15_rate.update(value)
# 
#     def tick(self):
#         self.m1_rate.tick()
#         self.m5_rate.tick()
#         self.m15_rate.tick()
# 
#     @property
#     @ticker
#     def one_minute_rate(self):
#         """Returns the one-minute average rate."""
#         return self.m1_rate.rate
# 
#     @property
#     @ticker
#     def five_minute_rate(self):
#         """Returns the five-minute average rate."""
#         return self.m5_rate.rate
# 
#     @property
#     @ticker
#     def fifteen_minute_rate(self):
#         """Returns the fifteen-minute average rate."""
#         return self.m15_rate.rate
# 
#     @property
#     def mean_rate(self):
#         """
#         Returns the mean rate of the events since the start of the process.
#         """
#         if self.counter.value == 0:
#             return 0.0
#         else:
#             elapsed = time() - self.start_time
#             return self.counter.value / elapsed
# 
#     def stop(self):
#         pass
# 
# Path: metrology/stats/ewma.py
# class EWMA(object):
#     INTERVAL = 5.0
#     SECONDS_PER_MINUTE = 60.0
# 
#     ONE_MINUTE = 1
#     FIVE_MINUTES = 5
#     FIFTEEN_MINUTES = 15
# 
#     M1_ALPHA = 1 - math.exp(-INTERVAL / SECONDS_PER_MINUTE / ONE_MINUTE)
#     M5_ALPHA = 1 - math.exp(-INTERVAL / SECONDS_PER_MINUTE / FIVE_MINUTES)
#     M15_ALPHA = 1 - math.exp(-INTERVAL / SECONDS_PER_MINUTE / FIFTEEN_MINUTES)
# 
#     @classmethod
#     def m1(cls):
#         return EWMA(cls.M1_ALPHA, cls.INTERVAL)
# 
#     @classmethod
#     def m5(cls):
#         return EWMA(cls.M5_ALPHA, cls.INTERVAL)
# 
#     @classmethod
#     def m15(cls):
#         return EWMA(cls.M15_ALPHA, cls.INTERVAL)
# 
#     def __init__(self, alpha, interval):
#         self.alpha = alpha
#         self.interval = interval
# 
#         self.initialized = False
#         self._rate = 0.0
#         self._uncounted = AtomicLong(0)
# 
#     def clear(self):
#         self.initialized = False
#         self._rate = 0.0
#         self._uncounted.value = 0
# 
#     def update(self, value):
#         self._uncounted += value
# 
#     def tick(self):
#         count = self._uncounted.swap(0)
#         instant_rate = count / self.interval
# 
#         if self.initialized:
#             self._rate += self.alpha * (instant_rate - self._rate)
#         else:
#             self._rate = instant_rate
#             self.initialized = True
# 
#     @property
#     def rate(self):
#         return self._rate


# Path: metrology/instruments/derive.py
from atomic import AtomicLong
from metrology.instruments.meter import Meter
from metrology.stats import EWMA




class Derive(Meter):
    """
    A derive is like a meter but accepts an absolute counter as input.

      derive = Metrology.derive('network.io')
      derive.mark()
      derive.count

    """

    def __init__(self, average_class=EWMA):