Skip to content

Commit

Permalink
Release 0.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
weiqiangliu committed Sep 27, 2022
1 parent 3552e61 commit fa3846f
Show file tree
Hide file tree
Showing 9 changed files with 514 additions and 151 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<groupId>com.sensorsdata.analytics.javasdk</groupId>
<artifactId>SensorsABTesting</artifactId>
<version>0.0.4</version>
<version>0.0.5</version>

<name>SensorsABTesting</name>
<url>http://sensorsdata.cn</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private SensorsABTestConst() {

static final String JAVA = "Java";

static final String VERSION = "0.0.4";
static final String VERSION = "0.0.5";

static final String VERSION_KEY = "abtest_lib_version";

Expand All @@ -39,6 +39,9 @@ private SensorsABTestConst() {
public static final String RESULTS_KEY = "results";
public static final String STATUS_KEY = "status";
public static final String SUCCESS = "SUCCESS";

// 自定义主体 id
public static final String CUSTOM_ID = "custom_id";


}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.google.common.collect.Maps;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.io.IOException;
Expand Down Expand Up @@ -195,16 +196,18 @@ <T> void trackABTestTrigger(Experiment<T> result, Map<String, Object> properties
}
//缓存中存在 A/B 事件
if (eventCacheManager.judgeEventCacheExist(result.getDistinctId(), result.getIsLoginId(),
result.getAbTestExperimentId(), customIds)) {
log.info("The event has been triggered.[distinctId:{},experimentId:{}]",
result.getDistinctId(), result.getAbTestExperimentId());
result.getAbTestExperimentId(), customIds, result.getAbTestExperimentGroupId())) {
log.info("The event has been triggered.[distinctId:{},experimentId:{},abTestExperimentGroupId:{}]",
result.getDistinctId(), result.getAbTestExperimentId(), result.getAbTestExperimentGroupId());
return;
}
if (properties == null) {
properties = Maps.newHashMap();
}
properties.put(SensorsABTestConst.EXPERIMENT_ID, result.getAbTestExperimentId());
properties.put(SensorsABTestConst.EXPERIMENT_GROUP_ID, result.getAbTestExperimentGroupId());
if (null != customIds)
properties.putAll(customIds);
//判断是否为当天首次上传,重启服务,升级 SDK 版本都会触发
String day = DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.format(Calendar.getInstance());
if (trigger == null || trigger.isEmpty() || !day.equals(trigger)) {
Expand All @@ -213,22 +216,22 @@ <T> void trackABTestTrigger(Experiment<T> result, Map<String, Object> properties
versions.add(String.format("%s:%s", SensorsABTestConst.AB_TEST_EVENT_LIB_VERSION, SensorsABTestConst.VERSION));
properties.put(SensorsABTestConst.LIB_PLUGIN_VERSION, versions);
log.debug(
"Meet the conditions:the first event of current day,the first events of server.[distinctId:{},isLoginId:{},experimentId:{}]",
result.getDistinctId(), result.getIsLoginId(), result.getAbTestExperimentId());
"Meet the conditions:the first event of current day,the first events of server.[distinctId:{},isLoginId:{},experimentId:{},abTestExperimentGroupId:{}]",
result.getDistinctId(), result.getIsLoginId(), result.getAbTestExperimentId(), result.getAbTestExperimentGroupId());
}
this.config.getSensorsAnalytics().track(result.getDistinctId(), result.getIsLoginId(),
SensorsABTestConst.EVENT_TYPE, properties);
log.debug("Successfully trigger AB event.[distinctId:{},isLoginId:{},experimentId:{}]",
result.getDistinctId(), result.getIsLoginId(), result.getAbTestExperimentId());
log.debug("Successfully trigger AB event.[distinctId:{},isLoginId:{},experimentId:{},abTestExperimentGroupId:{}]",
result.getDistinctId(), result.getIsLoginId(), result.getAbTestExperimentId(), result.getAbTestExperimentGroupId());
//判断是否需要缓存上报事件
if (config.getEnableEventCache() == null || config.getEnableEventCache()) {
log.debug("Enable event cache,will cache event.[distinctId:{},isLoginId:{},experimentId:{}]",
result.getDistinctId(), result.getIsLoginId(), result.getAbTestExperimentId());
log.debug("Enable event cache,will cache event.[distinctId:{},isLoginId:{},experimentId:{},abTestExperimentGroupId:{}]",
result.getDistinctId(), result.getIsLoginId(), result.getAbTestExperimentId(), result.getAbTestExperimentGroupId());
eventCacheManager.setEventCache(
result.getDistinctId(),
result.getIsLoginId(),
result.getAbTestExperimentId(),
customIds);
customIds, result.getAbTestExperimentGroupId());
}
}

