def get_course_mau_history_metrics(site, course_id, date_for, months_back): """Quick copy/modification of 'get_monthly_history_metric' for Course MAU """ date_for = as_date(date_for) history = [] for year, month, _ in previous_months_iterator( month_for=date_for, months_back=months_back, ): period = '{year}/{month}'.format(year=year, month=str(month).zfill(2)) active_users = get_mau_from_site_course(site=site, course_id=course_id, year=year, month=month) history.append(dict( period=period, value=active_users.count(), )) if history: # use the last entry current_month = history[-1]['value'] else: # This should work for float too since '0 == 0.0' resolves to True current_month = 0 return dict(current_month=current_month, history=history)
def test_get_mau_from_site_course(sm_test_data): """Basic test for coverage with simple check TODO: Improve by adding more students to the StudentModule instances for the first course constructed in the `sm_test_data` fixture data set. """ year_for = sm_test_data['year_for'] month_for = sm_test_data['month_for'] site = sm_test_data['site'] # Get first course, check student modules for that course_overview = sm_test_data['course_overviews'][0] users = get_mau_from_site_course(site=site, course_id=str(course_overview.id), year=year_for, month=month_for) course_sm = StudentModule.objects.filter(course_id=course_overview.id) sm_check = course_sm.values_list('student__id', flat=True).distinct() assert set(users) == set(sm_check)
def get_month_course_metrics(site, course_id, month_for, **_kwargs): """Returns a dict with the metrics for the given site, course, month This function provides first generation metrics Initially this function returns a partial set of the course monthly metrics: * active users * course enrollments * number of learners completed """ # TODO: handle invalid "month_for" exception # month, year = [int(val) for val in month_for.split('/')] # start_date = datetime.date(year=year, month=month, day=1) # end_date = datetime.date(year=year, month=month, day=days_in_month(start_date)) first_day, last_day = first_last_days_for_month(month_for) params_dict = dict(site=site, course_id=course_id, start_date=first_day, end_date=last_day) active_users = get_mau_from_site_course(site=site, course_id=course_id, year=first_day.year, month=first_day.month) course_enrollments = get_course_enrolled_users_for_time_period( **params_dict) num_learners_completed = get_course_num_learners_completed_for_time_period( **params_dict) avg_days_to_complete = get_course_average_days_to_complete_for_time_period( **params_dict) avg_progress = get_course_average_progress_for_time_period(**params_dict) return dict( course_id=course_id, month_for=month_for, active_users=active_users.count(), course_enrollments=course_enrollments, num_learners_completed=num_learners_completed, avg_days_to_complete=avg_days_to_complete, avg_progress=avg_progress, )