Skip to content

Commit

Permalink
fix review
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgeniiMunin committed Jan 6, 2025
1 parent ab14bf4 commit 9216dfd
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void initialize(Promise<Void> initializePromise) {
final Path databasePath = Files.createTempFile("GeoLite2-Country", ".mmdb");

try (InputStream inputStream = url.openStream();
FileOutputStream outputStream = new FileOutputStream(databasePath.toFile())) {
FileOutputStream outputStream = new FileOutputStream(databasePath.toFile())) {
inputStream.transferTo(outputStream);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.apache.commons.lang3.StringUtils;
import org.prebid.server.analytics.reporter.greenbids.model.ExplorationResult;
import org.prebid.server.analytics.reporter.greenbids.model.Ortb2ImpExtResult;
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.data.Partner;
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.data.GreenbidsConfig;
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.result.AnalyticsResult;
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.result.GreenbidsInvocationResult;
import org.prebid.server.hooks.v1.InvocationAction;
Expand All @@ -23,12 +23,12 @@ public class GreenbidsInvocationService {
private static final int RANGE_16_BIT_INTEGER_DIVISION_BASIS = 0x10000;

public GreenbidsInvocationResult createGreenbidsInvocationResult(
Partner partner,
GreenbidsConfig greenbidsConfig,
BidRequest bidRequest,
Map<String, Map<String, Boolean>> impsBiddersFilterMap) {

final String greenbidsId = UUID.randomUUID().toString();
final boolean isExploration = isExploration(partner, greenbidsId);
final boolean isExploration = isExploration(greenbidsConfig, greenbidsId);

final BidRequest updatedBidRequest = isExploration
? bidRequest
Expand All @@ -49,10 +49,10 @@ public GreenbidsInvocationResult createGreenbidsInvocationResult(
return GreenbidsInvocationResult.of(updatedBidRequest, invocationAction, analyticsResult);
}

private Boolean isExploration(Partner partner, String greenbidsId) {
private Boolean isExploration(GreenbidsConfig greenbidsConfig, String greenbidsId) {
final int hashInt = Integer.parseInt(
greenbidsId.substring(greenbidsId.length() - 4), 16);
return hashInt < partner.getExplorationRate() * RANGE_16_BIT_INTEGER_DIVISION_BASIS;
return hashInt < greenbidsConfig.getExplorationRate() * 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
@@ -1,7 +1,7 @@
package org.prebid.server.hooks.modules.greenbids.real.time.data.core;

import io.vertx.core.Future;
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.data.Partner;
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.data.GreenbidsConfig;

import java.util.Objects;

Expand All @@ -18,14 +18,14 @@ public OnnxModelRunnerWithThresholds(
this.thresholdCache = Objects.requireNonNull(thresholdCache);
}

public Future<OnnxModelRunner> retrieveOnnxModelRunner(Partner partner) {
final String onnxModelPath = "models_pbuid=" + partner.getPbuid() + ".onnx";
return modelCache.get(onnxModelPath, partner.getPbuid());
public Future<OnnxModelRunner> retrieveOnnxModelRunner(GreenbidsConfig greenbidsConfig) {
final String onnxModelPath = "models_pbuid=" + greenbidsConfig.getPbuid() + ".onnx";
return modelCache.get(onnxModelPath, greenbidsConfig.getPbuid());
}

public Future<Double> retrieveThreshold(Partner partner) {
final String thresholdJsonPath = "thresholds_pbuid=" + partner.getPbuid() + ".json";
return thresholdCache.get(thresholdJsonPath, partner.getPbuid())
.map(partner::getThreshold);
public Future<Double> retrieveThreshold(GreenbidsConfig greenbidsConfig) {
final String thresholdJsonPath = "thresholds_pbuid=" + greenbidsConfig.getPbuid() + ".json";
return thresholdCache.get(thresholdJsonPath, greenbidsConfig.getPbuid())
.map(greenbidsConfig::getThreshold);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
import java.util.stream.IntStream;

@Value(staticConstructor = "of")
public class Partner {

@JsonProperty(required = true)
Boolean enabled;
public class GreenbidsConfig {

String pbuid;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.prebid.server.hooks.modules.greenbids.real.time.data.core.GreenbidsInvocationService;
import org.prebid.server.hooks.modules.greenbids.real.time.data.core.OnnxModelRunner;
import org.prebid.server.hooks.modules.greenbids.real.time.data.core.OnnxModelRunnerWithThresholds;
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.data.Partner;
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.data.GreenbidsConfig;
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.data.ThrottlingMessage;
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.result.AnalyticsResult;
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.result.GreenbidsInvocationResult;
Expand Down Expand Up @@ -77,69 +77,57 @@ public Future<InvocationResult<AuctionRequestPayload>> call(

final AuctionContext auctionContext = invocationContext.auctionContext();
final BidRequest bidRequest = auctionContext.getBidRequest();
final Partner appliedPartner = Optional.ofNullable(parseBidRequestExt(auctionContext))
.orElse(parseAccountConfig(auctionContext));

if (!appliedPartner.getEnabled()) {
return Future.succeededFuture(toInvocationResult(
bidRequest, null, InvocationAction.no_action));
}
final GreenbidsConfig greenbidsConfig = Optional.ofNullable(parseBidRequestExt(auctionContext))
.orElse(parseAccountConfig(auctionContext.getAccount()));

return Future.all(
onnxModelRunnerWithThresholds.retrieveOnnxModelRunner(appliedPartner),
onnxModelRunnerWithThresholds.retrieveThreshold(appliedPartner))
onnxModelRunnerWithThresholds.retrieveOnnxModelRunner(greenbidsConfig),
onnxModelRunnerWithThresholds.retrieveThreshold(greenbidsConfig))
.compose(compositeFuture -> toInvocationResult(
bidRequest,
appliedPartner,
greenbidsConfig,
compositeFuture.resultAt(0),
compositeFuture.resultAt(1)))
.recover(throwable -> Future.succeededFuture(toInvocationResult(
bidRequest, null, InvocationAction.no_action)));
}

private Partner parseBidRequestExt(AuctionContext auctionContext) {
private GreenbidsConfig parseBidRequestExt(AuctionContext auctionContext) {
return Optional.ofNullable(auctionContext)
.map(AuctionContext::getBidRequest)
.map(BidRequest::getExt)
.map(ExtRequest::getPrebid)
.map(ExtRequestPrebid::getAnalytics)
.filter(this::isNotEmptyObjectNode)
.map(analytics -> (ObjectNode) analytics.get(BID_REQUEST_ANALYTICS_EXTENSION_NAME))
.map(this::toPartner)
.map(this::toGreenbidsConfig)
.orElse(null);
}

private boolean isNotEmptyObjectNode(JsonNode analytics) {
return analytics != null && analytics.isObject() && !analytics.isEmpty();
}

private Partner parseAccountConfig(AuctionContext auctionContext) {
final Map<String, ObjectNode> modules = Optional.ofNullable(auctionContext)
.map(AuctionContext::getAccount)
private GreenbidsConfig parseAccountConfig(Account account) {
return Optional.ofNullable(account)
.map(Account::getHooks)
.map(AccountHooksConfiguration::getModules)
.map(modules -> modules.get(name()))
.map(this::toGreenbidsConfig)
.orElse(null);

Partner partner = null;
if (modules != null && modules.containsKey("greenbids")) {
final ObjectNode moduleConfig = modules.get("greenbids");
partner = toPartner(moduleConfig);
}

return partner;
}

private Partner toPartner(ObjectNode adapterNode) {
private GreenbidsConfig toGreenbidsConfig(ObjectNode adapterNode) {
try {
return mapper.treeToValue(adapterNode, Partner.class);
return mapper.treeToValue(adapterNode, GreenbidsConfig.class);
} catch (JsonProcessingException e) {
return null;
}
}

private Future<InvocationResult<AuctionRequestPayload>> toInvocationResult(
BidRequest bidRequest,
Partner partner,
GreenbidsConfig greenbidsConfig,
OnnxModelRunner onnxModelRunner,
Double threshold) {

Expand All @@ -158,7 +146,7 @@ private Future<InvocationResult<AuctionRequestPayload>> toInvocationResult(
}

final GreenbidsInvocationResult greenbidsInvocationResult = greenbidsInvocationService
.createGreenbidsInvocationResult(partner, bidRequest, impsBiddersFilterMap);
.createGreenbidsInvocationResult(greenbidsConfig, bidRequest, impsBiddersFilterMap);

return Future.succeededFuture(toInvocationResult(
greenbidsInvocationResult.getUpdatedBidRequest(),
Expand Down Expand Up @@ -227,4 +215,8 @@ private ObjectNode toObjectNode(Map<String, Ortb2ImpExtResult> values) {
public String code() {
return CODE;
}

public String name() {
return "greenbids";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.prebid.server.analytics.reporter.greenbids.model.Ortb2ImpExtResult;
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.data.Partner;
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.data.GreenbidsConfig;
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.result.GreenbidsInvocationResult;
import org.prebid.server.hooks.v1.InvocationAction;

Expand Down Expand Up @@ -47,11 +47,11 @@ public void createGreenbidsInvocationResultShouldReturnUpdateBidRequestWhenNotEx
final Device device = givenDevice(identity());
final BidRequest bidRequest = givenBidRequest(request -> request, List.of(imp), device);
final Map<String, Map<String, Boolean>> impsBiddersFilterMap = givenImpsBiddersFilterMap();
final Partner partner = givenPartner(0.0);
final GreenbidsConfig greenbidsConfig = givenPartner(0.0);

// when
final GreenbidsInvocationResult result = target.createGreenbidsInvocationResult(
partner, bidRequest, impsBiddersFilterMap);
greenbidsConfig, bidRequest, impsBiddersFilterMap);

// then
final JsonNode updatedBidRequestExtPrebidBidders = result.getUpdatedBidRequest().getImp().getFirst().getExt()
Expand Down Expand Up @@ -84,11 +84,11 @@ public void createGreenbidsInvocationResultShouldReturnNoActionWhenExploration()
final Device device = givenDevice(identity());
final BidRequest bidRequest = givenBidRequest(request -> request, List.of(imp), device);
final Map<String, Map<String, Boolean>> impsBiddersFilterMap = givenImpsBiddersFilterMap();
final Partner partner = givenPartner(1.0);
final GreenbidsConfig greenbidsConfig = givenPartner(1.0);

// when
final GreenbidsInvocationResult result = target.createGreenbidsInvocationResult(
partner, bidRequest, impsBiddersFilterMap);
greenbidsConfig, bidRequest, impsBiddersFilterMap);

// then
final JsonNode updatedBidRequestExtPrebidBidders = result.getUpdatedBidRequest().getImp().getFirst().getExt()
Expand Down Expand Up @@ -120,7 +120,7 @@ private Map<String, Map<String, Boolean>> givenImpsBiddersFilterMap() {
return impsBiddersFilterMap;
}

private Partner givenPartner(Double explorationRate) {
return Partner.of(true, "test-pbuid", 0.60, explorationRate);
private GreenbidsConfig givenPartner(Double explorationRate) {
return GreenbidsConfig.of("test-pbuid", 0.60, explorationRate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.prebid.server.analytics.reporter.greenbids.model.GreenbidsAdUnit;
import org.prebid.server.analytics.reporter.greenbids.model.GreenbidsAnalyticsProperties;
import org.prebid.server.analytics.reporter.greenbids.model.GreenbidsBid;
import org.prebid.server.analytics.reporter.greenbids.model.GreenbidsPrebidExt;
import org.prebid.server.analytics.reporter.greenbids.model.GreenbidsConfig;
import org.prebid.server.analytics.reporter.greenbids.model.GreenbidsSource;
import org.prebid.server.analytics.reporter.greenbids.model.GreenbidsUnifiedCode;
import org.prebid.server.analytics.reporter.greenbids.model.MediaTypes;
Expand Down Expand Up @@ -119,10 +119,10 @@ public <T> Future<Void> processEvent(T event) {
return Future.failedFuture(new PreBidException("Bid response or auction context cannot be null"));
}

final GreenbidsPrebidExt greenbidsPrebidExt = Optional.ofNullable(parseBidRequestExt(auctionContext))
.orElse(parseAccountConfig(auctionContext));
final GreenbidsConfig greenbidsConfig = Optional.ofNullable(parseBidRequestExt(auctionContext))
.orElse(parseAccountConfig(auctionContext.getAccount()));

if (greenbidsPrebidExt == null) {
if (greenbidsConfig == null) {
return Future.succeededFuture();
}

Expand All @@ -133,7 +133,7 @@ public <T> Future<Void> processEvent(T event) {

final String greenbidsId = greenbidsId(analyticsResultFromAnalyticsTag);

if (!isSampled(greenbidsPrebidExt.getGreenbidsSampling(), greenbidsId)) {
if (!isSampled(greenbidsConfig.getGreenbidsSampling(), greenbidsId)) {
return Future.succeededFuture();
}

Expand All @@ -144,7 +144,7 @@ public <T> Future<Void> processEvent(T event) {
bidResponse,
greenbidsId,
billingId,
greenbidsPrebidExt,
greenbidsConfig,
analyticsResultFromAnalyticsTag);
commonMessageJson = jacksonMapper.encodeToString(commonMessage);
} catch (PreBidException e) {
Expand Down Expand Up @@ -172,41 +172,34 @@ public <T> Future<Void> processEvent(T event) {
return responseFuture.compose(this::processAnalyticServerResponse);
}

private GreenbidsPrebidExt parseBidRequestExt(AuctionContext auctionContext) {
private GreenbidsConfig parseBidRequestExt(AuctionContext auctionContext) {
return Optional.ofNullable(auctionContext)
.map(AuctionContext::getBidRequest)
.map(BidRequest::getExt)
.map(ExtRequest::getPrebid)
.map(ExtRequestPrebid::getAnalytics)
.filter(this::isNotEmptyObjectNode)
.map(analytics -> (ObjectNode) analytics.get(BID_REQUEST_ANALYTICS_EXTENSION_NAME))
.map(this::toGreenbidsPrebidExt)
.map(this::toGreenbidsConfig)
.orElse(null);
}

private boolean isNotEmptyObjectNode(JsonNode analytics) {
return analytics != null && analytics.isObject() && !analytics.isEmpty();
}

private GreenbidsPrebidExt parseAccountConfig(AuctionContext auctionContext) {
final Map<String, ObjectNode> modules = Optional.ofNullable(auctionContext)
.map(AuctionContext::getAccount)
private GreenbidsConfig parseAccountConfig(Account account) {
return Optional.ofNullable(account)
.map(Account::getAnalytics)
.map(AccountAnalyticsConfig::getModules)
.map(analyticsModules -> analyticsModules.get(name()))
.map(this::toGreenbidsConfig)
.orElse(null);

GreenbidsPrebidExt greenbidsPrebidExt = null;
if (modules != null && modules.containsKey(BID_REQUEST_ANALYTICS_EXTENSION_NAME)) {
final ObjectNode moduleConfig = modules.get(BID_REQUEST_ANALYTICS_EXTENSION_NAME);
greenbidsPrebidExt = toGreenbidsPrebidExt(moduleConfig);
}

return greenbidsPrebidExt;
}

private GreenbidsPrebidExt toGreenbidsPrebidExt(ObjectNode adapterNode) {
private GreenbidsConfig toGreenbidsConfig(ObjectNode adapterNode) {
try {
return jacksonMapper.mapper().treeToValue(adapterNode, GreenbidsPrebidExt.class);
return jacksonMapper.mapper().treeToValue(adapterNode, GreenbidsConfig.class);
} catch (JsonProcessingException e) {
throw new PreBidException("Error decoding bid request analytics extension: " + e.getMessage(), e);
}
Expand Down Expand Up @@ -309,7 +302,7 @@ private CommonMessage createBidMessage(
BidResponse bidResponse,
String greenbidsId,
String billingId,
GreenbidsPrebidExt greenbidsImpExt,
GreenbidsConfig greenbidsImpExt,
Map<String, Ortb2ImpExtResult> analyticsResultFromAnalyticsTag) {
final Optional<BidRequest> bidRequest = Optional.ofNullable(auctionContext.getBidRequest());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import lombok.Value;

@Value(staticConstructor = "of")
public class GreenbidsPrebidExt {

Boolean enabled;
public class GreenbidsConfig {

String pbuid;

Expand Down

0 comments on commit 9216dfd

Please sign in to comment.