2017-10-05 01:51:49 +02:00
|
|
|
from zerver.lib.timestamp import floor_to_hour, floor_to_day, \
|
2017-11-02 12:57:00 +01:00
|
|
|
verify_UTC
|
2017-01-07 01:46:18 +01:00
|
|
|
from analytics.lib.counts import CountStat
|
|
|
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from typing import List, Optional
|
|
|
|
|
2017-02-02 01:29:58 +01:00
|
|
|
# If min_length is None, returns end_times from ceiling(start) to floor(end), inclusive.
|
2017-01-07 01:46:18 +01:00
|
|
|
# If min_length is greater than 0, pads the list to the left.
|
|
|
|
# So informally, time_range(Sep 20, Sep 22, day, None) returns [Sep 20, Sep 21, Sep 22],
|
|
|
|
# and time_range(Sep 20, Sep 22, day, 5) returns [Sep 18, Sep 19, Sep 20, Sep 21, Sep 22]
|
2017-01-07 03:53:08 +01:00
|
|
|
def time_range(start, end, frequency, min_length):
|
2017-01-07 01:46:18 +01:00
|
|
|
# type: (datetime, datetime, str, Optional[int]) -> List[datetime]
|
2017-10-05 01:51:49 +02:00
|
|
|
verify_UTC(start)
|
|
|
|
verify_UTC(end)
|
2017-01-07 03:53:08 +01:00
|
|
|
if frequency == CountStat.HOUR:
|
2017-02-02 01:29:58 +01:00
|
|
|
end = floor_to_hour(end)
|
2017-01-07 01:46:18 +01:00
|
|
|
step = timedelta(hours=1)
|
2017-01-07 03:53:08 +01:00
|
|
|
elif frequency == CountStat.DAY:
|
2017-02-02 01:29:58 +01:00
|
|
|
end = floor_to_day(end)
|
2017-01-07 01:46:18 +01:00
|
|
|
step = timedelta(days=1)
|
|
|
|
else:
|
2017-03-12 01:43:51 +01:00
|
|
|
raise AssertionError("Unknown frequency: %s" % (frequency,))
|
2017-01-07 01:46:18 +01:00
|
|
|
|
|
|
|
times = []
|
|
|
|
if min_length is not None:
|
|
|
|
start = min(start, end - (min_length-1)*step)
|
|
|
|
current = end
|
|
|
|
while current >= start:
|
|
|
|
times.append(current)
|
|
|
|
current -= step
|
|
|
|
return list(reversed(times))
|