Class: Split::Experiment

Inherits:
Object
  • Object
show all
Defined in:
lib/split/experiment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Experiment

Returns a new instance of Experiment


9
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
# File 'lib/split/experiment.rb', line 9

def initialize(name, options = {})
  options = {
    :resettable => true,
  }.merge(options)

  @name = name.to_s

  alts = options[:alternatives] || []

  if alts.length == 1
    if alts[0].is_a? Hash
      alts = alts[0].map{|k,v| {k => v} }
    end
  end

  if alts.empty?
    exp_config = Split.configuration.experiment_for(name)
    if exp_config
      alts = load_alternatives_from_configuration
      options[:goals] = load_goals_from_configuration
      options[:resettable] = exp_config[:resettable]
      options[:algorithm] = exp_config[:algorithm]
    end
  end

  self.alternatives = alts
  self.goals = options[:goals]
  self.algorithm = options[:algorithm]
  self.resettable = options[:resettable]
end

Instance Attribute Details

#algorithmObject


112
113
114
# File 'lib/split/experiment.rb', line 112

def algorithm
  @algorithm ||= Split.configuration.algorithm
end

#alternativesObject

Returns the value of attribute alternatives


7
8
9
# File 'lib/split/experiment.rb', line 7

def alternatives
  @alternatives
end

#goalsObject

Returns the value of attribute goals


6
7
8
# File 'lib/split/experiment.rb', line 6

def goals
  @goals
end

#nameObject

Returns the value of attribute name


3
4
5
# File 'lib/split/experiment.rb', line 3

def name
  @name
end

#resettableObject

Returns the value of attribute resettable


5
6
7
# File 'lib/split/experiment.rb', line 5

def resettable
  @resettable
end

Class Method Details

.allObject


40
41
42
# File 'lib/split/experiment.rb', line 40

def self.all
  Split.redis.smembers(:experiments).map {|e| find(e)}
end

.find(name) ⇒ Object


44
45
46
47
48
49
50
51
52
# File 'lib/split/experiment.rb', line 44

def self.find(name)
  if Split.redis.exists(name)
    obj = self.new name
    obj.load_from_redis
  else
    obj = nil
  end
  obj
end

.find_or_create(label, *alternatives) ⇒ Object


54
55
56
57
58
59
60
61
# File 'lib/split/experiment.rb', line 54

def self.find_or_create(label, *alternatives)
  experiment_name_with_version, goals = normalize_experiment(label)
  name = experiment_name_with_version.to_s.split(':')[0]

  exp = self.new name, :alternatives => alternatives, :goals => goals
  exp.save
  exp
end

Instance Method Details

#==(obj) ⇒ Object


104
105
106
# File 'lib/split/experiment.rb', line 104

def ==(obj)
  self.name == obj.name
end

#[](name) ⇒ Object


108
109
110
# File 'lib/split/experiment.rb', line 108

def [](name)
  alternatives.find{|a| a.name == name}
end

#controlObject


150
151
152
# File 'lib/split/experiment.rb', line 150

def control
  alternatives.first
end

#deleteObject


209
210
211
212
213
214
215
216
# File 'lib/split/experiment.rb', line 209

def delete
  alternatives.each(&:delete)
  reset_winner
  Split.redis.srem(:experiments, name)
  Split.redis.del(name)
  delete_goals
  increment_version
end

#delete_goalsObject


218
219
220
# File 'lib/split/experiment.rb', line 218

def delete_goals
  Split.redis.del(goals_key)
end

#finished_keyObject


195
196
197
# File 'lib/split/experiment.rb', line 195

def finished_key
  "#{key}:finished"
end

#goals_keyObject


191
192
193
# File 'lib/split/experiment.rb', line 191

def goals_key
  "#{name}:goals"
end

#increment_versionObject


179
180
181
# File 'lib/split/experiment.rb', line 179

def increment_version
  @version = Split.redis.incr("#{name}:version")
end

#keyObject


183
184
185
186
187
188
189
# File 'lib/split/experiment.rb', line 183

