Skip to content

Commit

Permalink
Remove redundant comments from bidders (prebid#1430)
Browse files Browse the repository at this point in the history
+Code cleanup
  • Loading branch information
SerhiiNahornyi authored Sep 24, 2021
1 parent b088aae commit 2239a33
Show file tree
Hide file tree
Showing 122 changed files with 1,743 additions and 1,993 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public class BidsBlocker {
private final boolean debugEnabled;

private BidsBlocker(
List<BidderBid> bids,
String bidder,
ObjectNode accountConfig,
BlockedAttributes blockedAttributes,
boolean debugEnabled) {
List<BidderBid> bids,
String bidder,
ObjectNode accountConfig,
BlockedAttributes blockedAttributes,
boolean debugEnabled) {

this.bids = bids;
this.bidder = bidder;
Expand All @@ -57,137 +57,137 @@ private BidsBlocker(
}

public static BidsBlocker create(
List<BidderBid> bids,
String bidder,
ObjectNode accountConfig,
BlockedAttributes blockedAttributes,
boolean debugEnabled) {
List<BidderBid> bids,
String bidder,
ObjectNode accountConfig,
BlockedAttributes blockedAttributes,
boolean debugEnabled) {

return new BidsBlocker(
Objects.requireNonNull(bids),
Objects.requireNonNull(bidder),
accountConfig,
blockedAttributes,
debugEnabled);
Objects.requireNonNull(bids),
Objects.requireNonNull(bidder),
accountConfig,
blockedAttributes,
debugEnabled);
}

public ExecutionResult<BlockedBids> block() {
final AccountConfigReader accountConfigReader = AccountConfigReader.create(accountConfig, bidder, debugEnabled);

try {
final List<Result<BlockingResult>> blockedBidResults = bids.stream()
.sequential()
.map(bid -> isBlocked(bid, accountConfigReader))
.collect(Collectors.toList());
.sequential()
.map(bid -> isBlocked(bid, accountConfigReader))
.collect(Collectors.toList());

final Set<Integer> blockedBidIndexes = IntStream.range(0, bids.size())
.filter(index -> blockedBidResults.get(index).getValue().isBlocked())
.boxed()
.collect(Collectors.toSet());
.filter(index -> blockedBidResults.get(index).getValue().isBlocked())
.boxed()
.collect(Collectors.toSet());

final BlockedBids blockedBids = !blockedBidIndexes.isEmpty() ? BlockedBids.of(blockedBidIndexes) : null;
final List<String> warnings = MergeUtils.mergeMessages(blockedBidResults);

return ExecutionResult.<BlockedBids>builder()
.value(blockedBids)
.debugMessages(blockedBids != null ? debugMessages(blockedBidIndexes, blockedBidResults) : null)
.warnings(warnings)
.analyticsResults(toAnalyticsResults(blockedBidResults))
.build();
.value(blockedBids)
.debugMessages(blockedBids != null ? debugMessages(blockedBidIndexes, blockedBidResults) : null)
.warnings(warnings)
.analyticsResults(toAnalyticsResults(blockedBidResults))
.build();
} catch (InvalidAccountConfigurationException e) {
return debugEnabled ? ExecutionResult.withError(e.getMessage()) : ExecutionResult.empty();
}
}

private Result<BlockingResult> isBlocked(BidderBid bidderBid, AccountConfigReader accountConfigReader) {
final Result<ResponseBlockingConfig> blockingConfigResult =
accountConfigReader.responseBlockingConfigFor(bidderBid);
accountConfigReader.responseBlockingConfigFor(bidderBid);
final ResponseBlockingConfig blockingConfig = blockingConfigResult.getValue();

final BlockingResult blockingResult = BlockingResult.of(
bidderBid.getBid().getImpid(),
checkBadv(bidderBid, blockingConfig),
checkBcat(bidderBid, blockingConfig),
checkBapp(bidderBid, blockingConfig),
checkBattr(bidderBid, blockingConfig));
bidderBid.getBid().getImpid(),
checkBadv(bidderBid, blockingConfig),
checkBcat(bidderBid, blockingConfig),
checkBapp(bidderBid, blockingConfig),
checkBattr(bidderBid, blockingConfig));

return Result.of(blockingResult, blockingConfigResult.getMessages());
}

private AttributeCheckResult<String> checkBadv(BidderBid bidderBid, ResponseBlockingConfig blockingConfig) {
return checkAttribute(
bidderBid.getBid().getAdomain(),
blockingConfig.getBadv(),
blockedAttributeValues(BlockedAttributes::getBadv));
bidderBid.getBid().getAdomain(),
blockingConfig.getBadv(),
blockedAttributeValues(BlockedAttributes::getBadv));
}

private AttributeCheckResult<String> checkBcat(BidderBid bidderBid, ResponseBlockingConfig blockingConfig) {
return checkAttribute(
bidderBid.getBid().getCat(),
blockingConfig.getBcat(),
blockedAttributeValues(BlockedAttributes::getBcat));
bidderBid.getBid().getCat(),
blockingConfig.getBcat(),
blockedAttributeValues(BlockedAttributes::getBcat));
}

