Class: Growthbook::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/growthbook/util.rb

Overview

internal use only

Class Method Summary collapse

Class Method Details

.check_rule(actual, op, desired) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/growthbook/util.rb', line 10

def self.check_rule(actual, op, desired)
  # Check if both strings are numeric so we can do natural ordering
  # for greater than / less than operators
  numeric = begin
    (!Float(actual).nil? && !Float(desired).nil?)
  rescue StandardError
    false
  end

  case op
  when '='
    numeric ? Float(actual).to_d == Float(desired).to_d : actual == desired
  when '!='
    numeric ? Float(actual).to_d != Float(desired).to_d : actual != desired
  when '>'
    numeric ? Float(actual) > Float(desired) : actual > desired
  when '<'
    numeric ? Float(actual) < Float(desired) : actual < desired
  when '~'
    begin
      !!(actual =~ Regexp.new(desired))
    rescue StandardError
      false
    end
  when '!~'
    begin
      actual !~ Regexp.new(desired)
    rescue StandardError
      false
    end
  else
    true
  end
end

.choose_variation(num, ranges) ⇒ Object

Chose a variation based on a hash and range



99
100
101
102
103
104
# File 'lib/growthbook/util.rb', line 99

def self.choose_variation(num, ranges)
  ranges.each_with_index do |range, i|
    return i if num >= range[0] && num < range[1]
  end
  -1
end

.get_bucket_ranges(num_variations, coverage, weights) ⇒ Object

Determine bucket ranges for experiment variations



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/growthbook/util.rb', line 73

def self.get_bucket_ranges(num_variations, coverage, weights)
  # Make sure coverage is within bounds
  coverage = 1.0 if coverage.nil?
  coverage = 0.0 if coverage.negative?
  coverage = 1.0 if coverage > 1

  # Default to equal weights
  weights = get_equal_weights(num_variations) if !weights || weights.length != num_variations

  # If weights don't add up to 1 (or close to it), default to equal weights
  total = weights.sum
  weights = get_equal_weights(num_variations) if total < 0.99 || total > 1.01

  # Convert weights to ranges
  cumulative = 0.0
  ranges = []
  weights.each do |w|
    start = cumulative
    cumulative += w
    ranges << [start, start + (coverage * w)]
  end

  ranges
end

.get_equal_weights(num_variations) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/growthbook/util.rb', line 62

def self.get_equal_weights(num_variations)
  return [] if num_variations < 1

  weights = []
  (1..num_variations).each do |_i|
    weights << (1.0 / num_variations)
  end
  weights
end

.get_hash(seed:, value:, version:) ⇒ Float?

Returns Hash, or nil if the hash version is invalid.

Returns:

  • (Float, nil)

    Hash, or nil if the hash version is invalid



46
47
48
49
50
51
# File 'lib/growthbook/util.rb', line 46

def self.get_hash(seed:, value:, version:)
  return (FNV.new.fnv1a_32(value + seed) % 1000) / 1000.0 if version == 1
  return (FNV.new.fnv1a_32(FNV.new.fnv1a_32(seed + value).to_s) % 10_000) / 10_000.0 if version == 2

  nil
end

.get_query_string_override(id, url, num_variations) ⇒ Object

Get an override variation from a url querystring e.g. localhost?my-test=1 will return ‘1` for id `my-test`



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/growthbook/util.rb', line 108

def self.get_query_string_override(id, url, num_variations)
  # Skip if url is empty
  return nil if url == '' || id.nil?

  # Parse out the query string
  parsed = URI(url)
  parsed_query = parsed.query
  return nil if parsed_query.nil?

  qs = URI.decode_www_form(parsed_query)

  # Look for `id` in the querystring and get the value
  vals = qs.assoc(id)
  return nil unless vals

  val = vals.last
  return nil unless val

  # Parse the value as an integer
  n = begin
    Integer(val)
  rescue StandardError
    nil
  end

  # Make sure the integer is within range
  return nil if n.nil?
  return nil if n.negative?
  return nil if n >= num_variations

  n
end

.in_namespace?(hash_value, namespace) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
# File 'lib/growthbook/util.rb', line 53

def self.in_namespace?(hash_value, namespace)
  return false if namespace.nil?

  n = get_hash(seed: "__#{namespace[0]}", value: hash_value, version: 1)
  return false if n.nil?

  n >= namespace[1] && n < namespace[2]
end

.in_range?(num, range) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/growthbook/util.rb', line 141

def self.in_range?(num, range)
  num >= range[0] && num < range[1]
end