Files
zulip/zerver/lib/statistics.py
Anders Kaseorg 11741543da python: Reformat with Black, except quotes.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-12 13:11:19 -08:00

18 lines
604 B
Python

from datetime import datetime, timedelta
from zerver.models import UserActivityInterval, UserProfile
# Return the amount of Zulip usage for this user between the two
# given dates
def seconds_usage_between(user_profile: UserProfile, begin: datetime, end: datetime) -> timedelta:
intervals = UserActivityInterval.objects.filter(
user_profile=user_profile, end__gte=begin, start__lte=end
)
duration = timedelta(0)
for interval in intervals:
start = max(begin, interval.start)
finish = min(end, interval.end)
duration += finish - start
return duration