Added ability to specifiy different probability distributions for new and existing users

pull/14/head
Sam Bosley 13 years ago
parent c26a160855
commit 69cbd31148

@ -57,7 +57,7 @@ public class ABChooser {
int chosen = NO_OPTION; int chosen = NO_OPTION;
if (abTests.isValidTestKey(testKey)) { if (abTests.isValidTestKey(testKey)) {
int[] optionProbs = abTests.getProbsForTestKey(testKey); int[] optionProbs = abTests.getProbsForTestKey(testKey, newUser);
String[] optionDescriptions = abTests.getDescriptionsForTestKey(testKey); String[] optionDescriptions = abTests.getDescriptionsForTestKey(testKey);
chosen = chooseOption(optionProbs); chosen = chooseOption(optionProbs);
setChoiceForTest(testKey, chosen); setChoiceForTest(testKey, chosen);

@ -20,34 +20,18 @@ public class ABTests {
* @param key * @param key
* @return * @return
*/ */
public synchronized int[] getProbsForTestKey(String key) { public synchronized int[] getProbsForTestKey(String key, boolean newUser) {
if (bundles.containsKey(key)) { if (bundles.containsKey(key)) {
ABTestBundle bundle = bundles.get(key); ABTestBundle bundle = bundles.get(key);
return bundle.weightedProbs; if (newUser)
return bundle.newUserProbs;
else
return bundle.existingUserProbs;
} else { } else {
return null; return null;
} }
} }
/**
* Updates the weighted probability array for a given key. Returns true
* on success, false if they key doesn't exist or if the array is the wrong
* length.
* @param key
* @param newProbs
* @return
*/
public synchronized boolean setProbsForTestKey(String key, int[] newProbs) {
if (bundles.containsKey(newProbs)) {
ABTestBundle bundle = bundles.get(key);
if (bundle.descriptions == null || newProbs.length == bundle.descriptions.length) {
bundle.weightedProbs = newProbs;
return true;
}
}
return false;
}
/** /**
* Gets the string array of option descriptions for an option key * Gets the string array of option descriptions for an option key
* @param key * @param key
@ -88,11 +72,13 @@ public class ABTests {
private final HashMap<String, ABTestBundle> bundles; private final HashMap<String, ABTestBundle> bundles;
private static class ABTestBundle { private static class ABTestBundle {
public int[] weightedProbs; protected final int[] newUserProbs;
public String[] descriptions; protected final int[] existingUserProbs;
protected final String[] descriptions;
public ABTestBundle(int[] weightedProbs, String[] descriptions) { protected ABTestBundle(int[] newUserProbs, int[] existingUserProbs, String[] descriptions) {
this.weightedProbs = weightedProbs; this.newUserProbs = newUserProbs;
this.existingUserProbs = existingUserProbs;
this.descriptions = descriptions; this.descriptions = descriptions;
} }
} }
@ -124,23 +110,27 @@ public class ABTests {
* tagged from StatisticsService, they will be appended with attributes * tagged from StatisticsService, they will be appended with attributes
* that have that event in this array * that have that event in this array
*/ */
public void addTest(String testKey, int[] probs, String[] descriptions) { public void addTest(String testKey, int[] newUserProbs, int[] existingUserProbs, String[] descriptions) {
ABTestBundle bundle = new ABTestBundle(probs, descriptions); ABTestBundle bundle = new ABTestBundle(newUserProbs, existingUserProbs, descriptions);
bundles.put(testKey, bundle); bundles.put(testKey, bundle);
} }
private void initialize() { // Set up private void initialize() { // Set up
//Calls to addTest go here //Calls to addTest go here
addTest(AB_TEST_SWIPE_ENABLED_KEY, AB_TEST_SWIPE_ENABLED_PROBS, AB_TEST_SWIPE_ENABLED_DESC); addTest(AB_TEST_SWIPE_ENABLED_KEY, AB_TEST_SWIPE_ENABLED_PROBS_NEW_USER,
addTest(AB_TEST_CONTACTS_PICKER_ENABLED, AB_TEST_CONTACTS_ENABLED_PROBS, AB_TEST_CONTACTS_ENABLED_DESC); AB_TEST_SWIPE_ENABLED_PROBS_EXISTING_USER, AB_TEST_SWIPE_ENABLED_DESC);
addTest(AB_TEST_CONTACTS_PICKER_ENABLED, AB_TEST_CONTACTS_ENABLED_PROBS_NEW_USER,
AB_TEST_CONTACTS_ENABLED_PROBS_EXISTING_USER, AB_TEST_CONTACTS_ENABLED_DESC);
} }
public static final String AB_TEST_SWIPE_ENABLED_KEY = "swipeEnabled"; //$NON-NLS-1$ public static final String AB_TEST_SWIPE_ENABLED_KEY = "swipeEnabled"; //$NON-NLS-1$
private static final int[] AB_TEST_SWIPE_ENABLED_PROBS = { 1, 1 }; private static final int[] AB_TEST_SWIPE_ENABLED_PROBS_NEW_USER = { 1, 1 };
private static final int[] AB_TEST_SWIPE_ENABLED_PROBS_EXISTING_USER = { 1, 1 };
private static final String[] AB_TEST_SWIPE_ENABLED_DESC = { "swipe-lists-disabled", "swipe-lists-enabled" }; //$NON-NLS-1$//$NON-NLS-2$ private static final String[] AB_TEST_SWIPE_ENABLED_DESC = { "swipe-lists-disabled", "swipe-lists-enabled" }; //$NON-NLS-1$//$NON-NLS-2$
public static final String AB_TEST_CONTACTS_PICKER_ENABLED = "contactsEnabled"; //$NON-NLS-1$ public static final String AB_TEST_CONTACTS_PICKER_ENABLED = "contactsEnabled"; //$NON-NLS-1$
private static final int[] AB_TEST_CONTACTS_ENABLED_PROBS = { 1, 1 }; private static final int[] AB_TEST_CONTACTS_ENABLED_PROBS_NEW_USER = { 1, 1 };
private static final int[] AB_TEST_CONTACTS_ENABLED_PROBS_EXISTING_USER = { 1, 1 };
private static final String[] AB_TEST_CONTACTS_ENABLED_DESC = { "contacts-disabled", "contacts-enabled" }; //$NON-NLS-1$//$NON-NLS-2$ private static final String[] AB_TEST_CONTACTS_ENABLED_DESC = { "contacts-disabled", "contacts-enabled" }; //$NON-NLS-1$//$NON-NLS-2$

@ -53,15 +53,6 @@ public class FeatureFlipper {
if (settings.has(KEY_SET_OPTION)) { if (settings.has(KEY_SET_OPTION)) {
int option = settings.getInt(KEY_SET_OPTION); int option = settings.getInt(KEY_SET_OPTION);
abChooser.setChoiceForTest(key, option); abChooser.setChoiceForTest(key, option);
} else if (settings.has(KEY_PROBABILITIES)) {
JSONArray newProbabilities = settings.getJSONArray(KEY_PROBABILITIES);
int[] probs = new int[newProbabilities.length()];
for (int j = 0; j < newProbabilities.length(); j++) {
probs[j] = newProbabilities.getInt(j);
}
abTests.setProbsForTestKey(key, probs);
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

Loading…
Cancel
Save