2013-02-02 14:24:21 +01:00
|
|
|
import datetime
|
|
|
|
import pytz
|
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand
|
2013-04-05 18:43:31 +02:00
|
|
|
from django.db.models import Q
|
2013-03-29 21:16:51 +01:00
|
|
|
from zephyr.models import UserProfile, Realm, Stream, Message, Recipient, UserActivity, \
|
|
|
|
Subscription
|
2013-02-02 14:24:21 +01:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = "Generate statistics on realm activity."
|
|
|
|
|
2013-03-08 17:11:35 +01:00
|
|
|
def active_users(self, realm):
|
|
|
|
# Has been active (on the website, for now) in the last 7 days.
|
|
|
|
activity_cutoff = datetime.datetime.now(tz=pytz.utc) - datetime.timedelta(days=7)
|
|
|
|
return [activity.user_profile for activity in \
|
|
|
|
UserActivity.objects.filter(user_profile__realm=realm,
|
|
|
|
last_visit__gt=activity_cutoff,
|
|
|
|
query="/json/update_pointer",
|
|
|
|
client__name="website")]
|
|
|
|
|
2013-02-02 14:24:21 +01:00
|
|
|
def messages_sent_by(self, user, days_ago):
|
|
|
|
sent_time_cutoff = datetime.datetime.now(tz=pytz.utc) - datetime.timedelta(days=days_ago)
|
|
|
|
return Message.objects.filter(sender=user, pub_date__gt=sent_time_cutoff).count()
|
|
|
|
|
|
|
|
def stream_messages(self, realm, days_ago):
|
|
|
|
sent_time_cutoff = datetime.datetime.now(tz=pytz.utc) - datetime.timedelta(days=days_ago)
|
|
|
|
return Message.objects.filter(sender__realm=realm, pub_date__gt=sent_time_cutoff,
|
|
|
|
recipient__type=Recipient.STREAM).count()
|
|
|
|
|
|
|
|
def private_messages(self, realm, days_ago):
|
|
|
|
sent_time_cutoff = datetime.datetime.now(tz=pytz.utc) - datetime.timedelta(days=days_ago)
|
|
|
|
return Message.objects.filter(sender__realm=realm, pub_date__gt=sent_time_cutoff).exclude(
|
|
|
|
recipient__type=Recipient.STREAM).exclude(recipient__type=Recipient.HUDDLE).count()
|
|
|
|
|
|
|
|
def group_private_messages(self, realm, days_ago):
|
|
|
|
sent_time_cutoff = datetime.datetime.now(tz=pytz.utc) - datetime.timedelta(days=days_ago)
|
|
|
|
return Message.objects.filter(sender__realm=realm, pub_date__gt=sent_time_cutoff).exclude(
|
|
|
|
recipient__type=Recipient.STREAM).exclude(recipient__type=Recipient.PERSONAL).count()
|
|
|
|
|
2013-03-08 17:11:35 +01:00
|
|
|
def report_percentage(self, numerator, denominator, text):
|
|
|
|
if not denominator:
|
|
|
|
fraction = 0.0
|
|
|
|
else:
|
|
|
|
fraction = numerator / float(denominator)
|
|
|
|
print "%.2f%% of" % (fraction * 100,), text
|
|
|
|
|
2013-02-02 14:24:21 +01:00
|
|
|
def handle(self, *args, **options):
|
|
|
|
if args:
|
|
|
|
try:
|
|
|
|
realms = [Realm.objects.get(domain=domain) for domain in args]
|
|
|
|
except Realm.DoesNotExist, e:
|
|
|
|
print e
|
|
|
|
exit(1)
|
|
|
|
else:
|
|
|
|
realms = Realm.objects.all()
|
|
|
|
|
|
|
|
for realm in realms:
|
|
|
|
print realm.domain
|
2013-03-08 17:11:35 +01:00
|
|
|
|
2013-02-02 14:24:21 +01:00
|
|
|
user_profiles = UserProfile.objects.filter(realm=realm)
|
2013-03-08 17:11:35 +01:00
|
|
|
active_users = self.active_users(realm)
|
|
|
|
num_active = len(active_users)
|
|
|
|
|
|
|
|
print "%d active users (%d total)" % (num_active, len(user_profiles))
|
2013-02-02 14:24:21 +01:00
|
|
|
print "%d streams" % (Stream.objects.filter(realm=realm).count(),)
|
|
|
|
|
|
|
|
for days_ago in (1, 7, 30):
|
|
|
|
print "In last %d days, users sent:" % (days_ago,)
|
|
|
|
sender_quantities = [self.messages_sent_by(user, days_ago) for user in user_profiles]
|
|
|
|
for quantity in sorted(sender_quantities, reverse=True):
|
|
|
|
print quantity,
|
|
|
|
print ""
|
|
|
|
|
|
|
|
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 group private messages" % (self.group_private_messages(realm, days_ago),)
|
2013-03-08 17:11:35 +01:00
|
|
|
|
|
|
|
num_notifications_enabled = len(filter(lambda x: x.enable_desktop_notifications == True,
|
|
|
|
active_users))
|
|
|
|
self.report_percentage(num_notifications_enabled, num_active,
|
|
|
|
"active users have desktop notifications enabled")
|
|
|
|
|
2013-02-02 14:24:21 +01:00
|
|
|
colorizers = 0
|
2013-03-08 17:11:35 +01:00
|
|
|
for profile in active_users:
|
2013-04-05 18:43:31 +02:00
|
|
|
if Subscription.objects.filter(user_profile=profile).filter(
|
|
|
|
~Q(color=Subscription.DEFAULT_STREAM_COLOR)).count() > 0:
|
2013-02-02 14:24:21 +01:00
|
|
|
colorizers += 1
|
2013-03-08 17:11:35 +01:00
|
|
|
self.report_percentage(colorizers, num_active,
|
|
|
|
"active users have colorized streams")
|
2013-03-08 16:00:40 +01:00
|
|
|
|
2013-03-08 17:11:35 +01:00
|
|
|
num_enter_sends = len(filter(lambda x: x.enter_sends, active_users))
|
|
|
|
self.report_percentage(num_enter_sends, num_active,
|
|
|
|
"active users have enter-sends")
|
2013-03-08 16:00:40 +01:00
|
|
|
|
2013-02-02 14:24:21 +01:00
|
|
|
all_message_count = Message.objects.filter(sender__realm=realm).count()
|
2013-03-08 17:11:35 +01:00
|
|
|
multi_paragraph_message_count = Message.objects.filter(
|
|
|
|
sender__realm=realm, content__contains="\n\n").count()
|
|
|
|
self.report_percentage(multi_paragraph_message_count, all_message_count,
|
|
|
|
"all messages are multi-paragraph")
|
2013-02-02 14:24:21 +01:00
|
|
|
print ""
|