Skip to content

Adds preferDeals support #1528

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

Merged
merged 6 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion endpoints/openrtb2/amp_auction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,7 @@ func TestBuildAmpObject(t *testing.T) {
},
AT: 1,
TMax: 500,
Ext: json.RawMessage(`{"prebid":{"cache":{"bids":{"returnCreative":null},"vastxml":null},"targeting":{"pricegranularity":{"precision":2,"ranges":[{"min":0,"max":20,"increment":0.1}]},"includewinners":true,"includebidderkeys":true,"includebrandcategory":null,"includeformat":false,"durationrangesec":null}}}`),
Ext: json.RawMessage(`{"prebid":{"cache":{"bids":{"returnCreative":null},"vastxml":null},"targeting":{"pricegranularity":{"precision":2,"ranges":[{"min":0,"max":20,"increment":0.1}]},"includewinners":true,"includebidderkeys":true,"includebrandcategory":null,"includeformat":false,"durationrangesec":null,"preferdeals":false}}}`),
},
AuctionResponse: &openrtb.BidResponse{
SeatBid: []openrtb.SeatBid{{
Expand Down
17 changes: 15 additions & 2 deletions exchange/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (d *DebugLog) PutDebugLogError(cache prebid_cache_client.Client, timeout in
return nil
}

func newAuction(seatBids map[openrtb_ext.BidderName]*pbsOrtbSeatBid, numImps int) *auction {
func newAuction(seatBids map[openrtb_ext.BidderName]*pbsOrtbSeatBid, numImps int, preferDeals bool) *auction {
winningBids := make(map[string]*pbsOrtbBid, numImps)
winningBidsByBidder := make(map[string]map[openrtb_ext.BidderName]*pbsOrtbBid, numImps)

Expand All @@ -103,7 +103,7 @@ func newAuction(seatBids map[openrtb_ext.BidderName]*pbsOrtbSeatBid, numImps int
for _, bid := range seatBid.bids {
cpm := bid.bid.Price
wbid, ok := winningBids[bid.bid.ImpID]
if !ok || cpm > wbid.bid.Price {
if !ok || isNewWinningBid(bid.bid, wbid.bid, preferDeals) {
winningBids[bid.bid.ImpID] = bid
}
if bidMap, ok := winningBidsByBidder[bid.bid.ImpID]; ok {
Expand All @@ -125,6 +125,19 @@ func newAuction(seatBids map[openrtb_ext.BidderName]*pbsOrtbSeatBid, numImps int
}
}

// Calculate if the new bid (nbid) will win against the current winning bid (wbid) given preferDeals.
func isNewWinningBid(nbid, wbid *openrtb.Bid, preferDeals bool) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider changing the variable names to be more obvious:
nbid -> bid (i think the 'new' part is implied)
wbid -> winningBid

if preferDeals {
if len(wbid.DealID) > 0 && len(nbid.DealID) == 0 {
return false
}
if len(wbid.DealID) == 0 && len(nbid.DealID) > 0 {
return true
}
}
return nbid.Price > wbid.Price
}

func (a *auction) setRoundedPrices(priceGranularity openrtb_ext.PriceGranularity) {
roundedPrices := make(map[*pbsOrtbBid]string, 5*len(a.winningBids))
for _, topBidsPerImp := range a.winningBidsByBidder {
Expand Down
221 changes: 221 additions & 0 deletions exchange/auction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,227 @@ func runCacheSpec(t *testing.T, fileDisplayName string, specData *cacheSpec) {
}
}

type testNewAuctionSpec struct {
description string
seatBids map[openrtb_ext.BidderName]*pbsOrtbSeatBid
numImps int
preferDeals bool
expectedAuction auction
}

func TestNewAuction(t *testing.T) {
bid1p077 := pbsOrtbBid{
bid: &openrtb.Bid{
ImpID: "imp1",
Price: 0.77,
},
}
bid1p123 := pbsOrtbBid{
bid: &openrtb.Bid{
ImpID: "imp1",
Price: 1.23,
},
}
bid1p230 := pbsOrtbBid{
bid: &openrtb.Bid{
ImpID: "imp1",
Price: 2.30,
},
}
bid1p088d := pbsOrtbBid{
bid: &openrtb.Bid{
ImpID: "imp1",
Price: 0.88,
DealID: "SpecialDeal",
},
}
bid1p166d := pbsOrtbBid{
bid: &openrtb.Bid{
ImpID: "imp1",
Price: 1.66,
DealID: "BigDeal",
},
}
bid2p123 := pbsOrtbBid{
bid: &openrtb.Bid{
ImpID: "imp2",
Price: 1.23,
},
}
bid2p144 := pbsOrtbBid{
bid: &openrtb.Bid{
ImpID: "imp2",
Price: 1.44,
},
}
tests := []testNewAuctionSpec{
{
description: "Basic auction test",
seatBids: map[openrtb_ext.BidderName]*pbsOrtbSeatBid{
"appnexus": {
bids: []*pbsOrtbBid{&bid1p123},
},
"rubicon": {
bids: []*pbsOrtbBid{&bid1p230},
},
},
numImps: 1,
preferDeals: false,
expectedAuction: auction{
winningBids: map[string]*pbsOrtbBid{
"imp1": &bid1p230,
},
winningBidsByBidder: map[string]map[openrtb_ext.BidderName]*pbsOrtbBid{
"imp1": {
"appnexus": &bid1p123,
"rubicon": &bid1p230,
},
},
},
},
{
description: "Multi-imp auction",
seatBids: map[openrtb_ext.BidderName]*pbsOrtbSeatBid{
"appnexus": {
bids: []*pbsOrtbBid{&bid1p230, &bid2p123},
},
"rubicon": {
bids: []*pbsOrtbBid{&bid1p077, &bid2p144},
},
"openx": {
bids: []*pbsOrtbBid{&bid1p123},
},
},
numImps: 2,
preferDeals: false,
expectedAuction: auction{
winningBids: map[string]*pbsOrtbBid{
"imp1": &bid1p230,
"imp2": &bid2p144,
},
winningBidsByBidder: map[string]map[openrtb_ext.BidderName]*pbsOrtbBid{
"imp1": {
"appnexus": &bid1p230,
"rubicon": &bid1p077,
"openx": &bid1p123,
},
"imp2": {
"appnexus": &bid2p123,
"rubicon": &bid2p144,
},
},
},
},
{
description: "Basic auction with deals, no preference",
seatBids: map[openrtb_ext.BidderName]*pbsOrtbSeatBid{
"appnexus": {
bids: []*pbsOrtbBid{&bid1p123},
},
"rubicon": {
bids: []*pbsOrtbBid{&bid1p088d},
},
},
numImps: 1,
preferDeals: false,
expectedAuction: auction{
winningBids: map[string]*pbsOrtbBid{
"imp1": &bid1p123,
},
winningBidsByBidder: map[string]map[openrtb_ext.BidderName]*pbsOrtbBid{
"imp1": {
"appnexus": &bid1p123,
"rubicon": &bid1p088d,
},
},
},
},
{
description: "Basic auction with deals, prefer deals",
seatBids: map[openrtb_ext.BidderName]*pbsOrtbSeatBid{
"appnexus": {
bids: []*pbsOrtbBid{&bid1p123},
},
"rubicon": {
bids: []*pbsOrtbBid{&bid1p088d},
},
},
numImps: 1,
preferDeals: true,
expectedAuction: auction{
winningBids: map[string]*pbsOrtbBid{
"imp1": &bid1p088d,
},
winningBidsByBidder: map[string]map[openrtb_ext.BidderName]*pbsOrtbBid{
"imp1": {
"appnexus": &bid1p123,
"rubicon": &bid1p088d,
},
},
},
},
{
description: "Auction with 2 deals",
seatBids: map[openrtb_ext.BidderName]*pbsOrtbSeatBid{
"appnexus": {
bids: []*pbsOrtbBid{&bid1p166d},
},
"rubicon": {
bids: []*pbsOrtbBid{&bid1p088d},
},
},
numImps: 1,
preferDeals: true,
expectedAuction: auction{
winningBids: map[string]*pbsOrtbBid{
"imp1": &bid1p166d,
},
winningBidsByBidder: map[string]map[openrtb_ext.BidderName]*pbsOrtbBid{
"imp1": {
"appnexus": &bid1p166d,
"rubicon": &bid1p088d,
},
},
},
},
{
description: "Auction with 3 bids and 2 deals",
seatBids: map[openrtb_ext.BidderName]*pbsOrtbSeatBid{
"appnexus": {
bids: []*pbsOrtbBid{&bid1p166d},
},
"rubicon": {
bids: []*pbsOrtbBid{&bid1p088d},
},
"openx": {
bids: []*pbsOrtbBid{&bid1p230},
},
},
numImps: 1,
preferDeals: true,
expectedAuction: auction{
winningBids: map[string]*pbsOrtbBid{
"imp1": &bid1p166d,
},
winningBidsByBidder: map[string]map[openrtb_ext.BidderName]*pbsOrtbBid{
"imp1": {
"appnexus": &bid1p166d,
"rubicon": &bid1p088d,
"openx": &bid1p230,
},
},
},
},
}

for _, test := range tests {
auc := newAuction(test.seatBids, test.numImps, test.preferDeals)

assert.Equal(t, test.expectedAuction, *auc, test.description)
}

}

type cacheSpec struct {
BidRequest openrtb.BidRequest `json:"bidRequest"`
PbsBids []pbsBid `json:"pbsBids"`
Expand Down
4 changes: 2 additions & 2 deletions exchange/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ func (e *exchange) HoldAuction(ctx context.Context, bidRequest *openrtb.BidReque
}
}

auc = newAuction(adapterBids, len(bidRequest.Imp))

if targData != nil {
// A non-nil auction is only needed if targeting is active. (It is used below this block to extract cache keys)
auc = newAuction(adapterBids, len(bidRequest.Imp), targData.preferDeals)
auc.setRoundedPrices(targData.priceGranularity)

if requestExt.Prebid.SupportDeals {
Expand Down
1 change: 1 addition & 0 deletions exchange/targeting.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type targetData struct {
includeCacheBids bool
includeCacheVast bool
includeFormat bool
preferDeals bool
// cacheHost and cachePath exist to supply cache host and path as targeting parameters
cacheHost string
cachePath string
Expand Down
1 change: 1 addition & 0 deletions exchange/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ func getExtTargetData(requestExt *openrtb_ext.ExtRequest, cacheInstructions *ext
includeCacheBids: cacheInstructions.cacheBids,
includeCacheVast: cacheInstructions.cacheVAST,
includeFormat: requestExt.Prebid.Targeting.IncludeFormat,
preferDeals: requestExt.Prebid.Targeting.PreferDeals,
}
}
return targData
Expand Down
1 change: 1 addition & 0 deletions openrtb_ext/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ type ExtRequestTargeting struct {
IncludeBrandCategory *ExtIncludeBrandCategory `json:"includebrandcategory"`
IncludeFormat bool `json:"includeformat"`
DurationRangeSec []int `json:"durationrangesec"`
PreferDeals bool `json:"preferdeals"`
}

type ExtIncludeBrandCategory struct {
Expand Down