Class: Split::Experiment
- Inherits:
-
Object
- Object
- Split::Experiment
- Defined in:
- lib/split/experiment.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ :resettable => true, }
Instance Attribute Summary collapse
- #algorithm ⇒ Object
-
#alternatives ⇒ Object
Returns the value of attribute alternatives.
-
#goals ⇒ Object
Returns the value of attribute goals.
-
#name ⇒ Object
Returns the value of attribute name.
-
#resettable ⇒ Object
Returns the value of attribute resettable.
Class Method Summary collapse
- .all ⇒ Object
-
.all_active_first ⇒ Object
Return experiments without a winner (considered “active”) first.
- .find(name) ⇒ Object
- .find_or_create(label, *alternatives) ⇒ Object
Instance Method Summary collapse
- #==(obj) ⇒ Object
- #[](name) ⇒ Object
- #control ⇒ Object
- #delete ⇒ Object
- #delete_goals ⇒ Object
- #extract_alternatives_from_options(options) ⇒ Object
- #finished_key ⇒ Object
- #goals_key ⇒ Object
- #increment_version ⇒ Object
-
#initialize(name, options = {}) ⇒ Experiment
constructor
A new instance of Experiment.
- #key ⇒ Object
- #load_from_redis ⇒ Object
- #new_record? ⇒ Boolean
- #next_alternative ⇒ Object
- #participant_count ⇒ Object
- #random_alternative ⇒ Object
- #reset ⇒ Object
- #reset_winner ⇒ Object
- #resettable? ⇒ Boolean
- #save ⇒ Object
- #set_alternatives_and_options(options) ⇒ Object
- #start ⇒ Object
- #start_time ⇒ Object
- #validate! ⇒ Object
- #version ⇒ Object
- #winner ⇒ Object
- #winner=(winner_name) ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Experiment
Returns a new instance of Experiment.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/split/experiment.rb', line 13 def initialize(name, = {}) = DEFAULT_OPTIONS.merge() @name = name.to_s alternatives = () if alternatives.empty? && (exp_config = Split.configuration.experiment_for(name)) ( alternatives: load_alternatives_from_configuration, goals: load_goals_from_configuration, resettable: exp_config[:resettable], algorithm: exp_config[:algorithm] ) else ( alternatives: alternatives, goals: [:goals], resettable: [:resettable], algorithm: [:algorithm] ) end end |
Instance Attribute Details
#algorithm ⇒ Object
122 123 124 |
# File 'lib/split/experiment.rb', line 122 def algorithm @algorithm ||= Split.configuration.algorithm end |
#alternatives ⇒ Object
Returns the value of attribute alternatives.
7 8 9 |
# File 'lib/split/experiment.rb', line 7 def alternatives @alternatives end |
#goals ⇒ Object
Returns the value of attribute goals.
6 7 8 |
# File 'lib/split/experiment.rb', line 6 def goals @goals end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/split/experiment.rb', line 3 def name @name end |
#resettable ⇒ Object
Returns the value of attribute resettable.
5 6 7 |
# File 'lib/split/experiment.rb', line 5 def resettable @resettable end |
Class Method Details
.all ⇒ Object
56 57 58 |
# File 'lib/split/experiment.rb', line 56 def self.all ExperimentCatalog.all end |
.all_active_first ⇒ Object
Return experiments without a winner (considered “active”) first
61 62 63 |
# File 'lib/split/experiment.rb', line 61 def self.all_active_first ExperimentCatalog.all_active_first end |
.find(name) ⇒ Object
65 66 67 |
# File 'lib/split/experiment.rb', line 65 def self.find(name) ExperimentCatalog.find(name) end |
.find_or_create(label, *alternatives) ⇒ Object
69 70 71 |
# File 'lib/split/experiment.rb', line 69 def self.find_or_create(label, *alternatives) ExperimentCatalog.find_or_create(label, *alternatives) end |
Instance Method Details
#==(obj) ⇒ Object
114 115 116 |
# File 'lib/split/experiment.rb', line 114 def ==(obj) self.name == obj.name end |
#[](name) ⇒ Object
118 119 120 |
# File 'lib/split/experiment.rb', line 118 def [](name) alternatives.find{|a| a.name == name} end |
#control ⇒ Object
160 161 162 |
# File 'lib/split/experiment.rb', line 160 def control alternatives.first end |
#delete ⇒ Object
231 232 233 234 235 236 237 238 239 |
# File 'lib/split/experiment.rb', line 231 def delete alternatives.each(&:delete) reset_winner Split.redis.srem(:experiments, name) Split.redis.del(name) delete_goals Split.configuration.on_experiment_delete.call(self) increment_version end |
#delete_goals ⇒ Object
241 242 243 |
# File 'lib/split/experiment.rb', line 241 def delete_goals Split.redis.del(goals_key) end |
#extract_alternatives_from_options(options) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/split/experiment.rb', line 44 def () alts = [:alternatives] || [] if alts.length == 1 if alts[0].is_a? Hash alts = alts[0].map{|k,v| {k => v} } end end alts end |
#finished_key ⇒ Object
216 217 218 |
# File 'lib/split/experiment.rb', line 216 def finished_key "#{key}:finished" end |
#goals_key ⇒ Object
212 213 214 |
# File 'lib/split/experiment.rb', line 212 def goals_key "#{name}:goals" end |
#increment_version ⇒ Object
200 201 202 |
# File 'lib/split/experiment.rb', line 200 def increment_version @version = Split.redis.incr("#{name}:version") end |
#key ⇒ Object
204 205 206 207 208 209 210 |
# File 'lib/split/experiment.rb', line 204 def key if version.to_i > 0 "#{name}:#{version}" else name end end |
#load_from_redis ⇒ Object
245 246 247 248 249 250 251 |
# File 'lib/split/experiment.rb', line 245 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
110 111 112 |
# File 'lib/split/experiment.rb', line 110 def new_record? !Split.redis.exists(name) end |
#next_alternative ⇒ Object
184 185 186 |
# File 'lib/split/experiment.rb', line 184 def next_alternative winner || random_alternative end |
#participant_count ⇒ Object
156 157 158 |
# File 'lib/split/experiment.rb', line 156 def participant_count alternatives.inject(0){|sum,a| sum + a.participant_count} end |
#random_alternative ⇒ Object
188 189 190 191 192 193 194 |
# File 'lib/split/experiment.rb', line 188 def random_alternative if alternatives.length > 1 algorithm.choose_alternative(self) else alternatives.first end end |
#reset ⇒ Object
224 225 226 227 228 229 |
# File 'lib/split/experiment.rb', line 224 def reset alternatives.each(&:reset) reset_winner Split.configuration.on_experiment_reset.call(self) increment_version end |
#reset_winner ⇒ Object
164 165 166 |
# File 'lib/split/experiment.rb', line 164 def reset_winner Split.redis.hdel(:experiment_winner, name) end |
#resettable? ⇒ Boolean
220 221 222 |
# File 'lib/split/experiment.rb', line 220 def resettable? resettable end |
#save ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/split/experiment.rb', line 73 def save validate! if new_record? Split.redis.sadd(:experiments, name) start unless Split.configuration.start_manually @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 |
#set_alternatives_and_options(options) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/split/experiment.rb', line 37 def () self.alternatives = [:alternatives] self.goals = [:goals] self.resettable = [:resettable] self.algorithm = [:algorithm] end |
#start ⇒ Object
168 169 170 |
# File 'lib/split/experiment.rb', line 168 def start Split.redis.hset(:experiment_start_times, @name, Time.now.to_i) end |
#start_time ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/split/experiment.rb', line 172 def start_time t = Split.redis.hget(:experiment_start_times, @name) if t # Check if stored time is an integer if t =~ /^[-+]?[0-9]+$/ t = Time.at(t.to_i) else t = Time.parse(t) end end end |
#validate! ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/split/experiment.rb', line 100 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 |
#version ⇒ Object
196 197 198 |
# File 'lib/split/experiment.rb', line 196 def version @version ||= (Split.redis.get("#{name.to_s}:version").to_i || 0) end |
#winner ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/split/experiment.rb', line 144 def winner if w = Split.redis.hget(:experiment_winner, name) Split::Alternative.new(w, name) else nil end end |