Skip to content

Commit 84b3a70

Browse files
authored
Greenbids RTD: Add Module (#3242)
1 parent 7e41aa0 commit 84b3a70

File tree

46 files changed

+3414
-9
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+3414
-9
lines changed

extra/bundle/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@
4545
<artifactId>pb-response-correction</artifactId>
4646
<version>${project.version}</version>
4747
</dependency>
48+
<dependency>
49+
<groupId>org.prebid.server.hooks.modules</groupId>
50+
<artifactId>greenbids-real-time-data</artifactId>
51+
<version>${project.version}</version>
52+
</dependency>
4853
</dependencies>
4954

5055
<build>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>org.prebid.server.hooks.modules</groupId>
8+
<artifactId>all-modules</artifactId>
9+
<version>3.15.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>greenbids-real-time-data</artifactId>
13+
14+
<name>greenbids-real-time-data</name>
15+
<description>Greenbids Real Time Data</description>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.github.ua-parser</groupId>
20+
<artifactId>uap-java</artifactId>
21+
<version>1.6.1</version>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>com.microsoft.onnxruntime</groupId>
26+
<artifactId>onnxruntime</artifactId>
27+
<version>1.16.1</version> <!-- Use the latest available version -->
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>com.google.cloud</groupId>
32+
<artifactId>google-cloud-storage</artifactId>
33+
<version>2.41.0</version>
34+
</dependency>
35+
</dependencies>
36+
37+
</project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lombok.anyConstructor.addConstructorProperties = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.prebid.server.hooks.modules.greenbids.real.time.data.config;
2+
3+
import io.vertx.core.Promise;
4+
import io.vertx.core.Vertx;
5+
import com.maxmind.geoip2.DatabaseReader;
6+
import org.prebid.server.exception.PreBidException;
7+
import org.prebid.server.vertx.Initializable;
8+
9+
import java.io.FileOutputStream;
10+
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.net.URL;
13+
import java.nio.file.Files;
14+
import java.nio.file.Path;
15+
import java.util.concurrent.atomic.AtomicReference;
16+
17+
public class DatabaseReaderFactory implements Initializable {
18+
19+
private final String geoLiteCountryUrl;
20+
21+
private final Vertx vertx;
22+
23+
private final AtomicReference<DatabaseReader> databaseReaderRef = new AtomicReference<>();
24+
25+
public DatabaseReaderFactory(String geoLitCountryUrl, Vertx vertx) {
26+
this.geoLiteCountryUrl = geoLitCountryUrl;
27+
this.vertx = vertx;
28+
}
29+
30+
@Override
31+
public void initialize(Promise<Void> initializePromise) {
32+
33+
vertx.executeBlocking(() -> {
34+
try {
35+
final URL url = new URL(geoLiteCountryUrl);
36+
final Path databasePath = Files.createTempFile("GeoLite2-Country", ".mmdb");
37+
38+
try (InputStream inputStream = url.openStream();
39+
FileOutputStream outputStream = new FileOutputStream(databasePath.toFile())) {
40+
inputStream.transferTo(outputStream);
41+
}
42+
43+
databaseReaderRef.set(new DatabaseReader.Builder(databasePath.toFile()).build());
44+
} catch (IOException e) {
45+
throw new PreBidException("Failed to initialize DatabaseReader from URL", e);
46+
}
47+
return null;
48+
}).<Void>mapEmpty()
49+
.onComplete(initializePromise);
50+
}
51+
52+
public DatabaseReader getDatabaseReader() {
53+
return databaseReaderRef.get();
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.prebid.server.hooks.modules.greenbids.real.time.data.config;
2+
3+
import org.prebid.server.hooks.v1.Hook;
4+
import org.prebid.server.hooks.v1.InvocationContext;
5+
import org.prebid.server.hooks.v1.Module;
6+
7+
import java.util.Collection;
8+
import java.util.List;
9+
10+
public class GreenbidsRealTimeDataModule implements Module {
11+
12+
public static final String CODE = "greenbids-real-time-data";
13+
14+
private final List<? extends Hook<?, ? extends InvocationContext>> hooks;
15+
16+
public GreenbidsRealTimeDataModule(List<? extends Hook<?, ? extends InvocationContext>> hooks) {
17+
this.hooks = hooks;
18+
}
19+
20+
@Override
21+
public String code() {
22+
return CODE;
23+
}
24+
25+
@Override
26+
public Collection<? extends Hook<?, ? extends InvocationContext>> hooks() {
27+
return hooks;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.prebid.server.hooks.modules.greenbids.real.time.data.config;
2+
3+
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
6+
@ConfigurationProperties(prefix = "hooks.modules." + GreenbidsRealTimeDataModule.CODE)
7+
@Data
8+
public class GreenbidsRealTimeDataProperties {
9+
10+
String googleCloudGreenbidsProject;
11+
12+
String geoLiteCountryPath;
13+
14+
String gcsBucketName;
15+
16+
Integer cacheExpirationMinutes;
17+
18+
String onnxModelCacheKeyPrefix;
19+
20+
String thresholdsCacheKeyPrefix;
21+
}

0 commit comments

Comments
 (0)