Skip to content

Commit

Permalink
refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgeniiMunin committed Aug 20, 2024
1 parent e934a4b commit 5cac1d2
Show file tree
Hide file tree
Showing 22 changed files with 295 additions and 422 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ private InvocationResult<AllProcessedBidResponsesPayload> toInvocationResult(
.payloadUpdate(payload -> AllProcessedBidResponsesPayloadImpl.of(
Stream.concat(bidderResponsesWithoutIssues.stream(), notScannedBidderResponses.stream()).toList()));

System.out.println("ConfiantAdQualityBidResponsesScanHook/ InvocationResult: " + resultBuilder.build());

return resultBuilder.build();
}

Expand Down
36 changes: 36 additions & 0 deletions extra/modules/greenbids-real-time-data/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,40 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.4.0</version>
<executions>
<execution>
<id>checkstyle</id>
<phase>compile</phase> <!-- Run during the compile phase -->
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- Project rooted path to CheckStyle configuration file. -->
<configLocation>checkstyle.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<!-- true if project build should fails on any style violation. -->
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
</configuration>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>10.17.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class GreenbidsRealTimeDataConfiguration {

@Bean
GreenbidsRealTimeDataModule greenbidsRealTimeDataModule(
@Value("${hooks.modules.greenbids-real-time-data.google-cloud-greenbids-project}") String googleCloudGreenbidsProject,
@Value("${hooks.modules.greenbids-real-time-data.google-cloud-greenbids-project}")
String googleCloudGreenbidsProject,
@Value("${hooks.modules.greenbids-real-time-data.gcs-bucket-name}") String gcsBucketName,
@Value("${hooks.modules.greenbids-real-time-data.cache-expiration-minutes}") Integer cacheExpirationMinutes,
@Value("${hooks.modules.greenbids-real-time-data.geo-lite-country-path}") String geoLiteCountryPath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public Partner(
}

public Double getThresholdForPartner(ThrottlingThresholds throttlingThresholds) {
List<Double> truePositiveRates = throttlingThresholds.getTpr();
List<Double> thresholds = throttlingThresholds.getThresholds();
final List<Double> truePositiveRates = throttlingThresholds.getTpr();
final List<Double> thresholds = throttlingThresholds.getThresholds();

return truePositiveRates.stream()
.filter(truePositiveRate -> truePositiveRate >= targetTpr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

@Value(staticConstructor = "of")
public class GreenbidsRealTimeDataProperties {

@JsonProperty(value = "modelCacheWithExpiration", required = true)
Cache<String, OnnxModelRunner> modelCacheWithExpiration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@
import java.util.Set;

public class GreenbidsUserAgent {

public static final Set<String> PC_OS_FAMILIES = Set.of(
"Windows 95", "Windows 98", "Solaris");

private static final Parser UA_PARSER = new Parser();

private final String userAgentString;

private final UserAgent userAgent;

private final ua_parser.Device device;
private final ua_parser.OS os;

private static final Parser uaParser = new Parser();
private final ua_parser.OS os;

public GreenbidsUserAgent(String userAgentString) {
this.userAgentString = userAgentString;
Client client = uaParser.parse(userAgentString);
final Client client = UA_PARSER.parse(userAgentString);
this.userAgent = client.userAgent;
this.device = client.device;
this.os = client.os;
Expand All @@ -43,10 +47,10 @@ public String getBrowser() {
}

public boolean isPC() {
return userAgentString.contains("Windows NT") ||
PC_OS_FAMILIES.contains(os.family) ||
("Windows".equals(os.family) && "ME".equals(os.major)) ||
("Mac OS X".equals(os.family) && !userAgentString.contains("Silk")) ||
userAgentString.contains("Linux") && userAgentString.contains("X11");
return userAgentString.contains("Windows NT")
|| PC_OS_FAMILIES.contains(os.family)
|| ("Windows".equals(os.family) && "ME".equals(os.major))
|| ("Mac OS X".equals(os.family) && !userAgentString.contains("Silk"))
|| userAgentString.contains("Linux") && userAgentString.contains("X11");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@
import lombok.Getter;
import org.prebid.server.exception.PreBidException;

import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

public class ModelCache {

String gcsBucketName;

String modelPath;

@Getter
Cache<String, OnnxModelRunner> cache;

Storage storage;

ReentrantLock lock;

public ModelCache(
Expand All @@ -34,39 +37,19 @@ public ModelCache(
}

public OnnxModelRunner getModelRunner(String pbuid) {
String cacheKey = "onnxModelRunner_" + pbuid;

OnnxModelRunner cachedOnnxModelRunner = cache.getIfPresent(cacheKey);
System.out.println(
"getModelRunner: \n" +
" cacheKey: " + cacheKey + "\n" +
" cachedOnnxModelRunner: " + cachedOnnxModelRunner + "\n" +
" cache: " + cache
);

for (Map.Entry<String, OnnxModelRunner> entry: cache.asMap().entrySet()) {
System.out.println("\nKey: " + entry.getKey() + ", Value: " + entry.getValue() + "\n");
}
final String cacheKey = "onnxModelRunner_" + pbuid;
final OnnxModelRunner cachedOnnxModelRunner = cache.getIfPresent(cacheKey);

if (cachedOnnxModelRunner != null) {
System.out.println("cachedOnnxModelRunner available");
return cachedOnnxModelRunner;
};
}

boolean locked = lock.tryLock();
final boolean locked = lock.tryLock();
try {
if (locked) {
Blob blob = getBlob();

System.out.println(
"getModelRunner: \n" +
" blob: " + blob + "\n" +
" cache: " + cache
);

final Blob blob = getBlob();
cache.put(cacheKey, loadModelRunner(blob));
} else {
System.out.println("Another thread is updating the cache. Skipping fetching predictor.");
return null;
}
} finally {
Expand All @@ -80,23 +63,16 @@ public OnnxModelRunner getModelRunner(String pbuid) {

private Blob getBlob() {
try {
System.out.println(
"getBlob: \n" +
" storage: " + storage + "\n" +
" gcsBucketName: " + gcsBucketName + "\n" +
" modelPath: " + modelPath + "\n"
);
Bucket bucket = storage.get(gcsBucketName);
final Bucket bucket = storage.get(gcsBucketName);
return bucket.get(modelPath);
} catch (StorageException e) {
System.out.println("Error accessing GCS artefact for model: " + e);
throw new PreBidException("Error accessing GCS artefact for model: ", e);
}
}

private OnnxModelRunner loadModelRunner(Blob blob) {
try {
byte[] onnxModelBytes = blob.getContent();
final byte[] onnxModelBytes = blob.getContent();
return new OnnxModelRunner(onnxModelBytes);
} catch (OrtException e) {
throw new RuntimeException("Failed to load ONNX model", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@
import java.util.Collections;

public class OnnxModelRunner {

private OrtSession session;

private OrtEnvironment environment;

public OnnxModelRunner(byte[] onnxModelBytes) throws OrtException {
environment = OrtEnvironment.getEnvironment();
OrtSession.SessionOptions options = new OrtSession.SessionOptions();
final OrtSession.SessionOptions options = new OrtSession.SessionOptions();
session = environment.createSession(onnxModelBytes, options);
}

public OrtSession.Result runModel(String[][] throttlingInferenceRow) throws OrtException {
OnnxTensor inputTensor = OnnxTensor.createTensor(OrtEnvironment.getEnvironment(), throttlingInferenceRow);
final OnnxTensor inputTensor = OnnxTensor.createTensor(OrtEnvironment.getEnvironment(), throttlingInferenceRow);
return session.run(Collections.singletonMap("input", inputTensor));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.prebid.server.hooks.modules.greenbids.real.time.data.core.ThrottlingThresholds;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

public class ThresholdCache {
Expand Down Expand Up @@ -39,39 +38,19 @@ public ThresholdCache(
}

public ThrottlingThresholds getThrottlingThresholds(String pbuid) {
String cacheKey = "throttlingThresholds_" + pbuid;

ThrottlingThresholds cachedThrottlingThresholds = cache.getIfPresent(cacheKey);
System.out.println(
"getThrottlingThresholds: \n" +
" cacheKey: " + cacheKey + "\n" +
" cachedThrottlingThresholds: " + cachedThrottlingThresholds + "\n" +
" cache: " + cache
);

for (Map.Entry<String, ThrottlingThresholds> entry: cache.asMap().entrySet()) {
System.out.println("\nKey: " + entry.getKey() + ", Value: " + entry.getValue() + "\n");
}
final String cacheKey = "throttlingThresholds_" + pbuid;
final ThrottlingThresholds cachedThrottlingThresholds = cache.getIfPresent(cacheKey);

if (cachedThrottlingThresholds != null) {
System.out.println("cachedThrottlingThresholds available");
return cachedThrottlingThresholds;
};
}

boolean locked = lock.tryLock();
final boolean locked = lock.tryLock();
try {
if (locked) {
Blob blob = getBlob();

System.out.println(
"getThrottlingThresholds: \n" +
"read blob: " + blob + "\n" +
"put in cache: " + cache
);

final Blob blob = getBlob();
cache.put(cacheKey, loadThrottlingThresholds(blob));
} else {
System.out.println("Another thread is updating the cache. Skipping fetching predictor.");
return null;
}
} finally {
Expand All @@ -85,26 +64,18 @@ public ThrottlingThresholds getThrottlingThresholds(String pbuid) {

private Blob getBlob() {
try {
System.out.println(
"getBlob: \n" +
"storage: " + storage + "\n" +
"gcsBucketName: " + gcsBucketName + "\n" +
"thresholdPath: " + thresholdPath + "\n"
);
return storage.get(gcsBucketName).get(thresholdPath);
} catch (StorageException e) {
System.out.println("Error accessing GCS artefact for threshold: " + e);
throw new PreBidException("Error accessing GCS artefact for threshold: ", e);
}
}

private ThrottlingThresholds loadThrottlingThresholds(Blob blob) {
JsonNode thresholdsJsonNode;
final JsonNode thresholdsJsonNode;
try {
byte[] jsonBytes = blob.getContent();
final byte[] jsonBytes = blob.getContent();
thresholdsJsonNode = mapper.readTree(jsonBytes);
ThrottlingThresholds throttlingThresholds = mapper.treeToValue(thresholdsJsonNode, ThrottlingThresholds.class);
return throttlingThresholds;
return mapper.treeToValue(thresholdsJsonNode, ThrottlingThresholds.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;

public class GreenbidsRealTimeDataModule implements Module {

public static final String CODE = "greenbids-real-time-data";

private final List<? extends Hook<?, ? extends InvocationContext>> hooks;
Expand Down
Loading

0 comments on commit 5cac1d2

Please sign in to comment.