private AttributeCheckResult<String> checkBapp(BidderBid bidderBid, ResponseBlockingConfig blockingConfig) {
return checkAttribute(
bidderBid.getBid().getBundle(),
blockingConfig.getBapp(),
blockedAttributeValues(BlockedAttributes::getBapp));
bidderBid.getBid().getBundle(),
blockingConfig.getBapp(),
blockedAttributeValues(BlockedAttributes::getBapp));
}

private AttributeCheckResult<Integer> checkBattr(
BidderBid bidderBid, ResponseBlockingConfig blockingConfig) {
BidderBid bidderBid, ResponseBlockingConfig blockingConfig) {

return checkAttribute(
bidderBid.getBid().getAttr(),
blockingConfig.getBattr(),
blockedAttributeValues(BlockedAttributes::getBattr, bidderBid.getBid().getImpid()));
bidderBid.getBid().getAttr(),
blockingConfig.getBattr(),
blockedAttributeValues(BlockedAttributes::getBattr, bidderBid.getBid().getImpid()));
}

private <T> AttributeCheckResult<T> checkAttribute(
List<T> attribute, BidAttributeBlockingConfig<T> blockingConfig, List<T> blockedAttributeValues) {
List<T> attribute, BidAttributeBlockingConfig<T> blockingConfig, List<T> blockedAttributeValues) {

if (blockingConfig == null || !blockingConfig.isEnforceBlocks()) {
return AttributeCheckResult.succeeded();
}

if (CollectionUtils.isEmpty(attribute)) {
return blockingConfig.isBlockUnknownValues()
? AttributeCheckResult.failed()
: AttributeCheckResult.succeeded();
? AttributeCheckResult.failed()
: AttributeCheckResult.succeeded();
}

if (CollectionUtils.isNotEmpty(blockedAttributeValues)) {
final List<T> blockedBidValues = attribute.stream()
.filter(blockedAttributeValues::contains)
.filter(blockedBidValue -> !blockingConfig.getAllowedValues().contains(blockedBidValue))
.collect(Collectors.toList());
.filter(blockedAttributeValues::contains)
.filter(blockedBidValue -> !blockingConfig.getAllowedValues().contains(blockedBidValue))
.collect(Collectors.toList());

return CollectionUtils.isEmpty(blockedBidValues)
? AttributeCheckResult.succeeded()
: AttributeCheckResult.failed(blockedBidValues);
? AttributeCheckResult.succeeded()
: AttributeCheckResult.failed(blockedBidValues);
}

return AttributeCheckResult.succeeded();
}

private AttributeCheckResult<String> checkAttribute(
String attribute, BidAttributeBlockingConfig<String> blockingConfig, List<String> blockedAttributeValues) {
String attribute, BidAttributeBlockingConfig<String> blockingConfig, List<String> blockedAttributeValues) {

if (blockingConfig == null
|| !blockingConfig.isEnforceBlocks()
|| StringUtils.isEmpty(attribute)
|| CollectionUtils.isEmpty(blockedAttributeValues)) {
|| !blockingConfig.isEnforceBlocks()
|| StringUtils.isEmpty(attribute)
|| CollectionUtils.isEmpty(blockedAttributeValues)) {

return AttributeCheckResult.succeeded();
}

final boolean blocked =
blockedAttributeValues.contains(attribute) && !blockingConfig.getAllowedValues().contains(attribute);
blockedAttributeValues.contains(attribute) && !blockingConfig.getAllowedValues().contains(attribute);

return blocked
? AttributeCheckResult.failed(Collections.singletonList(attribute))
: AttributeCheckResult.succeeded();
? AttributeCheckResult.failed(Collections.singletonList(attribute))
: AttributeCheckResult.succeeded();
}

private <T> T blockedAttributeValues(Function<BlockedAttributes, T> getter) {
Expand All @@ -201,35 +201,35 @@ private <T> T blockedAttributeValues(Function<BlockedAttributes, Map<String, T>>
}

private List<String> debugMessages(
Set<Integer> blockedBidIndexes,
List<Result<BlockingResult>> blockedBidResults) {
Set<Integer> blockedBidIndexes,
List<Result<BlockingResult>> blockedBidResults) {

if (!debugEnabled) {
return null;
}

return blockedBidIndexes.stream()
.map(index -> debugEntryFor(index, blockedBidResults.get(index).getValue()))
.collect(Collectors.toList());
.map(index -> debugEntryFor(index, blockedBidResults.get(index).getValue()))
.collect(Collectors.toList());
}

private String debugEntryFor(int index, BlockingResult blockingResult) {
return String.format(
"Bid %d from bidder %s has been rejected, failed checks: %s",
index,
bidder,
blockingResult.getFailedChecks());
"Bid %d from bidder %s has been rejected, failed checks: %s",
index,
bidder,
blockingResult.getFailedChecks());
}

