2020-11-23 04:09:13 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Experimentation
|
|
|
|
class Experiment
|
2021-01-07 19:10:44 -05:00
|
|
|
FEATURE_FLAG_SUFFIX = "_experiment_percentage"
|
|
|
|
|
2021-01-19 13:11:04 -05:00
|
|
|
attr_reader :key, :tracking_category, :use_backwards_compatible_subject_index, :rollout_strategy
|
2020-11-23 04:09:13 -05:00
|
|
|
|
|
|
|
def initialize(key, **params)
|
2020-12-02 19:09:53 -05:00
|
|
|
@key = key
|
2020-11-23 04:09:13 -05:00
|
|
|
@tracking_category = params[:tracking_category]
|
|
|
|
@use_backwards_compatible_subject_index = params[:use_backwards_compatible_subject_index]
|
2021-01-19 13:11:04 -05:00
|
|
|
@rollout_strategy = params[:rollout_strategy] || :cookie
|
2020-11-23 04:09:13 -05:00
|
|
|
end
|
|
|
|
|
2020-12-02 19:09:53 -05:00
|
|
|
def active?
|
2021-01-07 19:10:44 -05:00
|
|
|
# TODO: just touch a feature flag
|
|
|
|
# Temporary change, we will change `experiment_percentage` in future to `Feature.enabled?
|
|
|
|
Feature.enabled?(feature_flag_name, type: :experiment, default_enabled: :yaml)
|
|
|
|
|
2020-11-23 04:09:13 -05:00
|
|
|
::Gitlab.dev_env_or_com? && experiment_percentage > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def enabled_for_index?(index)
|
|
|
|
return false if index.blank?
|
|
|
|
|
|
|
|
index <= experiment_percentage
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-01-07 19:10:44 -05:00
|
|
|
def experiment_percentage
|
|
|
|
feature_flag.percentage_of_time_value
|
|
|
|
end
|
|
|
|
|
|
|
|
def feature_flag
|
|
|
|
Feature.get(feature_flag_name) # rubocop:disable Gitlab/AvoidFeatureGet
|
|
|
|
end
|
|
|
|
|
|
|
|
def feature_flag_name
|
|
|
|
:"#{key}#{FEATURE_FLAG_SUFFIX}"
|
|
|
|
end
|
2020-11-23 04:09:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|