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 timestamp and biddercode to event URL - refactored #653

Merged
merged 3 commits into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public class NotificationEvent {

Account account;

String bidder;

Long timestamp;

HttpContext httpContext;

public enum Type {
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/prebid/server/auction/BidResponseCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public BidResponseCreator(CacheService cacheService, BidderCatalog bidderCatalog
*/
Future<BidResponse> create(List<BidderResponse> bidderResponses, BidRequest bidRequest,
ExtRequestTargeting targeting, BidRequestCacheInfo cacheInfo,
Account account, Timeout timeout, Long auctionTimestamp, boolean debugEnabled) {
Account account, Timeout timeout, long auctionTimestamp, boolean debugEnabled) {

final Future<BidResponse> result;

Expand Down Expand Up @@ -140,7 +140,7 @@ Future<BidResponse> create(List<BidderResponse> bidderResponses, BidRequest bidR
.compose(cacheResult -> videoStoredDataResult(bidRequest.getImp(), timeout)
.map(videoStoredDataResult -> toBidResponse(bidderResponses, bidRequest, targeting,
winningBids, winningBidsByBidder, cacheInfo, cacheResult, videoStoredDataResult,
account, debugEnabled, auctionTimestamp)));
account, auctionTimestamp, debugEnabled)));
}

return result;
Expand Down Expand Up @@ -280,8 +280,8 @@ private Future<CacheServiceResult> toBidsWithCacheIds(List<BidderResponse> bidde
final Map<String, List<String>> bidderToVideoBidIdsToModify = shouldCacheVideoBids && eventsEnabled
? getBidderAndVideoBidIdsToModify(bidderResponses, imps)
: Collections.emptyMap();
final Map<String, List<String>> biddersToCacheBidIds = bidderResponses.stream()
.collect(Collectors.toMap(BidderResponse::getBidder, bidderResponse -> getBids(bidderResponse)
final Map<String, List<String>> bidderToBidIds = bidderResponses.stream()
.collect(Collectors.toMap(BidderResponse::getBidder, bidderResponse -> getBids(bidderResponse)
.map(Bid::getId)
.collect(Collectors.toList())));

Expand All @@ -291,7 +291,7 @@ private Future<CacheServiceResult> toBidsWithCacheIds(List<BidderResponse> bidde
.shouldCacheBids(cacheInfo.isShouldCacheBids())
.shouldCacheVideoBids(shouldCacheVideoBids)
.bidderToVideoBidIdsToModify(bidderToVideoBidIdsToModify)
.biddersToCacheBidIds(biddersToCacheBidIds)
.bidderToBidIds(bidderToBidIds)
.build();

result = cacheService.cacheBidsOpenrtb(bidsWithNonZeroPrice, imps, cacheContext, account, timeout,
Expand Down Expand Up @@ -510,7 +510,7 @@ private BidResponse toBidResponse(
List<BidderResponse> bidderResponses, BidRequest bidRequest, ExtRequestTargeting targeting,
Set<Bid> winningBids, Set<Bid> winningBidsByBidder, BidRequestCacheInfo cacheInfo,
CacheServiceResult cacheResult, VideoStoredDataResult videoStoredDataResult, Account account,
boolean debugEnabled, long auctionTimestamp) {
long auctionTimestamp, boolean debugEnabled) {

final Map<String, List<ExtBidderError>> bidErrors = new HashMap<>();
final List<SeatBid> seatBids = bidderResponses.stream()
Expand Down Expand Up @@ -576,8 +576,8 @@ private boolean checkEchoVideoAttrs(Imp imp) {
private SeatBid toSeatBid(BidderResponse bidderResponse, ExtRequestTargeting targeting, BidRequest bidRequest,
Set<Bid> winningBids, Set<Bid> winningBidsByBidder, BidRequestCacheInfo cacheInfo,
Map<Bid, CacheIdInfo> cachedBids, VideoStoredDataResult videoStoredDataResult,
Account account, Map<String, List<ExtBidderError>> bidErrors,
long auctionTimestamp) {
Account account, Map<String, List<ExtBidderError>> bidErrors, long auctionTimestamp) {

final String bidder = bidderResponse.getBidder();

final List<Bid> bids = bidderResponse.getSeatBid().getBids().stream()
Expand Down
150 changes: 75 additions & 75 deletions src/main/java/org/prebid/server/auction/ExchangeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import org.prebid.server.validation.ResponseBidValidator;
import org.prebid.server.validation.model.ValidationResult;

import java.time.Instant;
import java.math.BigDecimal;
import java.time.Clock;
import java.util.ArrayList;
Expand Down Expand Up @@ -145,10 +144,10 @@ public Future<BidResponse> holdAuction(AuctionContext context) {
final List<SeatBid> storedResponse = new ArrayList<>();
final Map<String, String> aliases = aliases(requestExt);
final String publisherId = account.getId();
final Boolean isGdprEnforced = account.getEnforceGdpr();
final ExtRequestTargeting targeting = targeting(requestExt);
final Long auctionTimestamp = auctionTimestamp(requestExt);
final BidRequestCacheInfo cacheInfo = bidRequestCacheInfo(targeting, requestExt);
final Boolean isGdprEnforced = account.getEnforceGdpr();
final long auctionTimestamp = auctionTimestamp(requestExt);
final boolean debugEnabled = isDebugEnabled(bidRequest, requestExt);

return storedResponseProcessor.getStoredResponseResult(imps, aliases, timeout)
Expand Down Expand Up @@ -177,22 +176,14 @@ public Future<BidResponse> holdAuction(AuctionContext context) {
account));
}

/**
* Populates storedResponse parameter with stored {@link List<SeatBid>} and returns {@link List<Imp>} for which
* request to bidders should be performed.
*/
private List<Imp> populateStoredResponse(StoredResponseResult storedResponseResult, List<SeatBid> storedResponse) {
storedResponse.addAll(storedResponseResult.getStoredResponse());
return storedResponseResult.getRequiredRequestImps();
}

/**
* Extracts {@link ExtBidRequest} from {@link BidRequest}.
*/
private ExtBidRequest requestExt(BidRequest bidRequest) {
try {
return bidRequest.getExt() != null
? mapper.mapper().treeToValue(bidRequest.getExt(), ExtBidRequest.class) : null;
? mapper.mapper().treeToValue(bidRequest.getExt(), ExtBidRequest.class)
: null;
} catch (JsonProcessingException e) {
throw new PreBidException(String.format("Error decoding bidRequest.ext: %s", e.getMessage()), e);
}
Expand All @@ -207,6 +198,64 @@ private static Map<String, String> aliases(ExtBidRequest requestExt) {
return aliases != null ? aliases : Collections.emptyMap();
}

/**
* Extracts {@link ExtRequestTargeting} from {@link ExtBidRequest} model.
*/
private static ExtRequestTargeting targeting(ExtBidRequest requestExt) {
final ExtRequestPrebid prebid = requestExt != null ? requestExt.getPrebid() : null;
return prebid != null ? prebid.getTargeting() : null;
}

/**
* Creates {@link BidRequestCacheInfo} based on {@link ExtBidRequest} model.
*/
private BidRequestCacheInfo bidRequestCacheInfo(ExtRequestTargeting targeting, ExtBidRequest requestExt) {
final ExtRequestPrebid prebid = requestExt != null ? requestExt.getPrebid() : null;
final ExtRequestPrebidCache cache = prebid != null ? prebid.getCache() : null;

if (targeting != null && cache != null) {
final boolean shouldCacheBids = cache.getBids() != null;
final boolean shouldCacheVideoBids = cache.getVastxml() != null;
final boolean shouldCacheWinningBidsOnly = targeting.getIncludebidderkeys()
? false // ext.prebid.targeting.includebidderkeys takes precedence
: ObjectUtils.defaultIfNull(cache.getWinningonly(), false);

if (shouldCacheBids || shouldCacheVideoBids || shouldCacheWinningBidsOnly) {
final Integer cacheBidsTtl = shouldCacheBids ? cache.getBids().getTtlseconds() : null;
final Integer cacheVideoBidsTtl = shouldCacheVideoBids ? cache.getVastxml().getTtlseconds() : null;

final boolean returnCreativeBid = shouldCacheBids
? ObjectUtils.defaultIfNull(cache.getBids().getReturnCreative(), true)
: false;
final boolean returnCreativeVideoBid = shouldCacheVideoBids
? ObjectUtils.defaultIfNull(cache.getVastxml().getReturnCreative(), true)
: false;

return BidRequestCacheInfo.builder()
.doCaching(true)
.shouldCacheBids(shouldCacheBids)
.cacheBidsTtl(cacheBidsTtl)
.shouldCacheVideoBids(shouldCacheVideoBids)
.cacheVideoBidsTtl(cacheVideoBidsTtl)
.returnCreativeBids(returnCreativeBid)
.returnCreativeVideoBids(returnCreativeVideoBid)
.shouldCacheWinningBidsOnly(shouldCacheWinningBidsOnly)
.build();
}
}

return BidRequestCacheInfo.noCache();
}

/**
* Extracts auction timestamp from {@link ExtBidRequest} or get it from {@link Clock} if it is null.
*/
private long auctionTimestamp(ExtBidRequest requestExt) {
final ExtRequestPrebid prebid = requestExt != null ? requestExt.getPrebid() : null;
final Long auctionTimestamp = prebid != null ? prebid.getAuctiontimestamp() : null;
return auctionTimestamp != null ? auctionTimestamp : clock.millis();
}

/**
* Determines debug flag from {@link BidRequest} or {@link ExtBidRequest}.
*/
Expand All @@ -219,19 +268,12 @@ private static boolean isDebugEnabled(BidRequest bidRequest, ExtBidRequest extBi
}

/**
* Extracts bidAdjustments from {@link ExtBidRequest}.
*/
private static Map<String, BigDecimal> bidAdjustments(ExtBidRequest requestExt) {
final ExtRequestPrebid prebid = requestExt != null ? requestExt.getPrebid() : null;
final Map<String, BigDecimal> bidAdjustmentFactors = prebid != null ? prebid.getBidadjustmentfactors() : null;
return bidAdjustmentFactors != null ? bidAdjustmentFactors : Collections.emptyMap();
}

/**
* Extracts currency rates from {@link ExtRequestTargeting}.
* Populates storedResponse parameter with stored {@link List<SeatBid>} and returns {@link List<Imp>} for which
* request to bidders should be performed.
*/
private static Map<String, Map<String, BigDecimal>> currencyRates(ExtRequestTargeting targeting) {
return targeting != null && targeting.getCurrency() != null ? targeting.getCurrency().getRates() : null;
private List<Imp> populateStoredResponse(StoredResponseResult storedResponseResult, List<SeatBid> storedResponse) {
storedResponse.addAll(storedResponseResult.getStoredResponse());
return storedResponseResult.getRequiredRequestImps();
}

/**
Expand Down Expand Up @@ -455,7 +497,7 @@ private String resolveCookieFamilyName(String bidder) {
}

/**
* Returns Shuffled List of {@link BidderRequest}
* Returns Shuffled List of {@link BidderRequest}.
*/
private List<BidderRequest> getBidderRequests(
Map<String, PrivacyEnforcementResult> bidderToPrivacyEnforcementResult, BidRequest bidRequest,
Expand Down Expand Up @@ -700,61 +742,19 @@ private List<BidderRequest> updateRequestMetric(List<BidderRequest> bidderReques
}

/**
* Extracts {@link ExtRequestTargeting} from {@link ExtBidRequest} model.
*/
private static ExtRequestTargeting targeting(ExtBidRequest requestExt) {
final ExtRequestPrebid prebid = requestExt != null ? requestExt.getPrebid() : null;
return prebid != null ? prebid.getTargeting() : null;
}

/**
* Extracts auctiontimestamp or creates if it is null from {@link ExtBidRequest} model.
* Extracts currency rates from {@link ExtRequestTargeting}.
*/
private static Long auctionTimestamp(ExtBidRequest requestExt) {
final ExtRequestPrebid prebid = requestExt != null ? requestExt.getPrebid() : null;
final Long auctionTimestamp = prebid != null ? prebid.getAuctiontimestamp() : null;
return auctionTimestamp != null ? auctionTimestamp : Instant.now().toEpochMilli();
private static Map<String, Map<String, BigDecimal>> currencyRates(ExtRequestTargeting targeting) {
return targeting != null && targeting.getCurrency() != null ? targeting.getCurrency().getRates() : null;
}

/**
* Creates {@link BidRequestCacheInfo} based on {@link ExtBidRequest} model.
* Extracts bidAdjustments from {@link ExtBidRequest}.
*/
private BidRequestCacheInfo bidRequestCacheInfo(ExtRequestTargeting targeting, ExtBidRequest requestExt) {
private static Map<String, BigDecimal> bidAdjustments(ExtBidRequest requestExt) {
final ExtRequestPrebid prebid = requestExt != null ? requestExt.getPrebid() : null;
final ExtRequestPrebidCache cache = prebid != null ? prebid.getCache() : null;

if (targeting != null && cache != null) {
final boolean shouldCacheBids = cache.getBids() != null;
final boolean shouldCacheVideoBids = cache.getVastxml() != null;
final boolean shouldCacheWinningBidsOnly = targeting.getIncludebidderkeys()
? false // ext.prebid.targeting.includebidderkeys takes precedence
: ObjectUtils.defaultIfNull(cache.getWinningonly(), false);

if (shouldCacheBids || shouldCacheVideoBids || shouldCacheWinningBidsOnly) {
final Integer cacheBidsTtl = shouldCacheBids ? cache.getBids().getTtlseconds() : null;
final Integer cacheVideoBidsTtl = shouldCacheVideoBids ? cache.getVastxml().getTtlseconds() : null;

final boolean returnCreativeBid = shouldCacheBids
? ObjectUtils.defaultIfNull(cache.getBids().getReturnCreative(), true)
: false;
final boolean returnCreativeVideoBid = shouldCacheVideoBids
? ObjectUtils.defaultIfNull(cache.getVastxml().getReturnCreative(), true)
: false;

return BidRequestCacheInfo.builder()
.doCaching(true)
.shouldCacheBids(shouldCacheBids)
.cacheBidsTtl(cacheBidsTtl)
.shouldCacheVideoBids(shouldCacheVideoBids)
.cacheVideoBidsTtl(cacheVideoBidsTtl)
.returnCreativeBids(returnCreativeBid)
.returnCreativeVideoBids(returnCreativeVideoBid)
.shouldCacheWinningBidsOnly(shouldCacheWinningBidsOnly)
.build();
}
}

return BidRequestCacheInfo.noCache();
final Map<String, BigDecimal> bidAdjustmentFactors = prebid != null ? prebid.getBidadjustmentfactors() : null;
return bidAdjustmentFactors != null ? bidAdjustmentFactors : Collections.emptyMap();
}

/**
Expand Down
29 changes: 16 additions & 13 deletions src/main/java/org/prebid/server/cache/CacheService.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,32 +168,36 @@ private Future<BidCacheResponse> failResponse(Throwable exception, long startTim
* The returned result will always have the number of elements equals putObjects list size.
*/
public Future<BidCacheResponse> cachePutObjects(List<PutObject> putObjects, Set<String> biddersAllowingVastUpdate,
String accountId, Timeout timeout, Long auctionTimestamp) {
final List<PutObject> updatedPutObjects = updatePutObjects(putObjects, biddersAllowingVastUpdate,
accountId, auctionTimestamp);
String accountId, Timeout timeout) {
final List<PutObject> updatedPutObjects = updatePutObjects(putObjects, biddersAllowingVastUpdate, accountId);
return makeRequest(BidCacheRequest.of(updatedPutObjects), updatedPutObjects.size(), timeout);
}

/**
* Modify VAST value in putObjects.
*/
private List<PutObject> updatePutObjects(List<PutObject> putObjects, Set<String> biddersAllowingVastUpdate,
String accountId, Long auctionTimestamp) {
String accountId) {
if (CollectionUtils.isEmpty(biddersAllowingVastUpdate)) {
return putObjects;
}

final List<PutObject> updatedPutObjects = new ArrayList<>();
for (PutObject putObject : putObjects) {
final PutObject.PutObjectBuilder builder = putObject.toBuilder()
// remove "/vtrack" specific fields
.bidid(null)
.bidder(null)
.timestamp(null);

final JsonNode value = putObject.getValue();
if (biddersAllowingVastUpdate.contains(putObject.getBidder()) && value != null) {
final String updatedVastValue = modifyVastXml(value.asText(), putObject.getBidid(),
putObject.getBidder(), accountId, auctionTimestamp);
final PutObject updatedPutObject = putObject.toBuilder().value(new TextNode(updatedVastValue)).build();
updatedPutObjects.add(updatedPutObject);
} else {
updatedPutObjects.add(putObject);
final String updatedVastXml = modifyVastXml(value.asText(), putObject.getBidid(),
putObject.getBidder(), accountId, putObject.getTimestamp());
builder.value(new TextNode(updatedVastXml)).build();
}

updatedPutObjects.add(builder.build());
Comment on lines +199 to +200
Copy link
Contributor

Choose a reason for hiding this comment

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

suggest

   for (PutObject putObject : putObjects) {

            JsonNode value = putObject.getValue();
            if (biddersAllowingVastUpdate.contains(putObject.getBidder()) && value != null) {
                final String updatedVastXml = modifyVastXml(value.asText(), putObject.getBidid(),
                        putObject.getBidder(), accountId, putObject.getTimestamp());
                value = new TextNode(updatedVastXml);
            }

            updatedPutObjects.add(putObject.toBuilder()
                    // remove "/vtrack" specific fields
                    .bidid(null)
                    .bidder(null)
                    .timestamp(null)
                    
                    .value(value)
                    .build());
        }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

kind of taste-)

}
return updatedPutObjects;
}
Expand Down Expand Up @@ -228,7 +232,7 @@ public Future<CacheServiceResult> cacheBidsOpenrtb(List<com.iab.openrtb.response
impIdToTtl, videoImpIds, impWithNoExpExists, cacheContext.getCacheVideoBidsTtl(), account);

result = doCacheOpenrtb(cacheBids, videoCacheBids, cacheContext.getBidderToVideoBidIdsToModify(),
cacheContext.getBiddersToCacheBidIds(), account.getId(), timeout, timestamp);
cacheContext.getBidderToBidIds(), account.getId(), timeout, timestamp);
}

return result;
Expand Down Expand Up @@ -441,8 +445,7 @@ private PutObject createXmlPutObjectOpenrtb(CacheBid cacheBid,
.build();
}

private String modifyVastXml(String stringValue, String bidId, String bidder, String accountId,
Long timestamp) {
private String modifyVastXml(String stringValue, String bidId, String bidder, String accountId, Long timestamp) {
final String closeTag = "</Impression>";
final int closeTagIndex = stringValue.indexOf(closeTag);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ public class CacheContext {

Map<String, List<String>> bidderToVideoBidIdsToModify;

Map<String, List<String>> biddersToCacheBidIds;
Map<String, List<String>> bidderToBidIds;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ public class PutObject {

Integer expiry;

String bidid;
Integer ttlseconds;

String bidder;
String bidid; // this is "/vtrack" specific

Integer ttlseconds;
}
String bidder; // this is "/vtrack" specific

Long timestamp; // this is "/vtrack" specific
}
6 changes: 3 additions & 3 deletions src/main/java/org/prebid/server/events/EventRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ public class EventRequest {

String bidId;

String accountId;

String bidder;

String accountId;
Long timestamp;

Format format;

Analytics analytics;

Long timestamp;

public enum Type {

win, imp
Expand Down
Loading