Skip to content
This repository has been archived by the owner on Dec 22, 2022. It is now read-only.

Commit

Permalink
Moved hb_pc_cat_dur modification to be before caching (prebid#1250)
Browse files Browse the repository at this point in the history
  • Loading branch information
camrice authored Apr 9, 2020
1 parent b8b5f67 commit 28851f2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
20 changes: 11 additions & 9 deletions exchange/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,19 @@ func (e *exchange) HoldAuction(ctx context.Context, bidRequest *openrtb.BidReque
errs = append(errs, errors.New("Unable to marshal response ext for debugging"))
}
}

if requestExt.Prebid.SupportDeals {
dealErrs := applyDealSupport(bidRequest, auc, bidCategory)
errs = append(errs, dealErrs...)
}

cacheErrs := auc.doCache(ctx, e.cache, targData, bidRequest, 60, &e.defaultTTLs, bidCategory, debugLog)
if len(cacheErrs) > 0 {
errs = append(errs, cacheErrs...)
}
targData.setTargeting(auc, bidRequest.App != nil, bidCategory)
}

if requestExt.Prebid.SupportDeals {
dealErrs := applyDealSupport(bidRequest, auc)
errs = append(errs, dealErrs...)
}
}

// Build the response
Expand All @@ -210,7 +212,7 @@ type BidderDealTier struct {
}

// applyDealSupport updates targeting keys with deal prefixes if minimum deal tier exceeded
func applyDealSupport(bidRequest *openrtb.BidRequest, auc *auction) []error {
func applyDealSupport(bidRequest *openrtb.BidRequest, auc *auction, bidCategory map[string]string) []error {
errs := []error{}
impDealMap := getDealTiers(bidRequest)

Expand All @@ -221,7 +223,7 @@ func applyDealSupport(bidRequest *openrtb.BidRequest, auc *auction) []error {

if topBidPerBidder.dealPriority > 0 {
if validateAndNormalizeDealTier(impDeal[bidderString]) {
updateHbPbCatDur(topBidPerBidder, impDeal[bidderString].Info)
updateHbPbCatDur(topBidPerBidder, impDeal[bidderString].Info, bidCategory)
} else {
errs = append(errs, fmt.Errorf("dealTier configuration invalid for bidder '%s', imp ID '%s'", bidderString, impID))
}
Expand Down Expand Up @@ -258,16 +260,16 @@ func validateAndNormalizeDealTier(impDeal *DealTier) bool {
return len(impDeal.Info.Prefix) > 0 && impDeal.Info.MinDealTier > 0
}

func updateHbPbCatDur(bid *pbsOrtbBid, dealTierInfo *DealTierInfo) {
func updateHbPbCatDur(bid *pbsOrtbBid, dealTierInfo *DealTierInfo, bidCategory map[string]string) {
if bid.dealPriority >= dealTierInfo.MinDealTier {
prefixTier := fmt.Sprintf("%s%d_", dealTierInfo.Prefix, bid.dealPriority)

if oldCatDur, ok := bid.bidTargets["hb_pb_cat_dur"]; ok {
if oldCatDur, ok := bidCategory[bid.bid.ID]; ok {
oldCatDurSplit := strings.SplitAfterN(oldCatDur, "_", 2)
oldCatDurSplit[0] = prefixTier

newCatDur := strings.Join(oldCatDurSplit, "")
bid.bidTargets["hb_pb_cat_dur"] = newCatDur
bidCategory[bid.bid.ID] = newCatDur
}
}
}
Expand Down
18 changes: 12 additions & 6 deletions exchange/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1403,7 +1403,10 @@ func TestApplyDealSupport(t *testing.T) {
},
}

bid := pbsOrtbBid{&openrtb.Bid{}, "video", test.targ, &openrtb_ext.ExtBidPrebidVideo{}, test.dealPriority}
bid := pbsOrtbBid{&openrtb.Bid{ID: "123456"}, "video", map[string]string{}, &openrtb_ext.ExtBidPrebidVideo{}, test.dealPriority}
bidCategory := map[string]string{
bid.bid.ID: test.targ["hb_pb_cat_dur"],
}

auc := &auction{
winningBidsByBidder: map[string]map[openrtb_ext.BidderName]*pbsOrtbBid{
Expand All @@ -1413,9 +1416,9 @@ func TestApplyDealSupport(t *testing.T) {
},
}

dealErrs := applyDealSupport(bidRequest, auc)
dealErrs := applyDealSupport(bidRequest, auc, bidCategory)

assert.Equal(t, test.expectedHbPbCatDur, auc.winningBidsByBidder["imp_id1"][bidderName].bidTargets["hb_pb_cat_dur"], test.description)
assert.Equal(t, test.expectedHbPbCatDur, bidCategory[auc.winningBidsByBidder["imp_id1"][bidderName].bid.ID], test.description)
if len(test.expectedDealErr) > 0 {
assert.Containsf(t, dealErrs, errors.New(test.expectedDealErr), "Expected error message not found in deal errors")
}
Expand Down Expand Up @@ -1590,11 +1593,14 @@ func TestUpdateHbPbCatDur(t *testing.T) {
}

for _, test := range testCases {
bid := pbsOrtbBid{&openrtb.Bid{}, "video", test.targ, &openrtb_ext.ExtBidPrebidVideo{}, test.dealPriority}
bid := pbsOrtbBid{&openrtb.Bid{ID: "123456"}, "video", map[string]string{}, &openrtb_ext.ExtBidPrebidVideo{}, test.dealPriority}
bidCategory := map[string]string{
bid.bid.ID: test.targ["hb_pb_cat_dur"],
}

updateHbPbCatDur(&bid, test.dealTier)
updateHbPbCatDur(&bid, test.dealTier, bidCategory)

assert.Equal(t, test.expectedHbPbCatDur, bid.bidTargets["hb_pb_cat_dur"], test.description)
assert.Equal(t, test.expectedHbPbCatDur, bidCategory[bid.bid.ID], test.description)
}
}

Expand Down

0 comments on commit 28851f2

Please sign in to comment.