|
| 1 | +from functools import partial |
| 2 | +from itertools import chain, product |
| 3 | +from operator import attrgetter |
| 4 | + |
| 5 | +import pytz |
| 6 | +from django.db.models import Count, Max, Min, Sum |
| 7 | +from django.utils.datetime_safe import datetime |
| 8 | + |
1 | 9 | from pythonpro.dashboard.models import TopicInteraction as _TopicInteracion |
| 10 | +from pythonpro.modules.facade import get_entire_content_forest |
| 11 | +from pythonpro.modules.models import Topic |
2 | 12 |
|
3 | 13 |
|
4 | 14 | def has_watched_any_topic(user) -> bool: |
5 | 15 | return _TopicInteracion.objects.filter(user=user).exists() |
| 16 | + |
| 17 | + |
| 18 | +def calculate_topic_interaction_history(user): |
| 19 | + """ |
| 20 | + Calculate user's interaction history ordered by last topic interaction |
| 21 | + It will return a list of topics annotated with: |
| 22 | + 1. last_interaction: last time user interacted with topic |
| 23 | + 2. max_watched_time: max video time where user stopped watching |
| 24 | + 3. total_watched_time: sum of all time user spent watching video |
| 25 | + 4. interactions_count: number of times user started watching video |
| 26 | + 5. duration: topic duration based on the min time this class had |
| 27 | + :param user: |
| 28 | + :return: |
| 29 | + """ |
| 30 | + |
| 31 | + return Topic.objects.filter( |
| 32 | + topicinteraction__user=user |
| 33 | + ).annotate( |
| 34 | + last_interaction=Max('topicinteraction__creation'), |
| 35 | + max_watched_time=Max('topicinteraction__max_watched_time'), |
| 36 | + total_watched_time=Sum('topicinteraction__total_watched_time'), |
| 37 | + interactions_count=Count('topicinteraction'), |
| 38 | + duration=Min('topicinteraction__topic_duration') |
| 39 | + ).order_by('-last_interaction').select_related('chapter').select_related('chapter__section').select_related( |
| 40 | + 'chapter__section__module')[:20] |
| 41 | + |
| 42 | + |
| 43 | +def calculate_module_progresses(user): |
| 44 | + """ |
| 45 | + Calculate the user progress on all modules |
| 46 | + :param user: |
| 47 | + :return: |
| 48 | + """ |
| 49 | + # arbitrary default value for last interaction |
| 50 | + default_min_time = datetime(1970, 1, 1, tzinfo=pytz.utc) |
| 51 | + property_defaults = { |
| 52 | + 'last_interaction': default_min_time, |
| 53 | + 'max_watched_time': 0, |
| 54 | + 'total_watched_time': 0, |
| 55 | + 'interactions_count': 0, |
| 56 | + 'topics_count': 0, |
| 57 | + 'finished_topics_count': 0, |
| 58 | + 'duration': 0, |
| 59 | + } |
| 60 | + |
| 61 | + sum_with_start_0 = partial(sum, start=0) |
| 62 | + |
| 63 | + aggregation_functions = { |
| 64 | + 'last_interaction': partial(max, default=default_min_time), |
| 65 | + 'max_watched_time': sum_with_start_0, |
| 66 | + 'total_watched_time': sum_with_start_0, |
| 67 | + 'interactions_count': sum_with_start_0, |
| 68 | + 'topics_count': sum_with_start_0, |
| 69 | + 'finished_topics_count': sum_with_start_0, |
| 70 | + 'duration': sum_with_start_0, |
| 71 | + } |
| 72 | + |
| 73 | + def _aggregate_statistics(contents, content_children_property_name): |
| 74 | + for content, (property_, aggregation_function) in product(contents, aggregation_functions.items()): |
| 75 | + children = getattr(content, content_children_property_name) |
| 76 | + setattr(content, property_, aggregation_function(map(attrgetter(property_), children))) |
| 77 | + |
| 78 | + def _flaten(iterable, children_property_name): |
| 79 | + for i in iterable: |
| 80 | + for child in getattr(i, children_property_name): |
| 81 | + yield child |
| 82 | + |
| 83 | + def calculate_progression(content): |
| 84 | + try: |
| 85 | + return min(content.max_watched_time / content.duration, 1) |
| 86 | + except ZeroDivisionError: |
| 87 | + return 0 |
| 88 | + |
| 89 | + qs = Topic.objects.filter(topicinteraction__user=user).values('id').annotate( |
| 90 | + last_interaction=Max('topicinteraction__creation'), |
| 91 | + interactions_count=Max('topicinteraction'), |
| 92 | + max_watched_time=Max('topicinteraction__max_watched_time'), |
| 93 | + total_watched_time=Sum('topicinteraction__total_watched_time'), |
| 94 | + children_count=Count('topicinteraction'), |
| 95 | + duration=Min('topicinteraction__topic_duration')).all() |
| 96 | + |
| 97 | + user_interacted_topics = {t['id']: t for t in qs} |
| 98 | + modules = get_entire_content_forest() |
| 99 | + all_sections = list(_flaten(modules, 'sections')) |
| 100 | + all_chapters = list(_flaten(all_sections, 'chapters')) |
| 101 | + all_topics = list(_flaten(all_chapters, 'topics')) |
| 102 | + for topic, (property_, default_value) in product(all_topics, property_defaults.items()): |
| 103 | + user_interaction_data = user_interacted_topics.get(topic.id, {}) |
| 104 | + setattr(topic, property_, user_interaction_data.get(property_, default_value)) |
| 105 | + for topic in all_topics: |
| 106 | + topic.progress = calculate_progression(topic) |
| 107 | + topic.topics_count = 1 |
| 108 | + watched_to_end = topic.progress > 0.99 |
| 109 | + spent_half_time_watching = topic.total_watched_time * 2 > topic.duration |
| 110 | + topic.finished_topics_count = 1 if (watched_to_end and spent_half_time_watching) else 0 |
| 111 | + |
| 112 | + contents_with_children_property_name = [ |
| 113 | + (all_chapters, 'topics'), |
| 114 | + (all_sections, 'chapters'), |
| 115 | + (modules, 'sections') |
| 116 | + ] |
| 117 | + for contents, content_children_property_name in contents_with_children_property_name: |
| 118 | + _aggregate_statistics(contents, content_children_property_name) |
| 119 | + |
| 120 | + for content in chain(all_chapters, all_sections, modules): |
| 121 | + setattr(content, 'progress', calculate_progression(content)) |
| 122 | + |
| 123 | + return modules |
0 commit comments