Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Urisman authored and Igor Urisman committed Jun 19, 2023
1 parent 4a4c2d7 commit cd0ec4c
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 213 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.idea/
/target/
14 changes: 7 additions & 7 deletions bin/release.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/bin/bash
#
# Build server extension API standard library.
#

#

version=0.10.3
root_dir=$(cd $(dirname $0)/..; pwd)
Expand All @@ -12,10 +11,11 @@ cd $root_dir
# Build and package.
mvn clean package

# Unpackage the JAR and add the /db directory to it.
# This way these scripts to be easily discoverable by server tests.
# Un-package the JAR and add the /db directory to it. /db directory contains DB related SQL and shell
# scripts that are used by tests.
jar -uf target/variant-extapi-standard-${version}.jar $(find db)

# Copy the new buid into the server's distr directory.
# assuming the location of the variant local repo.
cp target/variant-extapi-standard-${version}.jar ../../variant/SERVER/src/universal/ext
# Copy the new build into the server's distribution directory. Assuming the location of the variant
# local repo.
cp target/variant-extapi-standard-${version}.jar ../variant/SERVER/src/universal/ext
cp target/variant-extapi-standard-${version}.jar ../variant/SERVER/lib
26 changes: 17 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,20 @@
<artifactId>logback-classic</artifactId>
<version>1.2.1</version>
</dependency>

<!-- Typesafe Config -->

<!-- Typesafe Config TO BE REMOVED -->
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.2.1</version>
</dependency>

<!-- SnakeYAML parser for schema files -->
<!-- https://mvnrepository.com/artifact/org.yaml/snakeyaml -->
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.2.1</version>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.0</version>
</dependency>

</dependencies>
Expand All @@ -77,11 +85,11 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public class UserQualifyingHook implements LifecycleHook<VariationQualificationL

private final String[] blackList;

