From 54722b770ae3f324a670554cedd314ba43763522 Mon Sep 17 00:00:00 2001 From: EvgeniiMunin Date: Fri, 6 Sep 2024 16:35:18 +0200 Subject: [PATCH] fix review3: refactos --- .../GreenbidsRealTimeDataConfiguration.java | 21 ++++++++++++------ .../time/data/core/ThrottlingThresholds.java | 8 ++----- .../model/data/GreenbidsInferenceData.java | 20 ++++++++--------- .../data/model/predictor/FilterService.java | 22 ++++++++++++------- ...alTimeDataProcessedAuctionRequestHook.java | 10 ++++----- ...meDataProcessedAuctionRequestHookTest.java | 6 +++-- 6 files changed, 48 insertions(+), 39 deletions(-) diff --git a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataConfiguration.java b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataConfiguration.java index b5e4e37570f..49aada8d4f8 100644 --- a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataConfiguration.java +++ b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataConfiguration.java @@ -5,7 +5,9 @@ import com.github.benmanes.caffeine.cache.Caffeine; import com.google.cloud.storage.Storage; import com.google.cloud.storage.StorageOptions; +import com.maxmind.geoip2.DatabaseReader; import io.vertx.core.Vertx; +import org.prebid.server.exception.PreBidException; import org.prebid.server.hooks.modules.greenbids.real.time.data.core.ThrottlingThresholds; import org.prebid.server.hooks.modules.greenbids.real.time.data.model.config.GreenbidsRealTimeDataProperties; import org.prebid.server.hooks.modules.greenbids.real.time.data.model.predictor.FilterService; @@ -20,6 +22,7 @@ import org.springframework.context.annotation.Configuration; import java.io.File; +import java.io.IOException; import java.util.List; import java.util.concurrent.TimeUnit; @@ -37,13 +40,17 @@ GreenbidsRealTimeDataModule greenbidsRealTimeDataModule( final ObjectMapper mapper = ObjectMapperProvider.mapper(); final File database = new File(properties.getGeoLiteCountryPath()); - - return new GreenbidsRealTimeDataModule(List.of( - new GreenbidsRealTimeDataProcessedAuctionRequestHook( - mapper, - database, - filterService, - onnxModelRunnerWithThresholds))); + try { + final DatabaseReader dbReader = new DatabaseReader.Builder(database).build(); + return new GreenbidsRealTimeDataModule(List.of( + new GreenbidsRealTimeDataProcessedAuctionRequestHook( + mapper, + dbReader, + filterService, + onnxModelRunnerWithThresholds))); + } catch (IOException e) { + throw new PreBidException("Failed build DatabaseReader from DB", e); + } } @Bean diff --git a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/core/ThrottlingThresholds.java b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/core/ThrottlingThresholds.java index ee8cd6bca16..8a68a68f2df 100644 --- a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/core/ThrottlingThresholds.java +++ b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/core/ThrottlingThresholds.java @@ -1,11 +1,13 @@ package org.prebid.server.hooks.modules.greenbids.real.time.data.core; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Value; import java.util.List; @Value(staticConstructor = "of") +@JsonIgnoreProperties(ignoreUnknown = true) public class ThrottlingThresholds { @JsonProperty("thresholds") @@ -13,10 +15,4 @@ public class ThrottlingThresholds { @JsonProperty("tpr") List tpr; - - @JsonProperty("fpr") - List fpr; - - @JsonProperty("version") - String version; } diff --git a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/model/data/GreenbidsInferenceData.java b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/model/data/GreenbidsInferenceData.java index a31eb34eb79..578c1dfb178 100644 --- a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/model/data/GreenbidsInferenceData.java +++ b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/model/data/GreenbidsInferenceData.java @@ -15,7 +15,6 @@ import org.prebid.server.exception.PreBidException; import org.prebid.server.proto.openrtb.ext.request.ExtImpPrebid; -import java.io.File; import java.io.IOException; import java.net.InetAddress; import java.time.ZoneId; @@ -34,7 +33,7 @@ public class GreenbidsInferenceData { public static GreenbidsInferenceData of( BidRequest bidRequest, - File database, + DatabaseReader dbReader, ObjectMapper mapper) { final GreenbidsUserAgent userAgent = Optional.ofNullable(bidRequest.getDevice()) @@ -45,7 +44,7 @@ public static GreenbidsInferenceData of( final List throttlingMessages = extractThrottlingMessages( bidRequest, userAgent, - database, + dbReader, mapper); return of(throttlingMessages); @@ -54,7 +53,7 @@ public static GreenbidsInferenceData of( private static List extractThrottlingMessages( BidRequest bidRequest, GreenbidsUserAgent greenbidsUserAgent, - File database, + DatabaseReader dbReader, ObjectMapper mapper) { final ZonedDateTime timestamp = ZonedDateTime.now(ZoneId.of("UTC")); @@ -72,7 +71,7 @@ private static List extractThrottlingMessages( hostname, hourBucket, minuteQuadrant, - database, + dbReader, mapper)) .collect(Collectors.toList()); } @@ -84,7 +83,7 @@ private static Stream extractMessagesForImp( String hostname, Integer hourBucket, Integer minuteQuadrant, - File database, + DatabaseReader dbReader, ObjectMapper mapper) { final String impId = imp.getId(); @@ -93,7 +92,7 @@ private static Stream extractMessagesForImp( final String ip = Optional.ofNullable(bidRequest.getDevice()) .map(Device::getIp) .orElse(null); - final String countryFromIp = getCountrySafely(ip, database); + final String countryFromIp = getCountrySafely(ip, dbReader); return createThrottlingMessages( bidderNode, impId, @@ -104,9 +103,9 @@ private static Stream extractMessagesForImp( minuteQuadrant).stream(); } - private static String getCountrySafely(String ip, File database) { + private static String getCountrySafely(String ip, DatabaseReader dbReader) { try { - return getCountry(ip, database); + return getCountry(ip, dbReader); } catch (IOException | GeoIp2Exception e) { throw new PreBidException("Failed to get country for IP", e); } @@ -172,11 +171,10 @@ private static ExtImpPrebid extImpPrebid(JsonNode extImpPrebid, ObjectMapper map } } - private static String getCountry(String ip, File database) throws IOException, GeoIp2Exception { + private static String getCountry(String ip, DatabaseReader dbReader) throws IOException, GeoIp2Exception { return Optional.ofNullable(ip) .map(ipAddress -> { try { - final DatabaseReader dbReader = new DatabaseReader.Builder(database).build(); final InetAddress inetAddress = InetAddress.getByName(ipAddress); final CountryResponse response = dbReader.country(inetAddress); final Country country = response.getCountry(); diff --git a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/model/predictor/FilterService.java b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/model/predictor/FilterService.java index 1c78d590b59..9dbf141ebd9 100644 --- a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/model/predictor/FilterService.java +++ b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/model/predictor/FilterService.java @@ -75,16 +75,22 @@ private float[][] extractProbabilitiesValues(OnnxTensor tensor) throws OrtExcept } private Map> processProbabilities( - float[][] probabiliies, + float[][] probabilities, List throttlingMessages, Double threshold) { - final Map> impsBiddersFilterMap = new HashMap<>(); - IntStream.range(0, probabiliies.length) - .mapToObj(i -> createEntry(throttlingMessages.get(i), probabiliies[i][1], threshold)) - .forEach(entry -> impsBiddersFilterMap - .computeIfAbsent(entry.getKey(), k -> new HashMap<>()) - .put(entry.getValue().getKey(), entry.getValue().getValue())); - return impsBiddersFilterMap; + return IntStream.range(0, probabilities.length) + .mapToObj(i -> createEntry(throttlingMessages.get(i), probabilities[i][1], threshold)) + .collect(Collectors.toMap( + Map.Entry::getKey, + entry -> { + final Map bidderToIsKeptInAuction = new HashMap<>(); + bidderToIsKeptInAuction.put(entry.getValue().getKey(), entry.getValue().getValue()); + return bidderToIsKeptInAuction; + }, + (existingMap, newMap) -> { + existingMap.putAll(newMap); + return existingMap; + })); } private Map.Entry> createEntry( diff --git a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/v1/GreenbidsRealTimeDataProcessedAuctionRequestHook.java b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/v1/GreenbidsRealTimeDataProcessedAuctionRequestHook.java index 394b6ee3f2f..57704f3698c 100644 --- a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/v1/GreenbidsRealTimeDataProcessedAuctionRequestHook.java +++ b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/v1/GreenbidsRealTimeDataProcessedAuctionRequestHook.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import com.iab.openrtb.request.BidRequest; +import com.maxmind.geoip2.DatabaseReader; import io.vertx.core.Future; import org.apache.commons.collections4.CollectionUtils; import org.prebid.server.auction.model.AuctionContext; @@ -34,7 +35,6 @@ import org.prebid.server.proto.openrtb.ext.request.ExtRequest; import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebid; -import java.io.File; import java.util.Collections; import java.util.List; import java.util.Map; @@ -49,17 +49,17 @@ public class GreenbidsRealTimeDataProcessedAuctionRequestHook implements Process private static final String BID_REQUEST_ANALYTICS_EXTENSION_NAME = "greenbids-rtd"; private final ObjectMapper mapper; - private final File database; + private final DatabaseReader dbReader; private final FilterService filterService; private final OnnxModelRunnerWithThresholds onnxModelRunnerWithThresholds; public GreenbidsRealTimeDataProcessedAuctionRequestHook( ObjectMapper mapper, - File database, + DatabaseReader dbReader, FilterService filterService, OnnxModelRunnerWithThresholds onnxModelRunnerWithThresholds) { this.mapper = Objects.requireNonNull(mapper); - this.database = Objects.requireNonNull(database); + this.dbReader = Objects.requireNonNull(dbReader); this.filterService = Objects.requireNonNull(filterService); this.onnxModelRunnerWithThresholds = Objects.requireNonNull(onnxModelRunnerWithThresholds); } @@ -121,7 +121,7 @@ private Future> toInvocationResult( final Map> impsBiddersFilterMap; try { final GreenbidsInferenceData greenbidsInferenceData = GreenbidsInferenceData - .of(bidRequest, database, mapper); + .of(bidRequest, dbReader, mapper); impsBiddersFilterMap = filterService.filterBidders( onnxModelRunner, diff --git a/extra/modules/greenbids-real-time-data/src/test/java/org/prebid/server/hooks/modules/greenbids/real/time/data/v1/GreenbidsRealTimeDataProcessedAuctionRequestHookTest.java b/extra/modules/greenbids-real-time-data/src/test/java/org/prebid/server/hooks/modules/greenbids/real/time/data/v1/GreenbidsRealTimeDataProcessedAuctionRequestHookTest.java index a4b96a2fbba..5333c22a27b 100644 --- a/extra/modules/greenbids-real-time-data/src/test/java/org/prebid/server/hooks/modules/greenbids/real/time/data/v1/GreenbidsRealTimeDataProcessedAuctionRequestHookTest.java +++ b/extra/modules/greenbids-real-time-data/src/test/java/org/prebid/server/hooks/modules/greenbids/real/time/data/v1/GreenbidsRealTimeDataProcessedAuctionRequestHookTest.java @@ -14,6 +14,7 @@ import com.iab.openrtb.request.Format; import com.iab.openrtb.request.Imp; import com.iab.openrtb.request.Site; +import com.maxmind.geoip2.DatabaseReader; import io.vertx.core.Future; import io.vertx.core.Vertx; import org.junit.jupiter.api.BeforeEach; @@ -68,7 +69,7 @@ public class GreenbidsRealTimeDataProcessedAuctionRequestHookTest { private Cache thresholdsCacheWithExpiration; @BeforeEach - public void setUp() { + public void setUp() throws IOException { final ObjectMapper mapper = new ObjectMapper(); jacksonMapper = new JacksonMapper(mapper); modelCacheWithExpiration = Caffeine.newBuilder() @@ -80,6 +81,7 @@ public void setUp() { final Storage storage = StorageOptions.newBuilder() .setProjectId("test_project").build().getService(); final File database = new File("src/test/resources/GeoLite2-Country.mmdb"); + final DatabaseReader dbReader = new DatabaseReader.Builder(database).build(); final FilterService filterService = new FilterService(); final OnnxModelRunnerWithThresholds onnxModelRunnerWithThresholds = new OnnxModelRunnerWithThresholds( modelCacheWithExpiration, @@ -91,7 +93,7 @@ public void setUp() { Vertx.vertx()); target = new GreenbidsRealTimeDataProcessedAuctionRequestHook( mapper, - database, + dbReader, filterService, onnxModelRunnerWithThresholds); }