2013-09-06 21:52:12 +02:00
|
|
|
from __future__ import absolute_import
|
2016-01-24 03:56:05 +01:00
|
|
|
from __future__ import division
|
2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
2013-09-06 21:52:12 +02:00
|
|
|
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any, Dict
|
|
|
|
|
2013-10-17 00:12:30 +02:00
|
|
|
from zerver.lib.statistics import seconds_usage_between
|
|
|
|
|
2013-09-06 21:52:12 +02:00
|
|
|
from optparse import make_option
|
2016-11-03 10:22:19 +01:00
|
|
|
from django.core.management.base import BaseCommand, CommandParser
|
2013-11-04 17:22:58 +01:00
|
|
|
from zerver.models import UserProfile
|
2013-09-06 21:52:12 +02:00
|
|
|
import datetime
|
2013-11-04 17:22:58 +01:00
|
|
|
from django.utils.timezone import utc
|
2013-09-06 21:52:12 +02:00
|
|
|
|
|
|
|
def analyze_activity(options):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (Dict[str, Any]) -> None
|
2013-09-06 21:52:12 +02:00
|
|
|
day_start = datetime.datetime.strptime(options["date"], "%Y-%m-%d").replace(tzinfo=utc)
|
2013-09-10 17:25:35 +02:00
|
|
|
day_end = day_start + datetime.timedelta(days=options["duration"])
|
2013-09-06 21:52:12 +02:00
|
|
|
|
|
|
|
user_profile_query = UserProfile.objects.all()
|
|
|
|
if options["realm"]:
|
|
|
|
user_profile_query = user_profile_query.filter(realm__domain=options["realm"])
|
|
|
|
|
2015-11-01 17:11:06 +01:00
|
|
|
print("Per-user online duration:\n")
|
2013-09-06 21:52:12 +02:00
|
|
|
total_duration = datetime.timedelta(0)
|
|
|
|
for user_profile in user_profile_query:
|
2013-10-17 00:12:30 +02:00
|
|
|
duration = seconds_usage_between(user_profile, day_start, day_end)
|
2013-09-06 21:52:12 +02:00
|
|
|
|
2013-10-17 00:12:30 +02:00
|
|
|
if duration == datetime.timedelta(0):
|
|
|
|
continue
|
2013-09-06 21:52:12 +02:00
|
|
|
|
|
|
|
total_duration += duration
|
2016-05-04 23:16:27 +02:00
|
|
|
print("%-*s%s" % (37, user_profile.email, duration,))
|
2013-09-06 21:52:12 +02:00
|
|
|
|
2015-11-01 17:11:06 +01:00
|
|
|
print("\nTotal Duration: %s" % (total_duration,))
|
|
|
|
print("\nTotal Duration in minutes: %s" % (total_duration.total_seconds() / 60.,))
|
|
|
|
print("Total Duration amortized to a month: %s" % (total_duration.total_seconds() * 30. / 60.,))
|
2013-09-06 21:52:12 +02:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2013-09-10 17:25:35 +02:00
|
|
|
help = """Report analytics of user activity on a per-user and realm basis.
|
|
|
|
|
|
|
|
This command aggregates user activity data that is collected by each user using Zulip. It attempts
|
|
|
|
to approximate how much each user has been using Zulip per day, measured by recording each 15 minute
|
|
|
|
period where some activity has occurred (mouse move or keyboard activity).
|
|
|
|
|
|
|
|
It will correctly not count server-initiated reloads in the activity statistics.
|
|
|
|
|
|
|
|
The duration flag can be used to control how many days to show usage duration for
|
|
|
|
|
2016-11-22 01:44:16 +01:00
|
|
|
Usage: ./manage.py analyze_user_activity [--realm=zulip.com] [--date=2013-09-10] [--duration=1]
|
2013-09-10 17:25:35 +02:00
|
|
|
|
|
|
|
By default, if no date is selected 2013-09-10 is used. If no realm is provided, information
|
|
|
|
is shown for all realms"""
|
|
|
|
|
2016-11-03 10:22:19 +01:00
|
|
|
def add_arguments(self, parser):
|
|
|
|
# type: (CommandParser) -> None
|
|
|
|
parser.add_argument('--realm', action='store')
|
|
|
|
parser.add_argument('--date', action='store', default="2013-09-06")
|
|
|
|
parser.add_argument('--duration', action='store', default=1, type=int,
|
|
|
|
help="How many days to show usage information for")
|
2013-09-06 21:52:12 +02:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (*Any, **Any) -> None
|
2013-09-06 21:52:12 +02:00
|
|
|
analyze_activity(options)
|