Skip to content

Commit

Permalink
Refactor, additional condition for targeting prefix resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
VeryExtraordinaryUsername committed Nov 27, 2023
1 parent 3f74679 commit 5b94381
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 160 deletions.
28 changes: 24 additions & 4 deletions src/main/java/org/prebid/server/auction/BidResponseCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
import org.prebid.server.settings.model.AccountAuctionConfig;
import org.prebid.server.settings.model.AccountAuctionEventConfig;
import org.prebid.server.settings.model.AccountEventsConfig;
import org.prebid.server.settings.model.AccountTargetingConfig;
import org.prebid.server.settings.model.VideoStoredDataResult;
import org.prebid.server.util.LineItemUtil;
import org.prebid.server.util.StreamUtil;
Expand All @@ -120,6 +121,11 @@

public class BidResponseCreator {

/**
* Default prefix for targeting keys.
* Used in cases, when correspond value is missing in account auction configuration or bid request ext.
*/
private static final String DEFAULT_KEY_PREFIX = "hb";
private static final String CACHE = "cache";
private static final String PREBID_EXT = "prebid";
private static final Integer DEFAULT_BID_LIMIT_MIN = 1;
Expand Down Expand Up @@ -1658,20 +1664,20 @@ private TargetingKeywordsCreator createKeywordsCreator(ExtRequestTargeting targe
JsonNode priceGranularity,
BidRequest bidRequest,
Account account) {

int resolvedTruncateAttrChars = resolveTruncateAttrChars(targeting, account);
String resolveKeyPrefix = resolveKeyPrefix(bidRequest, account, resolvedTruncateAttrChars);
return TargetingKeywordsCreator.create(
parsePriceGranularity(priceGranularity),
BooleanUtils.toBoolean(targeting.getIncludewinners()),
BooleanUtils.toBoolean(targeting.getIncludebidderkeys()),
BooleanUtils.toBoolean(targeting.getAlwaysincludedeals()),
BooleanUtils.isTrue(targeting.getIncludeformat()),
isApp,
resolveTruncateAttrChars(targeting, account),
resolvedTruncateAttrChars,
cacheHost,
cachePath,
TargetingKeywordsResolver.create(bidRequest, mapper),
bidRequest,
account);
resolveKeyPrefix);
}

/**
Expand All @@ -1688,6 +1694,20 @@ private int resolveTruncateAttrChars(ExtRequestTargeting targeting, Account acco
truncateAttrChars);
}

private static String resolveKeyPrefix(BidRequest bidRequest, Account account, int truncateAttrChars) {
final String prefix = Optional.of(bidRequest)
.map(BidRequest::getExt)
.map(ExtRequest::getPrebid)
.map(ExtRequestPrebid::getTargeting)
.map(ExtRequestTargeting::getPrefix)
.orElse(Optional.ofNullable(account)
.map(Account::getAuction)
.map(AccountAuctionConfig::getTargeting)
.map(AccountTargetingConfig::getPrefix)
.orElse(null));
return StringUtils.isNotEmpty(prefix) && prefix.length() + 11 < truncateAttrChars ? prefix : DEFAULT_KEY_PREFIX;
}

private static Integer truncateAttrCharsOrNull(Integer value) {
return value != null && value >= 0 && value <= 255 ? value : null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
package org.prebid.server.auction;

import com.iab.openrtb.request.BidRequest;
import com.iab.openrtb.response.Bid;
import org.apache.commons.lang3.StringUtils;
import org.prebid.server.proto.openrtb.ext.request.ExtPriceGranularity;
import org.prebid.server.proto.openrtb.ext.request.ExtRequest;
import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebid;
import org.prebid.server.proto.openrtb.ext.request.ExtRequestTargeting;
import org.prebid.server.settings.model.Account;
import org.prebid.server.settings.model.AccountAuctionConfig;
import org.prebid.server.settings.model.AccountTargetingConfig;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -88,12 +80,6 @@ public class TargetingKeywordsCreator {
*/
private static final String FORMAT_KEY = "_format";

/**
* Default prefix for targeting keys.
* Used in cases, when correspond value is missing in account auction configuration or bid request ext.
*/
private static final String DEFAULT_KEY_PREFIX = "hb";

private static final String DEFAULT_CPM = "0.0";

private final PriceGranularity priceGranularity;
Expand Down Expand Up @@ -146,8 +132,7 @@ public static TargetingKeywordsCreator create(ExtPriceGranularity extPriceGranul
String cacheHost,
String cachePath,
TargetingKeywordsResolver resolver,
BidRequest bidRequest,
Account account) {
String keyPrefix) {
return new TargetingKeywordsCreator(
PriceGranularity.createFromExtPriceGranularity(extPriceGranularity),
includeWinners,
Expand All @@ -159,21 +144,7 @@ public static TargetingKeywordsCreator create(ExtPriceGranularity extPriceGranul
cacheHost,
cachePath,
resolver,
resolvePrefix(bidRequest, account));
}

private static String resolvePrefix(BidRequest bidRequest, Account account) {
final String prefix = Optional.of(bidRequest)
.map(BidRequest::getExt)
.map(ExtRequest::getPrebid)
.map(ExtRequestPrebid::getTargeting)
.map(ExtRequestTargeting::getPrefix)
.orElse(Optional.ofNullable(account)
.map(Account::getAuction)
.map(AccountAuctionConfig::getTargeting)
.map(AccountTargetingConfig::getPrefix)
.orElse(null));
return StringUtils.isNotEmpty(prefix) ? prefix : DEFAULT_KEY_PREFIX;
keyPrefix);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,8 @@ private void validateTargetingPrefix(ExtRequestTargeting extRequestTargeting) th
&& prefixLength > 0
&& prefixLength + 11 > truncateattrchars; // 11 - length of the longest targeting keyword without prefix
if (prefixLengthInvalid) {
throw new ValidationException("ext.prebid.targeting: prefix length should be less than truncateattrchars"
+ " by 11 characters or more; increase truncateattrchars value or decrease prefix length");
throw new ValidationException("ext.prebid.targeting: decrease prefix length or increase truncateattrchars"
+ " by " + (prefixLength + 11 - truncateattrchars) + " characters");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3801,6 +3801,18 @@ public void shouldThrowErrorIfExtMediaTypePriceGranularityCannotBeParsed() {
+ ".ExtGranularityRange");
}

public void shouldComposeTargetingKeywordsCreatorWithProperCustomTargetingPrefixFromRequest() {
//TODO
}

public void shouldComposeTargetingKeywordsCreatorWithProperCustomTargetingPrefixFromAccount() {
//TODO
}

public void shouldComposeTargetingKeywordsCreatorWithDefaultTargetingPrefix() {
//TODO
}

private AuctionContext givenAuctionContext(BidRequest bidRequest,
UnaryOperator<AuctionContext.AuctionContextBuilder> contextCustomizer) {

Expand Down
Loading

0 comments on commit 5b94381

Please sign in to comment.