Skip to content

Commit

Permalink
Release 0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
weiqiangliu committed Dec 8, 2021
1 parent 5294feb commit 883d13d
Show file tree
Hide file tree
Showing 9 changed files with 302 additions and 22 deletions.
15 changes: 14 additions & 1 deletion SensorsABTestSDK/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'signing'
apply plugin: 'maven-publish'

version = "0.1.1"
version = "0.1.2"
android {
compileSdkVersion 29

Expand Down Expand Up @@ -50,6 +50,19 @@ dependencies {
compileOnly 'com.sensorsdata.analytics.android:SensorsAnalyticsSDK:6.0.0'
}

afterEvaluate {
assembleRelease {
doLast {
copy {
def rootPath = getProjectDir().getAbsolutePath()
from(rootPath + '/build/outputs/aar/')
into(rootPath + '/build/outputs/aar/')
rename('SensorsABTestSDK-release.aar', 'SensorsABTestSDK-' + version + '.aar')
}
}
}
}

task sourceJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private <T> void asyncFetchABTestInner(final String paramName, final T defaultVa
addTrackEventTask(new Runnable() {
@Override
public void run() {
requestExperimentWithParams(paramName, defaultValue, properties, timeoutMillSeconds, callBack);
requestExperimentWithParams(paramName, defaultValue, properties, timeoutMillSeconds, callBack, false);
}
});
} catch (Exception e) {
Expand All @@ -204,7 +204,7 @@ public void run() {
}
});
} else {
requestExperimentWithParams(paramName, defaultValue, properties, timeoutMillSeconds, callBack);
requestExperimentWithParams(paramName, defaultValue, properties, timeoutMillSeconds, callBack, true);
}
} catch (Exception e) {
SALog.printStackTrace(e);
Expand All @@ -216,7 +216,7 @@ public void run() {
}
}