def key
  if version.to_i > 0
    "#{name}:#{version}"
  else
    name
  end
end

#load_from_redisObject


222
223
224
225
226
227
228
# File 'lib/split/experiment.rb', line 222

def load_from_redis
  exp_config = Split.redis.hgetall(experiment_config_key)
  self.resettable = exp_config['resettable']
  self.algorithm = exp_config['algorithm']
  self.alternatives = load_alternatives_from_redis
  self.goals = load_goals_from_redis
end

#new_record?Boolean

Returns:

  • (Boolean)

100
101
102
# File 'lib/split/experiment.rb', line 100

def new_record?
  !Split.redis.exists(name)
end

#next_alternativeObject


163
164
165
# File 'lib/split/experiment.rb', line 163

def next_alternative
  winner || random_alternative
end

#participant_countObject


146
147
148
# File 'lib/split/experiment.rb', line 146

def participant_count
  alternatives.inject(0){|sum,a| sum + a.participant_count}
end

#random_alternativeObject


167
168
169
170
171
172
173
# File 'lib/split/experiment.rb', line 167

def random_alternative
  if alternatives.length > 1
    algorithm.choose_alternative(self)
  else
    alternatives.first
  end
end

#resetObject


203
204
205
206
207
# File 'lib/split/experiment.rb', line 203

def reset
  alternatives.each(&:reset)
  reset_winner
  increment_version
end

#reset_winnerObject


154
155
156
# File 'lib/split/experiment.rb', line 154

def reset_winner
  Split.redis.hdel(:experiment_winner, name)
end

#resettable?Boolean

Returns:

  • (Boolean)

199
200
201
# File 'lib/split/experiment.rb', line 199

def resettable?
  resettable
end

#saveObject


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/split/experiment.rb', line 63

def save
  validate!

  if new_record?
    Split.redis.sadd(:experiments, name)
    Split.redis.hset(:experiment_start_times, @name, Time.now)
    @alternatives.reverse.each {|a| Split.redis.lpush(name, a.name)}
    @goals.reverse.each {|a| Split.redis.lpush(goals_key, a)} unless @goals.nil?
  else

    existing_alternatives = load_alternatives_from_redis
    existing_goals = load_goals_from_redis
    unless existing_alternatives == @alternatives.map(&:name) && existing_goals == @goals
      reset
      @alternatives.each(&:delete)
      delete_goals
      Split.redis.del(@name)
      @alternatives.reverse.each {|a| Split.redis.lpush(name, a.name)}
      @goals.reverse.each {|a| Split.redis.lpush(goals_key, a)} unless @goals.nil?
    end
  end

  Split.redis.hset(experiment_config_key, :resettable, resettable)
  Split.redis.hset(experiment_config_key, :algorithm, algorithm.to_s)
  self
end

#start_timeObject


158
159
160
161
# File 'lib/split/experiment.rb', line 158

def start_time
  t = Split.redis.hget(:experiment_start_times, @name)
  Time.parse(t) if t
end

#validate!Object


90
91
92
93
94
95
96
97
98
# File 'lib/split/experiment.rb', line 90

def validate!
  if @alternatives.empty? && Split.configuration.experiment_for(@name).nil?
    raise ExperimentNotFound.new("Experiment #{@name} not found")
  end
  @alternatives.each {|a| a.validate! }
  unless @goals.nil? || goals.kind_of?(Array)
    raise ArgumentError, 'Goals must be an array'
  end
end

#versionObject


175
176
177
# File 'lib/split/experiment.rb', line 175

def version
  @version ||= (Split.redis.get("#{name.to_s}:version").to_i || 0)
end

#winnerObject


134
135
136
137
138
139
140
# File 'lib/split/experiment.rb', line 134

def winner
  if w = Split.redis.hget(:experiment_winner, name)
    Split::Alternative.new(w, name)
  else
    nil
  end
end

#winner=(winner_name) ⇒ Object


142
143
144
# File 'lib/split/experiment.rb', line 142

def winner=(winner_name)
  Split.redis.hset(:experiment_winner, name, winner_name.to_s)
end