django-DefectDojo icon indicating copy to clipboard operation
django-DefectDojo copied to clipboard

[performance] GET /api/v2/products/ runs 7 queries per product and returns every active finding id (findings_list / open_findings_list)

Open Ludocius opened this issue 4 weeks ago • 0 comments

Description

GET /api/v2/products/ runs seven extra queries per product and returns an unbounded list of finding ids per product:

  • findings_list calls Product.open_findings_list(), which executes Finding.objects.filter(test__engagement__product=self, active=True).values_list("id") for each product and embeds every active finding id in the response (a product with 50k active findings returns 50k integers on every list page).
  • findings_count falls back to a COUNT(*) per product because the API viewset (unlike the UI) does not annotate active_finding_count in 2.50.1.
  • tags, product_meta, authorized_users and the users' groups are also loaded per product (no prefetch_related in ProductViewSet.get_queryset).

2.50.1: serializers.py#L2042-L2072, models.py#L1344-L1346, views.py#L1731-L1732. dev: the viewset now annotates the count and prefetches tags/meta/users (product/api/views.py#L92-L116), but findings_list still calls open_findings_list() per product (product/api/serializer.py#L74-L75, product/models.py#L295-L298); the model comment itself says it "should be deprecated or at least prefetched".

Test setup: official image defectdojo/defectdojo-django:2.50.1 + defectdojo-nginx:2.50.1, upstream docker-compose.yml (Postgres 17.6, Redis 7.2, 4 uWSGI processes x 4 threads), Docker Desktop on Windows 11. Data: 6,000 findings created through /api/v2/import-scan/ (two Generic Findings Import reports of 3,000 findings, one endpoint each) plus 30 empty products. Query counts come from django.test.utils.CaptureQueriesContext around django.test.Client requests executed inside the uwsgi container; load numbers from hey -z 20s -c 20 against nginx; memory/CPU from docker stats.

Measured: 1 product -> 11 queries / 29 KB (6,000 ids). 31 products (30 of them empty) -> 223 queries, 1,912 ms wall, 48 KB; hey -c 20 gives 5.7 req/s with p50 3.07 s / p90 5.02 s. In pg_stat_statements the open_findings_list statement was the single most expensive query of the whole session (4,030 calls, 11.5 s total), ahead of the import itself.

PoC

[API products] GET /api/v2/products/?limit=100      (31 products)
   HTTP 200 | 223 SQL queries | SQL time 235 ms | wall 1912 ms | body 48 KB
      31 x  SELECT COUNT(*) FROM "dojo_finding" INNER JOIN "dojo_test" ... INNER JOIN "dojo_engagement" ...
      31 x  SELECT "dojo_finding"."id" FROM "dojo_finding" INNER JOIN "dojo_test" ... (open_findings_list)
      31 x  SELECT ... FROM "dojo_tagulous_product_tags" ...
      31 x  SELECT ... FROM "dojo_dojometa" ...
      31 x  SELECT ... FROM "auth_user" ...
      31 x  SELECT ... FROM "dojo_dojo_group" ...

DD-PERF-PRODUCTS

Before / After

Before (dojo/models.py, used by ProductSerializer.get_findings_list)

def open_findings_list(self):
    findings = Finding.objects.filter(test__engagement__product=self, active=True).values_list("id", flat=True)
    return list(findings)

After: stop embedding ids in the list payload; keep the count annotated in bulk and point clients to the filtered findings endpoint

# dojo/product/api/serializer.py
class ProductSerializer(serializers.ModelSerializer):
    findings_count = serializers.SerializerMethodField()
    findings_url = serializers.SerializerMethodField()      # replaces findings_list

    def get_findings_count(self, obj) -> int:
        return obj.findings_count                            # satisfied by the active_finding_count annotation

    def get_findings_url(self, obj) -> str:
        return f"/api/v2/findings/?test__engagement__product={obj.id}&active=true"

If findings_list must stay for compatibility, serve it only on retrieve (not on list) and cap it (values_list("id")[:1000] with a findings_truncated flag), or make it opt-in via ?prefetch=findings. In 2.50.1 the viewset should also get the select_related/prefetch_related/annotate that dev already has.

Measured on the 2.50.1 image (commit 8292998); the same code is present on dev at 237458c. Links above point to both.

Ludocius avatar Nov 07 '26 04:11 Ludocius