Skip to content

Commit

Permalink
OTT-216: add all SupportDeal features (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pubmatic-Dhruv-Sonone authored and sachin-pubmatic committed Aug 2, 2021
1 parent d1a6e62 commit 5bd8ae6
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 40 deletions.
81 changes: 41 additions & 40 deletions exchange/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,55 +768,56 @@ func applyCategoryMapping(ctx context.Context, bidRequest *openrtb2.BidRequest,
if appendBidderNames {
categoryDuration = fmt.Sprintf("%s_%s", categoryDuration, bidderName.String())
}
if !brandCatExt.SkipDedup {
if dupe, ok := dedupe[dupeKey]; ok {

if dupe, ok := dedupe[dupeKey]; ok {

dupeBidPrice, err := strconv.ParseFloat(dupe.bidPrice, 64)
if err != nil {
dupeBidPrice = 0
}
currBidPrice, err := strconv.ParseFloat(pb, 64)
if err != nil {
currBidPrice = 0
}
if dupeBidPrice == currBidPrice {
if booleanGenerator.Generate() {
dupeBidPrice = -1
} else {
currBidPrice = -1
dupeBidPrice, err := strconv.ParseFloat(dupe.bidPrice, 64)
if err != nil {
dupeBidPrice = 0
}
currBidPrice, err := strconv.ParseFloat(pb, 64)
if err != nil {
currBidPrice = 0
}
if dupeBidPrice == currBidPrice {
if booleanGenerator.Generate() {
dupeBidPrice = -1
} else {
currBidPrice = -1
}
}
}

if dupeBidPrice < currBidPrice {
if dupe.bidderName == bidderName {
// An older bid from the current bidder
bidsToRemove = append(bidsToRemove, dupe.bidIndex)
rejections = updateRejections(rejections, dupe.bidID, "Bid was deduplicated")
} else {
// An older bid from a different seatBid we've already finished with
oldSeatBid := (seatBids)[dupe.bidderName]
rejections = updateRejections(rejections, dupe.bidID, "Bid was deduplicated")
if len(oldSeatBid.bids) == 1 {
seatBidsToRemove = append(seatBidsToRemove, dupe.bidderName)
if dupeBidPrice < currBidPrice {
if dupe.bidderName == bidderName {
// An older bid from the current bidder
bidsToRemove = append(bidsToRemove, dupe.bidIndex)
rejections = updateRejections(rejections, dupe.bidID, "Bid was deduplicated")
} else {
// This is a very rare, but still possible case where bid needs to be removed from already processed bidder
// This happens when current processing bidder has a bid that has same deduplication key as a bid from already processed bidder
// and already processed bid was selected to be removed
// See example of input data in unit test `TestCategoryMappingTwoBiddersManyBidsEachNoCategorySamePrice`
// Need to remove bid by name, not index in this case
removeBidById(oldSeatBid, dupe.bidID)
// An older bid from a different seatBid we've already finished with
oldSeatBid := (seatBids)[dupe.bidderName]
rejections = updateRejections(rejections, dupe.bidID, "Bid was deduplicated")
if len(oldSeatBid.bids) == 1 {
seatBidsToRemove = append(seatBidsToRemove, dupe.bidderName)
} else {
// This is a very rare, but still possible case where bid needs to be removed from already processed bidder
// This happens when current processing bidder has a bid that has same deduplication key as a bid from already processed bidder
// and already processed bid was selected to be removed
// See example of input data in unit test `TestCategoryMappingTwoBiddersManyBidsEachNoCategorySamePrice`
// Need to remove bid by name, not index in this case
removeBidById(oldSeatBid, dupe.bidID)
}
}
delete(res, dupe.bidID)
} else {
// Remove this bid
bidsToRemove = append(bidsToRemove, bidInd)
rejections = updateRejections(rejections, bidID, "Bid was deduplicated")
continue
}
delete(res, dupe.bidID)
} else {
// Remove this bid
bidsToRemove = append(bidsToRemove, bidInd)
rejections = updateRejections(rejections, bidID, "Bid was deduplicated")
continue
}
dedupe[dupeKey] = bidDedupe{bidderName: bidderName, bidIndex: bidInd, bidID: bidID, bidPrice: pb}
}
res[bidID] = categoryDuration
dedupe[dupeKey] = bidDedupe{bidderName: bidderName, bidIndex: bidInd, bidID: bidID, bidPrice: pb}
}

if len(bidsToRemove) > 0 {
Expand Down
44 changes: 44 additions & 0 deletions exchange/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3761,3 +3761,47 @@ func (m *mockBidder) MakeBids(internalRequest *openrtb2.BidRequest, externalRequ
args := m.Called(internalRequest, externalRequest, response)
return args.Get(0).(*adapters.BidderResponse), args.Get(1).([]error)
}

func TestRecordAdaptorDuplicateBidIDs(t *testing.T) {
type bidderCollisions = map[string]int
testCases := []struct {
scenario string
bidderCollisions *bidderCollisions // represents no of collisions detected for bid.id at bidder level for given request
hasCollision bool
}{
{scenario: "invalid collision value", bidderCollisions: &map[string]int{"bidder-1": -1}, hasCollision: false},
{scenario: "no collision", bidderCollisions: &map[string]int{"bidder-1": 0}, hasCollision: false},
{scenario: "one collision", bidderCollisions: &map[string]int{"bidder-1": 1}, hasCollision: false},
{scenario: "multiple collisions", bidderCollisions: &map[string]int{"bidder-1": 2}, hasCollision: true}, // when 2 collisions it counter will be 1
{scenario: "multiple bidders", bidderCollisions: &map[string]int{"bidder-1": 2, "bidder-2": 4}, hasCollision: true},
{scenario: "multiple bidders with bidder-1 no collision", bidderCollisions: &map[string]int{"bidder-1": 1, "bidder-2": 4}, hasCollision: true},
{scenario: "no bidders", bidderCollisions: nil, hasCollision: false},
}
testEngine := metricsConf.NewMetricsEngine(&config.Configuration{}, nil)

for _, testcase := range testCases {
var adapterBids map[openrtb_ext.BidderName]*pbsOrtbSeatBid
if nil == testcase.bidderCollisions {
break
}
adapterBids = make(map[openrtb_ext.BidderName]*pbsOrtbSeatBid)
for bidder, collisions := range *testcase.bidderCollisions {
bids := make([]*pbsOrtbBid, 0)
testBidID := "bid_id_for_bidder_" + bidder
// add bids as per collisions value
bidCount := 0
for ; bidCount < collisions; bidCount++ {
bids = append(bids, &pbsOrtbBid{
bid: &openrtb2.Bid{
ID: testBidID,
},
})
}
if nil == adapterBids[openrtb_ext.BidderName(bidder)] {
adapterBids[openrtb_ext.BidderName(bidder)] = new(pbsOrtbSeatBid)
}
adapterBids[openrtb_ext.BidderName(bidder)].bids = bids
}
assert.Equal(t, testcase.hasCollision, recordAdaptorDuplicateBidIDs(testEngine, adapterBids))
}
}

0 comments on commit 5bd8ae6

Please sign in to comment.