Skip to content

Commit

Permalink
Fix Precompute Topics Script for Manually Set Parent Courses (#685)
Browse files Browse the repository at this point in the history
* Fix Precompute Topics Script

* Add Comment
  • Loading branch information
shiva-menta authored Nov 13, 2024
1 parent 2e82fd3 commit c7289c1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 13 additions & 0 deletions backend/courses/management/commands/recompute_topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def recompute_topics(min_semester: str = None, verbose=False, allow_null_parent_
- Any single child of a parent inherits the parent's topic.
- Any child with siblings that exactly matches their parent in full_code
and title inherits the parent's topic.
- Any child with siblings that is the only child with a manually set
parent_course_id inherits the parent topic.
- Any child with siblings that does not exactly match their parent
gets its own topic.
These rules are applied sequentially in increasing order of semester.
Expand All @@ -53,11 +55,16 @@ def recompute_topics(min_semester: str = None, verbose=False, allow_null_parent_
Course.objects.filter(semester=semester)
.select_related("parent_course", "parent_course__topic")
.prefetch_related("parent_course__children")
.order_by("-manually_set_parent_course")
)
topics_to_create = []
topics_to_update = [] # .most_recent
courses_to_update = [] # .topic
visited_courses = set()

for course in courses:
if course.id in visited_courses:
continue
parent = course.parent_course
if not parent:
topics_to_create.append(Topic(most_recent=course))
Expand All @@ -74,6 +81,11 @@ def recompute_topics(min_semester: str = None, verbose=False, allow_null_parent_
parent.full_code == course.full_code
and parent.title == course.title
or parent.children.count() == 1
or (
sum([child.manually_set_parent_course for child in parent.children.all()])
== 1
and course.manually_set_parent_course
)
):
# If a parent has multiple children and none are an exact match, we consider
# all the child courses as "branched from" the old. But if one child is an exact
Expand All @@ -86,6 +98,7 @@ def recompute_topics(min_semester: str = None, verbose=False, allow_null_parent_
topics_to_update.append(course.topic)
courses_to_update.append(course)
for child in parent.children.all():
visited_courses.add(child.id)
if child.id == course.id and not branched_from:
continue
topics_to_create.append(Topic(most_recent=child, branched_from=parent.topic))
Expand Down
6 changes: 4 additions & 2 deletions backend/review/management/commands/precompute_pcr_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ def precompute_pcr_views(verbose=False, is_new_data=False):
# Bulk create / update objects.
if verbose:
print("Creating and updating objects.")
CachedReviewResponse.objects.bulk_create(objs_to_insert)
CachedReviewResponse.objects.bulk_update(objs_to_update, ["response", "expired"])
CachedReviewResponse.objects.bulk_create(objs_to_insert, batch_size=4000)
CachedReviewResponse.objects.bulk_update(
objs_to_update, ["response", "expired"], batch_size=4000
)

# Bulk delete objects.
if verbose:
Expand Down

0 comments on commit c7289c1

Please sign in to comment.