Skip to content

Commit

Permalink
Merge pull request #198 from readthedocs/davidfischer/annotate-flight…
Browse files Browse the repository at this point in the history
…-query

Optimization to choose a flight with live ads
  • Loading branch information
davidfischer committed Jul 23, 2020
2 parents d615ca3 + 7cb3abc commit 5128dee
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions adserver/decisionengine/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,16 @@ def get_candidate_flights(self):
else:
flights = flights.filter(live=True, start_date__lte=get_ad_day().date())

# TODO: ensure there's a live ad for each flight
# Unfortunately, filtering in annotations was added in Django 2.x
# For now, we'll just ensure there's "an ad" for the flight
# https://docs.djangoproject.com/en/2.0/topics/db/aggregation/#filtering-on-annotations
flights = flights.annotate(num_ads=models.Count("advertisements")).filter(
num_ads__gt=0
)
# Ensure there's a live ad of the chosen types for each flight
flights = flights.annotate(
num_ads=models.Count(
"advertisements",
filter=models.Q(
advertisements__ad_types__slug__in=self.ad_types,
advertisements__live=True,
),
)
).filter(num_ads__gt=0)

# Ensure we prefetch necessary data so it doesn't result in N queries for each flight
return flights.select_related("campaign")
Expand Down

0 comments on commit 5128dee

Please sign in to comment.