diff --git a/gnowsys-ndf/gnowsys_ndf/ndf/management/commands/export_users_analytics.py b/gnowsys-ndf/gnowsys_ndf/ndf/management/commands/export_users_analytics.py new file mode 100644 index 0000000000..499383aaab --- /dev/null +++ b/gnowsys-ndf/gnowsys_ndf/ndf/management/commands/export_users_analytics.py @@ -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 \ No newline at end of file diff --git a/gnowsys-ndf/gnowsys_ndf/ndf/views/gcourse.py b/gnowsys-ndf/gnowsys_ndf/ndf/views/gcourse.py index 54f553f969..fe56a4e354 100644 --- a/gnowsys-ndf/gnowsys_ndf/ndf/views/gcourse.py +++ b/gnowsys-ndf/gnowsys_ndf/ndf/views/gcourse.py @@ -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 @@ -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", @@ -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):