Skip to content

Commit

Permalink
fix review: handle npe
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgeniiMunin committed Jan 27, 2025
1 parent f155ee3 commit 3e852db
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ public GreenbidsInvocationResult createGreenbidsInvocationResult(
}

private Boolean isExploration(GreenbidsConfig greenbidsConfig, String greenbidsId) {
final double explorationRate = Optional.ofNullable(greenbidsConfig.getExplorationRate()).orElse(1.0);
final int hashInt = Integer.parseInt(
greenbidsId.substring(greenbidsId.length() - 4), 16);
return hashInt < greenbidsConfig.getExplorationRate() * RANGE_16_BIT_INTEGER_DIVISION_BASIS;
return hashInt < explorationRate * RANGE_16_BIT_INTEGER_DIVISION_BASIS;
}

private List<Imp> updateImps(BidRequest bidRequest, Map<String, Map<String, Boolean>> impsBiddersFilterMap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.IntStream;

@Value(staticConstructor = "of")
Expand All @@ -20,13 +21,14 @@ public class GreenbidsConfig {
Double explorationRate;

public Double getThreshold(ThrottlingThresholds throttlingThresholds) {
final double safeTargetTpr = Optional.ofNullable(targetTpr).orElse(1.0);
final List<Double> truePositiveRates = throttlingThresholds.getTpr();
final List<Double> thresholds = throttlingThresholds.getThresholds();

final int minSize = Math.min(truePositiveRates.size(), thresholds.size());

return IntStream.range(0, minSize)
.filter(i -> truePositiveRates.get(i) >= targetTpr)
.filter(i -> truePositiveRates.get(i) >= safeTargetTpr)
.mapToObj(thresholds::get)
.max(Comparator.naturalOrder())
.orElse(0.0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public Future<InvocationResult<AuctionRequestPayload>> call(
final GreenbidsConfig greenbidsConfig = Optional.ofNullable(parseBidRequestExt(auctionContext))
.orElse(parseAccountConfig(auctionContext.getAccount()));

if (greenbidsConfig == null) {
return Future.failedFuture(
new PreBidException("Greenbids config is null; cannot proceed."));
}

return Future.all(
onnxModelRunnerWithThresholds.retrieveOnnxModelRunner(greenbidsConfig),
onnxModelRunnerWithThresholds.retrieveThreshold(greenbidsConfig))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.vertx.core.Future;
import io.vertx.core.MultiMap;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.prebid.server.analytics.AnalyticsReporter;
import org.prebid.server.analytics.model.AmpEvent;
import org.prebid.server.analytics.model.AuctionEvent;
Expand Down Expand Up @@ -134,7 +135,14 @@ public <T> Future<Void> processEvent(T event) {

final String greenbidsId = greenbidsId(analyticsResultFromAnalyticsTag);

if (!isSampled(greenbidsConfig.getGreenbidsSampling(), greenbidsId)) {
final double samplingRate = Optional.ofNullable(greenbidsConfig.getGreenbidsSampling())
.orElseGet(() -> {
logger.warn("Warning: Sampling rate is not defined in request. Set sampling at "
+ greenbidsAnalyticsProperties.getDefaultSamplingRate());
return greenbidsAnalyticsProperties.getDefaultSamplingRate();
});

if (!isSampled(samplingRate, greenbidsId)) {
return Future.succeededFuture();
}

Expand All @@ -146,7 +154,8 @@ public <T> Future<Void> processEvent(T event) {
greenbidsId,
billingId,
greenbidsConfig,
analyticsResultFromAnalyticsTag);
analyticsResultFromAnalyticsTag,
samplingRate);
commonMessageJson = jacksonMapper.encodeToString(commonMessage);
} catch (PreBidException e) {
return Future.failedFuture(e);
Expand Down Expand Up @@ -274,12 +283,6 @@ private Future<Void> processAnalyticServerResponse(HttpClientResponse response)
}

private boolean isSampled(Double samplingRate, String greenbidsId) {
if (samplingRate == null) {
logger.warn("Warning: Sampling rate is not defined in request. Set sampling at "
+ greenbidsAnalyticsProperties.getDefaultSamplingRate());
return true;
}

if (samplingRate < 0 || samplingRate > 1) {
logger.warn("Warning: Sampling rate must be between 0 and 1");
return true;
Expand All @@ -304,8 +307,9 @@ private CommonMessage createBidMessage(
BidResponse bidResponse,
String greenbidsId,
String billingId,
GreenbidsConfig greenbidsImpExt,
Map<String, Ortb2ImpExtResult> analyticsResultFromAnalyticsTag) {
GreenbidsConfig greenbidsConfig,
Map<String, Ortb2ImpExtResult> analyticsResultFromAnalyticsTag,
Double samplingRate) {
final Optional<BidRequest> bidRequest = Optional.ofNullable(auctionContext.getBidRequest());

final List<Imp> imps = bidRequest
Expand Down Expand Up @@ -337,17 +341,16 @@ private CommonMessage createBidMessage(
.map(Site::getPage)
.orElse(null);

final Double greenbidsSamplingRate = Optional.ofNullable(greenbidsImpExt.getGreenbidsSampling())
.orElse(greenbidsAnalyticsProperties.getDefaultSamplingRate());
final String pbuid = Optional.ofNullable(greenbidsConfig.getPbuid()).orElse(StringUtils.EMPTY);

return CommonMessage.builder()
.version(greenbidsAnalyticsProperties.getAnalyticsServerVersion())
.auctionId(auctionId)
.referrer(referrer)
.sampling(greenbidsSamplingRate)
.sampling(samplingRate)
.prebidServer(prebidVersionProvider.getNameVersionRecord())
.greenbidsId(greenbidsId)
.pbuid(greenbidsImpExt.getPbuid())
.pbuid(pbuid)
.billingId(billingId)
.adUnits(adUnitsWithBidResponses)
.auctionElapsed(auctionElapsed)
Expand Down

0 comments on commit 3e852db

Please sign in to comment.