Expand Down Expand Up @@ -258,8 +261,8 @@ private JsonNode getABTestByHttp(String distinctId, boolean isLoginId, String ex
params.put("param_name", experimentName);
}
String strJson = objectMapper.writeValueAsString(params);
log.debug("The parameter for making a network request is {}.", strJson);
String result = httpConsumer.consume(strJson, timeoutMilliseconds);
log.debug("Successfully get the httpConsumer result.[strJson:{},result:{}]", strJson, result);
JsonNode res = objectMapper.readTree(result);
if (res != null && SensorsABTestConst.SUCCESS.equals(res.findValue(SensorsABTestConst.STATUS_KEY).asText())
&& res.findValue(SensorsABTestConst.RESULTS_KEY).size() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import static com.sensorsdata.analytics.javasdk.util.ABTestUtil.map2Str;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
Expand All @@ -26,17 +26,15 @@ public class EventCacheManager {
/**
* 上报事件缓存
*/
private final LoadingCache<String, Object> eventCache;

private static final Object OBJ = new Object();
private final LoadingCache<String, String> eventCache;

public EventCacheManager(int cacheTime, int cacheSize) {
this.eventCache = CacheBuilder.newBuilder()
.expireAfterWrite(cacheTime, TimeUnit.MINUTES)
.maximumSize(cacheSize)
.build(new CacheLoader<String, Object>() {
.build(new CacheLoader<String, String>() {
@Override
public JsonNode load(String s) {
public String load(String s) {
return null;
}
});
Expand All @@ -45,13 +43,18 @@ public JsonNode load(String s) {


public boolean judgeEventCacheExist(String distinctId, Boolean isLoginId, String experimentId,
Map<String, String> customIds) {
return this.eventCache.getIfPresent(generateKey(distinctId, isLoginId, experimentId, customIds))
!= null;
Map<String, String> customIds, String abTestExperimentGroupId) {
if (this.eventCache.getIfPresent(generateKey(distinctId, isLoginId, experimentId, customIds)) == null)
return false;
else
return StringUtils.equals(
this.eventCache.getIfPresent(generateKey(distinctId, isLoginId, experimentId, customIds)),
abTestExperimentGroupId);
}

public void setEventCache(String distinctId, Boolean isLoginId, String experimentId, Map<String, String> customIds) {
this.eventCache.put(generateKey(distinctId, isLoginId, experimentId, customIds), OBJ);
public void setEventCache(String distinctId, Boolean isLoginId, String experimentId, Map<String, String> customIds,
String abTestExperimentGroupId) {
this.eventCache.put(generateKey(distinctId, isLoginId, experimentId, customIds), abTestExperimentGroupId);
}

public long getCacheSize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.*;
Expand Down Expand Up @@ -339,4 +340,39 @@ public void checkAsyncFetchOldEnv() throws InvalidArgumentException {
assertEquals(1, (int) experiment.getResult());
}

/**
* asyncFetchTest 请求,携带一个正确的 customIds
* 期望:properties 字段中存在 custom_id
*/
@Test
@Ignore
public void checkAsyncFetchWithCustomIds()
throws InvalidArgumentException, NoSuchFieldException, IllegalAccessException, IOException {
//初始化 AB Testing SDK
initSASDK();
initInstance(ABGlobalConfig.builder().setApiUrl(url).setSensorsAnalytics(sa).build());
initInnerClassInfo(sensorsABTest);
HashMap<String, String> customIdMap = new HashMap<>();
customIdMap.put("custom_id", "test11");
Experiment<String> result =
sensorsABTest.asyncFetchABTest(SensorsABParams.starter("a123", false, "test_group_id2", "{\"color\":\"grey\"}").customIds(customIdMap).build());
initInnerClassInfo(sensorsABTest);

// 检查试验结果
assertEquals("a123", result.getDistinctId());
assertEquals(false, result.getIsLoginId());
assertEquals("2", result.getAbTestExperimentGroupId());

// 检查事件缓存
assertEquals(1, eventCacheByReflect.size());

if(messageBuffer != null) {
assertNotEquals(0, messageBuffer.length());
JsonNode jsonNode = SensorsAnalyticsUtil.getJsonObjectMapper().readValue(messageBuffer.toString(), JsonNode.class);
assertEquals("\"$ABTestTrigger\"", jsonNode.get("event").toString());
// 验证属性中是否包含 custom_id
assertEquals("\"test11\"", jsonNode.get("properties").get("custom_id").toString());
}
assertNotNull(result.getResult());
}
}
Loading

0 comments on commit fa3846f

Please sign in to comment.