Skip to content

Commit

Permalink
Only make one DB query when string param provided in URL (mozilla#3128)
Browse files Browse the repository at this point in the history
Previously, the slow (takes seconds in some cases) DB query was repeated twice.

Also: Refactor prefetching entities data.
  • Loading branch information
mathjazz authored Mar 7, 2024
1 parent e8f2b19 commit f47fa67
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
56 changes: 33 additions & 23 deletions pontoon/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2576,11 +2576,9 @@ def between_review_time_interval(self, locale, start, end):
)
)

def prefetch_active_translations(self, locale):
"""
Prefetch active translations for given locale.
"""
return self.prefetch_related(
def prefetch_entities_data(self, locale, preferred_source_locale):
# Prefetch active translations for given locale
entities = self.prefetch_related(
Prefetch(
"translation_set",
queryset=(
Expand All @@ -2595,18 +2593,31 @@ def prefetch_active_translations(self, locale):
)
)

def prefetch_alternative_originals(self, code):
"""
Prefetch approved translations for given preferred source locale.
"""
return self.prefetch_related(
# Prefetch related Translations, Resources, Projects and ProjectLocales
entities = entities.prefetch_related(
Prefetch(
"translation_set",
queryset=(Translation.objects.filter(locale__code=code, approved=True)),
to_attr="alternative_originals",
"resource__project__project_locale",
queryset=ProjectLocale.objects.filter(locale=locale),
to_attr="projectlocale",
)
)

# Prefetch approved translations for given preferred source locale
if preferred_source_locale != "":
entities = entities.prefetch_related(
Prefetch(
"translation_set",
queryset=(
Translation.objects.filter(
locale__code=preferred_source_locale, approved=True
)
),
to_attr="alternative_originals",
)
)

return entities

def reset_active_translations(self, locale):
"""
Reset active translation for given set of entities and locale.
Expand Down Expand Up @@ -3040,20 +3051,19 @@ def map_entities(
preferred_source_locale,
entities,
is_sibling=False,
requested_entity=None,
):
entities_array = []

# Prefetch related Translations, Resources, Projects and ProjectLocales
entities = entities.prefetch_active_translations(locale).prefetch_related(
Prefetch(
"resource__project__project_locale",
queryset=ProjectLocale.objects.filter(locale=locale),
to_attr="projectlocale",
)
)
entities = entities.prefetch_entities_data(locale, preferred_source_locale)

if preferred_source_locale != "":
entities = entities.prefetch_alternative_originals(preferred_source_locale)
# If requested entity not in the current page
if requested_entity and requested_entity not in [e.pk for e in entities]:
entities = list(entities) + list(
Entity.objects.filter(pk=requested_entity).prefetch_entities_data(
locale, preferred_source_locale
)
)

for entity in entities:
translation_array = []
Expand Down
16 changes: 4 additions & 12 deletions pontoon/base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,21 +188,13 @@ def _get_paginated_entities(locale, preferred_source_locale, project, form, enti
has_next = entities_page.has_next()
entities_to_map = entities_page.object_list

# If requested entity not on the first page
if form.cleaned_data["entity"]:
entity_pk = form.cleaned_data["entity"]
entities_to_map_pks = [e.pk for e in entities_to_map]

# TODO: entities_to_map.values_list() doesn't return entities from selected page
if entity_pk not in entities_to_map_pks:
if entity_pk in entities.values_list("pk", flat=True):
entities_to_map_pks.append(entity_pk)
entities_to_map = entities.filter(pk__in=entities_to_map_pks)

return JsonResponse(
{
"entities": Entity.map_entities(
locale, preferred_source_locale, entities_to_map
locale,
preferred_source_locale,
entities_to_map,
requested_entity=form.cleaned_data["entity"],
),
"has_next": has_next,
"stats": TranslatedResource.objects.stats(
Expand Down

0 comments on commit f47fa67

Please sign in to comment.