Skip to content

Commit 8c94a74

Browse files
committed
Optimize dashboard tables related queries
1 parent f4056a8 commit 8c94a74

File tree

5 files changed

+39
-17
lines changed

5 files changed

+39
-17
lines changed

hypha/apply/dashboard/services.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
def get_paf_for_review(user, is_paf_approval_sequential):
77
"""Return a list of paf approvals ready for user's review"""
88

9+
user_groups = list(user.groups.all())
910
paf_approvals = PAFApprovals.objects.annotate(
1011
roles_count=Count("paf_reviewer_role__user_roles")
1112
).filter(
12-
roles_count=len(list(user.groups.all())),
13+
roles_count=len(user_groups),
1314
approved=False,
1415
)
1516

16-
for role in user.groups.all():
17+
for role in user_groups:
1718
paf_approvals = paf_approvals.filter(paf_reviewer_role__user_roles__id=role.id)
1819

1920
if is_paf_approval_sequential:

hypha/apply/dashboard/views.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,13 @@ def awaiting_reviews(self, submissions):
144144
}
145145

146146
def active_invoices(self):
147-
invoices = Invoice.objects.filter(
148-
project__lead=self.request.user,
149-
).in_progress()
147+
invoices = (
148+
Invoice.objects.filter(
149+
project__lead=self.request.user,
150+
)
151+
.in_progress()
152+
.select_related("project", "by")
153+
)
150154

151155
return {
152156
"count": invoices.count(),
@@ -160,13 +164,18 @@ def projects(self):
160164
data=self.request.GET or None, request=self.request, queryset=projects
161165
)
162166

167+
filtered_qs = filterset.qs
168+
163169
limit = 10
170+
limited_qs = list(filtered_qs[: limit + 1])
171+
has_more = len(limited_qs) > limit
172+
table_data = limited_qs[:limit]
164173

165174
return {
166-
"count": projects.count(),
175+
"count": filtered_qs.count(),
167176
"filterset": filterset,
168-
"table": ProjectsDashboardTable(data=projects[:limit], prefix="project-"),
169-
"display_more": projects.count() > limit,
177+
"table": ProjectsDashboardTable(data=table_data, prefix="project-"),
178+
"display_more": has_more,
170179
"url": reverse("apply:projects:all"),
171180
}
172181

@@ -492,6 +501,7 @@ def active_invoices(self):
492501
)
493502
return {"count": active_invoices.count(), "data": active_invoices}
494503

504+
# todo: if we don't need, we can remove these historical tables
495505
def historical_project_data(self):
496506
historical_projects = (
497507
Project.objects.filter(user=self.request.user).complete().for_table()

hypha/apply/projects/models/payment.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,18 @@ def in_progress(self):
6969
return self.exclude(status__in=[DECLINED, PAID])
7070

7171
def approved_by_staff(self):
72-
return self.filter(status=APPROVED_BY_STAFF)
72+
return self.filter(status=APPROVED_BY_STAFF).select_related("project", "by")
7373

7474
def approved_by_finance_1(self):
75-
return self.filter(status=APPROVED_BY_FINANCE)
75+
return self.filter(status=APPROVED_BY_FINANCE).select_related("project", "by")
7676

7777
def waiting_to_convert(self):
78-
return self.filter(status=APPROVED_BY_FINANCE)
78+
return self.filter(status=APPROVED_BY_FINANCE).select_related("project", "by")
7979

8080
def for_finance_1(self):
81-
return self.filter(status__in=[APPROVED_BY_STAFF, APPROVED_BY_FINANCE])
81+
return self.filter(
82+
status__in=[APPROVED_BY_STAFF, APPROVED_BY_FINANCE]
83+
).select_related("project", "by")
8284

8385
def rejected(self):
8486
return self.filter(status=DECLINED).order_by("-requested_at")

hypha/apply/projects/models/project.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,10 @@ def for_table(self):
173173
.with_outstanding_reports()
174174
.select_related(
175175
"report_config",
176+
"submission",
176177
"submission__page",
177178
"lead",
179+
"user",
178180
)
179181
)
180182

hypha/apply/todo/views.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,20 @@ def remove_tasks_of_related_obj(related_obj):
186186

187187

188188
def get_tasks_for_user(user):
189-
user_tasks = Task.objects.filter(user=user).annotate(
190-
group_count=Count("user_group")
189+
user_groups = list(user.groups.all())
190+
191+
user_tasks = (
192+
Task.objects.filter(user=user)
193+
.annotate(group_count=Count("user_group"))
194+
.select_related("user", "related_content_type")
191195
)
192-
user_group_tasks = Task.objects.annotate(group_count=Count("user_group")).filter(
193-
group_count=len(user.groups.all())
196+
user_group_tasks = (
197+
Task.objects.annotate(group_count=Count("user_group"))
198+
.filter(group_count=len(user_groups))
199+
.select_related("user", "related_content_type")
200+
.prefetch_related("user_group")
194201
)
195-
for group in user.groups.all():
202+
for group in user_groups:
196203
user_group_tasks = user_group_tasks.filter(user_group__id=group.id)
197204

198205
return user_tasks.union(user_group_tasks).order_by("-created_at")

0 commit comments

Comments
 (0)