def retrieve(self, request, **kwargs): # pylint: disable=unused-argument """ TODO: Make sure we have a test to handle invalid or empty course id """ site, course_id = self.site_course_helper(kwargs.get('pk', '')) date_for = datetime.utcnow().date() month_for = '{}/{}'.format(date_for.month, date_for.year) data = metrics.get_month_course_metrics(site=site, course_id=course_id, month_for=month_for) return Response(data)
def list(self, request): """ Returns site metrics data for current month TODO: NEXT Add query params to get data from previous months TODO: Add paginagation """ site = django.contrib.sites.shortcuts.get_current_site(request) course_keys = figures.sites.get_course_keys_for_site(site) date_for = datetime.utcnow().date() month_for = '{}/{}'.format(date_for.month, date_for.year) data = [] for course_key in course_keys: data.append(metrics.get_month_course_metrics(site=site, course_id=str(course_key), month_for=month_for)) return Response(data)
def test_get_month_course_metrics(monkeypatch): site = SiteFactory() course_overview = CourseOverviewFactory() course_id = str(course_overview.id) [UserFactory(), UserFactory()] users_qs = get_user_model().objects.all().values_list('id') expected_data = dict( course_id=course_id, month_for='02/2020', active_users=users_qs.count(), course_enrollments=101, num_learners_completed=102, avg_days_to_complete=103, avg_progress=0.5, ) def mock_get_mau_from_site_course(site, course_id, year, month): assert year return users_qs # Set up the mocks that our function under test will call monkeypatch.setattr('figures.metrics.get_mau_from_site_course', mock_get_mau_from_site_course) monkeypatch.setattr( 'figures.metrics.get_course_enrolled_users_for_time_period', lambda **_kwargs: expected_data['course_enrollments']) monkeypatch.setattr( 'figures.metrics.get_course_num_learners_completed_for_time_period', lambda **_kwargs: expected_data['num_learners_completed']) monkeypatch.setattr( 'figures.metrics.get_course_average_days_to_complete_for_time_period', lambda **_kwargs: expected_data['avg_days_to_complete']) monkeypatch.setattr( 'figures.metrics.get_course_average_progress_for_time_period', lambda **_kwargs: expected_data['avg_progress']) data = get_month_course_metrics(site=site, course_id=course_id, month_for=expected_data['month_for']) assert data == expected_data