Commit 2f5e1b9c by John Hess

Readable admin_dashboard

parent 85432353
# Create your views here. # Create your views here.
import json import json
from datetime import datetime from datetime import datetime
from django.http import HttpResponse, Http404 from django.http import Http404
from mitxmako.shortcuts import render_to_response
from student.models import CourseEnrollment, CourseEnrollmentAllowed
from django.contrib.auth.models import User
def dictfetchall(cursor): def dictfetchall(cursor):
'''Returns all rows from a cursor as a dict. '''Returns a list of all rows from a cursor as a column: result dict.
Borrowed from Django documentation''' Borrowed from Django documentation'''
desc = cursor.description desc = cursor.description
return [ table=[]
dict(zip([col[0] for col in desc], row)) table.append([col[0] for col in desc])
for row in cursor.fetchall() table = table + cursor.fetchall()
] print "Table: " + str(table)
return table
def SQL_query_to_list(cursor, query_string):
cursor.execute(query_string)
raw_result=dictfetchall(cursor)
print raw_result
return raw_result
def dashboard(request): def dashboard(request):
""" """
Quick hack to show staff enrollment numbers. This should be Slightly less hackish hack to show staff enrollment numbers and other
replaced with a real dashboard later. This version is a short-term simple queries.
bandaid for the next couple weeks.
All queries here should be indexed and simple. Mostly, this means don't
touch courseware_studentmodule, as tempting as it may be.
""" """
if not request.user.is_staff: if not request.user.is_staff:
raise Http404 raise Http404
queries = [] # results are passed to the template. The template knows how to render
queries.append("select count(user_id) as students, course_id from student_courseenrollment group by course_id order by students desc;") # two types of results: scalars and tables. Scalars should be represented
queries.append("select count(distinct user_id) as unique_students from student_courseenrollment;") # as "Visible Title": Value and tables should be lists of lists where each
queries.append("select registrations, count(registrations) from (select count(user_id) as registrations from student_courseenrollment group by user_id) as registrations_per_user group by registrations;") # inner list represents a single row of the table
results = {"scalars":{},"tables":{}}
# count how many users we have
results["scalars"]["Unique Usernames"]=User.objects.filter().count()
results["scalars"]["Activated Usernames"]=User.objects.filter(is_active=1).count()
# count how many enrollments we have
results["scalars"]["Total Enrollments Across All Courses"]=CourseEnrollment.objects.count()
# establish a direct connection to the database (for executing raw SQL)
from django.db import connection from django.db import connection
cursor = connection.cursor() cursor = connection.cursor()
results = []
for query in queries: # define the queries that will generate our user-facing tables
cursor.execute(query) # table queries need not take the form of raw SQL, but do in this case since
results.append(dictfetchall(cursor)) # the MySQL backend for django isn't very friendly with group by or distinct
table_queries = {}
table_queries["course enrollments"]="select count(user_id) as students, course_id from student_courseenrollment group by course_id order by students desc;"
table_queries["number of students in each number of classes"]="select registrations, count(registrations) from (select count(user_id) as registrations from student_courseenrollment group by user_id) as registrations_per_user group by registrations;"
# add the result for each of the table_queries to the results object
for query in table_queries.keys():
cursor.execute(table_queries[query])
results["tables"][query] = SQL_query_to_list(cursor, table_queries[query])
context={"results":results}
return HttpResponse(json.dumps(results, indent=4)) return render_to_response("admin_dashboard.html",context)
<%namespace name='static' file='static_content.html'/>
<%inherit file="main.html" />
<section class="container about">
<section class="basic_stats">
<div class="edx_summary">
<h2>edX-wide Summary</h2>
<table style="margin-left:auto;margin-right:auto;width:50%">
% for key in results["scalars"]:
<tr>
<td>${key}</td>
<td>${results["scalars"][key]}</td>
</tr>
% endfor
</table>
</div>
% for table in results["tables"]:
<br/>
<div class="table_display">
<h2>${table}</h2>
<table style="margin-left:auto;margin-right:auto;width:50%">
% for row in results["tables"][table]:
<tr>
% for column in row:
<td>${column}</td>
% endfor
</tr>
% endfor
</table>
</div>
% endfor
</section>
</section>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment