def process(self, request, course, start_date, end_date, interval): download_stats = UserCourseSummary.objects \ .filter(course=course.id) \ .aggregated_stats('total_downloads', single=True) data = {} data['start_date'] = start_date data['end_date'] = end_date data['interval'] = interval form = DateRangeIntervalForm(initial=data) dates = [] if interval == 'days': daily_stats = CourseDailyStats.objects.filter(course=course, day__gte=start_date, day__lte=end_date) \ .annotate(stat_date=TruncDay('day')) \ .values('stat_date', 'type') \ .annotate(total=Sum('total')) dates = generate_graph_data(daily_stats, False) else: monthly_stats = CourseDailyStats.objects \ .filter(course=course, day__gte=start_date, day__lte=end_date) \ .annotate(month=TruncMonth('day'), year=TruncYear('day')) \ .values('month', 'year', 'type') \ .annotate(total=Sum('total')) \ .order_by('year', 'month') dates = generate_graph_data(monthly_stats, True) leaderboard = Points.get_leaderboard( constants.LEADERBOARD_HOMEPAGE_RESULTS_PER_PAGE, course) return render( request, 'course/detail.html', { 'course': course, 'monthly': interval == 'months', 'form': form, 'data': dates, 'download_stats': download_stats, 'leaderboard': leaderboard })
def get_activity(self, start_date, end_date, interval): if interval == 'days': daily_stats = CourseDailyStats.objects\ .filter(course=self.object, day__gte=start_date, day__lte=end_date) \ .annotate(stat_date=TruncDay('day')) \ .values('stat_date', 'type') \ .annotate(total=Sum('total')) return generate_graph_data(daily_stats, False) else: monthly_stats = CourseDailyStats.objects \ .filter(course=self.object, day__gte=start_date, day__lte=end_date) \ .annotate(month=TruncMonth('day'), year=TruncYear('day')) \ .values('month', 'year', 'type') \ .annotate(total=Sum('total')) \ .order_by('year', 'month') return generate_graph_data(monthly_stats, True)
def test_graph_data_daily(self): course = Course.objects.get(pk=1) start_date = datetime.datetime(2017, 1, 1) end_date = datetime.datetime(2017, 12, 31) daily_stats = CourseDailyStats.objects.filter(course=course, day__gte=start_date, day__lte=end_date) \ .annotate(stat_date=TruncDay('day')) \ .values('stat_date', 'type') \ .annotate(total=Sum('total')) result = generate_graph_data(daily_stats) self.assertEqual(34, len(result))
def test_graph_data_monthly(self): course = Course.objects.get(pk=1) start_date = datetime.datetime(2017, 1, 1) end_date = datetime.datetime(2017, 12, 31) monthly_stats = CourseDailyStats.objects \ .filter(course=course, day__gte=start_date, day__lte=end_date) \ .annotate(month=TruncMonth('day'), year=TruncYear('day')) \ .values('month', 'year', 'type') \ .annotate(total=Sum('total')) \ .order_by('year', 'month') result = generate_graph_data(monthly_stats, True) self.assertEqual(10, len(result))