Skip to content

Commit

Permalink
Improve efficiency and prefetching for people related people counts (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
blms committed Sep 17, 2024
1 parent a029f91 commit 6d801c0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions geniza/corpus/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from copy import deepcopy
from functools import cached_property
from itertools import chain
from time import sleep

from django.conf import settings
from django.contrib import admin, messages
Expand Down
44 changes: 43 additions & 1 deletion geniza/entities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime
from math import modf
from operator import itemgetter
from time import sleep

from django.conf import settings
from django.contrib.admin.models import CHANGE, LogEntry
Expand Down Expand Up @@ -475,6 +476,22 @@ def get_absolute_url(self):
else:
return None

@property
def related_people_count(self):
"""Get a count of related people without duplicates, taking into account
converse relationships"""
# used for indexing and display
people_relations = (
self.from_person.annotate(related_id=F("from_person"))
.values_list("related_id", flat=True)
.union(
self.to_person.annotate(
related_id=F("to_person"),
).values_list("related_id", flat=True)
)
)
return len(set(people_relations))

def related_people(self):
"""Set of all people related to this person, with relationship type and
any notes on the relationship, taking into account converse relations"""
Expand Down Expand Up @@ -805,6 +822,30 @@ def items_to_index(cls):
"names",
"role",
"relationships",
"from_person",
"to_person",
"personplacerelation_set",
Prefetch(
"persondocumentrelation_set",
queryset=PersonDocumentRelation.objects.select_related("type"),
),
Prefetch(
"documents",
queryset=Document.objects.prefetch_related("dating_set"),
),
)

@classmethod
def prep_index_chunk(cls, chunk):
"""Prefetch related information when indexing in chunks
(modifies queryset chunk in place)"""
models.prefetch_related_objects(
chunk,
"names",
"role",
"relationships",
"from_person",
"to_person",
"personplacerelation_set",
Prefetch(
"persondocumentrelation_set",
Expand All @@ -815,6 +856,7 @@ def items_to_index(cls):
queryset=Document.objects.prefetch_related("dating_set"),
),
)
return chunk

def index_data(self):
"""data for indexing in Solr"""
Expand All @@ -830,7 +872,7 @@ def index_data(self):
"url_s": self.get_absolute_url(),
# related object counts
"documents_i": self.documents.count(),
"people_i": self.relationships.count(),
"people_i": self.related_people_count,
"places_i": self.personplacerelation_set.count(),
# kinds of relationships to documents
"document_relation_ss": list(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!-- person detail page navigation -->
{# Translators: accessibility text label for person detail view tabs navigation #}
<nav aria-label="{% translate "tabs" %}" id="tabs">
{% with n_reldocs=person.documents.count n_relpeople=person.related_people|length n_relplaces=person.personplacerelation_set.count %}
{% with n_reldocs=person.documents.count n_relpeople=person.related_people_count n_relplaces=person.personplacerelation_set.count %}
<ul class="tabs">
{% url 'entities:person' slug=person.slug as person_url %}
{% translate "Person Details" as details_text %}
Expand Down
2 changes: 1 addition & 1 deletion geniza/entities/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ class PersonPeopleView(RelatedPeopleMixin, PersonDetailView):
def page_description(self):
"""Description of a person related people page, with count"""
obj = self.get_object()
count = len(obj.related_people())
count = obj.related_people_count
# Translators: description of related people page, for search engines
return ngettext(
"%(count)d related person",
Expand Down

0 comments on commit 6d801c0

Please sign in to comment.