debian-mirror-gitlab/app/experiments/application_experiment.rb

70 lines
2.1 KiB
Ruby
Raw Normal View History

2021-02-22 17:27:13 +05:30
# frozen_string_literal: true
2021-03-11 19:13:27 +05:30
class ApplicationExperiment < Gitlab::Experiment # rubocop:disable Gitlab/NamespacedClass
def enabled?
return false if Feature::Definition.get(feature_flag_name).nil? # there has to be a feature flag yaml file
2021-04-17 20:07:23 +05:30
return false unless Gitlab.dev_env_or_com? # we have to be in an environment that allows experiments
2021-03-11 19:13:27 +05:30
2021-04-17 20:07:23 +05:30
# the feature flag has to be rolled out
2021-03-11 19:13:27 +05:30
Feature.get(feature_flag_name).state != :off # rubocop:disable Gitlab/AvoidFeatureGet
end
2021-04-17 20:07:23 +05:30
def publish(_result = nil)
2021-09-30 23:02:18 +05:30
super
2021-04-17 20:07:23 +05:30
2021-10-27 15:23:28 +05:30
publish_to_client
publish_to_database if @record
2021-09-04 01:27:46 +05:30
end
2021-09-30 23:02:18 +05:30
def publish_to_client
2021-10-27 15:23:28 +05:30
return unless should_track?
2021-09-04 01:27:46 +05:30
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?
2021-02-22 17:27:13 +05:30
end
2021-09-30 23:02:18 +05:30
def publish_to_database
2021-10-27 15:23:28 +05:30
return unless should_track?
2021-09-30 23:02:18 +05:30
# 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)
2021-02-22 17:27:13 +05:30
2021-09-30 23:02:18 +05:30
variant = :experimental if @variant_name != :control
Experiment.add_subject(name, variant: variant || :control, subject: subject)
2021-02-22 17:27:13 +05:30
end
2021-09-04 01:27:46 +05:30
def record!
@record = true
end
2021-06-08 01:23:25 +05:30
def control_behavior
# define a default nil control behavior so we can omit it when not needed
end
2021-09-30 23:02:18 +05:30
# 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
2021-02-22 17:27:13 +05:30
private
2021-03-11 19:13:27 +05:30
def feature_flag_name
name.tr('/', '_')
end
2021-04-17 20:07:23 +05:30
def experiment_group?
Feature.enabled?(feature_flag_name, self, type: :experiment, default_enabled: :yaml)
2021-02-22 17:27:13 +05:30
end
end