Class: Split::Experiment
- Inherits:
-
Object
- Object
- Split::Experiment
- Defined in:
- lib/split/experiment.rb
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
Instance Method Summary collapse
- #==(obj) ⇒ Object
- #[](name) ⇒ Object
- #control ⇒ Object
- #delete ⇒ Object
- #delete_goals ⇒ 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
- #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.
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, = {}) = { :resettable => true, }.merge() @name = name.to_s alts = [: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 [:goals] = load_goals_from_configuration [:resettable] = exp_config[:resettable] [:algorithm] = exp_config[:algorithm] end end self.alternatives = alts self.goals = [:goals] self.algorithm = [:algorithm] self.resettable = [:resettable] end |
Instance Attribute Details
#algorithm ⇒ Object
112 113 114 |
# File 'lib/split/experiment.rb', line 112 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
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 |
#control ⇒ Object
150 151 152 |
# File 'lib/split/experiment.rb', line 150 def control alternatives.first end |
#delete ⇒ Object
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_goals ⇒ Object
218 219 220 |
# File 'lib/split/experiment.rb', line 218 def delete_goals Split.redis.del(goals_key) end |
#finished_key ⇒ Object
195 196 197 |
# File 'lib/split/experiment.rb', line 195 def finished_key "#{key}:finished" end |
#goals_key ⇒ Object
191 192 193 |
# File 'lib/split/experiment.rb', line 191 def goals_key "#{name}:goals" end |
#increment_version ⇒ Object
179 180 181 |
# File 'lib/split/experiment.rb', line 179 def increment_version @version = Split.redis.incr("#{name}:version") end |
#key ⇒ Object
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_redis ⇒ Object
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
100 101 102 |
# File 'lib/split/experiment.rb', line 100 def new_record? !Split.redis.exists(name) end |
#next_alternative ⇒ Object
163 164 165 |
# File 'lib/split/experiment.rb', line 163 def next_alternative winner || random_alternative end |
#participant_count ⇒ Object
146 147 148 |
# File 'lib/split/experiment.rb', line 146 def participant_count alternatives.inject(0){|sum,a| sum + a.participant_count} end |
#random_alternative ⇒ Object
167 168 169 170 171 172 173 |
# File 'lib/split/experiment.rb', line 167 def random_alternative if alternatives.length > 1 Split.configuration.algorithm.choose_alternative(self) else alternatives.first end end |
#reset ⇒ Object
203 204 205 206 207 |
# File 'lib/split/experiment.rb', line 203 def reset alternatives.each(&:reset) reset_winner increment_version end |
#reset_winner ⇒ Object
154 155 156 |
# File 'lib/split/experiment.rb', line 154 def reset_winner Split.redis.hdel(:experiment_winner, name) end |
#resettable? ⇒ Boolean
199 200 201 |
# File 'lib/split/experiment.rb', line 199 def resettable? resettable end |
#save ⇒ Object
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_time ⇒ Object
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 |
#version ⇒ Object
175 176 177 |
# File 'lib/split/experiment.rb', line 175 def version @version ||= (Split.redis.get("#{name.to_s}:version").to_i || 0) end |
#winner ⇒ Object
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 |