@@ -0,0 +1,2609 @@
+from __future__ import absolute_import%0A%0Afrom django.core.management.base import BaseCommand%0Afrom django.db.models import Count%0A%0Afrom zerver.models import UserActivity, UserProfile, Realm, %5C%0A    get_realm, get_user_profile_by_email%0A%0Aimport datetime%0A%0Aclass Command(BaseCommand):%0A    help = %22%22%22Report rough client activity globally, for a realm, or for a user%0A%0AUsage examples:%0A%0Apython manage.py client-activity%0Apython manage.py client-activity zulip.com%0Apython manage.py client-activity jesstess@zulip.com%22%22%22%0A%0A    def compute_activity(self, user_activity_objects):%0A        # Report data from the past week.%0A        #%0A        # This is a rough report of client activity because we inconsistently%0A        # register activity from various clients; think of it as telling you%0A        # approximately how many people from a group have used a particular%0A        # client recently. For example, this might be useful to get a sense of%0A        # how popular different versions of a desktop client are.%0A        #%0A        # Importantly, this does NOT tell you anything about the relative%0A        # volumes of requests from clients.%0A        threshold = datetime.datetime.now() - datetime.timedelta(days=7)%0A        client_counts = user_activity_objects.filter(%0A            last_visit__gt=threshold).values(%22client__name%22).annotate(%0A            count=Count('client__name'))%0A%0A        total = 0%0A        counts = %5B%5D%0A        for client_type in client_counts:%0A            count = client_type%5B%22count%22%5D%0A            client = client_type%5B%22client__name%22%5D%0A            total += count%0A            counts.append((count, client))%0A%0A        counts.sort()%0A%0A        for count in counts:%0A            print %22%2525s %2515d%22 %25 (count%5B1%5D, count%5B0%5D)%0A        print %22Total:%22, total%0A%0A%0A    def handle(self, *args, **options):%0A        if len(args) == 0:%0A            # Report global activity.%0A            self.compute_activity(UserActivity.objects.all())%0A        elif len(args) == 1:%0A            try:%0A                # Report activity for a user.%0A                user_profile = get_user_profile_by_email(args%5B0%5D)%0A                self.compute_activity(UserActivity.objects.filter(%0A                        user_profile=user_profile))%0A            except UserProfile.DoesNotExist:%0A                try:%0A                    # Report activity for a realm.%0A                    realm = get_realm(args%5B0%5D)%0A                    self.compute_activity(UserActivity.objects.filter(%0A                            user_profile__realm=realm))%0A                except Realm.DoesNotExist:%0A                    print %22Unknown user or domain %25s%22 %25 (args%5B0%5D,)%0A                    exit(1)%0A

Add a utility to report some rough recent client activity metrics.