Skip to content

Commit

Permalink
Support MultiBid feature
Browse files Browse the repository at this point in the history
Detailed prebid/prebid-server#1715

Refactor winningBid logic.
Added IT test.
  • Loading branch information
DGarbar committed Feb 26, 2021
1 parent c748093 commit 91a6cf2
Show file tree
Hide file tree
Showing 22 changed files with 1,542 additions and 397 deletions.
243 changes: 158 additions & 85 deletions src/main/java/org/prebid/server/auction/BidResponseCreator.java

Large diffs are not rendered by default.

85 changes: 0 additions & 85 deletions src/main/java/org/prebid/server/auction/BidResponseReducer.java

This file was deleted.

18 changes: 16 additions & 2 deletions src/main/java/org/prebid/server/auction/ExchangeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebidBidderConfig;
import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebidCache;
import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebidData;
import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebidMultiBid;
import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebidSchain;
import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebidSchainSchain;
import org.prebid.server.proto.openrtb.ext.request.ExtRequestTargeting;
Expand All @@ -69,6 +70,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -148,6 +150,7 @@ public Future<BidResponse> holdAuction(AuctionContext context) {
final String publisherId = account.getId();
final BidRequestCacheInfo cacheInfo = bidRequestCacheInfo(bidRequest);
final boolean debugEnabled = isDebugEnabled(bidRequest);
final Map<String, ExtRequestPrebidMultiBid> bidderToMultiBids = bidderToMultiBids(bidRequest);

return storedResponseProcessor.getStoredResponseResult(bidRequest.getImp(), aliases, timeout)
.map(storedResponseResult -> populateStoredResponse(storedResponseResult, storedResponse))
Expand All @@ -173,6 +176,7 @@ public Future<BidResponse> holdAuction(AuctionContext context) {
bidderResponses,
context,
cacheInfo,
bidderToMultiBids,
debugEnabled))
.compose(bidResponse -> bidResponsePostProcessor.postProcess(
context.getRoutingContext(), uidsCookie, bidRequest, bidResponse, account));
Expand Down Expand Up @@ -249,6 +253,17 @@ private static ExtRequestPrebid extRequestPrebid(BidRequest bidRequest) {
return requestExt != null ? requestExt.getPrebid() : null;
}

private static Map<String, ExtRequestPrebidMultiBid> bidderToMultiBids(BidRequest bidRequest) {
final ExtRequestPrebid extRequestPrebid = extRequestPrebid(bidRequest);
final List<ExtRequestPrebidMultiBid> multiBids = extRequestPrebid != null
? extRequestPrebid.getMultibid()
: null;
return multiBids == null
? Collections.emptyMap()
: multiBids.stream()
.collect(Collectors.toMap(ExtRequestPrebidMultiBid::getBidder, Function.identity()));
}

