2013-02-02 14:24:06 +01:00
|
|
|
import datetime
|
2017-11-16 00:55:49 +01:00
|
|
|
from argparse import ArgumentParser
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
2013-02-02 14:24:06 +01:00
|
|
|
|
2019-05-03 23:20:39 +02:00
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2017-04-15 04:03:56 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2017-02-25 21:50:51 +01:00
|
|
|
|
2017-11-16 00:55:49 +01:00
|
|
|
from zerver.models import Message, Realm, Stream, UserProfile, get_realm
|
2013-02-02 14:24:06 +01:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = "Generate statistics on user activity."
|
|
|
|
|
2017-11-05 06:54:00 +01:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2015-08-21 02:10:41 +02:00
|
|
|
parser.add_argument('realms', metavar='<realm>', type=str, nargs='*',
|
|
|
|
help="realm to generate statistics for")
|
|
|
|
|
2017-11-05 06:54:00 +01:00
|
|
|
def messages_sent_by(self, user: UserProfile, week: int) -> int:
|
2017-04-15 04:03:56 +02:00
|
|
|
start = timezone_now() - datetime.timedelta(days=(week + 1)*7)
|
|
|
|
end = timezone_now() - datetime.timedelta(days=week*7)
|
2013-02-02 14:24:06 +01:00
|
|
|
return Message.objects.filter(sender=user, pub_date__gt=start, pub_date__lte=end).count()
|
|
|
|
|
2017-11-05 06:54:00 +01:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2015-08-21 02:10:41 +02:00
|
|
|
if options['realms']:
|
2013-02-02 14:24:06 +01:00
|
|
|
try:
|
2017-01-04 05:30:48 +01:00
|
|
|
realms = [get_realm(string_id) for string_id in options['realms']]
|
2015-11-01 17:08:33 +01:00
|
|
|
except Realm.DoesNotExist as e:
|
2019-05-03 23:20:39 +02:00
|
|
|
raise CommandError(e)
|
2013-02-02 14:24:06 +01:00
|
|
|
else:
|
|
|
|
realms = Realm.objects.all()
|
|
|
|
|
|
|
|
for realm in realms:
|
2016-11-16 21:16:02 +01:00
|
|
|
print(realm.string_id)
|
2013-03-28 20:47:22 +01:00
|
|
|
user_profiles = UserProfile.objects.filter(realm=realm, is_active=True)
|
2015-11-01 17:11:06 +01:00
|
|
|
print("%d users" % (len(user_profiles),))
|
|
|
|
print("%d streams" % (len(Stream.objects.filter(realm=realm)),))
|
2013-02-02 14:24:06 +01:00
|
|
|
|
|
|
|
for user_profile in user_profiles:
|
2015-11-01 17:11:06 +01:00
|
|
|
print("%35s" % (user_profile.email,), end=' ')
|
2013-02-02 14:24:06 +01:00
|
|
|
for week in range(10):
|
2019-04-20 01:00:46 +02:00
|
|
|
print("%5d" % (self.messages_sent_by(user_profile, week),), end=' ')
|
2015-11-01 17:11:06 +01:00
|
|
|
print("")
|