private <T> void requestExperimentWithParams(final String paramName, final T defaultValue, Map<String, Object> properties, int timeoutMillSeconds, final OnABTestReceivedData<T> callBack) {
private <T> void requestExperimentWithParams(final String paramName, final T defaultValue, Map<String, Object> properties, int timeoutMillSeconds, final OnABTestReceivedData<T> callBack, boolean mergeRequest) {
try {
if (timeoutMillSeconds > 0) {
SALog.i(TAG, "timeoutMillSeconds minimum value is 1000ms");
Expand All @@ -229,7 +229,7 @@ private <T> void requestExperimentWithParams(final String paramName, final T def
final String distinctId = SensorsDataAPI.sharedInstance().getDistinctId();
final String loginId = SensorsDataAPI.sharedInstance().getLoginId();
final String anonymousId = SensorsDataAPI.sharedInstance().getAnonymousId();
new SensorsABTestApiRequestHelper<T>().requestExperimentByParamName(distinctId, loginId, anonymousId, paramName, defaultValue, properties, timeoutMillSeconds, callBack);
new SensorsABTestApiRequestHelper<T>().requestExperimentByParamName(distinctId, loginId, anonymousId, paramName, defaultValue, properties, timeoutMillSeconds, callBack, mergeRequest);
} catch (Exception e) {
SALog.printStackTrace(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import com.sensorsdata.abtest.entity.Experiment;

import org.json.JSONArray;

import java.util.concurrent.ConcurrentHashMap;

interface IExperimentCacheAPI {
Expand Down Expand Up @@ -60,4 +62,18 @@ interface IExperimentCacheAPI {
* @return 试验实体
*/
<T> T getExperimentVariableValue(String paramName, T defaultValue);

/**
* 内存缓存不确定试验状态的试验
*
* @param fuzzyExperiments 不确定试验状态的试验
*/
void saveFuzzyExperiments(JSONArray fuzzyExperiments);

/**
* 判断是否是不确定状态的试验
*
* @return 是否是不确定状态的试验
*/
boolean isFuzzyExperiments(String experimentName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Created by luweibin on 2021/11/15.
* Copyright 2015-2021 Sensors Data Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.sensorsdata.abtest.core;

import android.text.TextUtils;

import com.sensorsdata.abtest.OnABTestReceivedData;

import java.util.LinkedHashMap;
import java.util.Map;

public class RequestExperimentTaskRecorder {
private final String mLoginId;
private final String mAnonymousId;
private final String mParamName;
private final Map<String, Object> mProperties;
private final int mTimeoutMillSeconds;
private final Map<OnABTestReceivedData<Object>, Object> mCallbacksAndDefaultValueMap = new LinkedHashMap<>();
private boolean mIsMergedTask = false;

RequestExperimentTaskRecorder(String loginId, String anonymousId, String paramName, Map<String, Object> properties, int timeoutMillSeconds) {
this.mLoginId = loginId;
this.mAnonymousId = anonymousId;
this.mParamName = paramName;
this.mProperties = properties;
this.mTimeoutMillSeconds = timeoutMillSeconds;
}

void putCallbackAndDefaultValue(OnABTestReceivedData<Object> onABTestReceivedData, Object defaultValue) {
if (!this.mCallbacksAndDefaultValueMap.containsKey(onABTestReceivedData)) {
this.mCallbacksAndDefaultValueMap.put(onABTestReceivedData, defaultValue);
}
}

Map<OnABTestReceivedData<Object>, Object> getCallbacksAndDefaultValueMap() {
return mCallbacksAndDefaultValueMap;
}

boolean isSameExperimentTask(String loginId, String anonymousId, String paramName, Map<String, Object> properties, int timeoutMillSeconds) {
return TextUtils.equals(loginId, mLoginId)
&& TextUtils.equals(anonymousId, mAnonymousId)
&& (properties == null || TextUtils.equals(paramName, mParamName))
&& isSameProperties(properties)
&& timeoutMillSeconds == mTimeoutMillSeconds;
}

private boolean isSameProperties(Map<String, Object> properties) {
if (properties == null && mProperties == null) {
return true;
} else if (properties != null && mProperties != null) {
if (properties.size() == mProperties.size()) {
for (Map.Entry<String, Object> entry : mProperties.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (!properties.containsKey(key) || !value.equals(properties.get(key))) {
return false;
}
}
return true;
}
}
return false;
}

void setIsMergedTask(boolean isMergedTask) {
this.mIsMergedTask = isMergedTask;
}

boolean isMergedTask() {
return mIsMergedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Created by luweibin on 2021/11/15.
* Copyright 2015-2021 Sensors Data Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.sensorsdata.abtest.core;

import com.sensorsdata.abtest.OnABTestReceivedData;
import com.sensorsdata.analytics.android.sdk.SALog;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class RequestExperimentTaskRecorderManager {
private static final String TAG = "SAB.RequestExperimentTaskManager";
private final List<RequestExperimentTaskRecorder> mTaskList = new LinkedList<>();

private static class SingleHolder {
private static final RequestExperimentTaskRecorderManager INSTANCE = new RequestExperimentTaskRecorderManager();
}

private RequestExperimentTaskRecorderManager() {
}

static RequestExperimentTaskRecorderManager getInstance() {
return SingleHolder.INSTANCE;
}

@SuppressWarnings("unchecked")
synchronized <T> RequestExperimentTaskRecorder createRequest(String loginId, String anonymousId, String paramName,
Map<String, Object> properties, int timeoutMillSeconds,
OnABTestReceivedData<T> onABTestReceivedData, T defaultValue) {
SALog.i(TAG, "create new request task");
RequestExperimentTaskRecorder currentTask = new RequestExperimentTaskRecorder(loginId, anonymousId, paramName, properties, timeoutMillSeconds);
mTaskList.add(currentTask);
currentTask.putCallbackAndDefaultValue((OnABTestReceivedData<Object>) onABTestReceivedData, defaultValue);
currentTask.setIsMergedTask(false);
return currentTask;
}

@SuppressWarnings("unchecked")
synchronized <T> RequestExperimentTaskRecorder mergeRequest(String loginId, String anonymousId, String paramName,
Map<String, Object> properties, int timeoutMillSeconds,
OnABTestReceivedData<?> onABTestReceivedData, T defaultValue) {
boolean isTaskExist = false;
RequestExperimentTaskRecorder currentTask = null;
for (RequestExperimentTaskRecorder task : mTaskList) {
if (task.isSameExperimentTask(loginId, anonymousId, paramName, properties, timeoutMillSeconds)) {
currentTask = task;
isTaskExist = true;
break;
}
}
if (currentTask == null) {
currentTask = new RequestExperimentTaskRecorder(loginId, anonymousId, paramName, properties, timeoutMillSeconds);
mTaskList.add(currentTask);
isTaskExist = false;
}
SALog.i(TAG, "create new request task if not exist, task is exist = " + isTaskExist);
currentTask.putCallbackAndDefaultValue((OnABTestReceivedData<Object>) onABTestReceivedData, defaultValue);
currentTask.setIsMergedTask(isTaskExist);
return currentTask;
}

synchronized void removeTask(RequestExperimentTaskRecorder task) {
mTaskList.remove(task);
}
}
Loading

0 comments on commit 883d13d

Please sign in to comment.