public UserQualifyingHook(Config config) {
blackList = config.getList("blackList").stream().map(e -> e.unwrapped()).toArray(String[]::new);
public UserQualifyingHook(String init) {
blackList = new String[0];
//blackList = config.getList("blackList").stream().map(e -> e.unwrapped()).toArray(String[]::new);
}

@Override
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/com/variant/extapi/std/demo/UserTargetingHook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.variant.extapi.std.demo;

import com.variant.server.api.Session;
import com.variant.server.api.lifecycle.LifecycleHook;
import com.variant.server.api.lifecycle.VariationQualificationLifecycleEvent;
import com.variant.server.api.lifecycle.VariationTargetingLifecycleEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Optional;


/**
* Life-cycle hook to disqualify blacklisted users.
* The black list of users is given in the hook's initializer object, e.g. as following:
* <code>
* 'init': {'blackList':['Nikita Krushchev']}
* </code>
*
*/
public class UserTargetingHook implements LifecycleHook<VariationTargetingLifecycleEvent> {

private static final Logger LOG = LoggerFactory.getLogger(UserTargetingHook.class);

private final String[] blackList;

public UserTargetingHook(String init) {
blackList = new String[0];
//blackList = config.getList("blackList").stream().map(e -> e.unwrapped()).toArray(String[]::new);
}

@Override
public Class<VariationTargetingLifecycleEvent> getLifecycleEventClass() {
return VariationTargetingLifecycleEvent.class;
}

@Override
public Optional<VariationTargetingLifecycleEvent.PostResult> post(VariationTargetingLifecycleEvent event) {

Session ssn = event.getSession();
String user = ssn.getAttributes().get("user");

boolean blacklisted = Arrays.stream(blackList).filter(d -> user.equals(d)).findFirst().isPresent();

if (blacklisted)
LOG.info("Disqualified blacklisted user [" + user + "] from variation " + event.getVariation().getName() + "]");

VariationTargetingLifecycleEvent.PostResult result = event.mkPostResult();
// result.setQualified(!blacklisted);
return Optional.of(result);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
import java.nio.file.Paths;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.Optional;

import com.typesafe.config.Config;
import org.yaml.snakeyaml.Yaml;
import com.variant.share.schema.Variation.Experience;
import com.variant.server.api.FlushableTraceEvent;
import com.variant.server.api.TraceEventFlusher;
Expand All @@ -34,25 +33,21 @@
*/
public class TraceEventFlusherCsv implements TraceEventFlusher {

private String fileName = "variant-events.csv";
private BufferedWriter out;

public TraceEventFlusherCsv(Config config) throws Exception {

boolean header = false;

if (config != null) {
header = Optional.ofNullable(config.getBoolean("header")).orElse(header);
fileName = Optional.ofNullable(config.getString("file")).orElse(fileName);
}

out = Files.newBufferedWriter(Paths.get(fileName), CREATE, WRITE, TRUNCATE_EXISTING );
private BufferedWriter out = null;

public TraceEventFlusherCsv(String string) throws Exception {
Map<String, ?> map = new Yaml().load(string);
boolean header = (boolean) map.get("header");
String outFileName = (String) map.get("file");
out = Files.newBufferedWriter(Paths.get(outFileName), CREATE, WRITE, TRUNCATE_EXISTING );

if (header) {
writeLine(new Object[] {"event_id", "event_name", "created_on", "session_id", "attributes", "variation", "experience", "is_control"});
writeLine("event_id", "event_name", "created_on", "session_id", "attributes", "variation", "experience", "is_control");
out.flush();
}

}
public TraceEventFlusherCsv() throws Exception {
this("{header: false, file: /tmp/variant-events.csv}");
}

/**
Expand Down Expand Up @@ -115,5 +110,9 @@ private void writeLine(Object...tokens) throws IOException {
}
out.append(System.lineSeparator());
}


public static void main(String[] args) throws Exception {
var flusher = new TraceEventFlusherCsv("{header: true, outFileName: /tmp/foo.bar}");
System.out.println(flusher);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.variant.extapi.std.flush;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.variant.server.api.FlushableTraceEvent;
import com.variant.server.api.TraceEventFlusher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* An implementation of {@link TraceEventFlusher}, which discards trace events.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package com.variant.extapi.std.flush;

import java.time.format.DateTimeFormatter;
import java.util.Map;

import com.variant.server.api.FlushableTraceEvent;
import com.variant.server.api.TraceEventFlusher;
import com.variant.share.schema.Variation.Experience;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigValue;
import com.typesafe.config.ConfigValueType;
import com.variant.share.error.VariantException;
import com.variant.share.schema.Variation.Experience;
import com.variant.server.api.FlushableTraceEvent;
import com.variant.server.api.TraceEventFlusher;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
* An implementation of {@link TraceEventFlusher}, which appends trace events
Expand All @@ -35,16 +31,10 @@ public class TraceEventFlusherServerLog implements TraceEventFlusher {
private static enum Level {Trace, Debug, Info, Error}
private Level level = Level.Info; // The default.

public TraceEventFlusherServerLog(Config config) {

if (config != null && config.hasPath("level")) {
ConfigValue val = config.getValue("level");
if (val.valueType() == ConfigValueType.STRING) {
level = Level.valueOf((String)val.unwrapped());
}
else {
throw new VariantException("Config property 'level' must be a string, e.g. \"level\":\"info\"");
}
public TraceEventFlusherServerLog(String init) {
if (init != null) {
Map<String, ?> initMap = new Yaml().load(init);
level = Level.valueOf((String) initMap.get("level"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,19 @@
import java.util.Properties;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.variant.server.api.ServerException;
import com.variant.server.api.TraceEventFlusher;

/**
* An implementation of {@link TraceEventFlusher}, which writes trace events to an
* instance of H2 database. The required database schema can be created by the
* {@code create-schema.sql} SQL script, included with Variant server.
* <p>
* Configuration.<br/>You may use the <code>variant.event.flusher.class.init</code> configuration property to pass configuration details to this object.
*
* <ul>
* <li><code>url</code> - specifies the URL to the H2 database.
* <li><code>user</code> - the H2 database user.
* <li><code>password</code> - the H2 database user's password.
* </ul>
* Example:<br/>
* <code>variant.event.flusher.class.init = {"url":"jdbc:h2:mem:variant;MVCC=true;DB_CLOSE_DELAY=-1;","user":"variant","password":"variant"}</code>
*
* @since 0.5
*/
public class TraceEventFlusherH2 extends TraceEventFlusherJdbc {

private Connection conn = null;

public TraceEventFlusherH2(Config config) throws Exception {

String url = config.getString("url");
if (url == null) throw new ServerException("Missing configuration property [url]");


String user = config.getString("user");
if (user == null) throw new ServerException("Missing configuration property [user]");

String password = config.getString("password");
if (password == null) throw new ServerException("Missing configuration property [password]");

Properties props = new Properties();
props.setProperty("user", user);
props.setProperty("password", password);
conn = DriverManager.getConnection(url, props);
}

@Override
public Connection getJdbcConnection() {
return conn;
public TraceEventFlusherH2(String init) throws Exception {
super(init);
}

@Override
protected JdbcVendor getJdbcVendor() {
return JdbcVendor.H2;
}

@Override
public void destroy() throws Exception {
if (conn != null) conn.close();
}
}
Loading

0 comments on commit cd0ec4c

Please sign in to comment.