def test_general_range(self): start_date = datetime.datetime(2016, 1, 1) end_date = datetime.datetime(2016, 1, 5) self.assertEqual(list(date_range(start_date, end_date)), [ datetime.datetime(2016, 1, 1), datetime.datetime(2016, 1, 2), datetime.datetime(2016, 1, 3), datetime.datetime(2016, 1, 4), ])
def test_general_range(self): start_date = datetime.datetime(2016, 1, 1) end_date = datetime.datetime(2016, 1, 5) self.assertEqual([date for date in date_range(start_date, end_date)], [ datetime.datetime(2016, 1, 1), datetime.datetime(2016, 1, 2), datetime.datetime(2016, 1, 3), datetime.datetime(2016, 1, 4), ])
def get_timeline(self, course_id, username): queryset = ModuleEngagement.objects.all().filter(course_id=course_id, username=username) \ .values('date', 'entity_type', 'event') \ .annotate(total_count=Sum('count')) \ .annotate(distinct_entity_count=Count('entity_id', distinct=True)) \ .order_by('date') timelines = [] for date, engagements in groupby(queryset, lambda x: (x['date'])): # Iterate over engagements for this day and create a single day with # engagement data. day = { u'date': date, } for engagement in engagements: engagement_type = EngagementType(engagement['entity_type'], engagement['event']) if engagement_type.is_counted_by_entity: count_delta = engagement['distinct_entity_count'] else: count_delta = engagement['total_count'] day[engagement_type.name] = day.get(engagement_type.name, 0) + count_delta timelines.append(day) # Fill in dates that may be missing, since the result store doesn't # store empty engagement entries. full_timeline = [] default_timeline_entry = { engagement_type: 0 for engagement_type in EngagementType.ALL_TYPES } for index, current_date in enumerate(timelines): full_timeline.append(current_date) try: next_date = timelines[index + 1] except IndexError: continue one_day = datetime.timedelta(days=1) if next_date['date'] > current_date['date'] + one_day: full_timeline += [ dict(date=date, **default_timeline_entry) for date in date_range(current_date['date'] + one_day, next_date['date']) ] return full_timeline
def get_timeline(self, course_id, username): queryset = ModuleEngagement.objects.all().filter(course_id=course_id, username=username) \ .values('date', 'entity_type', 'event') \ .annotate(total_count=Sum('count')) \ .annotate(distinct_entity_count=Count('entity_id', distinct=True)) \ .order_by('date') timelines = [] for date, engagements in groupby(queryset, lambda x: (x['date'])): # Iterate over engagements for this day and create a single day with # engagement data. day = { u'date': date, } for engagement in engagements: engagement_type = EngagementType(engagement['entity_type'], engagement['event']) if engagement_type.is_counted_by_entity: count_delta = engagement['distinct_entity_count'] else: count_delta = engagement['total_count'] day[engagement_type.name] = day.get(engagement_type.name, 0) + count_delta timelines.append(day) # Fill in dates that may be missing, since the result store doesn't # store empty engagement entries. full_timeline = [] default_timeline_entry = {engagement_type: 0 for engagement_type in EngagementType.ALL_TYPES} for index, current_date in enumerate(timelines): full_timeline.append(current_date) try: next_date = timelines[index + 1] except IndexError: continue one_day = datetime.timedelta(days=1) if next_date['date'] > current_date['date'] + one_day: full_timeline += [ dict(date=date, **default_timeline_entry) for date in date_range(current_date['date'] + one_day, next_date['date']) ] return full_timeline
def test_empty_range(self): same_date = datetime.datetime(2016, 1, 1) self.assertEqual(list(date_range(same_date, same_date)), [])
def test_delta_goes_past_end_date(self): start_date = datetime.datetime(2016, 1, 1) end_date = datetime.datetime(2016, 1, 3) time_delta = datetime.timedelta(days=5) self.assertEqual(list(date_range(start_date, end_date, time_delta)), [start_date])
def test_range_exclusive(self): start_date = datetime.datetime(2016, 1, 1) end_date = datetime.datetime(2016, 1, 2) self.assertEqual(list(date_range(start_date, end_date)), [start_date])
def test_empty_range(self): date = datetime.datetime(2016, 1, 1) self.assertEqual([date for date in date_range(date, date)], [])
def test_delta_goes_past_end_date(self): start_date = datetime.datetime(2016, 1, 1) end_date = datetime.datetime(2016, 1, 3) time_delta = datetime.timedelta(days=5) self.assertEqual([date for date in date_range(start_date, end_date, time_delta)], [start_date])
def test_range_exclusive(self): start_date = datetime.datetime(2016, 1, 1) end_date = datetime.datetime(2016, 1, 2) self.assertEqual([date for date in date_range(start_date, end_date)], [start_date])