activity: Clean up table computation a bit

(imported from commit c599d7ca572010c07b457ec15a305eef7dff78dd)
This commit is contained in:
Keegan McAllister 2012-12-04 16:14:44 -05:00
parent a54e009a3b
commit 83d86f33ca
2 changed files with 33 additions and 30 deletions

View File

@ -4,7 +4,7 @@
{% block content %}
{% for name, has_pointer, rows in data %}
{% for name, activity in data.iteritems %}
<h2>{{ name }} usage</h2>
<table class="table sortable table-striped table-bordered">
@ -13,7 +13,7 @@
<th>User</th>
<th>Last get_updates</th>
<th>Last send_message</th>
{% if has_pointer %}
{% if activity.has_pointer %}
<th>Last update_pointer</th>
<th>Number of pointer updates</th>
{% endif %}
@ -21,12 +21,12 @@
</tr>
</thead>
<tbody>
{% for row in rows %}
{% for email, row in activity.rows.iteritems %}
<tr>
<td><strong>{{ row.email }}</strong></td>
<td><strong>{{ email }}</strong></td>
<td sorttable_customkey="{{ row.get_updates_last|date:'YmdHi' }}">{{ row.get_updates_last }}</td>
<td sorttable_customkey="{{ row.send_message_last|date:'YmdHi' }}">{{ row.send_message_last }}</td>
{% if has_pointer %}
{% if activity.has_pointer %}
<td sorttable_customkey="{{ row.update_pointer_last|date:'YmdHi' }}">{{ row.update_pointer_last }}</td>
<td>{{ row.update_pointer_count }}</td>
{% endif %}

View File

@ -968,40 +968,43 @@ def json_fetch_api_key(request, user_profile, password=POST):
return json_error("Your username or password is incorrect.")
return json_success({"api_key": user_profile.api_key})
class ActivityTable(object):
def __init__(self, client_name, queries):
self.has_pointer = False
self.rows = {}
for url, query_name in queries:
if 'pointer' in query_name:
self.has_pointer = True
for record in UserActivity.objects.filter(
query=url,
client__name=client_name):
row = self.rows.setdefault(record.user_profile.user.email, {})
row[query_name + '_count'] = record.count
row[query_name + '_last' ] = record.last_visit
@login_required(login_url = settings.HOME_NOT_LOGGED_IN)
def get_activity(request):
user_profile = request.user.userprofile
if user_profile.realm.domain != "humbughq.com":
return HttpResponseRedirect(reverse('zephyr.views.login_page'))
def add_activity(activity, url, query_name, client_name):
for row in UserActivity.objects.filter(query=url,
client__name=client_name):
email = row.user_profile.user.email
activity.setdefault(email, {})
activity[email]['email'] = email
activity[email][query_name + '_count'] = row.count
activity[email][query_name + '_last'] = row.last_visit
web_queries = (
("/json/get_updates", "get_updates"),
("/json/send_message", "send_message"),
("/json/update_pointer", "update_pointer"),
)
website_activity = {}
add_activity(website_activity, "/json/get_updates", "get_updates", "website")
add_activity(website_activity, "/json/send_message", "send_message", "website")
add_activity(website_activity, "/json/update_pointer", "update_pointer", "website")
mirror_activity = {}
add_activity(mirror_activity, "/api/v1/get_messages", "get_updates", "zephyr_mirror")
add_activity(mirror_activity, "/api/v1/send_message", "send_message", "zephyr_mirror")
api_activity = {}
add_activity(api_activity, "/api/v1/get_messages", "get_updates", "API")
add_activity(api_activity, "/api/v1/send_message", "send_message", "API")
api_queries = (
("/api/v1/get_messages", "get_updates"),
("/api/v1/send_message", "send_message"),
)
return render_to_response('zephyr/activity.html',
{ 'data': [
('Website', True, website_activity.values()),
('Mirror', False, mirror_activity.values()),
('API', False, api_activity.values())
]}, context_instance=RequestContext(request))
{ 'data': {
'Website': ActivityTable('website', web_queries),
'Mirror': ActivityTable('zephyr_mirror', api_queries),
'API': ActivityTable('API', api_queries)
}}, context_instance=RequestContext(request))
@authenticated_api_view
@has_request_variables