Skip to content

Commit

Permalink
fix review3: refactos
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgeniiMunin committed Sep 6, 2024
1 parent 3545bae commit 54722b7
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
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")
List<Double> thresholds;

@JsonProperty("tpr")
List<Double> tpr;

@JsonProperty("fpr")
List<Double> fpr;

@JsonProperty("version")
String version;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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())
Expand All @@ -45,7 +44,7 @@ public static GreenbidsInferenceData of(
final List<ThrottlingMessage> throttlingMessages = extractThrottlingMessages(
bidRequest,
userAgent,
database,
dbReader,
mapper);

return of(throttlingMessages);
Expand All @@ -54,7 +53,7 @@ public static GreenbidsInferenceData of(
private static List<ThrottlingMessage> extractThrottlingMessages(
BidRequest bidRequest,
GreenbidsUserAgent greenbidsUserAgent,
File database,
DatabaseReader dbReader,
ObjectMapper mapper) {

final ZonedDateTime timestamp = ZonedDateTime.now(ZoneId.of("UTC"));
Expand All @@ -72,7 +71,7 @@ private static List<ThrottlingMessage> extractThrottlingMessages(
hostname,
hourBucket,
minuteQuadrant,
database,
dbReader,
mapper))
.collect(Collectors.toList());
}
Expand All @@ -84,7 +83,7 @@ private static Stream<ThrottlingMessage> extractMessagesForImp(
String hostname,
Integer hourBucket,
Integer minuteQuadrant,
File database,
DatabaseReader dbReader,
ObjectMapper mapper) {

final String impId = imp.getId();
Expand All @@ -93,7 +92,7 @@ private static Stream<ThrottlingMessage> 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,
Expand All @@ -104,9 +103,9 @@ private static Stream<ThrottlingMessage> 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);
}
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,22 @@ private float[][] extractProbabilitiesValues(OnnxTensor tensor) throws OrtExcept
}

private Map<String, Map<String, Boolean>> processProbabilities(
float[][] probabiliies,
float[][] probabilities,
List<ThrottlingMessage> throttlingMessages,
Double threshold) {
final Map<String, Map<String, Boolean>> 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<String, Boolean> bidderToIsKeptInAuction = new HashMap<>();
bidderToIsKeptInAuction.put(entry.getValue().getKey(), entry.getValue().getValue());
return bidderToIsKeptInAuction;
},
(existingMap, newMap) -> {
existingMap.putAll(newMap);
return existingMap;
}));
}

private Map.Entry<String, Map.Entry<String, Boolean>> createEntry(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -121,7 +121,7 @@ private Future<InvocationResult<AuctionRequestPayload>> toInvocationResult(
final Map<String, Map<String, Boolean>> impsBiddersFilterMap;
try {
final GreenbidsInferenceData greenbidsInferenceData = GreenbidsInferenceData
.of(bidRequest, database, mapper);
.of(bidRequest, dbReader, mapper);

impsBiddersFilterMap = filterService.filterBidders(
onnxModelRunner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,7 +69,7 @@ public class GreenbidsRealTimeDataProcessedAuctionRequestHookTest {
private Cache<String, ThrottlingThresholds> thresholdsCacheWithExpiration;

@BeforeEach
public void setUp() {
public void setUp() throws IOException {
final ObjectMapper mapper = new ObjectMapper();
jacksonMapper = new JacksonMapper(mapper);
modelCacheWithExpiration = Caffeine.newBuilder()
Expand All @@ -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,
Expand All @@ -91,7 +93,7 @@ public void setUp() {
Vertx.vertx());
target = new GreenbidsRealTimeDataProcessedAuctionRequestHook(
mapper,
database,
dbReader,
filterService,
onnxModelRunnerWithThresholds);
}
Expand Down

0 comments on commit 54722b7

Please sign in to comment.