def _annotate_with_enrollment(self, summary, trends, enrollment_data): """ Add weekly enrollment data to the summary and trends so we can display stats as a percentage of the total enrollment at the time. """ # Approximate the weekly enrollment using the last day of each week: enrollment_by_day = {datum['date']: datum['count'] for datum in enrollment_data} has_enrollment_data = any(enrollment_by_day.get(week['weekEnding']) for week in trends) if not has_enrollment_data: return for week in trends: week['enrollment'] = enrollment_by_day.get(week['weekEnding']) num_active = week.get('any', 0) if num_active == 0: week['active_percent'] = 0 elif week['enrollment']: week['active_percent'] = num_active / float(week['enrollment']) else: week['active_percent'] = None # Avoid divide-by-zero but add an entry for this column so it appears most_recent = trends[-1] summary_enrollment = enrollment_by_day.get(most_recent['weekEnding']) if summary_enrollment: for key in self.get_activity_types(): if summary.get(key): # Translators: '{percentage}' in as a placeholder for the percentage value. percent_str = _("{percentage} of current students") summary[key + '_percent_str'] = percent_str.format( percentage=metric_percentage(summary[key] / float(summary_enrollment)) ) elif key in summary: summary[key + '_percent_str'] = '--'
def _annotate_with_enrollment(self, summary, trends, enrollment_data): """ Add weekly enrollment data to the summary and trends so we can display stats as a percentage of the total enrollment at the time. """ # Approximate the weekly enrollment using the last day of each week: enrollment_by_day = { datum['date']: datum['count'] for datum in enrollment_data } has_enrollment_data = any( enrollment_by_day.get(week['weekEnding']) for week in trends) if not has_enrollment_data: return for week in trends: week['enrollment'] = enrollment_by_day.get(week['weekEnding']) num_active = week.get('any', 0) if num_active == 0: week['active_percent'] = 0 elif week['enrollment']: week['active_percent'] = num_active / float(week['enrollment']) else: week[ 'active_percent'] = None # Avoid divide-by-zero but add an entry for this column so it appears # Find the latest weekEnding in the trends that we have enrollment data for. # (should usually be trends[-1] unless the enrollment data somehow got behind) summary_enrollment = None trends_step_back = 1 while not summary_enrollment: if trends_step_back > len(trends): # No enrollment data for any of the dates in the activity trend data. # This should never be executed because of the has_enrollment_data check above. return most_recent = trends[-trends_step_back] summary_enrollment = enrollment_by_day.get( most_recent['weekEnding']) trends_step_back += 1 if summary_enrollment: for key in self.get_activity_types(): if summary.get(key): # Translators: '{percentage}' in as a placeholder for the percentage value. percent_str = _("{percentage} of current learners") summary[key + '_percent_str'] = percent_str.format( percentage=metric_percentage( summary[key] / float(summary_enrollment))) elif key in summary: summary[key + '_percent_str'] = '--'
def _annotate_with_enrollment(self, summary, trends, enrollment_data): """ Add weekly enrollment data to the summary and trends so we can display stats as a percentage of the total enrollment at the time. """ # Approximate the weekly enrollment using the last day of each week: enrollment_by_day = {datum['date']: datum['count'] for datum in enrollment_data} has_enrollment_data = any(enrollment_by_day.get(week['weekEnding']) for week in trends) if not has_enrollment_data: return for week in trends: week['enrollment'] = enrollment_by_day.get(week['weekEnding']) num_active = week.get('any', 0) if num_active == 0: week['active_percent'] = 0 elif week['enrollment']: week['active_percent'] = num_active / float(week['enrollment']) else: week['active_percent'] = None # Avoid divide-by-zero but add an entry for this column so it appears # Find the latest weekEnding in the trends that we have enrollment data for. # (should usually be trends[-1] unless the enrollment data somehow got behind) summary_enrollment = None trends_step_back = 1 while not summary_enrollment: if trends_step_back > len(trends): # No enrollment data for any of the dates in the activity trend data. # This should never be executed because of the has_enrollment_data check above. return most_recent = trends[-trends_step_back] summary_enrollment = enrollment_by_day.get(most_recent['weekEnding']) trends_step_back += 1 if summary_enrollment: for key in self.get_activity_types(): if summary.get(key): # Translators: '{percentage}' in as a placeholder for the percentage value. percent_str = _("{percentage} of current learners") summary[key + '_percent_str'] = percent_str.format( percentage=metric_percentage(summary[key] / float(summary_enrollment)) ) elif key in summary: summary[key + '_percent_str'] = '--'
def test_metric_percentage(self): self.assertEqual(dashboard_extras.metric_percentage(0), '0%') self.assertEqual(dashboard_extras.metric_percentage(0.009), '< 1%') self.assertEqual(dashboard_extras.metric_percentage(0.5066), '50.7%') self.assertEqual(dashboard_extras.metric_percentage(0.5044), '50.4%')
def test_metric_percentage(self): self.assertEqual(dashboard_extras.metric_percentage(0), '0%') self.assertEqual(dashboard_extras.metric_percentage(0.009), '< 1%') self.assertEqual(dashboard_extras.metric_percentage(0.5066), '50.7%') self.assertEqual(dashboard_extras.metric_percentage(0.5044), '50.4%')