Class: Gitlab::Experiment

Inherits:
Object
  • Object
show all
Includes:
BaseInterface, Cache, Callbacks
Defined in:
lib/gitlab/experiment.rb,
lib/gitlab/experiment/dsl.rb,
lib/gitlab/experiment/cache.rb,
lib/gitlab/experiment/rspec.rb,
lib/gitlab/experiment/engine.rb,
lib/gitlab/experiment/context.rb,
lib/gitlab/experiment/cookies.rb,
lib/gitlab/experiment/rollout.rb,
lib/gitlab/experiment/variant.rb,
lib/gitlab/experiment/version.rb,
lib/gitlab/experiment/callbacks.rb,
lib/gitlab/experiment/configuration.rb,
lib/gitlab/experiment/rollout/first.rb,
lib/gitlab/experiment/base_interface.rb,
lib/gitlab/experiment/rollout/random.rb,
lib/gitlab/experiment/rollout/round_robin.rb,
lib/gitlab/experiment/cache/redis_hash_store.rb

Defined Under Namespace

Modules: BaseInterface, Cache, Callbacks, Cookies, Dsl, RSpecHelpers, RSpecMatchers, Rollout Classes: Configuration, Context, Engine, Variant

Constant Summary collapse

VERSION =
'0.5.0'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Cache

#cache, #cache_key, #cache_variant

Methods included from BaseInterface

#behaviors, #flipper_id, #id, #initialize, #inspect, #variant_names

Class Method Details

.default_rollout(rollout = nil) ⇒ Object



30
31
32
33
34
# File 'lib/gitlab/experiment.rb', line 30

def default_rollout(rollout = nil)
  return @rollout ||= Configuration.default_rollout if rollout.blank?

  @rollout = Rollout.resolve(rollout)
end

Instance Method Details

#context(value = nil) ⇒ Object



50
51
52
53
54
55
# File 'lib/gitlab/experiment.rb', line 50

def context(value = nil)
  return @context if value.blank?

  @context.value(value)
  @context
end

#enabled?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/gitlab/experiment.rb', line 98

def enabled?
  true
end

#excluded?Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
# File 'lib/gitlab/experiment.rb', line 102

def excluded?
  return @excluded if defined?(@excluded)

  @excluded = !@context.trackable? || # adhere to DNT headers
    !run_callbacks(:exclusion_check) { :not_excluded } # didn't pass exclusion check
end

#experiment_group?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/gitlab/experiment.rb', line 109

def experiment_group?
  instance_exec(@variant_name, &Configuration.inclusion_resolver)
end

#key_for(hash) ⇒ Object



121
122
123
# File 'lib/gitlab/experiment.rb', line 121

def key_for(hash)
  instance_exec(hash, &Configuration.context_hash_strategy)
end

#nameObject



37
38
39
# File 'lib/gitlab/experiment.rb', line 37

def name
  [Configuration.name_prefix, @name].compact.join('_')
end

#publish(result) ⇒ Object



88
89
90
# File 'lib/gitlab/experiment.rb', line 88

def publish(result)
  instance_exec(result, &Configuration.publishing_behavior)
end

#rollout(rollout = nil) ⇒ Object



78
79
80
81
82
# File 'lib/gitlab/experiment.rb', line 78

def rollout(rollout = nil)
  return @rollout ||= self.class.default_rollout if rollout.blank?

  @rollout = Rollout.resolve(rollout)
end

#run(variant_name = nil) ⇒ Object



84
85
86
# File 'lib/gitlab/experiment.rb', line 84

def run(variant_name = nil)
  @result ||= super(variant(variant_name).name)
end

#should_track?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/gitlab/experiment.rb', line 113

def should_track?
  enabled? && !excluded?
end

#signatureObject



117
118
119
# File 'lib/gitlab/experiment.rb', line 117

def signature
  { variant: variant.name, experiment: name }.merge(context.signature)
end

#track(action, **event_args) ⇒ Object



92
93
94
95
96
# File 'lib/gitlab/experiment.rb', line 92

def track(action, **event_args)
  return unless should_track?

  instance_exec(action, event_args, &Configuration.tracking_behavior)
end

#try(name = nil, &block) ⇒ Object



45
46
47
48
# File 'lib/gitlab/experiment.rb', line 45

def try(name = nil, &block)
  name = (name || :candidate).to_s
  behaviors[name] = block
end

#use(&block) ⇒ Object



41
42
43
# File 'lib/gitlab/experiment.rb', line 41

def use(&block)
  try(:control, &block)
end

#variant(value = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/gitlab/experiment.rb', line 57

def variant(value = nil)
  @variant_name = cache_variant(value) if value.present?
  return Variant.new(name: (@variant_name || :unresolved).to_s) if @variant_name || @resolving_variant

  if enabled?
    @variant_name ||= :control if excluded?

    @resolving_variant = true
    if (result = cache_variant(@variant_name) { resolve_variant_name }).present?
      @variant_name = result.to_sym
    end
  end

  run_callbacks(segmentation_callback_chain) do
    @variant_name ||= :control
    Variant.new(name: @variant_name.to_s)
  end
ensure
  @resolving_variant = false
end