From f79ebeb92dffd4c8f8f9691dd88c338bca28f61c Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Mon, 13 May 2013 13:18:50 -0400 Subject: [PATCH] active_user_stats: Track user reading. (imported from commit 3e8ff76c1f355686af74b1dc4081a7c130deb2e9) --- zephyr/management/commands/active_user_stats.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/zephyr/management/commands/active_user_stats.py b/zephyr/management/commands/active_user_stats.py index 8fcf9d7368..bd4cba49c3 100644 --- a/zephyr/management/commands/active_user_stats.py +++ b/zephyr/management/commands/active_user_stats.py @@ -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))