2017-11-16 00:55:49 +01:00
|
|
|
import datetime
|
|
|
|
from argparse import ArgumentParser
|
2017-03-03 19:01:52 +01:00
|
|
|
from typing import Any, List
|
2016-06-04 16:52:18 +02:00
|
|
|
|
2013-02-02 14:24:21 +01:00
|
|
|
from django.core.management.base import BaseCommand
|
2013-05-01 17:01:47 +02:00
|
|
|
from django.db.models import Count
|
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, Recipient, Stream, \
|
|
|
|
Subscription, UserActivity, UserMessage, UserProfile, get_realm
|
2013-02-02 14:24:21 +01:00
|
|
|
|
2013-12-03 20:31:47 +01:00
|
|
|
MOBILE_CLIENT_LIST = ["Android", "ios"]
|
2013-04-05 16:59:31 +02:00
|
|
|
HUMAN_CLIENT_LIST = MOBILE_CLIENT_LIST + ["website"]
|
|
|
|
|
|
|
|
human_messages = Message.objects.filter(sending_client__name__in=HUMAN_CLIENT_LIST)
|
|
|
|
|
2013-02-02 14:24:21 +01:00
|
|
|
class Command(BaseCommand):
|
|
|
|
help = "Generate statistics on realm 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 active_users(self, realm: Realm) -> List[UserProfile]:
|
2013-03-08 17:11:35 +01:00
|
|
|
# Has been active (on the website, for now) in the last 7 days.
|
2017-04-15 04:03:56 +02:00
|
|
|
activity_cutoff = timezone_now() - datetime.timedelta(days=7)
|
2016-11-30 14:17:35 +01:00
|
|
|
return [activity.user_profile for activity in (
|
2017-01-24 07:06:13 +01:00
|
|
|
UserActivity.objects.filter(user_profile__realm=realm,
|
|
|
|
user_profile__is_active=True,
|
|
|
|
last_visit__gt=activity_cutoff,
|
|
|
|
query="/json/users/me/pointer",
|
|
|
|
client__name="website"))]
|
2013-03-08 17:11:35 +01:00
|
|
|
|
2017-11-05 06:54:00 +01:00
|
|
|
def messages_sent_by(self, user: UserProfile, days_ago: int) -> int:
|
2017-04-15 04:03:56 +02:00
|
|
|
sent_time_cutoff = timezone_now() - datetime.timedelta(days=days_ago)
|
2013-04-05 16:59:31 +02:00
|
|
|
return human_messages.filter(sender=user, pub_date__gt=sent_time_cutoff).count()
|
|
|
|
|
2017-11-05 06:54:00 +01:00
|
|
|
def total_messages(self, realm: Realm, days_ago: int) -> int:
|
2017-04-15 04:03:56 +02:00
|
|
|
sent_time_cutoff = timezone_now() - datetime.timedelta(days=days_ago)
|
2013-04-05 16:59:31 +02:00
|
|
|
return Message.objects.filter(sender__realm=realm, pub_date__gt=sent_time_cutoff).count()
|
|
|
|
|
2017-11-05 06:54:00 +01:00
|
|
|
def human_messages(self, realm: Realm, days_ago: int) -> int:
|
2017-04-15 04:03:56 +02:00
|
|
|
sent_time_cutoff = timezone_now() - datetime.timedelta(days=days_ago)
|
2013-04-05 16:59:31 +02:00
|
|
|
return human_messages.filter(sender__realm=realm, pub_date__gt=sent_time_cutoff).count()
|
|
|
|
|
2017-11-05 06:54:00 +01:00
|
|
|
def api_messages(self, realm: Realm, days_ago: int) -> int:
|
2013-04-05 16:59:31 +02:00
|
|
|
return (self.total_messages(realm, days_ago) - self.human_messages(realm, days_ago))
|
2013-02-02 14:24:21 +01:00
|
|
|
|
2017-11-05 06:54:00 +01:00
|
|
|
def stream_messages(self, realm: Realm, days_ago: int) -> int:
|
2017-04-15 04:03:56 +02:00
|
|
|
sent_time_cutoff = timezone_now() - datetime.timedelta(days=days_ago)
|
2013-04-05 16:59:31 +02:00
|
|
|
return human_messages.filter(sender__realm=realm, pub_date__gt=sent_time_cutoff,
|
|
|
|
recipient__type=Recipient.STREAM).count()
|
2013-02-02 14:24:21 +01:00
|
|
|
|
2017-11-05 06:54:00 +01:00
|
|
|
def private_messages(self, realm: Realm, days_ago: int) -> int:
|
2017-04-15 04:03:56 +02:00
|
|
|
sent_time_cutoff = timezone_now() - datetime.timedelta(days=days_ago)
|
2013-04-05 16:59:31 +02:00
|
|
|
return human_messages.filter(sender__realm=realm, pub_date__gt=sent_time_cutoff).exclude(
|
2013-02-02 14:24:21 +01:00
|
|
|
recipient__type=Recipient.STREAM).exclude(recipient__type=Recipient.HUDDLE).count()
|
|
|
|
|
2017-11-05 06:54:00 +01:00
|
|
|
def group_private_messages(self, realm: Realm, days_ago: int) -> int:
|
2017-04-15 04:03:56 +02:00
|
|
|
sent_time_cutoff = timezone_now() - datetime.timedelta(days=days_ago)
|
2013-04-05 16:59:31 +02:00
|
|
|
return human_messages.filter(sender__realm=realm, pub_date__gt=sent_time_cutoff).exclude(
|
2013-02-02 14:24:21 +01:00
|
|
|
recipient__type=Recipient.STREAM).exclude(recipient__type=Recipient.PERSONAL).count()
|
|
|
|
|
2017-11-05 06:54:00 +01:00
|
|
|
def report_percentage(self, numerator: float, denominator: float, text: str) -> None:
|
2013-03-08 17:11:35 +01:00
|
|
|
if not denominator:
|
|
|
|
fraction = 0.0
|
|
|
|
else:
|
|
|
|
fraction = numerator / float(denominator)
|
2015-11-01 17:11:06 +01:00
|
|
|
print("%.2f%% of" % (fraction * 100,), text)
|
2013-03-08 17:11:35 +01:00
|
|
|
|
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:21 +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:
|
2015-11-01 17:11:06 +01:00
|
|
|
print(e)
|
2013-02-02 14:24:21 +01:00
|
|
|
exit(1)
|
|
|
|
else:
|
|
|
|
realms = Realm.objects.all()
|
|
|
|
|
|
|
|
for realm in realms:
|
2016-11-16 21:16:02 +01:00
|
|
|
print(realm.string_id)
|
2013-03-08 17:11:35 +01:00
|
|
|
|
2013-05-01 17:01:08 +02:00
|
|
|
user_profiles = UserProfile.objects.filter(realm=realm, is_active=True)
|
2013-03-08 17:11:35 +01:00
|
|
|
active_users = self.active_users(realm)
|
|
|
|
num_active = len(active_users)
|
|
|
|
|
2015-11-01 17:11:06 +01:00
|
|
|
print("%d active users (%d total)" % (num_active, len(user_profiles)))
|
2013-05-01 16:58:09 +02:00
|
|
|
streams = Stream.objects.filter(realm=realm).extra(
|
2013-08-06 22:15:05 +02:00
|
|
|
tables=['zerver_subscription', 'zerver_recipient'],
|
|
|
|
where=['zerver_subscription.recipient_id = zerver_recipient.id',
|
|
|
|
'zerver_recipient.type = 2',
|
|
|
|
'zerver_recipient.type_id = zerver_stream.id',
|
|
|
|
'zerver_subscription.active = true']).annotate(count=Count("name"))
|
2015-11-01 17:11:06 +01:00
|
|
|
print("%d streams" % (streams.count(),))
|
2013-02-02 14:24:21 +01:00
|
|
|
|
|
|
|
for days_ago in (1, 7, 30):
|
2015-11-01 17:11:06 +01:00
|
|
|
print("In last %d days, users sent:" % (days_ago,))
|
2013-02-02 14:24:21 +01:00
|
|
|
sender_quantities = [self.messages_sent_by(user, days_ago) for user in user_profiles]
|
|
|
|
for quantity in sorted(sender_quantities, reverse=True):
|
2015-11-01 17:11:06 +01:00
|
|
|
print(quantity, end=' ')
|
|
|
|
print("")
|
2013-02-02 14:24:21 +01:00
|
|
|
|
2015-11-01 17:11:06 +01:00
|
|
|
print("%d stream messages" % (self.stream_messages(realm, days_ago),))
|
|
|
|
print("%d one-on-one private messages" % (self.private_messages(realm, days_ago),))
|
|
|
|
print("%d messages sent via the API" % (self.api_messages(realm, days_ago),))
|
|
|
|
print("%d group private messages" % (self.group_private_messages(realm, days_ago),))
|
2013-03-08 17:11:35 +01:00
|
|
|
|
2017-01-24 06:11:18 +01:00
|
|
|
num_notifications_enabled = len([x for x in active_users if x.enable_desktop_notifications])
|
2013-03-08 17:11:35 +01:00
|
|
|
self.report_percentage(num_notifications_enabled, num_active,
|
|
|
|
"active users have desktop notifications enabled")
|
|
|
|
|
2015-11-01 17:14:31 +01:00
|
|
|
num_enter_sends = len([x for x in active_users if x.enter_sends])
|
2013-03-08 17:11:35 +01:00
|
|
|
self.report_percentage(num_enter_sends, num_active,
|
|
|
|
"active users have enter-sends")
|
2013-03-08 16:00:40 +01:00
|
|
|
|
2013-04-05 16:59:31 +02:00
|
|
|
all_message_count = human_messages.filter(sender__realm=realm).count()
|
|
|
|
multi_paragraph_message_count = human_messages.filter(
|
2013-03-08 17:11:35 +01:00
|
|
|
sender__realm=realm, content__contains="\n\n").count()
|
|
|
|
self.report_percentage(multi_paragraph_message_count, all_message_count,
|
|
|
|
"all messages are multi-paragraph")
|
2013-05-01 17:26:13 +02:00
|
|
|
|
|
|
|
# Starred messages
|
|
|
|
starrers = UserMessage.objects.filter(user_profile__in=user_profiles,
|
|
|
|
flags=UserMessage.flags.starred).values(
|
|
|
|
"user_profile").annotate(count=Count("user_profile"))
|
2015-11-01 17:11:06 +01:00
|
|
|
print("%d users have starred %d messages" % (
|
|
|
|
len(starrers), sum([elt["count"] for elt in starrers])))
|
2013-05-01 17:26:13 +02:00
|
|
|
|
|
|
|
active_user_subs = Subscription.objects.filter(
|
|
|
|
user_profile__in=user_profiles, active=True)
|
|
|
|
|
|
|
|
# Streams not in home view
|
|
|
|
non_home_view = active_user_subs.filter(in_home_view=False).values(
|
|
|
|
"user_profile").annotate(count=Count("user_profile"))
|
2015-11-01 17:11:06 +01:00
|
|
|
print("%d users have %d streams not in home view" % (
|
|
|
|
len(non_home_view), sum([elt["count"] for elt in non_home_view])))
|
2013-05-01 17:26:13 +02:00
|
|
|
|
|
|
|
# Code block markup
|
|
|
|
markup_messages = human_messages.filter(
|
|
|
|
sender__realm=realm, content__contains="~~~").values(
|
|
|
|
"sender").annotate(count=Count("sender"))
|
2015-11-01 17:11:06 +01:00
|
|
|
print("%d users have used code block markup on %s messages" % (
|
|
|
|
len(markup_messages), sum([elt["count"] for elt in markup_messages])))
|
2013-05-01 17:26:13 +02:00
|
|
|
|
|
|
|
# Notifications for stream messages
|
2018-07-26 23:06:22 +02:00
|
|
|
notifications = active_user_subs.filter(desktop_notifications=True).values(
|
2013-05-01 17:26:13 +02:00
|
|
|
"user_profile").annotate(count=Count("user_profile"))
|
2015-11-01 17:11:06 +01:00
|
|
|
print("%d users receive desktop notifications for %d streams" % (
|
|
|
|
len(notifications), sum([elt["count"] for elt in notifications])))
|
2013-05-01 17:26:13 +02:00
|
|
|
|
2015-11-01 17:11:06 +01:00
|
|
|
print("")
|