Skip to content

Commit

Permalink
default account config + account config from auctioncontext
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgeniiMunin committed Dec 12, 2024
1 parent 743d17e commit 8534f0c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
@Value(staticConstructor = "of")
public class Partner {

@JsonProperty(required = true)
Boolean enabled;

String pbuid;

@JsonProperty("targetTpr")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.prebid.server.hooks.modules.greenbids.real.time.data.v1;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.iab.openrtb.request.BidRequest;
Expand Down Expand Up @@ -33,8 +32,6 @@
import org.prebid.server.hooks.v1.auction.AuctionInvocationContext;
import org.prebid.server.hooks.v1.auction.AuctionRequestPayload;
import org.prebid.server.hooks.v1.auction.ProcessedAuctionRequestHook;
import org.prebid.server.proto.openrtb.ext.request.ExtRequest;
import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebid;
import org.prebid.server.settings.model.Account;
import org.prebid.server.settings.model.AccountHooksConfiguration;

Expand All @@ -49,7 +46,6 @@ public class GreenbidsRealTimeDataProcessedAuctionRequestHook implements Process
private static final String CODE = "greenbids-real-time-data-processed-auction-request";
private static final String ACTIVITY = "greenbids-filter";
private static final String SUCCESS_STATUS = "success";
private static final String BID_REQUEST_ANALYTICS_EXTENSION_NAME = "greenbids-rtd";

private final ObjectMapper mapper;
private final FilterService filterService;
Expand Down Expand Up @@ -77,8 +73,12 @@ public Future<InvocationResult<AuctionRequestPayload>> call(

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

if (!appliedPartner.getEnabled()) {
return Future.succeededFuture(toInvocationResult(
bidRequest, null, InvocationAction.no_action));
}

return Future.all(
onnxModelRunnerWithThresholds.retrieveOnnxModelRunner(appliedPartner),
Expand All @@ -92,17 +92,6 @@ public Future<InvocationResult<AuctionRequestPayload>> call(
bidRequest, null, InvocationAction.no_action)));
}

private Partner parseBidRequestExt(BidRequest bidRequest) {
return Optional.ofNullable(bidRequest)
.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)
.orElse(null);
}

