All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.groupbyinc.common.config.featureflags.ABTestFeatureFlag Maven / Gradle / Ivy

package com.groupbyinc.common.config.featureflags;

import com.groupbyinc.experimentapi.bucketer.BucketConfiguration;
import com.groupbyinc.experimentapi.bucketer.Bucketer;
import com.groupbyinc.experimentapi.bucketer.ConfigurationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ABTestFeatureFlag
 *
 * @author groupby
 */
public abstract class ABTestFeatureFlag extends CustomerFeatureFlag {

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

  private static final int BUCKET_ID = 1;
  private static final int DEFAULT_BUCKET_PERCENTAGE = 50;
  private static final int DEFAULT_TRAFFIC_ALLOCATION = 100;
  private static final int DEFAULT_TRAFFIC_ALLOCATION_OFFSET = 0;

  protected static boolean splitById(String id) {
    return splitById(DEFAULT_BUCKET_PERCENTAGE, id);
  }

  protected static boolean splitById(int percentage, String id) {
    return splitById(percentage, id, DEFAULT_TRAFFIC_ALLOCATION_OFFSET);
  };

  protected static boolean splitById(int percentage, String id, int trafficAllocationOffset) {
    int[] percentages = new int[]{100 - percentage, percentage};

    try {
      Bucketer bucketer = new Bucketer(new BucketConfiguration(percentages, DEFAULT_TRAFFIC_ALLOCATION, trafficAllocationOffset));
      int bucketId = bucketer.getBucketId(id);
      return bucketId == BUCKET_ID;
    } catch (ConfigurationException e) {
      LOG.error("Error in bucketing for AB feature flag. Setting to false. Error: " + e.getMessage());
      return false;
    }
  }

  protected static boolean matchesAndSplitById(String sessionId, String customerId, String... customerIds) {
    return matchesAndSplitById(DEFAULT_BUCKET_PERCENTAGE, sessionId, customerId, customerIds);
  }

  protected static boolean matchesAndSplitById(int percentage, String sessionId, String customerId, String... customerIds) {
    return matches(customerId, customerIds) && splitById(percentage, sessionId);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy