Skip to content

Commit

Permalink
Round of queryset optimizations #138
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Jul 26, 2024
1 parent e6c4248 commit f70c48a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 13 deletions.
13 changes: 13 additions & 0 deletions component_catalog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1633,6 +1633,19 @@ def annotate_sortable_identifier(self):
sortable_identifier=Concat(*PACKAGE_URL_FIELDS, "filename", output_field=CharField())
)

def only_rendering_fields(self):
"""
Minimum requirements to render a Package element in the UI.
"""
return self.only(
"uuid",
*PACKAGE_URL_FIELDS,
"filename",
"license_expression",
"dataspace__name",
"dataspace__show_usage_policy_in_user_views",
)


class Package(
ExternalReferenceMixin,
Expand Down
7 changes: 5 additions & 2 deletions license_library/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,11 @@ def for_expression(self, license_keys=None):
"short_name",
"spdx_license_key",
"is_exception",
"usage_policy",
"dataspace",
"usage_policy__label",
"usage_policy__icon",
"usage_policy__color_code",
"dataspace__name",
"dataspace__show_usage_policy_in_user_views",
).select_related("dataspace", "usage_policy")

if license_keys:
Expand Down
4 changes: 4 additions & 0 deletions product_portfolio/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,10 @@ def product_secured(self, user=None, perms="view_product"):
product_qs = Product.objects.get_queryset(user, perms)
return self.filter(product__in=product_qs)

def product(self, product):
"""Filter based on the provided ``product`` object."""
return self.filter(product=product)


class ProductComponentQuerySet(ProductSecuredQuerySet):
def catalogs(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<div class="h6 mt-2 mb-0 smaller">
{% if page_obj.paginator.count != total_count %}
{{ page_obj.paginator.count|intcomma }} of
<a href="#" hx-get="{{ request.path }}?all=true#inventory" hx-target="{{ tab_id_html }}">
<a href="#" hx-get="{{ request.path }}?all=true#dependencies" hx-target="{{ tab_id_html }}">
{{ total_count }} results
</a>
{% else %}
Expand Down Expand Up @@ -120,7 +120,7 @@
</a>
{% endif %}
</strong>
<div class="license-expression">
<div>
{{ dependency.resolved_to_package.license_expression_html|default_if_none:"" }}
</div>
{% else %}
Expand Down Expand Up @@ -156,7 +156,9 @@
<td colspan="10">
No results.
{% if filter_dependency.is_active %}
<a href="?all=true#dependencies">Clear search and filters</a>
<a href="#" hx-get="{{ request.path }}?all=true#dependencies" hx-target="{{ tab_id_html }}">
Clear search and filters
</a>
{% endif %}
</td>
</tr>
Expand Down
37 changes: 29 additions & 8 deletions product_portfolio/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,10 @@ def tab_notice(self):

def tab_hierarchy(self):
template = "product_portfolio/tabs/tab_hierarchy.html"
product = self.object

productcomponent_qs = (
self.object.productcomponents.select_related(
product.productcomponents.select_related(
"component",
)
.prefetch_related(
Expand All @@ -410,11 +412,12 @@ def tab_hierarchy(self):
)

declared_dependencies_prefetch = models.Prefetch(
"package__declared_dependencies", ProductDependency.objects.all()
"package__declared_dependencies",
ProductDependency.objects.product(product)
)

productpackage_qs = (
self.object.productpackages.select_related(
product.productpackages.select_related(
"package",
)
.prefetch_related(
Expand Down Expand Up @@ -648,7 +651,8 @@ def get_context_data(self, **kwargs):
"licenses", License.objects.select_related("usage_policy")
)
declared_dependencies_prefetch = models.Prefetch(
"package__declared_dependencies", ProductDependency.objects.all()
"package__declared_dependencies",
ProductDependency.objects.product(self.object)
)

productpackage_qs = (
Expand Down Expand Up @@ -964,9 +968,16 @@ class ProductTabDependenciesView(
def get_context_data(self, **kwargs):
context_data = super().get_context_data(**kwargs)

dependency_qs = self.object.dependencies.select_related(
"for_package__dataspace",
"resolved_to_package__dataspace",
dependency_qs = (
self.object.dependencies
.prefetch_related(
models.Prefetch(
"for_package", Package.objects.only_rendering_fields()
),
models.Prefetch(
"resolved_to_package", Package.objects.only_rendering_fields()
)
)
)

filter_dependency = DependencyFilterSet(
Expand All @@ -977,7 +988,17 @@ def get_context_data(self, **kwargs):
)

# Annotate the filtered queryset with the count of declared_dependencies
filtered_and_annotated_qs = filter_dependency.qs.with_resolved_to_dependencies_count()
filtered_and_annotated_qs = (
filter_dependency.qs
# TODO: This is not scoped by product
.with_resolved_to_dependencies_count()
.order_by(
"for_package__type",
"for_package__namespace",
"for_package__name",
"for_package__version",
)
)

paginator = Paginator(filtered_and_annotated_qs, self.paginate_by)
page_number = self.request.GET.get(self.query_dict_page_param)
Expand Down

0 comments on commit f70c48a

Please sign in to comment.