/**
* Populates storedResponse parameter with stored {@link List<SeatBid>} and returns {@link List<Imp>} for which
* request to bidders should be performed.
Expand Down Expand Up @@ -529,8 +544,7 @@ private List<BidderRequest> getBidderRequests(List<BidderPrivacyResult> bidderPr
* Extracts a map of bidders to their arguments from {@link ObjectNode} prebid.bidders.
*/
private static Map<String, JsonNode> bidderToPrebidBidders(BidRequest bidRequest) {
final ExtRequest requestExt = bidRequest.getExt();
final ExtRequestPrebid prebid = requestExt == null ? null : requestExt.getPrebid();
final ExtRequestPrebid prebid = extRequestPrebid(bidRequest);
final ObjectNode bidders = prebid == null ? null : prebid.getBidders();

if (bidders == null || bidders.isNull()) {
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/org/prebid/server/auction/WinningBidComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.prebid.server.auction;

import com.iab.openrtb.request.Imp;
import org.prebid.server.auction.model.BidInfo;

import java.util.Comparator;
import java.util.Objects;

/**
* Re
*/
public class WinningBidComparator implements Comparator<BidInfo> {

private final Comparator<BidInfo> priceComparator = Comparator.comparing(o -> o.getBid().getPrice());

@Override
public int compare(BidInfo bidInfo1, BidInfo bidInfo2) {
final Imp imp = bidInfo1.getCorrespondingImp();
// this should never happen
if (!Objects.equals(imp, bidInfo2.getCorrespondingImp())) {
throw new IllegalStateException(
String.format("Error while determining winning bid: "
+ "Multiple bids for was found for impId: %s", imp.getId()));
}

return priceComparator.compare(bidInfo1, bidInfo2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.prebid.server.auction.model;

import lombok.Builder;
import lombok.Value;

@Builder(toBuilder = true)
@Value
public class TargetingBidInfo {

BidInfo bidInfo;

String bidderCode;

boolean isTargetingEnabled;

boolean isWinningBid;

boolean isBidderWinningBid;

boolean isAddTargetBidderCode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,9 @@ public class ExtRequestPrebid {
* Defines the contract for bidrequest.ext.prebid.channel
*/
ExtRequestPrebidChannel channel;

/**
* Defines the contract for bidrequest.ext.prebid.multibid
*/
List<ExtRequestPrebidMultiBid> multibid;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.prebid.server.proto.openrtb.ext.request;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Value;

/**
* Defines the contract for bidrequest.ext.prebid.targeting
*/
@Value(staticConstructor = "of")
public class ExtRequestPrebidMultiBid {

/**
* Defines the contract for bidrequest.ext.prebid.multibid.bidder
*/
String bidder;

/**
* Defines the contract for bidrequest.ext.prebid.multibid.maxbids
*/
@JsonProperty("maxbids")
Integer maxBids;

/**
* Defines the contract for bidrequest.ext.prebid.multibid.targetbiddercodeprefix
*/
@JsonProperty("targetbiddercodeprefix")
String targetBidderCodePrefix;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class ExtBidPrebid {

Map<String, String> targeting;

@JsonProperty("targetbiddercode")
String targetBidderCode;

ExtResponseCache cache;

@JsonProperty("storedrequestattributes")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.prebid.server.auction.AuctionRequestFactory;
import org.prebid.server.auction.BidResponseCreator;
import org.prebid.server.auction.BidResponsePostProcessor;
import org.prebid.server.auction.BidResponseReducer;
import org.prebid.server.auction.ExchangeService;
import org.prebid.server.auction.FpdResolver;
import org.prebid.server.auction.ImplicitParametersExtractor;
Expand All @@ -26,6 +25,7 @@
import org.prebid.server.auction.VideoRequestFactory;
import org.prebid.server.auction.VideoResponseFactory;
import org.prebid.server.auction.VideoStoredRequestProcessor;
import org.prebid.server.auction.WinningBidComparator;
import org.prebid.server.bidder.BidderCatalog;
import org.prebid.server.bidder.BidderDeps;
import org.prebid.server.bidder.BidderErrorNotifier;
Expand Down Expand Up @@ -457,7 +457,7 @@ BidResponseCreator bidResponseCreator(
VastModifier vastModifier,
EventsService eventsService,
StoredRequestProcessor storedRequestProcessor,
BidResponseReducer bidResponseReducer,
WinningBidComparator winningBidComparator,
IdGenerator bidIdGenerator,
@Value("${settings.targeting.truncate-attr-chars}") int truncateAttrChars,
Clock clock,
Expand All @@ -469,7 +469,7 @@ BidResponseCreator bidResponseCreator(
vastModifier,
eventsService,
storedRequestProcessor,
bidResponseReducer,
winningBidComparator,
bidIdGenerator,
truncateAttrChars,
clock,
Expand Down Expand Up @@ -533,8 +533,8 @@ StoredRequestProcessor storedRequestProcessor(
}

@Bean
BidResponseReducer bidResponseReducer() {
return new BidResponseReducer();
WinningBidComparator winningBidComparator() {
return new WinningBidComparator();
}

@Bean
Expand Down
Loading

0 comments on commit 91a6cf2

Please sign in to comment.