Skip to content

Commit

Permalink
Merge pull request #1587 from kedar2a/master
Browse files Browse the repository at this point in the history
Added command to export (course) users analytics in CSV format.
  • Loading branch information
kedar2a authored Nov 22, 2016
2 parents 07c207a + f79ccdb commit 6651f64
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
''' -- imports from python libraries -- '''
import os
# import json
import time
import datetime
import csv

from pymongo import ASCENDING

''' imports from installed packages '''
from django.utils.text import slugify
from django.core.management.base import BaseCommand, CommandError
# from django.contrib.auth.models import User

''' imports from application folders/files '''
from gnowsys_ndf.ndf.models import Group
from gnowsys_ndf.settings import GSTUDIO_LOGS_DIR_PATH, GSTUDIO_DATA_ROOT
from gnowsys_ndf.ndf.views.gcourse import course_analytics
# from gnowsys_ndf.ndf.views.methods import get_group_name_id
# from gnowsys_ndf.ndf.views.analytics_methods import *

try:
from gnowsys_ndf.server_settings import GSTUDIO_INSTITUTE_ID
except Exception, e:
from gnowsys_ndf.settings import GSTUDIO_INSTITUTE_ID

if not os.path.exists(GSTUDIO_LOGS_DIR_PATH):
os.makedirs(GSTUDIO_LOGS_DIR_PATH)

log_file_name = 'export_users_analytics.log'
log_file_path = os.path.join(GSTUDIO_LOGS_DIR_PATH, log_file_name)
log_file = open(log_file_path, 'a+')
script_start_str = "######### Script ran on : " + time.strftime("%c") + " #########\n----------------\n"
log_file.write(str(script_start_str))

class Command(BaseCommand):

def handle(self, *args, **options):
print "Enter 'group name(case sensitive)' OR 'id': "
group_name_or_id = raw_input()
group_obj = Group.get_group_name_id(group_name_or_id, get_obj=True)

if not group_obj:
raise ValueError('\nSorry. Request could not be completed. \
\nGroup/Course, matching argument entered "' + group_name_or_id + '" does not exists!')

group_users = group_obj.author_set
# print group_users

# CSV file name-convention: schoolcode-course-name-datetimestamp.csv
try:
group_name = slugify(group_obj['name'])
except Exception, e:
print e
group_name = 'i2c'

# dt: date time
# e.g: '21-November-2016-19h-08m-10s'
dt = "{:%d-%B-%Y-%Hh-%Mm-%Ss}".format(datetime.datetime.now())

file_name = GSTUDIO_INSTITUTE_ID + '-' + group_name + '-' + dt + '.csv'

GSTUDIO_EXPORTED_CSVS_DIRNAME = 'gstudio-exported-users-analytics-csvs'
GSTUDIO_EXPORTED_CSVS_DIR_PATH = os.path.join('/data/', GSTUDIO_EXPORTED_CSVS_DIRNAME)

if not os.path.exists(GSTUDIO_EXPORTED_CSVS_DIR_PATH):
os.makedirs(GSTUDIO_EXPORTED_CSVS_DIR_PATH)

file_name_path = os.path.join(GSTUDIO_EXPORTED_CSVS_DIR_PATH, file_name)

for index, each_user in enumerate(group_users):
try:
analytics_data = course_analytics(None, group_obj._id, each_user, get_result_dict=True)
# refactor dict:
analytics_data.pop('users_points_breakup')
analytics_data.pop('users_points')

temp_units_stat_str = analytics_data['units_progress_stmt']
analytics_data['units_completed'] = int(temp_units_stat_str.split(' ')[0])
analytics_data['total_units'] = int(temp_units_stat_str.split(' ')[3])
analytics_data.pop('units_progress_stmt')

temp_modules_stat_str = analytics_data['module_progress_stmt']
analytics_data['modules_completed'] = int(temp_modules_stat_str.split(' ')[0])
analytics_data['total_modules'] = int(temp_modules_stat_str.split(' ')[3])
analytics_data.pop('module_progress_stmt')
# print analytics_data

with open(file_name_path, 'a') as f: # Just use 'w' mode in 3.x
w = csv.DictWriter(f, analytics_data.keys())
if index == 0:
w.writeheader()
w.writerow(analytics_data)

except Exception, e:
print "\nUser with id: " + str(each_user) + " does not exists!!"
continue
42 changes: 27 additions & 15 deletions gnowsys-ndf/gnowsys_ndf/ndf/views/gcourse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2472,25 +2472,31 @@ def course_filters(request, group_id):
)


@login_required
@get_execution_time
def course_analytics(request, group_id, user_id, render_template=False):


cache_key = u'course_analytics' + unicode(group_id) + "_" + unicode(user_id)
cache_result = cache.get(cache_key)
if cache_result:
return render_to_response("ndf/user_course_analytics.html",
cache_result,
context_instance = RequestContext(request)
)
# @login_required
# @get_execution_time
def course_analytics(request, group_id, user_id, render_template=False, get_result_dict=False):

# set get_result_dict=True to get only raw data in dict format,
# without being redirected to template. So that this method can
# use to get dict result data in shell or for any command.
# this will omit data from request.
if request and not get_result_dict:
cache_key = u'course_analytics' + unicode(group_id) + "_" + unicode(user_id)
cache_result = cache.get(cache_key)
if cache_result:
return render_to_response("ndf/user_course_analytics.html",
cache_result,
context_instance = RequestContext(request)
)

analytics_data = {}
data_points_dict = request.GET.get('data_points_dict', {})
# print '== data_points_dict: ', data_points_dict
data_points_dict = {}

if request and not get_result_dict:
data_points_dict = request.GET.get('data_points_dict', {})
# print '== data_points_dict: ', data_points_dict

if data_points_dict and not isinstance(data_points_dict, dict):
from gnowsys_ndf.settings import GSTUDIO_NOTE_CREATE_POINTS, GSTUDIO_QUIZ_CORRECT_POINTS, GSTUDIO_COMMENT_POINTS, GSTUDIO_FILE_UPLOAD_POINTS

data_points_dict = json.loads(data_points_dict)
# print "\n\ndata_points_dict",data_points_dict
Expand Down Expand Up @@ -2641,6 +2647,11 @@ def course_analytics(request, group_id, user_id, render_template=False):
analytics_data['users_points_breakup'] = counter_obj.get_all_user_points_dict()

del analytics_instance
# print analytics_data

if get_result_dict:
return analytics_data

cache.set(cache_key, analytics_data, 60*10)

return render_to_response("ndf/user_course_analytics.html",
Expand All @@ -2650,6 +2661,7 @@ def course_analytics(request, group_id, user_id, render_template=False):

# return HttpResponse(json.dumps(analytics_data))


@login_required
@get_execution_time
def course_analytics_admin(request, group_id):
Expand Down

0 comments on commit 6651f64

Please sign in to comment.