Skip to content

Commit

Permalink
Use optimized queryset in risk pipeline
Browse files Browse the repository at this point in the history
- Prefetch related vulnerability, severities, references, and exploits
  for better performance

Signed-off-by: Keshav Priyadarshi <git@keshav.space>
  • Loading branch information
keshav-space committed Nov 11, 2024
1 parent 93cba92 commit 84a3a93
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions vulnerabilities/pipelines/compute_package_risk.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ def steps(cls):
return (cls.add_package_risk_score,)

def add_package_risk_score(self):
affected_packages = Package.objects.filter(
affected_by_vulnerabilities__isnull=False
affected_packages = (
Package.objects.filter(affected_by_vulnerabilities__isnull=False).prefetch_related(
"affectedbypackagerelatedvulnerability_set__vulnerability",
"affectedbypackagerelatedvulnerability_set__vulnerability__references",
"affectedbypackagerelatedvulnerability_set__vulnerability__severities",
"affectedbypackagerelatedvulnerability_set__vulnerability__exploits",
)
).distinct()

self.log(f"Calculating risk for {affected_packages.count():,d} affected package records")
Expand Down

0 comments on commit 84a3a93

Please sign in to comment.