Skip to content

Commit

Permalink
Optimize QuerySet performances and ordering #70
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <tdruez@nexb.com>
  • Loading branch information
tdruez committed May 29, 2024
1 parent 8b657a9 commit a6bc71f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
{% endif %}
connectNodes(jsPlumbHierarchy, connectionOptions);

{% if related_parent.parent.related_parents.exists %}
{% if related_parent.parent_count > 0 %}
var linkUrl = '{{ related_parent.parent.get_absolute_url }}#hierarchy';
addEndpointWithLink(jsPlumbHierarchy, target_id, 'LeftMiddle', linkUrl);
{% endif %}
Expand Down Expand Up @@ -58,7 +58,7 @@
{% endif %}
connectNodes(jsPlumbHierarchy, connectionOptions);

{% if related_child.child.children.exists %}
{% if related_child.child_count > 0 %}
var linkUrl = '{{ related_child.child.get_absolute_url }}#hierarchy';
addEndpointWithLink(jsPlumbHierarchy, source_id, 'RightMiddle', linkUrl);
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion component_catalog/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ def test_component_catalog_detail_view_owner_tab_hierarchy_availability(self):
self.client.login(username="nexb_user", password="t3st")
url = self.component1.get_absolute_url()
response = self.client.get(url)
self.assertNotContains(response, "jsPlumbHierarchy")
self.assertNotContains(response, "jsPlumb")
self.assertNotContains(response, "Selected Owner")
self.assertNotContains(response, "Child Owners")

Expand Down
41 changes: 31 additions & 10 deletions component_catalog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core import signing
from django.core.validators import EMPTY_VALUES
from django.db.models import Count
from django.db.models import Prefetch
from django.http import FileResponse
from django.http import Http404
Expand Down Expand Up @@ -630,18 +631,38 @@ def get_queryset(self):
"licenses",
)

related_children_qs = Subcomponent.objects.select_related(
"usage_policy",
).prefetch_related(
"licenses",
Prefetch("child", queryset=component_prefetch_qs),
related_children_qs = (
Subcomponent.objects.select_related(
"usage_policy",
)
.prefetch_related(
"licenses",
Prefetch("child", queryset=component_prefetch_qs),
)
.annotate(
child_count=Count("child__children"),
)
.order_by(
"child__name",
"child__version",
)
)

related_parents_qs = Subcomponent.objects.select_related(
"usage_policy",
).prefetch_related(
"licenses",
Prefetch("parent", queryset=component_prefetch_qs),
related_parents_qs = (
Subcomponent.objects.select_related(
"usage_policy",
)
.prefetch_related(
"licenses",
Prefetch("parent", queryset=component_prefetch_qs),
)
.annotate(
parent_count=Count("parent__related_parents"),
)
.order_by(
"parent__name",
"parent__version",
)
)

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
{% endif %}
connectNodes(jsPlumbHierarchy, connectionOptions);

{# Hierarchy is only supported on components, not yet on packages.
{% if relation.component.children.exists %}
{% if relation.children_count > 0 %}
var linkUrl = '{{ relation.component.get_absolute_url }}#hierarchy';
addEndpointWithLink(jsPlumbHierarchy, source_id, 'RightMiddle', linkUrl);
{% endif %}
Expand Down
3 changes: 3 additions & 0 deletions product_portfolio/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ def tab_hierarchy(self):
.prefetch_related(
"component__licenses",
)
.annotate(
children_count=Count("component__children"),
)
.order_by(
"feature",
"component",
Expand Down

0 comments on commit a6bc71f

Please sign in to comment.