Skip to content

Commit d6969d6

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

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
@@ -547,17 +547,17 @@ def build_patch_set_map(patches_map, advisory_ids_by_set):
547547

548548

549549
def get_affected_advisories_bulk(packages, max_advisories, base_url, reachability=False):
550-
package_ids = [p.id for p in packages]
550+
package_ids = []
551+
package_ids_with_multiple_importers = []
552+
packages_without_multiple_importers = []
551553

552-
package_ids_with_multiple_importers = PackageV2.objects.filter(
553-
type__in=TYPES_WITH_MULTIPLE_IMPORTERS, id__in=package_ids
554-
).values_list("id", flat=True)
554+
for p in packages:
555+
package_ids.append(p.id)
555556

556-
packages_without_multiple_importers = (
557-
PackageV2.objects.filter(id__in=package_ids)
558-
.exclude(id__in=package_ids_with_multiple_importers)
559-
.only("id", "package_url")
560-
)
557+
if p.type in TYPES_WITH_MULTIPLE_IMPORTERS:
558+
package_ids_with_multiple_importers.append(p.id)
559+
else:
560+
packages_without_multiple_importers.append(p)
561561

562562
result = {}
563563

@@ -780,6 +780,9 @@ def get_affected_advisories_bulk(packages, max_advisories, base_url, reachabilit
780780

781781
packages = list(packages_without_multiple_importers)
782782

783+
if not packages:
784+
return result
785+
783786
package_by_purl = {package.package_url: package for package in packages}
784787

785788
purls = list(package_by_purl.keys())
@@ -901,7 +904,9 @@ def get_fixing_advisories_bulk(packages, max_advisories, base_url):
901904
AdvisorySet.objects.filter(
902905
package_id__in=package_ids_with_multiple_importers,
903906
relation_type="fixing",
904-
).only(
907+
)
908+
.select_related("primary_advisory")
909+
.only(
905910
"id",
906911
"package_id",
907912
"primary_advisory__advisory_id",
@@ -937,6 +942,9 @@ def get_fixing_advisories_bulk(packages, max_advisories, base_url):
937942

938943
packages = list(packages_without_multiple_importers)
939944

945+
if not packages:
946+
return result
947+
940948
package_by_purl = {package.package_url: package for package in packages}
941949

942950
purls = list(package_by_purl.keys())

0 commit comments

Comments
 (0)