# Path: wizzat/dateutil.py
# def now():
#     """
#     Returns the current timestamp.
#     Can be manipulated or frozen with `wizzat.set_now` and `wizzat.reset_now`, generally for testing purposes.
#     """
#     global _now
#     return _now or datetime.datetime.utcnow()
# 
# def to_epoch(dt):
#     """
#     Converts a datetime into Unix epoch (in seconds)
#     """
#     if isinstance(dt, numbers.Number):
#         return dt
#     return calendar.timegm(coerce_date(dt).timetuple())


from builtins import *
from future.utils import iteritems
from wizzat.dateutil import now, to_epoch
import collections
import itertools
import math

class RollingTimePercentile(object):
    """
    Logarithmic percentile approximation for non-negative values.
    Expected variance from actual median is about 5%.
    Expected memory consumption is ~8kb with constant time calculation.

    p = Percentile()
    for x in iterable:
        p.add_value(x)

    p.percentile(0.0) # Min value
    p.percentile(0.5) # Median
    p.percentile(1.0) # Max value
    """
    def __init__(self, window_sec, max_sec):
        self.window_sec = window_sec
        self.max_sec = max_sec
        self.windows = {}

    def add_value(self, value):
        curr_window = to_epoch(now()) // self.window_sec
        if curr_window not in self.windows:
            self.windows[curr_window] = Percentile()
            self.trim_windows()

        self.windows[curr_window].add_value(value)

    def trim_windows(self):
        curr_window = to_epoch(now()) // self.window_sec

# Path: wizzat/dateutil.py
# def now():
#     """
#     Returns the current timestamp.
#     Can be manipulated or frozen with `wizzat.set_now` and `wizzat.reset_now`, generally for testing purposes.
#     """
#     global _now
#     return _now or datetime.datetime.utcnow()
# 
# def to_epoch(dt):
#     """
#     Converts a datetime into Unix epoch (in seconds)
#     """
#     if isinstance(dt, numbers.Number):
#         return dt
#     return calendar.timegm(coerce_date(dt).timetuple())


# Path: wizzat/mathutil.py
from builtins import *
from future.utils import iteritems
from wizzat.dateutil import now, to_epoch
import collections
import itertools
import math


class RollingTimePercentile(object):
    """
    Logarithmic percentile approximation for non-negative values.
    Expected variance from actual median is about 5%.
    Expected memory consumption is ~8kb with constant time calculation.

    p = Percentile()
    for x in iterable:
        p.add_value(x)

    p.percentile(0.0) # Min value
    p.percentile(0.5) # Median
    p.percentile(1.0) # Max value
    """
    def __init__(self, window_sec, max_sec):
        self.window_sec = window_sec
        self.max_sec = max_sec
        self.windows = {}

    def add_value(self, value):
        curr_window = to_epoch(now()) // self.window_sec
        if curr_window not in self.windows:
            self.windows[curr_window] = Percentile()
            self.trim_windows()

        self.windows[curr_window].add_value(value)

    def trim_windows(self):
        curr_window = to_epoch(now()) // self.window_sec

        min_window = (to_epoch(now()) - self.max_sec) // self.window_sec