<commit_before>
''' -- imports from python libraries -- '''
import os
import json
import time
import datetime
from pymongo import ASCENDING
''' imports from installed packages '''
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 *
from gnowsys_ndf.settings import GSTUDIO_LOGS_DIR_PATH
from gnowsys_ndf.ndf.views.analytics_methods import *


if not os.path.exists(GSTUDIO_LOGS_DIR_PATH):
    os.makedirs(GSTUDIO_LOGS_DIR_PATH)
log_file_name = 'fillCounter.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):
        try:
            ce_gst = node_collection.one({'_type': 'GSystemType', 'name': 'CourseEventGroup'},{'_id':1})
            all_course_events_cur = node_collection.find({'member_of': ce_gst._id})
            # Loop over each Announced Course and get their members (using author_set)
            for each_ce in all_course_events_cur:
                log_file.write("\n\n------- Course/Group: "+ str(each_ce._id) + " ----------")
                print "\n Group/Course: ", each_ce.name
                users_set = each_ce.author_set
                for each_user in users_set:
                    user_obj = User.objects.get(pk=each_user)
                    create_or_update_counter(user_obj,each_ce._id)
            log_file.close()
        except Exception as fillCounterError:
            print "\n Error occurred!!!" + str(fillCounterError)


def create_or_update_counter(user_obj, group_id):

    counter_obj = counter_collection.one({'user_id': user_obj.id, 'group_id': group_id})
    if counter_obj:
        counter_obj.enrolled = True
        counter_obj.save()
    else:
        counter_obj = counter_collection.collection.Counter()
        counter_obj.user_id=user_obj.id
        auth_obj= node_collection.one({'_type':'Author','created_by':user_obj.id})
        counter_obj.auth_id=ObjectId(auth_obj._id)
        counter_obj.group_id=ObjectId(group_id)
        counter_obj.last_update=datetime.datetime.now()
        counter_obj.enrolled = True
        counter_obj.save()
    if not counter_obj:
        log_file.write("\n\nCreating Counter for User: "+ str(user_obj.id)+ "  Group :"+ str(group_id))
        counter_obj = counter_collection.collection.Counter()
    else:
        log_file.write("\n\nUpdating Counter for User: "+ str(user_obj.id)+ "  Group :"+ str(group_id))
    auth_node = node_collection.one({'_type': 'Author', 'created_by': user_obj.id})

    analytics_instance = AnalyticsMethods(user_obj.id,user_obj.username, group_id)

    counter_obj.auth_id = auth_node._id
    counter_obj.group_id = group_id
    counter_obj.user_id = user_obj.id

    #counter_obj.modules_completed = analytics_instance.get_completed_modules_count()
    #counter_obj.units_completed = analytics_instance.get_completed_units_count()
    # Calculate points of user
    #counter_obj.course_score = analytics_instance.get_users_points()
    # Get all comments posted by user<commit_msg>enroll set to true in script<commit_after>
''' -- imports from python libraries -- '''
import os
import json
import time
import datetime
from pymongo import ASCENDING
''' imports from installed packages '''
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 *
from gnowsys_ndf.settings import GSTUDIO_LOGS_DIR_PATH
from gnowsys_ndf.ndf.views.analytics_methods import *


if not os.path.exists(GSTUDIO_LOGS_DIR_PATH):
    os.makedirs(GSTUDIO_LOGS_DIR_PATH)
log_file_name = 'fillCounter.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):
        try:
            ce_gst = node_collection.one({'_type': 'GSystemType', 'name': 'CourseEventGroup'},{'_id':1})
            all_course_events_cur = node_collection.find({'member_of': ce_gst._id})
            # Loop over each Announced Course and get their members (using author_set)
            for each_ce in all_course_events_cur:
                log_file.write("\n\n------- Course/Group: "+ str(each_ce._id) + " ----------")
                print "\n Group/Course: ", each_ce.name
                users_set = each_ce.author_set
                for each_user in users_set:
                    user_obj = User.objects.get(pk=each_user)
                    create_or_update_counter(user_obj,each_ce._id)
            log_file.close()
        except Exception as fillCounterError:
            print "\n Error occurred!!!" + str(fillCounterError)


def create_or_update_counter(user_obj, group_id):

    counter_obj = counter_collection.one({'user_id': user_obj.id, 'group_id': group_id})

    if not counter_obj:
        log_file.write("\n\nCreating Counter for User: "+ str(user_obj.id)+ "  Group :"+ str(group_id))
        counter_obj = counter_collection.collection.Counter()
    else:
        log_file.write("\n\nUpdating Counter for User: "+ str(user_obj.id)+ "  Group :"+ str(group_id))
    auth_node = node_collection.one({'_type': 'Author', 'created_by': user_obj.id})

    analytics_instance = AnalyticsMethods(user_obj.id,user_obj.username, group_id)

    counter_obj.auth_id = auth_node._id
    counter_obj.group_id = group_id
    counter_obj.user_id = user_obj.id
    counter_obj.enrolled = True
    #counter_obj.modules_completed = analytics_instance.get_completed_modules_count()
    #counter_obj.units_completed = analytics_instance.get_completed_units_count()
    # Calculate points of user
    #counter_obj.course_score = analytics_instance.get_users_points()
    # Get all comments posted by user
{'fraction_non_alphanumeric': 0.1218249474195114, 'fraction_numerical': 0.00032357223750202233, 'mean_word_length': 3.5058309037900877, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7659015', 'n_tokens_mistral': 1987, 'n_tokens_neox': 1929, 'n_words': 377}