private Partner parseAccountConfig(AuctionContext auctionContext) {
final Map<String, ObjectNode> modules = Optional.ofNullable(auctionContext)
.map(AuctionContext::getAccount)
Expand All @@ -119,10 +108,6 @@ private Partner parseAccountConfig(AuctionContext auctionContext) {
return partner;
}

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

private Partner toPartner(ObjectNode adapterNode) {
try {
return mapper.treeToValue(adapterNode, Partner.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void extractThrottlingMessagesFromBidRequestShouldReturnValidThrottlingMe
.banner(banner)
.build();
final Device device = givenDevice(identity());
final BidRequest bidRequest = givenBidRequest(request -> request, List.of(imp), device, null);
final BidRequest bidRequest = givenBidRequest(request -> request, List.of(imp), device);

final CountryResponse countryResponse = mock(CountryResponse.class);

Expand Down Expand Up @@ -110,7 +110,7 @@ public void extractThrottlingMessagesFromBidRequestShouldHandleMissingIp() {
.banner(banner)
.build();
final Device device = givenDeviceWithoutIp(identity());
final BidRequest bidRequest = givenBidRequest(request -> request, List.of(imp), device, null);
final BidRequest bidRequest = givenBidRequest(request -> request, List.of(imp), device);

final ZonedDateTime timestamp = ZonedDateTime.now(ZoneId.of("UTC"));
final Integer expectedHourBucket = timestamp.getHour();
Expand Down Expand Up @@ -147,7 +147,7 @@ public void extractThrottlingMessagesFromBidRequestShouldThrowPreBidExceptionWhe
.banner(banner)
.build();
final Device device = givenDevice(identity());
final BidRequest bidRequest = givenBidRequest(request -> request, List.of(imp), device, null);
final BidRequest bidRequest = givenBidRequest(request -> request, List.of(imp), device);

when(databaseReader.country(any(InetAddress.class))).thenThrow(new GeoIp2Exception("GeoIP failure"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void createGreenbidsInvocationResultShouldReturnUpdateBidRequestWhenNotEx
.banner(banner)
.build();
final Device device = givenDevice(identity());
final BidRequest bidRequest = givenBidRequest(request -> request, List.of(imp), device, null);
final BidRequest bidRequest = givenBidRequest(request -> request, List.of(imp), device);
final Map<String, Map<String, Boolean>> impsBiddersFilterMap = givenImpsBiddersFilterMap();
final Partner partner = givenPartner(0.0);

Expand Down Expand Up @@ -82,7 +82,7 @@ public void createGreenbidsInvocationResultShouldReturnNoActionWhenExploration()
.banner(banner)
.build();
final Device device = givenDevice(identity());
final BidRequest bidRequest = givenBidRequest(request -> request, List.of(imp), device, null);
final BidRequest bidRequest = givenBidRequest(request -> request, List.of(imp), device);
final Map<String, Map<String, Boolean>> impsBiddersFilterMap = givenImpsBiddersFilterMap();
final Partner partner = givenPartner(1.0);

Expand Down Expand Up @@ -121,6 +121,6 @@ private Map<String, Map<String, Boolean>> givenImpsBiddersFilterMap() {
}

private Partner givenPartner(Double explorationRate) {
return Partner.of("test-pbuid", 0.60, explorationRate);
return Partner.of(true, "test-pbuid", 0.60, explorationRate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.iab.openrtb.request.Imp;
import com.iab.openrtb.request.Site;
import org.prebid.server.json.ObjectMapperProvider;
import org.prebid.server.proto.openrtb.ext.request.ExtRequest;

import java.util.Collections;
import java.util.List;
Expand All @@ -25,15 +24,13 @@ private TestBidRequestProvider() { }
public static BidRequest givenBidRequest(
UnaryOperator<BidRequest.BidRequestBuilder> bidRequestCustomizer,
List<Imp> imps,
Device device,
ExtRequest extRequest) {
Device device) {

return bidRequestCustomizer.apply(BidRequest.builder()
.id("request")
.imp(imps)
.site(givenSite(site -> site))
.device(device)
.ext(extRequest)).build();
.device(device)).build();
}

public static Site givenSite(UnaryOperator<Site.SiteBuilder> siteCustomizer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@
import org.prebid.server.hooks.v1.auction.AuctionInvocationContext;
import org.prebid.server.hooks.v1.auction.AuctionRequestPayload;
import org.prebid.server.model.HttpRequestContext;
import org.prebid.server.proto.openrtb.ext.request.ExtRequest;
import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebid;
import org.prebid.server.settings.model.Account;
import org.prebid.server.settings.model.AccountHooksConfiguration;

Expand Down Expand Up @@ -144,8 +142,8 @@ public void callShouldFilterBiddersAndFallbackToAccountLevelConfigWhenPartnerNot

final Double explorationRate = 0.0001;
final Device device = givenDevice(identity());
final BidRequest bidRequest = givenBidRequest(request -> request, List.of(imp), device, null);
final AuctionContext auctionContext = givenAuctionContext(bidRequest, context -> context);
final BidRequest bidRequest = givenBidRequest(request -> request, List.of(imp), device);
final AuctionContext auctionContext = givenAuctionContext(bidRequest, context -> context, explorationRate);
final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(auctionContext);
when(invocationContext.auctionContext()).thenReturn(auctionContext);
when(modelCacheWithExpiration.getIfPresent("onnxModelRunner_test-pbuid"))
Expand All @@ -154,7 +152,7 @@ public void callShouldFilterBiddersAndFallbackToAccountLevelConfigWhenPartnerNot
.thenReturn(givenThrottlingThresholds());

final BidRequest expectedBidRequest = expectedUpdatedBidRequest(
request -> request, explorationRate, device, false);
request -> request, device);
final AnalyticsResult expectedAnalyticsResult = expectedAnalyticsResult(false, false);

// when
Expand Down Expand Up @@ -205,9 +203,8 @@ public void callShouldNotFilterBiddersAndReturnAnalyticsTagWhenExploration() thr

final Double explorationRate = 1.0;
final Device device = givenDevice(identity());
final ExtRequest extRequest = givenExtRequest(explorationRate);
final BidRequest bidRequest = givenBidRequest(request -> request, List.of(imp), device, extRequest);
final AuctionContext auctionContext = givenAuctionContext(bidRequest, context -> context);
final BidRequest bidRequest = givenBidRequest(request -> request, List.of(imp), device);
final AuctionContext auctionContext = givenAuctionContext(bidRequest, context -> context, explorationRate);
final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(auctionContext);
when(invocationContext.auctionContext()).thenReturn(auctionContext);
when(modelCacheWithExpiration.getIfPresent("onnxModelRunner_test-pbuid"))
Expand Down Expand Up @@ -259,9 +256,8 @@ public void callShouldFilterBiddersBasedOnModelWhenAnyFeatureNotAvailable() thro

final Double explorationRate = 0.0001;
final Device device = givenDeviceWithoutUserAgent(identity());
final ExtRequest extRequest = givenExtRequest(explorationRate);
final BidRequest bidRequest = givenBidRequest(request -> request, List.of(imp), device, extRequest);
final AuctionContext auctionContext = givenAuctionContext(bidRequest, context -> context);
final BidRequest bidRequest = givenBidRequest(request -> request, List.of(imp), device);
final AuctionContext auctionContext = givenAuctionContext(bidRequest, context -> context, explorationRate);
final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(auctionContext);
when(invocationContext.auctionContext()).thenReturn(auctionContext);
when(modelCacheWithExpiration.getIfPresent("onnxModelRunner_test-pbuid"))
Expand All @@ -270,7 +266,7 @@ public void callShouldFilterBiddersBasedOnModelWhenAnyFeatureNotAvailable() thro
.thenReturn(givenThrottlingThresholds());

final BidRequest expectedBidRequest = expectedUpdatedBidRequest(
request -> request, explorationRate, device, true);
request -> request, device);
final AnalyticsResult expectedAnalyticsResult = expectedAnalyticsResult(false, false);

// when
Expand Down Expand Up @@ -320,9 +316,8 @@ public void callShouldFilterBiddersBasedOnModelResults() throws OrtException, IO

final Double explorationRate = 0.0001;
final Device device = givenDevice(identity());
final ExtRequest extRequest = givenExtRequest(explorationRate);
final BidRequest bidRequest = givenBidRequest(request -> request, List.of(imp), device, extRequest);
final AuctionContext auctionContext = givenAuctionContext(bidRequest, context -> context);
final BidRequest bidRequest = givenBidRequest(request -> request, List.of(imp), device);
final AuctionContext auctionContext = givenAuctionContext(bidRequest, context -> context, explorationRate);
final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(auctionContext);
when(invocationContext.auctionContext()).thenReturn(auctionContext);
when(modelCacheWithExpiration.getIfPresent("onnxModelRunner_test-pbuid"))
Expand All @@ -331,7 +326,7 @@ public void callShouldFilterBiddersBasedOnModelResults() throws OrtException, IO
.thenReturn(givenThrottlingThresholds());

final BidRequest expectedBidRequest = expectedUpdatedBidRequest(
request -> request, explorationRate, device, true);
request -> request, device);
final AnalyticsResult expectedAnalyticsResult = expectedAnalyticsResult(false, false);

// when
Expand Down Expand Up @@ -382,29 +377,15 @@ static DatabaseReader givenDatabaseReader() throws IOException {
return new DatabaseReader.Builder(databasePath.toFile()).build();
}

static ExtRequest givenExtRequest(Double explorationRate) {
final ObjectNode greenbidsNode = TestBidRequestProvider.MAPPER.createObjectNode();
greenbidsNode.put("pbuid", "test-pbuid");
greenbidsNode.put("targetTpr", 0.60);
greenbidsNode.put("explorationRate", explorationRate);

final ObjectNode analyticsNode = TestBidRequestProvider.MAPPER.createObjectNode();
analyticsNode.set("greenbids-rtd", greenbidsNode);

return ExtRequest.of(ExtRequestPrebid
.builder()
.analytics(analyticsNode)
.build());
}

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

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

return auctionContextCustomizer.apply(auctionContextBuilder).build();
}
Expand All @@ -415,18 +396,19 @@ private AuctionInvocationContext givenAuctionInvocationContext(AuctionContext au
return invocationContext;
}

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

private AccountHooksConfiguration givenAccountHooksConfiguration() {
private AccountHooksConfiguration givenAccountHooksConfiguration(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", 0.0001);
greenbidsNode.put("explorationRate", explorationRate);
final Map<String, ObjectNode> modules = Map.of("greenbids", greenbidsNode);
return AccountHooksConfiguration.of(null, modules, null);
}
Expand All @@ -447,9 +429,7 @@ private ThrottlingThresholds givenThrottlingThresholds() throws IOException {

private BidRequest expectedUpdatedBidRequest(
UnaryOperator<BidRequest.BidRequestBuilder> bidRequestCustomizer,
Double explorationRate,
Device device,
Boolean addExtRequest) {
Device device) {

final Banner banner = givenBanner();

Expand All @@ -473,10 +453,6 @@ private BidRequest expectedUpdatedBidRequest(
.site(givenSite(site -> site))
.device(device);

if (addExtRequest) {
bidRequestBuilder.ext(givenExtRequest(explorationRate));
}

return bidRequestCustomizer.apply(bidRequestBuilder).build();
}

Expand Down

0 comments on commit 8534f0c

Please sign in to comment.