From 06811bf93e5946c242dcff2e1b6bcb1d0c275b5e Mon Sep 17 00:00:00 2001 From: msj Date: Fri, 7 Jun 2024 09:53:05 -0400 Subject: [PATCH 1/7] Reduce db calls --- lametro/models.py | 2 +- lametro/templates/person/person.html | 4 ++-- lametro/views.py | 5 ++++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lametro/models.py b/lametro/models.py index 2e4fca2ee..307200362 100644 --- a/lametro/models.py +++ b/lametro/models.py @@ -358,7 +358,7 @@ class Meta: def slug_name(self): return slugify(self.name) - @property + @cached_property def latest_council_membership(self): filter_kwarg = { "organization__name": settings.OCD_CITY_COUNCIL_NAME, diff --git a/lametro/templates/person/person.html b/lametro/templates/person/person.html index 379f93cfb..f4a49132f 100644 --- a/lametro/templates/person/person.html +++ b/lametro/templates/person/person.html @@ -64,9 +64,9 @@

{% endif %} - {% if person.headshot_source %} + {% if headshot_source %}

- Credit: {{person.headshot_source}} + Credit: {{ headshot_source }}

{% endif %} diff --git a/lametro/views.py b/lametro/views.py index a88a60309..318c1cb8a 100644 --- a/lametro/views.py +++ b/lametro/views.py @@ -707,7 +707,8 @@ def get_context_data(self, **kwargs): context["sponsored_legislation"] = [] context["memberships_list"] = ( - person.current_memberships.exclude(organization__name="Board of Directors") + person.current_memberships.prefetch_related("organization") + .exclude(organization__name="Board of Directors") .annotate( index=Case( When(role="Chair", then=Value(0)), @@ -730,6 +731,8 @@ def get_context_data(self, **kwargs): except PersonLink.DoesNotExist: pass + context["headshot_source"] = person.headshot_source + return context From cf0e95a2dba328e9f9d2f5f6b0547282080113f6 Mon Sep 17 00:00:00 2001 From: msj Date: Fri, 7 Jun 2024 10:40:07 -0400 Subject: [PATCH 2/7] Try using iterator over persons' committee sponsorships --- lametro/views.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lametro/views.py b/lametro/views.py index 318c1cb8a..98845d024 100644 --- a/lametro/views.py +++ b/lametro/views.py @@ -701,8 +701,9 @@ def get_context_data(self, **kwargs): except AttributeError: context["map_geojson"] = None - if person.committee_sponsorships: - context["sponsored_legislation"] = person.committee_sponsorships + sponsored_legislation = list(person.committee_sponsorships.iterator()) + if sponsored_legislation: + context["sponsored_legislation"] = sponsored_legislation else: context["sponsored_legislation"] = [] From bdefd09104fbbfe3427cab39a600da8ebe8165c0 Mon Sep 17 00:00:00 2001 From: msj Date: Fri, 7 Jun 2024 10:49:32 -0400 Subject: [PATCH 3/7] Remove cached property --- lametro/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lametro/models.py b/lametro/models.py index 307200362..2392d925e 100644 --- a/lametro/models.py +++ b/lametro/models.py @@ -409,7 +409,7 @@ def board_office(self): return office_membership - @cached_property + @property def committee_sponsorships(self): """ This property returns a list of five bills, which have recent actions From 725032a73e9c61799c2a14c7bbd0a26288043fbb Mon Sep 17 00:00:00 2001 From: msj Date: Fri, 7 Jun 2024 10:52:45 -0400 Subject: [PATCH 4/7] Revert "Remove cached property" This reverts commit bdefd09104fbbfe3427cab39a600da8ebe8165c0. --- lametro/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lametro/models.py b/lametro/models.py index 2392d925e..307200362 100644 --- a/lametro/models.py +++ b/lametro/models.py @@ -409,7 +409,7 @@ def board_office(self): return office_membership - @property + @cached_property def committee_sponsorships(self): """ This property returns a list of five bills, which have recent actions From 879d13bc00c98244e335780ef018961a850a04c5 Mon Sep 17 00:00:00 2001 From: msj Date: Tue, 11 Jun 2024 09:10:13 -0400 Subject: [PATCH 5/7] Try prefetch --- lametro/models.py | 3 ++- lametro/views.py | 7 ++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/lametro/models.py b/lametro/models.py index 307200362..a910dd9ae 100644 --- a/lametro/models.py +++ b/lametro/models.py @@ -424,7 +424,8 @@ def committee_sponsorships(self): actions__organization__memberships__in=self.current_memberships, ) .order_by("-actions__date") - .distinct()[:5] + .distinct() + .prefetch_related("topics", "pseudo_topics")[:5] ) return qs diff --git a/lametro/views.py b/lametro/views.py index 98845d024..ba1d124eb 100644 --- a/lametro/views.py +++ b/lametro/views.py @@ -701,11 +701,8 @@ def get_context_data(self, **kwargs): except AttributeError: context["map_geojson"] = None - sponsored_legislation = list(person.committee_sponsorships.iterator()) - if sponsored_legislation: - context["sponsored_legislation"] = sponsored_legislation - else: - context["sponsored_legislation"] = [] + if self.request.GET.get("view") == "board-reports": + context["sponsored_legislation"] = person.committee_sponsorships.iterator() context["memberships_list"] = ( person.current_memberships.prefetch_related("organization") From 1b52e76769fc5bfab0fe3f7efaf25cb13df702df Mon Sep 17 00:00:00 2001 From: msj Date: Fri, 14 Jun 2024 10:19:13 -0400 Subject: [PATCH 6/7] Try subquery --- lametro/models.py | 23 +++++++++++++++-------- lametro/views.py | 2 +- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lametro/models.py b/lametro/models.py index a910dd9ae..08450ba63 100644 --- a/lametro/models.py +++ b/lametro/models.py @@ -15,7 +15,7 @@ from django.utils.functional import cached_property from django.core.cache import cache -from django.db.models import Prefetch, Case, When, Value, Q, F +from django.db.models import Prefetch, Case, When, Value, Q, F, Subquery from django.db.models.functions import Now, Cast from django.templatetags.static import static from opencivicdata.legislative.models import ( @@ -24,6 +24,7 @@ EventRelatedEntity, RelatedBill, BillVersion, + BillAction, ) from proxy_overrides.related import ProxyForeignKey @@ -417,15 +418,21 @@ def committee_sponsorships(self): Organizations do not include the Board of Directors. """ - qs = ( - LAMetroBill.objects.defer("extras") + actions = ( + BillAction.objects.select_related("organization") .filter( - actions__organization__classification="committee", - actions__organization__memberships__in=self.current_memberships, + organization__classification="committee", + organization__memberships__in=Subquery( + self.current_memberships.values("pk") + ), ) - .order_by("-actions__date") - .distinct() - .prefetch_related("topics", "pseudo_topics")[:5] + .order_by("-date") + ) + + qs = ( + LAMetroBill.objects.defer("extras") + .filter(actions__in=Subquery(actions.values("pk"))) + .distinct()[:5] ) return qs diff --git a/lametro/views.py b/lametro/views.py index ba1d124eb..a50b3e265 100644 --- a/lametro/views.py +++ b/lametro/views.py @@ -702,7 +702,7 @@ def get_context_data(self, **kwargs): context["map_geojson"] = None if self.request.GET.get("view") == "board-reports": - context["sponsored_legislation"] = person.committee_sponsorships.iterator() + context["sponsored_legislation"] = person.committee_sponsorships context["memberships_list"] = ( person.current_memberships.prefetch_related("organization") From 9e73f23f0de3408081cd697419a3314ff1f79153 Mon Sep 17 00:00:00 2001 From: msj Date: Fri, 14 Jun 2024 10:28:27 -0400 Subject: [PATCH 7/7] Don't move order by --- lametro/models.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/lametro/models.py b/lametro/models.py index 08450ba63..3f1a80066 100644 --- a/lametro/models.py +++ b/lametro/models.py @@ -418,20 +418,17 @@ def committee_sponsorships(self): Organizations do not include the Board of Directors. """ - actions = ( - BillAction.objects.select_related("organization") - .filter( - organization__classification="committee", - organization__memberships__in=Subquery( - self.current_memberships.values("pk") - ), - ) - .order_by("-date") + actions = BillAction.objects.select_related("organization").filter( + organization__classification="committee", + organization__memberships__in=Subquery( + self.current_memberships.values("pk") + ), ) qs = ( LAMetroBill.objects.defer("extras") .filter(actions__in=Subquery(actions.values("pk"))) + .order_by("-actions__date") .distinct()[:5] )