2020-12-16 04:10:26 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-02-03 10:09:24 -05:00
|
|
|
class ApplicationExperiment < Gitlab::Experiment # rubocop:disable Gitlab/NamespacedClass
|
2021-02-01 07:09:03 -05:00
|
|
|
def enabled?
|
2021-02-03 10:09:24 -05:00
|
|
|
return false if Feature::Definition.get(feature_flag_name).nil? # there has to be a feature flag yaml file
|
2021-02-23 04:10:45 -05:00
|
|
|
return false unless Gitlab.dev_env_or_com? # we have to be in an environment that allows experiments
|
2021-02-01 07:09:03 -05:00
|
|
|
|
2021-02-23 04:10:45 -05:00
|
|
|
# the feature flag has to be rolled out
|
2021-02-03 10:09:24 -05:00
|
|
|
Feature.get(feature_flag_name).state != :off # rubocop:disable Gitlab/AvoidFeatureGet
|
2021-02-01 07:09:03 -05:00
|
|
|
end
|
|
|
|
|
2021-03-11 16:09:09 -05:00
|
|
|
def publish(_result = nil)
|
2021-06-28 17:10:13 -04:00
|
|
|
super
|
2021-03-10 10:09:11 -05:00
|
|
|
|
2021-08-17 05:10:02 -04:00
|
|
|
publish_to_client
|
2021-08-17 14:10:35 -04:00
|
|
|
publish_to_database if @record
|
2021-06-15 20:10:15 -04:00
|
|
|
end
|
|
|
|
|
2021-06-28 17:10:13 -04:00
|
|
|
def publish_to_client
|
2021-08-17 05:10:02 -04:00
|
|
|
return unless should_track?
|
|
|
|
|
2021-06-15 20:10:15 -04:00
|
|
|
Gon.push({ experiment: { name => signature } }, true)
|
|
|
|
rescue NoMethodError
|
|
|
|
# means we're not in the request cycle, and can't add to Gon. Log a warning maybe?
|
2020-12-16 04:10:26 -05:00
|
|
|
end
|
|
|
|
|
2021-06-28 17:10:13 -04:00
|
|
|
def publish_to_database
|
2021-08-17 05:10:02 -04:00
|
|
|
return unless should_track?
|
|
|
|
|
2021-06-28 17:10:13 -04:00
|
|
|
# if the context contains a namespace, group, project, user, or actor
|
|
|
|
value = context.value
|
|
|
|
subject = value[:namespace] || value[:group] || value[:project] || value[:user] || value[:actor]
|
|
|
|
return unless ExperimentSubject.valid_subject?(subject)
|
|
|
|
|
|
|
|
variant = :experimental if @variant_name != :control
|
|
|
|
Experiment.add_subject(name, variant: variant || :control, subject: subject)
|
|
|
|
end
|
|
|
|
|
2021-05-26 11:10:57 -04:00
|
|
|
def record!
|
|
|
|
@record = true
|
|
|
|
end
|
|
|
|
|
2021-05-12 14:10:35 -04:00
|
|
|
def control_behavior
|
|
|
|
# define a default nil control behavior so we can omit it when not needed
|
|
|
|
end
|
|
|
|
|
2021-06-28 17:10:13 -04:00
|
|
|
# TODO: remove
|
|
|
|
# This is deprecated logic as of v0.6.0 and should eventually be removed, but
|
|
|
|
# needs to stay intact for actively running experiments. The new strategy
|
|
|
|
# utilizes Digest::SHA2, a secret seed, and generates a 64-byte string.
|
|
|
|
def key_for(source, seed = name)
|
|
|
|
source = source.keys + source.values if source.is_a?(Hash)
|
|
|
|
|
|
|
|
ingredients = Array(source).map { |v| identify(v) }
|
|
|
|
ingredients.unshift(seed)
|
|
|
|
|
|
|
|
Digest::MD5.hexdigest(ingredients.join('|'))
|
|
|
|
end
|
|
|
|
|
2020-12-16 04:10:26 -05:00
|
|
|
private
|
|
|
|
|
2021-02-12 16:09:01 -05:00
|
|
|
def feature_flag_name
|
|
|
|
name.tr('/', '_')
|
|
|
|
end
|
|
|
|
|
2021-03-12 19:09:16 -05:00
|
|
|
def experiment_group?
|
|
|
|
Feature.enabled?(feature_flag_name, self, type: :experiment, default_enabled: :yaml)
|
2021-02-03 10:09:24 -05:00
|
|
|
end
|
2020-12-16 04:10:26 -05:00
|
|
|
end
|