Skip to content

Commit

Permalink
Fix O(N) queries on staff semester view
Browse files Browse the repository at this point in the history
  • Loading branch information
he3lixxx committed Dec 29, 2022
1 parent e61b2b1 commit 5f339e8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
5 changes: 5 additions & 0 deletions evap/evaluation/templatetags/evaluation_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
register = Library()


@register.filter(name="len")
def _len(a):
return len(a)


@register.filter(name="zip")
def _zip(a, b):
return zip(a, b)
Expand Down
6 changes: 3 additions & 3 deletions evap/staff/templates/staff_semester_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,11 @@ <h3>
{{ course.responsibles_names }}
</div>
</td>
<td data-col="evaluation-count" data-order="{{ course.evaluations.count }}">
{% if course.evaluations.count == 0 %}
<td data-col="evaluation-count" data-order="{{ course.evaluations.all|len }}">
{% if course.evaluations.all|len == 0 %}
<span class="badge bg-warning">{% trans 'No evaluations' %}</span>
{% else %}
{{ course.evaluations.count }}
{{ course.evaluations.all|len }}
{% endif %}
</td>
<td class="icon-buttons">
Expand Down
5 changes: 4 additions & 1 deletion evap/staff/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def get_evaluations_with_prefetched_data(semester):
),
"course__degrees",
"course__responsibles",
"course__semester",
)
.annotate(
num_contributors=Count("contributions", filter=~Q(contributions__contributor=None), distinct=True),
Expand Down Expand Up @@ -180,7 +181,9 @@ def semester_view(request, semester_id):

evaluations = get_evaluations_with_prefetched_data(semester)
evaluations = sorted(evaluations, key=lambda cr: cr.full_name)
courses = Course.objects.filter(semester=semester)
courses = Course.objects.filter(semester=semester).prefetch_related(
"type", "degrees", "responsibles", "evaluations"
)

# semester statistics (per degree)
@dataclass
Expand Down

0 comments on commit 5f339e8

Please sign in to comment.