From 430aee6f87bf43047edc2444651340877f671d8d Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Thu, 26 Sep 2013 10:46:30 -0400 Subject: [PATCH] Add a "General" tab to the /activity summary report. It shows domains and how many active users they have. A user is consider active if they have done something at least as active as updating their pointer in the last day. Domains with no meaningful activity in the last two weeks are excluded from the report. (imported from commit 700cecfc7f1732e9ac3ea590177da18f75b01303) --- templates/zerver/realm_summary_table.html | 30 ++++++++++ zerver/views/__init__.py | 68 ++++++++++++++++++++++- 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 templates/zerver/realm_summary_table.html diff --git a/templates/zerver/realm_summary_table.html b/templates/zerver/realm_summary_table.html new file mode 100644 index 0000000000..0dee031374 --- /dev/null +++ b/templates/zerver/realm_summary_table.html @@ -0,0 +1,30 @@ + + + + + + + + + + + {% for row in rows %} + + + {# Domain #} + + + {# Active User Count #} + + + + {% endfor %} + + +
DomainActive User Count
+ + {{ row.domain }} + + + {{ row.active_user_count }} +
diff --git a/zerver/views/__init__.py b/zerver/views/__init__.py index c4a537968c..3655b69dcc 100644 --- a/zerver/views/__init__.py +++ b/zerver/views/__init__.py @@ -1834,6 +1834,67 @@ class ActivityTable(object): def content(self): return loader.render_to_string('zerver/activity_table.html', dict(table=self)) + +def dictfetchall(cursor): + "Returns all rows from a cursor as a dict" + desc = cursor.description + return [ + dict(zip([col[0] for col in desc], row)) + for row in cursor.fetchall() + ] + +def realm_summary_table(): + query = ''' + SELECT + realm.domain, + coalesce(user_counts.active_user_count, 0) active_user_count + FROM zerver_realm realm + LEFT OUTER JOIN + ( + SELECT + up.realm_id realm_id, + count(distinct(ua.user_profile_id)) active_user_count + FROM zerver_useractivity ua + JOIN zerver_userprofile up + ON up.id = ua.user_profile_id + WHERE + query in ( + '/json/send_message', + '/json/update_pointer' + ) + AND + last_visit > now() - interval '1 day' + GROUP BY realm_id + ) user_counts + ON user_counts.realm_id = realm.id + WHERE EXISTS ( + SELECT * + FROM zerver_useractivity ua + JOIN zerver_userprofile up + ON up.id = ua.user_profile_id + WHERE + query in ( + '/json/send_message', + '/json/update_pointer' + ) + AND + up.realm_id = realm.id + AND + last_visit > now() - interval '2 week' + ) + ORDER BY active_user_count DESC, domain ASC + ''' + + cursor = connection.cursor() + cursor.execute(query) + rows = dictfetchall(cursor) + cursor.close() + content = loader.render_to_string( + 'zerver/realm_summary_table.html', + dict(rows=rows) + ) + return dict(content=content) + def can_view_activity(request): return request.user.realm.domain == 'zulip.com' @@ -1854,7 +1915,12 @@ def get_activity(request, realm=REQ(default=None)): ("send_message", ["/api/v1/send_message"]), ) - data = [ + data = [] + + if realm is None: + data.append(('General', realm_summary_table())) + + data += [ ('Website', ActivityTable(realm, 'website', web_queries)), ('Mirror', ActivityTable(realm, 'zephyr_mirror', api_queries)), ('Desktop', ActivityTable(realm, 'desktop', api_queries)),