2017-11-16 00:55:49 +01:00
|
|
|
from argparse import ArgumentParser
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2019-05-03 23:20:39 +02:00
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2013-05-01 17:12:30 +02:00
|
|
|
from django.db.models import Q
|
2017-11-16 00:55:49 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.models import Message, Realm, Recipient, Stream, Subscription, get_realm
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2013-05-01 17:12:30 +02:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = "Generate statistics on the streams for a realm."
|
|
|
|
|
2017-11-05 06:54:00 +01:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"realms", metavar="<realm>", nargs="*", help="realm to generate statistics for"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2015-08-21 02:10:41 +02:00
|
|
|
|
2017-11-05 06:54:00 +01:00
|
|
|
def handle(self, *args: Any, **options: str) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
if options["realms"]:
|
2013-05-01 17:12:30 +02:00
|
|
|
try:
|
2021-02-12 08:20:45 +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-05-01 17:12:30 +02:00
|
|
|
else:
|
|
|
|
realms = Realm.objects.all()
|
|
|
|
|
|
|
|
for realm in realms:
|
|
|
|
streams = Stream.objects.filter(realm=realm).exclude(Q(name__istartswith="tutorial-"))
|
2019-01-31 22:39:08 +01:00
|
|
|
# private stream count
|
|
|
|
private_count = 0
|
|
|
|
# public stream count
|
|
|
|
public_count = 0
|
2013-05-01 17:12:30 +02:00
|
|
|
for stream in streams:
|
|
|
|
if stream.invite_only:
|
2019-01-31 22:39:08 +01:00
|
|
|
private_count += 1
|
|
|
|
else:
|
|
|
|
public_count += 1
|
|
|
|
print("------------")
|
2021-02-12 08:20:45 +01:00
|
|
|
print(realm.string_id, end=" ")
|
|
|
|
print("{:>10} {} public streams and".format("(", public_count), end=" ")
|
2020-06-13 08:59:37 +02:00
|
|
|
print(f"{private_count} private streams )")
|
2019-01-31 22:39:08 +01:00
|
|
|
print("------------")
|
2020-06-10 06:41:04 +02:00
|
|
|
print("{:>25} {:>15} {:>10} {:>12}".format("stream", "subscribers", "messages", "type"))
|
2019-01-31 22:39:08 +01:00
|
|
|
|
|
|
|
for stream in streams:
|
2019-01-31 23:14:25 +01:00
|
|
|
if stream.invite_only:
|
2021-02-12 08:20:45 +01:00
|
|
|
stream_type = "private"
|
2019-01-31 23:14:25 +01:00
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
stream_type = "public"
|
|
|
|
print(f"{stream.name:>25}", end=" ")
|
2013-05-01 17:12:30 +02:00
|
|
|
recipient = Recipient.objects.filter(type=Recipient.STREAM, type_id=stream.id)
|
2021-02-12 08:19:30 +01:00
|
|
|
print(
|
|
|
|
"{:10}".format(
|
|
|
|
len(Subscription.objects.filter(recipient=recipient, active=True))
|
|
|
|
),
|
2021-02-12 08:20:45 +01:00
|
|
|
end=" ",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2013-05-01 17:12:30 +02:00
|
|
|
num_messages = len(Message.objects.filter(recipient=recipient))
|
2021-02-12 08:20:45 +01:00
|
|
|
print(f"{num_messages:12}", end=" ")
|
2020-06-10 06:41:04 +02:00
|
|
|
print(f"{stream_type:>15}")
|
2015-11-01 17:11:06 +01:00
|
|
|
print("")
|