debian-mirror-gitlab/lib/gitlab/experimentation.rb

122 lines
4 KiB
Ruby
Raw Normal View History

2019-12-21 20:55:43 +05:30
# frozen_string_literal: true
# == Experimentation
#
2020-05-24 23:13:21 +05:30
# Utility module for A/B testing experimental features. Define your experiments in the `EXPERIMENTS` constant.
# Experiment options:
# - tracking_category (optional, used to set the category when tracking an experiment event)
2021-03-11 19:13:27 +05:30
# - rollout_strategy: default is `:cookie` based rollout. We may also set it to `:user` based rollout
2021-02-22 17:27:13 +05:30
#
2020-05-24 23:13:21 +05:30
# The experiment is controlled by a Feature Flag (https://docs.gitlab.com/ee/development/feature_flags/controls.html),
# which is named "#{experiment_key}_experiment_percentage" and *must* be set with a percentage and not be used for other purposes.
#
# To enable the experiment for 10% of the users:
#
# chatops: `/chatops run feature set experiment_key_experiment_percentage 10`
2020-06-23 00:09:42 +05:30
# console: `Feature.enable_percentage_of_time(:experiment_key_experiment_percentage, 10)`
2020-05-24 23:13:21 +05:30
#
# To disable the experiment:
#
# chatops: `/chatops run feature delete experiment_key_experiment_percentage`
2020-06-23 00:09:42 +05:30
# console: `Feature.remove(:experiment_key_experiment_percentage)`
2020-05-24 23:13:21 +05:30
#
# To check the current rollout percentage:
#
# chatops: `/chatops run feature get experiment_key_experiment_percentage`
# console: `Feature.get(:experiment_key_experiment_percentage).percentage_of_time_value`
2019-12-21 20:55:43 +05:30
#
2020-06-23 00:09:42 +05:30
# TODO: see https://gitlab.com/gitlab-org/gitlab/-/issues/217490
2019-12-21 20:55:43 +05:30
module Gitlab
module Experimentation
EXPERIMENTS = {
2021-10-27 15:23:28 +05:30
remove_known_trial_form_fields_welcoming: {
tracking_category: 'Growth::Conversion::Experiment::RemoveKnownTrialFormFieldsWelcoming',
rollout_strategy: :user
2020-10-24 23:57:45 +05:30
},
2021-10-27 15:23:28 +05:30
remove_known_trial_form_fields_noneditable: {
tracking_category: 'Growth::Conversion::Experiment::RemoveKnownTrialFormFieldsNoneditable',
rollout_strategy: :user
2021-02-22 17:27:13 +05:30
},
2021-03-08 18:12:59 +05:30
invite_members_new_dropdown: {
tracking_category: 'Growth::Expansion::Experiment::InviteMembersNewDropdown'
2019-12-21 20:55:43 +05:30
}
}.freeze
class << self
2021-02-22 17:27:13 +05:30
def get_experiment(experiment_key)
return unless EXPERIMENTS.key?(experiment_key)
2019-12-21 20:55:43 +05:30
2021-02-22 17:27:13 +05:30
::Gitlab::Experimentation::Experiment.new(experiment_key, **EXPERIMENTS[experiment_key])
2019-12-26 22:10:19 +05:30
end
2019-12-21 20:55:43 +05:30
2021-02-22 17:27:13 +05:30
def active?(experiment_key)
experiment = get_experiment(experiment_key)
return false unless experiment
2021-01-03 14:25:43 +05:30
2021-02-22 17:27:13 +05:30
experiment.active?
2019-12-21 20:55:43 +05:30
end
2021-02-22 17:27:13 +05:30
def in_experiment_group?(experiment_key, subject:)
return false if subject.blank?
return false unless active?(experiment_key)
2019-12-21 20:55:43 +05:30
2021-03-11 19:13:27 +05:30
log_invalid_rollout(experiment_key, subject)
2021-02-22 17:27:13 +05:30
experiment = get_experiment(experiment_key)
return false unless experiment
2019-12-21 20:55:43 +05:30
2021-02-22 17:27:13 +05:30
experiment.enabled_for_index?(index_for_subject(experiment, subject))
2019-12-21 20:55:43 +05:30
end
2021-03-11 19:13:27 +05:30
def rollout_strategy(experiment_key)
experiment = get_experiment(experiment_key)
return unless experiment
experiment.rollout_strategy
end
def log_invalid_rollout(experiment_key, subject)
return if valid_subject_for_rollout_strategy?(experiment_key, subject)
logger = Gitlab::ExperimentationLogger.build
logger.warn message: 'Subject must conform to the rollout strategy',
experiment_key: experiment_key,
subject: subject.class.to_s,
rollout_strategy: rollout_strategy(experiment_key)
end
def valid_subject_for_rollout_strategy?(experiment_key, subject)
case rollout_strategy(experiment_key)
when :user
subject.is_a?(User)
when :group
subject.is_a?(Group)
when :cookie
subject.nil? || subject.is_a?(String)
else
false
end
end
2021-02-22 17:27:13 +05:30
private
2020-05-24 23:13:21 +05:30
2021-02-22 17:27:13 +05:30
def index_for_subject(experiment, subject)
2021-11-11 11:23:49 +05:30
index = Zlib.crc32("#{experiment.key}#{subject_id(subject)}")
2020-05-24 23:13:21 +05:30
2021-02-22 17:27:13 +05:30
index % 100
end
2019-12-21 20:55:43 +05:30
2021-02-22 17:27:13 +05:30
def subject_id(subject)
if subject.respond_to?(:to_global_id)
subject.to_global_id.to_s
elsif subject.respond_to?(:to_s)
subject.to_s
else
2021-06-08 01:23:25 +05:30
raise ArgumentError, 'Subject must respond to `to_global_id` or `to_s`'
2021-02-22 17:27:13 +05:30
end
2019-12-21 20:55:43 +05:30
end
end
end
end