|
| 1 | +package org.prebid.server.hooks.modules.greenbids.real.time.data.config; |
| 2 | + |
| 3 | +import com.github.benmanes.caffeine.cache.Cache; |
| 4 | +import com.github.benmanes.caffeine.cache.Caffeine; |
| 5 | +import com.google.cloud.storage.Storage; |
| 6 | +import com.google.cloud.storage.StorageOptions; |
| 7 | +import io.vertx.core.Vertx; |
| 8 | +import org.prebid.server.hooks.modules.greenbids.real.time.data.model.filter.ThrottlingThresholds; |
| 9 | +import org.prebid.server.hooks.modules.greenbids.real.time.data.core.ThrottlingThresholdsFactory; |
| 10 | +import org.prebid.server.hooks.modules.greenbids.real.time.data.core.GreenbidsInferenceDataService; |
| 11 | +import org.prebid.server.hooks.modules.greenbids.real.time.data.core.FilterService; |
| 12 | +import org.prebid.server.hooks.modules.greenbids.real.time.data.core.ModelCache; |
| 13 | +import org.prebid.server.hooks.modules.greenbids.real.time.data.core.OnnxModelRunner; |
| 14 | +import org.prebid.server.hooks.modules.greenbids.real.time.data.core.OnnxModelRunnerFactory; |
| 15 | +import org.prebid.server.hooks.modules.greenbids.real.time.data.core.OnnxModelRunnerWithThresholds; |
| 16 | +import org.prebid.server.hooks.modules.greenbids.real.time.data.core.ThresholdCache; |
| 17 | +import org.prebid.server.hooks.modules.greenbids.real.time.data.core.GreenbidsInvocationService; |
| 18 | +import org.prebid.server.hooks.modules.greenbids.real.time.data.v1.GreenbidsRealTimeDataProcessedAuctionRequestHook; |
| 19 | +import org.prebid.server.json.ObjectMapperProvider; |
| 20 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 21 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 22 | +import org.springframework.context.annotation.Bean; |
| 23 | +import org.springframework.context.annotation.Configuration; |
| 24 | + |
| 25 | +import java.util.List; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | + |
| 28 | +@ConditionalOnProperty(prefix = "hooks." + GreenbidsRealTimeDataModule.CODE, name = "enabled", havingValue = "true") |
| 29 | +@Configuration |
| 30 | +@EnableConfigurationProperties(GreenbidsRealTimeDataProperties.class) |
| 31 | +public class GreenbidsRealTimeDataConfiguration { |
| 32 | + |
| 33 | + @Bean |
| 34 | + DatabaseReaderFactory databaseReaderFactory(GreenbidsRealTimeDataProperties properties, Vertx vertx) { |
| 35 | + return new DatabaseReaderFactory(properties.getGeoLiteCountryPath(), vertx); |
| 36 | + } |
| 37 | + |
| 38 | + @Bean |
| 39 | + GreenbidsInferenceDataService greenbidsInferenceDataService(DatabaseReaderFactory databaseReaderFactory) { |
| 40 | + return new GreenbidsInferenceDataService( |
| 41 | + databaseReaderFactory, ObjectMapperProvider.mapper()); |
| 42 | + } |
| 43 | + |
| 44 | + @Bean |
| 45 | + GreenbidsRealTimeDataModule greenbidsRealTimeDataModule( |
| 46 | + FilterService filterService, |
| 47 | + OnnxModelRunnerWithThresholds onnxModelRunnerWithThresholds, |
| 48 | + GreenbidsInferenceDataService greenbidsInferenceDataService, |
| 49 | + GreenbidsInvocationService greenbidsInvocationService) { |
| 50 | + |
| 51 | + return new GreenbidsRealTimeDataModule(List.of( |
| 52 | + new GreenbidsRealTimeDataProcessedAuctionRequestHook( |
| 53 | + ObjectMapperProvider.mapper(), |
| 54 | + filterService, |
| 55 | + onnxModelRunnerWithThresholds, |
| 56 | + greenbidsInferenceDataService, |
| 57 | + greenbidsInvocationService))); |
| 58 | + } |
| 59 | + |
| 60 | + @Bean |
| 61 | + FilterService filterService() { |
| 62 | + return new FilterService(); |
| 63 | + } |
| 64 | + |
| 65 | + @Bean |
| 66 | + Storage storage(GreenbidsRealTimeDataProperties properties) { |
| 67 | + return StorageOptions.newBuilder() |
| 68 | + .setProjectId(properties.getGoogleCloudGreenbidsProject()).build().getService(); |
| 69 | + } |
| 70 | + |
| 71 | + @Bean |
| 72 | + OnnxModelRunnerFactory onnxModelRunnerFactory() { |
| 73 | + return new OnnxModelRunnerFactory(); |
| 74 | + } |
| 75 | + |
| 76 | + @Bean |
| 77 | + ThrottlingThresholdsFactory throttlingThresholdsFactory() { |
| 78 | + return new ThrottlingThresholdsFactory(); |
| 79 | + } |
| 80 | + |
| 81 | + @Bean |
| 82 | + ModelCache modelCache( |
| 83 | + GreenbidsRealTimeDataProperties properties, |
| 84 | + Vertx vertx, |
| 85 | + Storage storage, |
| 86 | + OnnxModelRunnerFactory onnxModelRunnerFactory) { |
| 87 | + |
| 88 | + final Cache<String, OnnxModelRunner> modelCacheWithExpiration = Caffeine.newBuilder() |
| 89 | + .expireAfterWrite(properties.getCacheExpirationMinutes(), TimeUnit.MINUTES) |
| 90 | + .build(); |
| 91 | + |
| 92 | + return new ModelCache( |
| 93 | + storage, |
| 94 | + properties.getGcsBucketName(), |
| 95 | + modelCacheWithExpiration, |
| 96 | + properties.getOnnxModelCacheKeyPrefix(), |
| 97 | + vertx, |
| 98 | + onnxModelRunnerFactory); |
| 99 | + } |
| 100 | + |
| 101 | + @Bean |
| 102 | + ThresholdCache thresholdCache( |
| 103 | + GreenbidsRealTimeDataProperties properties, |
| 104 | + Vertx vertx, |
| 105 | + Storage storage, |
| 106 | + ThrottlingThresholdsFactory throttlingThresholdsFactory) { |
| 107 | + |
| 108 | + final Cache<String, ThrottlingThresholds> thresholdsCacheWithExpiration = Caffeine.newBuilder() |
| 109 | + .expireAfterWrite(properties.getCacheExpirationMinutes(), TimeUnit.MINUTES) |
| 110 | + .build(); |
| 111 | + |
| 112 | + return new ThresholdCache( |
| 113 | + storage, |
| 114 | + properties.getGcsBucketName(), |
| 115 | + ObjectMapperProvider.mapper(), |
| 116 | + thresholdsCacheWithExpiration, |
| 117 | + properties.getThresholdsCacheKeyPrefix(), |
| 118 | + vertx, |
| 119 | + throttlingThresholdsFactory); |
| 120 | + } |
| 121 | + |
| 122 | + @Bean |
| 123 | + OnnxModelRunnerWithThresholds onnxModelRunnerWithThresholds( |
| 124 | + ModelCache modelCache, |
| 125 | + ThresholdCache thresholdCache) { |
| 126 | + |
| 127 | + return new OnnxModelRunnerWithThresholds(modelCache, thresholdCache); |
| 128 | + } |
| 129 | + |
| 130 | + @Bean |
| 131 | + GreenbidsInvocationService greenbidsInvocationService() { |
| 132 | + return new GreenbidsInvocationService(); |
| 133 | + } |
| 134 | +} |
0 commit comments