Skip to content

Commit f7fccba

Browse files
committed
Optimize V3 API queries
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent 73b2f75 commit f7fccba

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

‎vulnerabilities/api_v3.py‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -450,17 +450,17 @@ class AffectedByAdvisoriesViewSet(PackageAdvisoriesViewSet):
450450

451451

452452
def get_affected_advisories_bulk(packages, max_advisories, base_url):
453-
package_ids = [p.id for p in packages]
453+
package_ids = []
454+
package_ids_with_multiple_importers = []
455+
packages_without_multiple_importers = []
454456

455-
package_ids_with_multiple_importers = PackageV2.objects.filter(
456-
type__in=TYPES_WITH_MULTIPLE_IMPORTERS, id__in=package_ids
457-
).values_list("id", flat=True)
457+
for p in packages:
458+
package_ids.append(p.id)
458459

459-
packages_without_multiple_importers = (
460-
PackageV2.objects.filter(id__in=package_ids)
461-
.exclude(id__in=package_ids_with_multiple_importers)
462-
.only("id", "package_url")
463-
)
460+
if p.type in TYPES_WITH_MULTIPLE_IMPORTERS:
461+
package_ids_with_multiple_importers.append(p.id)
462+
else:
463+
packages_without_multiple_importers.append(p)
464464

465465
result = {}
466466

@@ -658,6 +658,9 @@ def get_affected_advisories_bulk(packages, max_advisories, base_url):
658658

659659
packages = list(packages_without_multiple_importers)
660660

661+
if not packages:
662+
return result
663+
661664
package_by_purl = {package.package_url: package for package in packages}
662665

663666
purls = list(package_by_purl.keys())
@@ -777,7 +780,9 @@ def get_fixing_advisories_bulk(packages, max_advisories, base_url):
777780
AdvisorySet.objects.filter(
778781
package_id__in=package_ids_with_multiple_importers,
779782
relation_type="fixing",
780-
).only(
783+
)
784+
.select_related("primary_advisory")
785+
.only(
781786
"id",
782787
"package_id",
783788
"primary_advisory__advisory_id",
@@ -813,6 +818,9 @@ def get_fixing_advisories_bulk(packages, max_advisories, base_url):
813818

814819
packages = list(packages_without_multiple_importers)
815820

821+
if not packages:
822+
return result
823+
816824
package_by_purl = {package.package_url: package for package in packages}
817825

818826
purls = list(package_by_purl.keys())

0 commit comments

Comments
 (0)