2013-09-25 19:30:45 +02:00
|
|
|
from __future__ import absolute_import
|
2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
2013-09-25 19:30:45 +02:00
|
|
|
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from argparse import ArgumentParser
|
2013-09-25 19:30:45 +02:00
|
|
|
from django.core.management.base import BaseCommand
|
2016-06-04 16:52:18 +02:00
|
|
|
from django.db.models import Count, QuerySet
|
2017-02-26 03:37:55 +01:00
|
|
|
from django.utils import timezone
|
2013-09-25 19:30:45 +02:00
|
|
|
|
|
|
|
from zerver.models import UserActivity, UserProfile, Realm, \
|
2017-01-04 05:30:48 +01:00
|
|
|
get_realm, get_user_profile_by_email
|
2013-09-25 19:30:45 +02:00
|
|
|
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = """Report rough client activity globally, for a realm, or for a user
|
|
|
|
|
|
|
|
Usage examples:
|
|
|
|
|
2016-11-22 01:44:16 +01:00
|
|
|
./manage.py client_activity
|
2016-11-16 21:16:02 +01:00
|
|
|
./manage.py client_activity zulip
|
|
|
|
./manage.py client_activity hamlet@zulip.com"""
|
2013-09-25 19:30:45 +02:00
|
|
|
|
2015-08-21 02:10:41 +02:00
|
|
|
def add_arguments(self, parser):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (ArgumentParser) -> None
|
2015-08-21 02:10:41 +02:00
|
|
|
parser.add_argument('arg', metavar='<arg>', type=str, nargs='?', default=None,
|
|
|
|
help="realm or user to estimate client activity for")
|
|
|
|
|
2013-09-25 19:30:45 +02:00
|
|
|
def compute_activity(self, user_activity_objects):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (QuerySet) -> None
|
2013-09-25 19:30:45 +02:00
|
|
|
# Report data from the past week.
|
|
|
|
#
|
|
|
|
# This is a rough report of client activity because we inconsistently
|
|
|
|
# register activity from various clients; think of it as telling you
|
|
|
|
# approximately how many people from a group have used a particular
|
|
|
|
# client recently. For example, this might be useful to get a sense of
|
|
|
|
# how popular different versions of a desktop client are.
|
|
|
|
#
|
|
|
|
# Importantly, this does NOT tell you anything about the relative
|
|
|
|
# volumes of requests from clients.
|
2017-02-26 03:37:55 +01:00
|
|
|
threshold = timezone.now() - datetime.timedelta(days=7)
|
2013-09-25 19:30:45 +02:00
|
|
|
client_counts = user_activity_objects.filter(
|
|
|
|
last_visit__gt=threshold).values("client__name").annotate(
|
|
|
|
count=Count('client__name'))
|
|
|
|
|
|
|
|
total = 0
|
|
|
|
counts = []
|
|
|
|
for client_type in client_counts:
|
|
|
|
count = client_type["count"]
|
|
|
|
client = client_type["client__name"]
|
|
|
|
total += count
|
|
|
|
counts.append((count, client))
|
|
|
|
|
|
|
|
counts.sort()
|
|
|
|
|
|
|
|
for count in counts:
|
2015-11-01 17:11:06 +01:00
|
|
|
print("%25s %15d" % (count[1], count[0]))
|
|
|
|
print("Total:", total)
|
2013-09-25 19:30:45 +02:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (*Any, **str) -> None
|
2015-08-21 02:10:41 +02:00
|
|
|
if options['arg'] is None:
|
2013-09-25 19:30:45 +02:00
|
|
|
# Report global activity.
|
|
|
|
self.compute_activity(UserActivity.objects.all())
|
2015-08-21 02:10:41 +02:00
|
|
|
else:
|
|
|
|
arg = options['arg']
|
2013-09-25 19:30:45 +02:00
|
|
|
try:
|
|
|
|
# Report activity for a user.
|
2015-08-21 02:10:41 +02:00
|
|
|
user_profile = get_user_profile_by_email(arg)
|
2013-09-25 19:30:45 +02:00
|
|
|
self.compute_activity(UserActivity.objects.filter(
|
2017-01-24 07:06:13 +01:00
|
|
|
user_profile=user_profile))
|
2013-09-25 19:30:45 +02:00
|
|
|
except UserProfile.DoesNotExist:
|
|
|
|
try:
|
|
|
|
# Report activity for a realm.
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm(arg)
|
2013-09-25 19:30:45 +02:00
|
|
|
self.compute_activity(UserActivity.objects.filter(
|
2017-01-24 07:06:13 +01:00
|
|
|
user_profile__realm=realm))
|
2013-09-25 19:30:45 +02:00
|
|
|
except Realm.DoesNotExist:
|
2017-01-08 19:48:22 +01:00
|
|
|
print("Unknown user or realm %s" % (arg,))
|
2013-09-25 19:30:45 +02:00
|
|
|
exit(1)
|