private List<AnalyticsResult> toAnalyticsResults(List<Result<BlockingResult>> blockedBidResults) {
return blockedBidResults.stream()
.map(Result::getValue)
.map(blockingResult -> AnalyticsResult.of(
blockingResult.isBlocked() ? SUCCESS_BLOCKED_STATUS : SUCCESS_ALLOW_STATUS,
blockingResult.isBlocked() ? toAnalyticsResultValues(blockingResult) : null,
bidder,
blockingResult.getImpId()))
.collect(Collectors.toList());
.map(Result::getValue)
.map(blockingResult -> AnalyticsResult.of(
blockingResult.isBlocked() ? SUCCESS_BLOCKED_STATUS : SUCCESS_ALLOW_STATUS,
blockingResult.isBlocked() ? toAnalyticsResultValues(blockingResult) : null,
bidder,
blockingResult.getImpId()))
.collect(Collectors.toList());
}

private Map<String, Object> toAnalyticsResultValues(BlockingResult blockingResult) {
Expand Down Expand Up @@ -278,25 +278,25 @@ private static class BlockingResult {
AttributeCheckResult<Integer> battrCheckResult;

public static BlockingResult of(
String impId,
AttributeCheckResult<String> badvCheckResult,
AttributeCheckResult<String> bcatCheckResult,
AttributeCheckResult<String> bappCheckResult,
AttributeCheckResult<Integer> battrCheckResult) {
String impId,
AttributeCheckResult<String> badvCheckResult,
AttributeCheckResult<String> bcatCheckResult,
AttributeCheckResult<String> bappCheckResult,
AttributeCheckResult<Integer> battrCheckResult) {

final boolean blocked =
badvCheckResult.isFailed()
|| bcatCheckResult.isFailed()
|| bappCheckResult.isFailed()
|| battrCheckResult.isFailed();
badvCheckResult.isFailed()
|| bcatCheckResult.isFailed()
|| bappCheckResult.isFailed()
|| battrCheckResult.isFailed();

return of(
impId,
blocked,
badvCheckResult,
bcatCheckResult,
bappCheckResult,
battrCheckResult);
impId,
blocked,
badvCheckResult,
bcatCheckResult,
bappCheckResult,
battrCheckResult);
}

public List<String> getFailedChecks() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public class BlockedAttributesResolver {
private final boolean debugEnabled;

private BlockedAttributesResolver(
BidRequest bidRequest,
String bidder,
ObjectNode accountConfig,
boolean debugEnabled) {
BidRequest bidRequest,
String bidder,
ObjectNode accountConfig,
boolean debugEnabled) {

this.bidRequest = bidRequest;
this.bidder = bidder;
Expand All @@ -29,29 +29,29 @@ private BlockedAttributesResolver(
}

public static BlockedAttributesResolver create(
BidRequest bidRequest,
String bidder,
ObjectNode accountConfig,
boolean debugEnabled) {
BidRequest bidRequest,
String bidder,
ObjectNode accountConfig,
boolean debugEnabled) {

return new BlockedAttributesResolver(
Objects.requireNonNull(bidRequest),
Objects.requireNonNull(bidder),
accountConfig,
debugEnabled);
Objects.requireNonNull(bidRequest),
Objects.requireNonNull(bidder),
accountConfig,
debugEnabled);
}

public ExecutionResult<BlockedAttributes> resolve() {
final AccountConfigReader accountConfigReader = AccountConfigReader.create(accountConfig, bidder, debugEnabled);

try {
final Result<BlockedAttributes> blockedAttributesResult =
accountConfigReader.blockedAttributesFor(bidRequest);
accountConfigReader.blockedAttributesFor(bidRequest);

return ExecutionResult.<BlockedAttributes>builder()
.value(blockedAttributesResult.getValue())
.warnings(blockedAttributesResult.getMessages())
.build();
.value(blockedAttributesResult.getValue())
.warnings(blockedAttributesResult.getMessages())
.build();
} catch (InvalidAccountConfigurationException e) {
return debugEnabled ? ExecutionResult.withError(e.getMessage()) : ExecutionResult.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static <T> ExecutionResult<T> empty() {

public static <T> ExecutionResult<T> withError(String error) {
return ExecutionResult.<T>builder()
.errors(Collections.singletonList(error))
.build();
.errors(Collections.singletonList(error))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ static <T> List<String> mergeMessages(Result<?>... results) {

static <T> List<String> mergeMessages(Stream<? extends Result<?>> results) {
final List<String> warnings = results
.map(Result::getMessages)
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.distinct()
.collect(Collectors.toList());
.map(Result::getMessages)
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.distinct()
.collect(Collectors.toList());

return !warnings.isEmpty() ? warnings : null;
}
Expand Down
Loading

0 comments on commit 2239a33

Please sign in to comment.