Skip to content

Commit

Permalink
fix review: invocationContext.accountConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgeniiMunin committed Jan 28, 2025
1 parent b0c5c69 commit e6a10a8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public class GreenbidsConfig {

String pbuid;

@JsonProperty("targetTpr")
@JsonProperty("target-tpr")
Double targetTpr;

@JsonProperty("explorationRate")
@JsonProperty("exploration-rate")
Double explorationRate;

public Double getThreshold(ThrottlingThresholds throttlingThresholds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Future<InvocationResult<AuctionRequestPayload>> call(
final AuctionContext auctionContext = invocationContext.auctionContext();
final BidRequest bidRequest = auctionContext.getBidRequest();
final GreenbidsConfig greenbidsConfig = Optional.ofNullable(parseBidRequestExt(auctionContext))
.orElse(parseAccountConfig(auctionContext.getAccount()));
.orElse(parseAccountConfig(invocationContext.accountConfig()));

if (greenbidsConfig == null) {
return Future.failedFuture(
Expand Down Expand Up @@ -113,13 +113,12 @@ private boolean isNotEmptyObjectNode(JsonNode analytics) {
return analytics != null && analytics.isObject() && !analytics.isEmpty();
}

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);
private GreenbidsConfig parseAccountConfig(ObjectNode accountConfig) {
try {
return mapper.treeToValue(accountConfig, GreenbidsConfig.class);
} catch (JsonProcessingException e) {
throw new PreBidException(e.getMessage());
}
}

private GreenbidsConfig toGreenbidsConfig(ObjectNode adapterNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ public void callShouldFilterBiddersWhenPartnerActivatedInBidRequest()
final BidRequest bidRequest = givenBidRequestWithExtension(identity(), List.of(imp));
final AuctionContext auctionContext = givenAuctionContext(
bidRequest,
context -> context,
explorationRate);
final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(auctionContext);
context -> context);
final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(
auctionContext, explorationRate);
when(invocationContext.auctionContext()).thenReturn(auctionContext);
when(modelCacheWithExpiration.getIfPresent("onnxModelRunner_test-pbuid"))
.thenReturn(givenOnnxModelRunner());
Expand Down Expand Up @@ -229,8 +229,9 @@ public void callShouldNotFilterBiddersAndReturnAnalyticsTagWhenExploration() thr
final Double explorationRate = 1.0;
final Device device = givenDevice(identity());
final BidRequest bidRequest = givenBidRequest(request -> request, List.of(imp), device);
final AuctionContext auctionContext = givenAuctionContext(bidRequest, context -> context, explorationRate);
final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(auctionContext);
final AuctionContext auctionContext = givenAuctionContext(bidRequest, context -> context);
final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(
auctionContext, explorationRate);
when(invocationContext.auctionContext()).thenReturn(auctionContext);
when(modelCacheWithExpiration.getIfPresent("onnxModelRunner_test-pbuid"))
.thenReturn(givenOnnxModelRunner());
Expand Down Expand Up @@ -282,8 +283,9 @@ public void callShouldFilterBiddersBasedOnModelWhenAnyFeatureNotAvailable() thro
final Double explorationRate = 0.0001;
final Device device = givenDeviceWithoutUserAgent(identity());
final BidRequest bidRequest = givenBidRequest(request -> request, List.of(imp), device);
final AuctionContext auctionContext = givenAuctionContext(bidRequest, context -> context, explorationRate);
final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(auctionContext);
final AuctionContext auctionContext = givenAuctionContext(bidRequest, context -> context);
final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(
auctionContext, explorationRate);
when(invocationContext.auctionContext()).thenReturn(auctionContext);
when(modelCacheWithExpiration.getIfPresent("onnxModelRunner_test-pbuid"))
.thenReturn(givenOnnxModelRunner());
Expand Down Expand Up @@ -342,8 +344,9 @@ public void callShouldFilterBiddersBasedOnModelResults() throws OrtException, IO
final Double explorationRate = 0.0001;
final Device device = givenDevice(identity());
final BidRequest bidRequest = givenBidRequest(request -> request, List.of(imp), device);
final AuctionContext auctionContext = givenAuctionContext(bidRequest, context -> context, explorationRate);
final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(auctionContext);
final AuctionContext auctionContext = givenAuctionContext(bidRequest, context -> context);
final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(
auctionContext, explorationRate);
when(invocationContext.auctionContext()).thenReturn(auctionContext);
when(modelCacheWithExpiration.getIfPresent("onnxModelRunner_test-pbuid"))
.thenReturn(givenOnnxModelRunner());
Expand Down Expand Up @@ -391,38 +394,30 @@ public void callShouldFilterBiddersBasedOnModelResults() throws OrtException, IO

private AuctionContext givenAuctionContext(
BidRequest bidRequest,
UnaryOperator<AuctionContext.AuctionContextBuilder> auctionContextCustomizer,
Double explorationRate) {
UnaryOperator<AuctionContext.AuctionContextBuilder> auctionContextCustomizer) {

final AuctionContext.AuctionContextBuilder auctionContextBuilder = AuctionContext.builder()
.httpRequest(HttpRequestContext.builder().build())
.bidRequest(bidRequest)
.account(givenAccount(explorationRate));
.bidRequest(bidRequest);

return auctionContextCustomizer.apply(auctionContextBuilder).build();
}

private AuctionInvocationContext givenAuctionInvocationContext(AuctionContext auctionContext) {
private AuctionInvocationContext givenAuctionInvocationContext(
AuctionContext auctionContext, Double explorationRate) {
final AuctionInvocationContext invocationContext = mock(AuctionInvocationContext.class);
when(invocationContext.auctionContext()).thenReturn(auctionContext);
when(invocationContext.accountConfig()).thenReturn(givenAccountConfig(explorationRate));
return invocationContext;
}

private Account givenAccount(Double explorationRate) {
return Account.builder()
.id("test-account")
.hooks(givenAccountHooksConfiguration(explorationRate))
.build();
}

private AccountHooksConfiguration givenAccountHooksConfiguration(Double explorationRate) {
private ObjectNode givenAccountConfig(Double explorationRate) {
final ObjectNode greenbidsNode = TestBidRequestProvider.MAPPER.createObjectNode();
greenbidsNode.put("enabled", true);
greenbidsNode.put("pbuid", "test-pbuid");
greenbidsNode.put("targetTpr", 0.60);
greenbidsNode.put("explorationRate", explorationRate);
final Map<String, ObjectNode> modules = Map.of("greenbids", greenbidsNode);
return AccountHooksConfiguration.of(null, modules, null);
greenbidsNode.put("target-tpr", 0.60);
greenbidsNode.put("exploration-rate", explorationRate);
return greenbidsNode;
}

private OnnxModelRunner givenOnnxModelRunner() throws OrtException, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class GreenbidsConfig {

String pbuid;

@JsonProperty("greenbidsSampling")
@JsonProperty("greenbids-sampling")
Double greenbidsSampling;
}
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ private static Account givenAccount() {
private static AccountAnalyticsConfig givenAccountHooksConfiguration() {
final ObjectNode greenbidsNode = mapper.createObjectNode();
greenbidsNode.put("pbuid", "leparisien");
greenbidsNode.put("greenbidsSampling", 1.0);
greenbidsNode.put("greenbids-sampling", 1.0);
final Map<String, ObjectNode> modules = Map.of("greenbids", greenbidsNode);
return AccountAnalyticsConfig.of(true, null, modules);
}
Expand Down Expand Up @@ -701,7 +701,7 @@ private static BidRequest givenBidRequestWithExtension(
private static ExtRequest givenExtRequest() {
final ObjectNode greenbidsNode = new ObjectMapper().createObjectNode();
greenbidsNode.put("pbuid", "leparisien");
greenbidsNode.put("greenbidsSampling", 1.0);
greenbidsNode.put("greenbids-sampling", 1.0);

final ObjectNode analyticsNode = new ObjectMapper().createObjectNode();
analyticsNode.set("greenbids", greenbidsNode);
Expand Down

0 comments on commit e6a10a8

Please sign in to comment.