Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize assorted Django operations #627

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions backend/clubs/management/commands/update_club_counts.py
aviupadhyayula marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.core.management.base import BaseCommand
from django.db.models import Count, Q
from django.db.models import Count, OuterRef, Q, Subquery

from clubs.models import Club

Expand All @@ -9,17 +9,23 @@

def handle(self, *args, **kwargs):
try:
queryset = Club.objects.all().annotate(
temp_favorite_count=Count("favorite", distinct=True),
temp_membership_count=Count(
"membership", distinct=True, filter=Q(active=True)
),
favorite_count_subquery = (

Check warning on line 12 in backend/clubs/management/commands/update_club_counts.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/management/commands/update_club_counts.py#L12

Added line #L12 was not covered by tests
Club.objects.filter(pk=OuterRef("pk"))
.annotate(count=Count("favorite", distinct=True))
.values("count")
)
membership_count_subquery = (

Check warning on line 17 in backend/clubs/management/commands/update_club_counts.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/management/commands/update_club_counts.py#L17

Added line #L17 was not covered by tests
Club.objects.filter(pk=OuterRef("pk"))
.annotate(
count=Count("membership", distinct=True, filter=Q(active=True))
)
.values("count")
)

for club in queryset:
club.favorite_count = club.temp_favorite_count
club.membership_count = club.temp_membership_count
Club.objects.bulk_update(queryset, ["favorite_count", "membership_count"])
Club.objects.update(

Check warning on line 25 in backend/clubs/management/commands/update_club_counts.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/management/commands/update_club_counts.py#L25

Added line #L25 was not covered by tests
favorite_count=Subquery(favorite_count_subquery),
membership_count=Subquery(membership_count_subquery),
)

self.stdout.write(
self.style.SUCCESS(
Expand Down
42 changes: 27 additions & 15 deletions backend/clubs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from django.core.exceptions import ValidationError
from django.core.mail import EmailMultiAlternatives
from django.core.validators import validate_email
from django.db import models, transaction
from django.db import models
from django.db.models import Sum
from django.dispatch import receiver
from django.template.loader import render_to_string
Expand Down Expand Up @@ -833,21 +833,33 @@ def create_events(
club_query = self.participating_clubs.all()
if filter is not None:
club_query = club_query.filter(filter)
events = []
with transaction.atomic():
for club in club_query:
obj, _ = Event.objects.get_or_create(
code=f"fair-{club.code}-{self.id}-{suffix}",
club=club,
type=Event.FAIR,
defaults={
"name": self.name,
"start_time": start_time,
"end_time": end_time,
},

# Find events that already exist
event_codes = [f"fair-{club.code}-{self.id}-{suffix}" for club in club_query]
existing_events = Event.objects.filter(code__in=event_codes).values_list(
"code", flat=True
)

# Create new events in bulk
new_events = []
for club in club_query:
event_code = f"fair-{club.code}-{self.id}-{suffix}"
if event_code not in existing_events:
new_events.append(
Event(
code=event_code,
club=club,
type=Event.FAIR,
name=self.name,
start_time=start_time,
end_time=end_time,
)
)
events.append(obj)
return events
Event.objects.bulk_create(new_events)

# Return all event objects
events = Event.objects.filter(code__in=event_codes)
return list(events)

def __str__(self):
fmt = "%b %d, %Y"
Expand Down
Loading
Loading