Skip to content

Commit

Permalink
Only run if the set of flights targeting has weights.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericholscher committed Jun 24, 2024
1 parent 66cf03a commit fc4d75f
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions adserver/decisionengine/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.conf import settings
from django.db import models
from django.utils import timezone
from regex import W
from user_agents import parse

from ..constants import AFFILIATE_CAMPAIGN
Expand Down Expand Up @@ -309,10 +310,8 @@ def filter_flight(self, flight):
return False

# Skip if the flight if the similarity is not high enough
if hasattr(self, "niche_weights"):
if not flight.show_to_niche_targeting(self.niche_weights):
log.debug("Flight %s does not match niche targeting", flight)
return False
if not flight.show_to_niche_targeting(self.niche_weights):
return False

return True

Expand Down Expand Up @@ -364,21 +363,15 @@ def select_flight(self):
"""
flights = self.get_candidate_flights()

self.niche_weights = None
paid_flights = []
affiliate_flights = []
community_flights = []
publisher_house_flights = []
house_flights = []

niche_flights = []

for flight in flights:

# Apply niche targeting only when any flight has it.
# This is to track whether we should do expensive distance queries.
if flight.niche_targeting:
niche_flights.append(flight)

# Separate flights by campaign type, so we can prioritize them in this order
if flight.campaign.campaign_type == PAID_CAMPAIGN:
paid_flights.append(flight)
Expand All @@ -395,17 +388,6 @@ def select_flight(self):
# Ignore priorities for forcing a specific ad/campaign
return random.choice(flights)

# If any flight is using niche targeting, query the embedding tables
if niche_flights and "ethicalads_ext.embedding" in settings.INSTALLED_APPS:
from ethicalads_ext.embedding.utils import get_niche_weights

self.niche_weights = get_niche_weights(
self.url,
flights=niche_flights,
)

log.info("Niche targeting weights: %s", self.niche_weights)

# We iterate over the possible flights in order of priority,
# and serve the first type that has any budget.
for possible_flights in (
Expand All @@ -418,8 +400,25 @@ def select_flight(self):
# Choose a flight based on the impressions needed
flight_range = []
total_clicks_needed = 0

for flight in possible_flights:

# Apply niche targeting only when any flight has it.
# This is to track whether we should do expensive distance queries.
if (
flight.niche_targeting
and "ethicalads_ext.embedding" in settings.INSTALLED_APPS
):
if not self.niche_weights:
from ethicalads_ext.embedding.utils import (
get_niche_weights,
) # noqa

self.niche_weights = get_niche_weights(
self.url,
)
log.info("Niche targeting weights: %s", self.niche_weights)

# Handle excluding flights based on targeting
if not self.filter_flight(flight):
continue
Expand Down

0 comments on commit fc4d75f

Please sign in to comment.