Class: Gitlab::Experiment::Rollout::Percent

Inherits:
Base
  • Object
show all
Defined in:
lib/gitlab/experiment/rollout/percent.rb

Instance Attribute Summary

Attributes inherited from Base

#experiment

Instance Method Summary collapse

Methods inherited from Base

#initialize, #rollout_for

Constructor Details

This class inherits a constructor from Gitlab::Experiment::Rollout::Base

Instance Method Details

#executeObject



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/gitlab/experiment/rollout/percent.rb', line 9

def execute
  crc = normalized_id
  total = 0

  case distribution_rules
  # run through the rules until finding an acceptable one
  when Array then variant_names[distribution_rules.find_index { |percent| crc % 100 <= total += percent }]
  # run through the variant names until finding an acceptable one
  when Hash then distribution_rules.find { |_, percent| crc % 100 <= total += percent }.first
  # when there are no rules, assume even distribution
  else variant_names[crc % variant_names.length]
  end
end

#validate!Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gitlab/experiment/rollout/percent.rb', line 23

def validate!
  case distribution_rules
  when nil then nil
  when Array, Hash
    if distribution_rules.length != variant_names.length
      raise InvalidRolloutRules, "the distribution rules don't match the number of variants defined"
    end
  else
    raise InvalidRolloutRules, 'unknown distribution options type'
  end
end