active_user_stats: Track user reading.

(imported from commit 3e8ff76c1f355686af74b1dc4081a7c130deb2e9)
This commit is contained in:
Tim Abbott 2013-05-13 13:18:50 -04:00 committed by Leo Franchi
parent 1960c8e8f1
commit f79ebeb92d
1 changed files with 15 additions and 1 deletions

View File

@ -3,7 +3,7 @@ from __future__ import absolute_import
from django.core.management.base import BaseCommand
from django.conf import settings
from zephyr.models import UserProfile, UserPresence
from zephyr.models import UserProfile, UserPresence, UserActivity
from zephyr.lib.utils import statsd, statsd_key
from datetime import datetime, timedelta
@ -43,3 +43,17 @@ class Command(BaseCommand):
print("\tUsers for %s: %s" % (hr, len(users)))
statsd.gauge("users.active.%s.%shr" % (statsd_key(realm, True), statsd_key(hr, True)), len(users))
# Also do stats for how many users have been reading the app.
users_reading = UserActivity.objects.select_related().filter(query="/json/update_message_flags")
user_info = {}
for activity in users_reading:
for bucket in hour_buckets:
if not bucket in user_info[activity.user_profile.realm.domain]:
user_info[activity.user_profile.realm.domain][bucket] = []
if datetime.now(activity.last_visit.tzinfo) - activity.last_visit < timedelta(hours=bucket):
user_info[activity.user_profile.realm.domain][bucket].append(activity.user_profile.email)
for realm, buckets in user_info.items():
print("Realm %s" % realm)
for hr, users in sorted(buckets.items()):
print("\tUsers reading for %s: %s" % (hr, len(users)))
statsd.gauge("users.reading.%s.%shr" % (statsd_key(realm, True), statsd_key(hr, True)), len(users))