Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of an array of price buckets and standard granuarlities #104

Closed
wants to merge 12 commits into from
8 changes: 4 additions & 4 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@

PREBID_BIDDER_CODE = None

# Price buckets. This should match your Prebid settings for the partner. See:
# http://prebid.org/dev-docs/publisher-api-reference.html#module_pbjs.setPriceGranularity
# FIXME: this should be an array of buckets. See:
# https://github.com/prebid/Prebid.js/blob/8fed3d7aaa814e67ca3efc103d7d306cab8c692c/src/cpmBucketManager.js
# Price buckets. This should match your Prebid settings for the partner.
# Can be an a single bucket, an array of buckerts or string for standard Prebid
# price granularities such as 'medium', 'dense' or 'auto'
# See: http://prebid.org/dev-docs/publisher-api-reference.html#module_pbjs.setPriceGranularity
PREBID_PRICE_BUCKETS = {
'precision': 2,
'min' : 0,
Expand Down
33 changes: 27 additions & 6 deletions tasks/add_new_prebid_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys
from builtins import input
from pprint import pprint
from collections import OrderedDict

from colorama import init

Expand All @@ -29,6 +30,7 @@
get_prices_summary_string,
micro_amount_to_num,
num_to_str,
get_prebid_standard_bucketing
)

# Colorama for cross-platform support for colored logging.
Expand Down Expand Up @@ -317,15 +319,34 @@ def main():
if bidder_code is None:
raise MissingSettingException('PREBID_BIDDER_CODE')

price_buckets = getattr(settings, 'PREBID_PRICE_BUCKETS', None)
if price_buckets is None:
raise MissingSettingException('PREBID_PRICE_BUCKETS')
price_buckets = None
price_buckets_config = getattr(settings, 'PREBID_PRICE_BUCKETS', None)
if price_buckets_config is None:
raise MissingSettingException('PREBID_PRICE_BUCKETS')
elif isinstance(price_buckets_config, dict):
price_buckets = [ price_buckets_config ]
elif isinstance(price_buckets_config, list):
price_buckets = price_buckets_config
elif isinstance(price_buckets_config, str):
price_buckets = get_prebid_standard_bucketing(price_buckets_config)
if price_buckets == None:
raise BadSettingException('Unknown price bucketing scheme \'{0}\' for PREBID_PRICE_BUCKETS'.format(price_buckets_config))
else:
raise BadSettingException('The setting "PREBID_PRICE_BUCKETS" '
'is an invalid type.')

for pb in price_buckets:
check_price_buckets_validity(pb)

prices = []
for pb in price_buckets:
prices.extend(get_prices_array(pb))

check_price_buckets_validity(price_buckets)
# Remove duplicates price buckets
prices = list(OrderedDict.fromkeys(prices))

prices = get_prices_array(price_buckets)
prices_summary = get_prices_summary_string(prices,
price_buckets['precision'])
price_buckets[0]['precision'])

logger.info(
u"""
Expand Down
71 changes: 68 additions & 3 deletions tasks/price_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def num_to_str(num, precision=2):
Returns:
a string
"""
return '%.{0}f'.format(str(precision)) % num
return '%.{0}f'.format(str(precision)) % num

def get_prices_array(price_bucket):
"""
Expand All @@ -48,7 +48,7 @@ def get_prices_array(price_bucket):
price_bucket (object): the price bucket configuration
Returns:
an array of integers: every price bucket cutoff from:
int(round(price_bucket['min'] * 10**6, precision)) to
int(round(price_bucket['min'] * 10**6, precision)) to
int(round(price_bucket['max'] * 10**6, precision))
"""
start_cpm = price_bucket['min'] if price_bucket['min'] >=0 else 0.00
Expand All @@ -65,7 +65,7 @@ def get_prices_array(price_bucket):
while current_cpm_micro_amount <= end_cpm_micro_amount:
prices.append(current_cpm_micro_amount)
current_cpm_micro_amount += increment_micro_amount

return prices

def get_prices_summary_string(prices_array, precision=2):
Expand Down Expand Up @@ -94,3 +94,68 @@ def get_prices_summary_string(prices_array, precision=2):
)

return summary

prebid_bucketing_schemes = {
"low": [{
'precision': 2,
'min' : 0,
'max' : 5,
'increment': 0.50,
}],
"medium": [{
'precision': 2,
'min' : 0,
'max' : 20,
'increment': 0.10,
}],
# High will try to create too many line items at once resulting in an error
#"high": [{
# 'precision': 2,
# 'min' : 0,
# 'max' : 20,
# 'increment': 0.01,
#}],
"auto": [
{
'precision': 2,
'min' : 0,
'max' : 3,
'increment': 0.05,
},
{
'precision': 2,
'min' : 3,
'max' : 8,
'increment': 0.05,
},
{
'precision': 2,
'min' : 8,
'max' : 20,
'increment': 0.50,
}],
"dense": [{
'precision': 2,
'min' : 0,
'max' : 3,
'increment': 0.01
},
{
'precision': 2,
'min' : 3,
'max' : 8,
'increment': 0.05,
},
{
'precision': 2,
'min' : 8,
'max' : 20,
'increment': 0.50,
}]
}

def get_prebid_standard_bucketing(bucketing_scheme):
if bucketing_scheme in prebid_bucketing_schemes:
return prebid_bucketing_schemes[bucketing_scheme]
else:
return None