From f2be9f046a2812eb1906b71684945146ce39b803 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Wed, 16 Oct 2013 16:48:47 -0400 Subject: [PATCH] Create /queries page with canned queries for Waseem. These are some queries on API usage, desktop usage, and Android usage that would be of interest to Waseem. These will eventually be subsumed into /activity, but some interim data issues may make them easier to keep separate for now. (imported from commit 697a8496cbf4447d557a3fc89f64c1c4d3e67e70) --- templates/zerver/ad_hoc_queries.html | 39 ++++++ zerver/views/__init__.py | 195 +++++++++++++++++++++++++++ zproject/urls.py | 1 + 3 files changed, 235 insertions(+) create mode 100644 templates/zerver/ad_hoc_queries.html diff --git a/templates/zerver/ad_hoc_queries.html b/templates/zerver/ad_hoc_queries.html new file mode 100644 index 0000000000..2fe21ec5ff --- /dev/null +++ b/templates/zerver/ad_hoc_queries.html @@ -0,0 +1,39 @@ +{% extends "zerver/base.html" %} + +{% load compressed %} +{% load minified_js %} + + +{% block customhead %} +{{ block.super }} +{% minified_js 'activity' %} +{% compressed_css 'activity' %} +{% endblock %} + +{% block content %} + + {% for query in queries %} +

{{ query.title }}

+ + + + + + {% for col in query.cols %} + + {% endfor %} + + + + {% for row in query.rows %} + + {% for field in row %} + + {% endfor %} + + {% endfor %} + +
{{ col }}
{{ field }}
+ {% endfor %} + +{% endblock %} diff --git a/zerver/views/__init__.py b/zerver/views/__init__.py index 56c46d7a38..f015aea308 100644 --- a/zerver/views/__init__.py +++ b/zerver/views/__init__.py @@ -2036,6 +2036,201 @@ def user_activity_intervals(): def can_view_activity(request): return request.user.realm.domain == 'zulip.com' +@login_required(login_url = settings.HOME_NOT_LOGGED_IN) +@has_request_variables +def ad_hoc_queries(request): + def get_data(query, cols, title): + cursor = connection.cursor() + cursor.execute(query) + rows = cursor.fetchall() + cursor.close() + + data = dict( + rows=rows, + cols=cols, + title=title + ) + return data + + queries = [] + + ### + + title = 'Android usage' + + query = ''' + select + realm.domain, + up.id user_id, + client.name, + sum(count) as hits, + max(last_visit) as last_time + from zerver_useractivity ua + join zerver_client client on client.id = ua.client_id + join zerver_userprofile up on up.id = ua.user_profile_id + join zerver_realm realm on realm.id = up.realm_id + where + client.name like 'Android' + and + query = 'send_message_backend' + group by domain, up.id, client.name + having max(last_visit) > now() - interval '2 week' + order by domain, up.id, client.name + ''' + + cols = [ + 'Domain', + 'User id', + 'Name', + 'Hits', + 'Last time' + ] + + data = get_data(query, cols, title) + queries.append(data) + + ### + + title = 'Desktop users' + + query = ''' + select + realm.domain, + client.name, + sum(count) as hits, + max(last_visit) as last_time + from zerver_useractivity ua + join zerver_client client on client.id = ua.client_id + join zerver_userprofile up on up.id = ua.user_profile_id + join zerver_realm realm on realm.id = up.realm_id + where + client.name like 'desktop%%' + group by domain, client.name + having max(last_visit) > now() - interval '2 week' + order by domain, client.name + ''' + + cols = [ + 'Domain', + 'Client', + 'Hits', + 'Last time' + ] + + data = get_data(query, cols, title) + queries.append(data) + + ### + + title = 'Pure API' + + query = ''' + select + realm.domain, + sum(count) as hits, + max(last_visit) as last_time + from zerver_useractivity ua + join zerver_client client on client.id = ua.client_id + join zerver_userprofile up on up.id = ua.user_profile_id + join zerver_realm realm on realm.id = up.realm_id + where + query = '/api/v1/send_message' + and + client.name = 'API' + and + domain != 'zulip.com' + group by domain + having max(last_visit) > now() - interval '2 week' + order by domain + ''' + + cols = [ + 'Domain', + 'Hits', + 'Last time' + ] + + data = get_data(query, cols, title) + queries.append(data) + + ### + + title = 'Integrations by domain' + + query = ''' + select + realm.domain, + case + when client.name = 'phabricator' then 'phabricator' + else split_part(query, '/', 5) + end client_name, + sum(count) as hits, + max(last_visit) as last_time + from zerver_useractivity ua + join zerver_client client on client.id = ua.client_id + join zerver_userprofile up on up.id = ua.user_profile_id + join zerver_realm realm on realm.id = up.realm_id + where + client.name = 'phabricator' + or + query like '%%external%%' + group by domain, client_name + having max(last_visit) > now() - interval '2 week' + order by domain, client_name + ''' + + cols = [ + 'Domain', + 'Client', + 'Hits', + 'Last time' + ] + + data = get_data(query, cols, title) + queries.append(data) + + ### + + title = 'Integrations by client' + + query = ''' + select + case + when client.name = 'phabricator' then 'phabricator' + else split_part(query, '/', 5) + end client_name, + realm.domain, + sum(count) as hits, + max(last_visit) as last_time + from zerver_useractivity ua + join zerver_client client on client.id = ua.client_id + join zerver_userprofile up on up.id = ua.user_profile_id + join zerver_realm realm on realm.id = up.realm_id + where + client.name = 'phabricator' + or + query like '%%external%%' + group by client_name, domain + having max(last_visit) > now() - interval '2 week' + order by client_name, domain + ''' + + cols = [ + 'Client', + 'Domain', + 'Hits', + 'Last time' + ] + + data = get_data(query, cols, title) + queries.append(data) + + return render_to_response( + 'zerver/ad_hoc_queries.html', + dict(queries=queries), + context_instance=RequestContext(request) + ) + @login_required(login_url = settings.HOME_NOT_LOGGED_IN) @has_request_variables def get_activity(request, realm=REQ(default=None)): diff --git a/zproject/urls.py b/zproject/urls.py index cb10423459..4c58800a72 100644 --- a/zproject/urls.py +++ b/zproject/urls.py @@ -40,6 +40,7 @@ urlpatterns = patterns('', url(r'^activity$', 'zerver.views.get_activity'), + url(r'^queries$', 'zerver.views.ad_hoc_queries'), # Registration views, require a confirmation ID. url(r'^accounts/home/', 'zerver.views.accounts_home'),