Skip to content

Commit

Permalink
fix review8: refactos
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgeniiMunin committed Oct 3, 2024
1 parent ce284ff commit 56749dd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
public class GreenbidsRealTimeDataConfiguration {

@Bean
public DatabaseReader dbReader(GreenbidsRealTimeDataProperties properties) {
DatabaseReader dbReader(GreenbidsRealTimeDataProperties properties) {
final File database = new File(properties.getGeoLiteCountryPath());
try {
return new DatabaseReader.Builder(database).build();
Expand All @@ -45,7 +45,7 @@ public DatabaseReader dbReader(GreenbidsRealTimeDataProperties properties) {
}

@Bean
public GreenbidsInferenceDataService greenbidsInferenceDataService(DatabaseReader dbReader) {
GreenbidsInferenceDataService greenbidsInferenceDataService(DatabaseReader dbReader) {
return new GreenbidsInferenceDataService(dbReader, ObjectMapperProvider.mapper());
}

Expand All @@ -66,18 +66,18 @@ GreenbidsRealTimeDataModule greenbidsRealTimeDataModule(
}

@Bean
public FilterService filterService() {
FilterService filterService() {
return new FilterService();
}

@Bean
public Storage storage(GreenbidsRealTimeDataProperties properties) {
Storage storage(GreenbidsRealTimeDataProperties properties) {
return StorageOptions.newBuilder()
.setProjectId(properties.getGoogleCloudGreenbidsProject()).build().getService();
}

@Bean
public ModelCache modelCache(GreenbidsRealTimeDataProperties properties, Vertx vertx, Storage storage) {
ModelCache modelCache(GreenbidsRealTimeDataProperties properties, Vertx vertx, Storage storage) {
final Cache<String, OnnxModelRunner> modelCacheWithExpiration = Caffeine.newBuilder()
.expireAfterWrite(properties.getCacheExpirationMinutes(), TimeUnit.MINUTES)
.build();
Expand All @@ -91,7 +91,7 @@ public ModelCache modelCache(GreenbidsRealTimeDataProperties properties, Vertx v
}

@Bean
public ThresholdCache thresholdCache(
ThresholdCache thresholdCache(
GreenbidsRealTimeDataProperties properties,
Vertx vertx,
Storage storage) {
Expand All @@ -110,15 +110,15 @@ public ThresholdCache thresholdCache(
}

@Bean
public OnnxModelRunnerWithThresholds onnxModelRunnerWithThresholds(
OnnxModelRunnerWithThresholds onnxModelRunnerWithThresholds(
ModelCache modelCache,
ThresholdCache thresholdCache) {

return new OnnxModelRunnerWithThresholds(modelCache, thresholdCache);
}

@Bean
public GreenbidsInvocationService greenbidsInvocationService() {
GreenbidsInvocationService greenbidsInvocationService() {
return new GreenbidsInvocationService();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.data.ThrottlingMessage;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -54,33 +55,31 @@ private Map<String, Map<String, Boolean>> processModelResults(
validateThrottlingMessages(throttlingMessages);

return StreamSupport.stream(results.spliterator(), false)
.filter(onnxItem -> {
validateOnnxTensor(onnxItem);
return Objects.equals(onnxItem.getKey(), "probabilities");
})
.map(onnxItem -> (OnnxTensor) onnxItem.getValue())
.map(tensor -> {
validateTensorSize(tensor, throttlingMessages.size());
return extractAndProcessProbabilities(tensor, throttlingMessages, threshold);
})
.flatMap(map -> map.entrySet().stream())
.peek(FilterService::validateOnnxTensor)
.filter(onnxItem -> Objects.equals(onnxItem.getKey(), "probabilities"))
.map(Map.Entry::getValue)
.map(OnnxTensor.class::cast)
.peek(tensor -> validateTensorSize(tensor, throttlingMessages.size()))
.map(tensor -> extractAndProcessProbabilities(tensor, throttlingMessages, threshold))
.map(Map::entrySet)
.flatMap(Collection::stream)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

private void validateThrottlingMessages(List<ThrottlingMessage> throttlingMessages) {
private static void validateThrottlingMessages(List<ThrottlingMessage> throttlingMessages) {
if (throttlingMessages == null || CollectionUtils.isEmpty(throttlingMessages)) {
throw new PreBidException("throttlingMessages cannot be null or empty");
}
}

private void validateOnnxTensor(Map.Entry<String, OnnxValue> onnxItem) {
private static void validateOnnxTensor(Map.Entry<String, OnnxValue> onnxItem) {
if (!(onnxItem.getValue() instanceof OnnxTensor)) {
throw new PreBidException("Expected OnnxTensor for 'probabilities', but found: "
+ onnxItem.getValue().getClass().getName());
}
}

private void validateTensorSize(OnnxTensor tensor, int expectedSize) {
private static void validateTensorSize(OnnxTensor tensor, int expectedSize) {
final long[] tensorShape = tensor.getInfo().getShape();
if (tensorShape.length == 0 || tensorShape[0] != expectedSize) {
throw new PreBidException("Mismatch between tensor size and throttlingMessages size");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

public class GreenbidsRealTimeDataProcessedAuctionRequestHook implements ProcessedAuctionRequestHook {

private static final String CODE = "greenbids-real-time-data-processed-auction-request-hook";
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";
Expand Down Expand Up @@ -151,7 +151,7 @@ private InvocationResult<AuctionRequestPayload> toInvocationResult(
AnalyticsResult analyticsResult,
InvocationAction action) {

final List<AnalyticsResult> analyticsResults = (analyticsResult != null)
final List<AnalyticsResult> analyticsResults = analyticsResult != null
? Collections.singletonList(analyticsResult)
: Collections.emptyList();

Expand Down

0 comments on commit 56749dd

Please sign in to comment.