Skip to content

Commit

Permalink
fix(stats): apply union only if needed
Browse files Browse the repository at this point in the history
The component sharing is rarely used so optimize for the case there is
no shared component at the expense of one additional (cheap) query in
case it is.
  • Loading branch information
nijel committed Feb 24, 2025
1 parent f240901 commit 8537b5b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 5 additions & 1 deletion weblate/trans/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,9 +564,13 @@ def filter_access(qs):

return self.get_child_components_filter(filter_access).order()

@cached_property
def has_shared_components(self) -> bool:
return self.shared_components.exists()

def get_child_components_filter(self, filter_callback):
own = filter_callback(self.component_set.defer_huge())
if self.shared_components.exists():
if self.has_shared_components:
shared = filter_callback(self.shared_components.defer_huge())
return own.union(shared)
return own
Expand Down
6 changes: 3 additions & 3 deletions weblate/utils/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -1081,9 +1081,9 @@ def get_translate_url(self):
@cached_property
def translation_set(self):
all_langs = self.language.translation_set.prefetch()
result = all_langs.filter(component__project=self.project).union(
all_langs.filter(component__links=self.project)
)
result = all_langs.filter(component__project=self.project)
if self.project.has_shared_components:
result = result.union(all_langs.filter(component__links=self.project))
for item in result:
item.is_shared = (
None
Expand Down

0 comments on commit 8537b5b

Please sign in to comment.