2016-03-22 06:52:36 +01:00
|
|
|
from datetime import datetime, timedelta
|
2013-10-17 00:12:30 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.models import UserActivityInterval, UserProfile
|
|
|
|
|
|
|
|
|
2013-10-17 00:12:30 +02:00
|
|
|
# Return the amount of Zulip usage for this user between the two
|
|
|
|
# given dates
|
2017-11-27 05:27:04 +01:00
|
|
|
def seconds_usage_between(user_profile: UserProfile, begin: datetime, end: datetime) -> timedelta:
|
2021-02-12 08:19:30 +01:00
|
|
|
intervals = UserActivityInterval.objects.filter(
|
|
|
|
user_profile=user_profile, end__gte=begin, start__lte=end
|
|
|
|
)
|
2013-10-17 00:12:30 +02:00
|
|
|
duration = timedelta(0)
|
|
|
|
for interval in intervals:
|
|
|
|
start = max(begin, interval.start)
|
|
|
|
finish = min(end, interval.end)
|
2021-02-12 08:19:30 +01:00
|
|
|
duration += finish - start
|
2013-10-17 00:12:30 +02:00
|
|
|
return duration
|