2013-10-10 21:37:26 +02:00
|
|
|
from __future__ import absolute_import
|
2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
2013-10-10 21:37:26 +02:00
|
|
|
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from argparse import ArgumentParser
|
2013-05-01 17:12:30 +02:00
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django.db.models import Q
|
2017-01-04 05:30:48 +01:00
|
|
|
from zerver.models import Realm, Stream, Message, Subscription, Recipient, get_realm
|
2013-05-01 17:12:30 +02:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = "Generate statistics on the streams for a realm."
|
|
|
|
|
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('realms', metavar='<realm>', type=str, nargs='*',
|
|
|
|
help="realm to generate statistics for")
|
|
|
|
|
2013-05-01 17:12:30 +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['realms']:
|
2013-05-01 17:12:30 +02: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:
|
2015-11-01 17:11:06 +01:00
|
|
|
print(e)
|
2013-05-01 17:12:30 +02:00
|
|
|
exit(1)
|
|
|
|
else:
|
|
|
|
realms = Realm.objects.all()
|
|
|
|
|
|
|
|
for realm in realms:
|
2016-11-16 21:16:02 +01:00
|
|
|
print(realm.string_id)
|
2015-11-01 17:11:06 +01:00
|
|
|
print("------------")
|
|
|
|
print("%25s %15s %10s" % ("stream", "subscribers", "messages"))
|
2013-05-01 17:12:30 +02:00
|
|
|
streams = Stream.objects.filter(realm=realm).exclude(Q(name__istartswith="tutorial-"))
|
|
|
|
invite_only_count = 0
|
|
|
|
for stream in streams:
|
|
|
|
if stream.invite_only:
|
|
|
|
invite_only_count += 1
|
|
|
|
continue
|
2015-11-01 17:11:06 +01:00
|
|
|
print("%25s" % (stream.name,), end=' ')
|
2013-05-01 17:12:30 +02:00
|
|
|
recipient = Recipient.objects.filter(type=Recipient.STREAM, type_id=stream.id)
|
2015-11-01 17:11:06 +01:00
|
|
|
print("%10d" % (len(Subscription.objects.filter(recipient=recipient, active=True)),), end=' ')
|
2013-05-01 17:12:30 +02:00
|
|
|
num_messages = len(Message.objects.filter(recipient=recipient))
|
2015-11-01 17:11:06 +01:00
|
|
|
print("%12d" % (num_messages,))
|
|
|
|
print("%d invite-only streams" % (invite_only_count,))
|
|
|
|
print("")
|