Skip to content

Commit

Permalink
refactor: speed up bluesheet_data
Browse files Browse the repository at this point in the history
The affiliation helper was extremely slow. Using
queryset annotation speeds it up by almost an order
of magnitude.
  • Loading branch information
jennifer-richards committed Aug 22, 2024
1 parent 34ce2ba commit 227e63a
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions ietf/meeting/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

from django.conf import settings
from django.contrib import messages
from django.db.models import Q
from django.db.models import OuterRef, Subquery, TextField, Q, Value
from django.db.models.functions import Coalesce
from django.template.loader import render_to_string
from django.utils import timezone
from django.utils.encoding import smart_str
Expand Down Expand Up @@ -149,19 +150,28 @@ def create_proceedings_templates(meeting):


def bluesheet_data(session):
def affiliation(meeting, person):
# from OidcExtraScopeClaims.scope_registration()
email_list = person.email_set.values_list("address")
q = Q(person=person, meeting=meeting) | Q(email__in=email_list, meeting=meeting)
reg = MeetingRegistration.objects.filter(q).exclude(affiliation="").first()
return reg.affiliation if reg else ""

attendance = Attended.objects.filter(session=session).order_by("time")
attendance = (
Attended.objects.filter(session=session)
.annotate(
affiliation=Coalesce(
Subquery(
MeetingRegistration.objects.filter(
Q(meeting=session.meeting),
Q(person=OuterRef("person")) | Q(email=OuterRef("person__email")),
).values("affiliation")[:1]
),
Value(""),
output_field=TextField(),
)
).distinct()
.order_by("time")
)

meeting = session.meeting
return [
{
"name": attended.person.plain_name(),
"affiliation": affiliation(meeting, attended.person),
"affiliation": attended.affiliation,
}
for attended in attendance
]
Expand Down

0 comments on commit 227e63a

Please sign in to comment.