Class: TrailGuide::Catalog
- Inherits:
-
Object
- Object
- TrailGuide::Catalog
- Includes:
- Enumerable
- Defined in:
- lib/trail_guide/catalog.rb
Defined Under Namespace
Classes: DSL
Instance Attribute Summary collapse
-
#combined ⇒ Object
readonly
Returns the value of attribute combined.
-
#experiments ⇒ Object
readonly
Returns the value of attribute experiments.
Class Method Summary collapse
- .catalog ⇒ Object
- .combined_experiment(combined, name) ⇒ Object
- .load_experiments!(configs: [], classes: []) ⇒ Object
- .load_yaml_experiments(file) ⇒ Object
Instance Method Summary collapse
- #adopted(key) ⇒ Object
- #all ⇒ Object
- #by_started ⇒ Object
- #calibrating ⇒ Object
- #combined_experiment(exp, name) ⇒ Object
- #deregister(key, remove_const = false) ⇒ Object
- #ended ⇒ Object
- #export ⇒ Object
- #find(name) ⇒ Object
- #groups ⇒ Object
- #import(state) ⇒ Object
-
#initialize(experiments = [], combined = []) ⇒ Catalog
constructor
A new instance of Catalog.
- #method_missing(meth, *args, &block) ⇒ Object
- #missing ⇒ Object
- #not_running ⇒ Object
- #orphaned(key, trace) ⇒ Object
- #orphans ⇒ Object
- #paused ⇒ Object
- #register(klass) ⇒ Object
- #respond_to_missing?(meth, include_private = false) ⇒ Boolean
- #running ⇒ Object
- #scheduled ⇒ Object
- #select(name) ⇒ Object
- #started ⇒ Object
- #stopped ⇒ Object
- #unstarted ⇒ Object
Constructor Details
#initialize(experiments = [], combined = []) ⇒ Catalog
Returns a new instance of Catalog.
87 88 89 90 |
# File 'lib/trail_guide/catalog.rb', line 87 def initialize(experiments=[], combined=[]) @experiments = experiments @combined = combined end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
345 346 347 348 |
# File 'lib/trail_guide/catalog.rb', line 345 def method_missing(meth, *args, &block) return experiments.send(meth, *args, &block) if experiments.respond_to?(meth, true) super end |
Instance Attribute Details
#combined ⇒ Object (readonly)
Returns the value of attribute combined.
83 84 85 |
# File 'lib/trail_guide/catalog.rb', line 83 def combined @combined end |
#experiments ⇒ Object (readonly)
Returns the value of attribute experiments.
83 84 85 |
# File 'lib/trail_guide/catalog.rb', line 83 def experiments @experiments end |
Class Method Details
.catalog ⇒ Object
6 7 8 |
# File 'lib/trail_guide/catalog.rb', line 6 def catalog @catalog ||= new end |
.combined_experiment(combined, name) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/trail_guide/catalog.rb', line 70 def combined_experiment(combined, name) experiment = Class.new(TrailGuide::CombinedExperiment) experiment.configure combined.configuration.to_h.merge({ name: name.to_s.underscore.to_sym, parent: combined, combined: [], variants: combined.configuration.variants.map { |var| var.dup(experiment) }, goals: combined.configuration.goals.map { |goal| goal.dup(experiment) } }) experiment end |
.load_experiments!(configs: [], classes: []) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/trail_guide/catalog.rb', line 10 def load_experiments!(configs: [], classes: []) @catalog = nil # Load experiments from YAML configs if any exists [configs].flatten.each do |path| Dir[Rails.root.join(path)].each { |f| load_yaml_experiments(f) if ['.yml', '.yaml'].include?(File.extname(f)) } end # Load experiments from ruby configs if any exist [configs].flatten.each do |path| Dir[Rails.root.join(path)].each { |f| DSL.instance_eval(File.read(f)) if File.extname(f) == '.rb' } end # Load any experiment classes defined in the app [classes].flatten.each do |path| Dir[Rails.root.join(path)].each { |f| load f } end end |
.load_yaml_experiments(file) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/trail_guide/catalog.rb', line 29 def load_yaml_experiments(file) experiments = (YAML.load_file(file) || {} rescue {}) .symbolize_keys.map { |k,v| [k, v.symbolize_keys] }.to_h experiments.each do |name, | expvars = [:variants].map do |var| if var.is_a?(Array) [var[0], var[1].symbolize_keys] else [var] end end expgoals = [:goals] # TODO is it worth parsing these out for complex funnels, etc.? or is # it better to just force use of the DSL? DSL.experiment(name) do |config| expvars.each do |expvar| variant *expvar end expgoals.each do |expgoal| goal expgoal end if expgoals.present? config.control = [:control] if [:control] config.groups = [:groups] if [:groups] config.algorithm = [:algorithm] if [:algorithm] config.combined = [:combined] if [:combined] config.reset_manually = [:reset_manually] if .key?(:reset_manually) config.start_manually = [:start_manually] if .key?(:start_manually) config.store_override = [:store_override] if .key?(:store_override) config.track_override = [:track_override] if .key?(:track_override) config.allow_multiple_conversions = [:allow_multiple_conversions] if .key?(:allow_multiple_conversions) config.allow_multiple_goals = [:allow_multiple_goals] if .key?(:allow_multiple_goals) # TODO need to remember to update this with all the new config vars end end end |
Instance Method Details
#adopted(key) ⇒ Object
339 340 341 342 343 |
# File 'lib/trail_guide/catalog.rb', line 339 def adopted(key) TrailGuide.redis.del("orphans:#{key}") rescue Errno::ECONNREFUSED, Redis::BaseError, SocketError => e false end |
#all ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/trail_guide/catalog.rb', line 107 def all exploded = experiments.map do |exp| if exp.combined? exp.combined.map { |name| combined_experiment(exp, name) } else exp end end.flatten new(exploded, @combined) end |
#by_started ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/trail_guide/catalog.rb', line 155 def by_started scoped = to_a.sort do |a,b| # TODO finish implementing specs, then implement `experiment.fresh?`, then (maybe) re-work this all # into an experiment spaceship operator if !(a.started? || a.scheduled? || a.winner?) && !(b.started? || b.scheduled? || b.winner?) a.experiment_name.to_s <=> b.experiment_name.to_s elsif !(a.started? || a.scheduled? || a.winner?) -1 elsif !(b.started? || b.scheduled? || b.winner?) 1 else if a.winner? && !b.winner? 1 elsif !a.winner? && b.winner? -1 elsif a.winner? && b.winner? a.experiment_name.to_s <=> b.experiment_name.to_s elsif a.running? && !b.running? -1 elsif !a.running? && b.running? 1 elsif a.running? && b.running? if a.started_at == b.started_at a.experiment_name.to_s <=> b.experiment_name.to_s else a.started_at <=> b.started_at end elsif a.paused? && !b.paused? -1 elsif !a.paused? && b.paused? 1 elsif a.paused? && b.paused? if a.paused_at == b.paused_at a.experiment_name.to_s <=> b.experiment_name.to_s else a.paused_at <=> b.paused_at end elsif a.scheduled? && !b.scheduled? -1 elsif !a.scheduled? && b.scheduled? 1 elsif a.scheduled? && b.scheduled? if a.started_at == b.started_at a.experiment_name.to_s <=> b.experiment_name.to_s else a.started_at <=> b.started_at end elsif a.stopped? && !b.stopped? -1 # TODO remove unused case elsif !a.stopped? && b.stopped? 1 # TODO remove unused case elsif a.stopped? && b.stopped? if a.stopped_at == b.stopped_at a.experiment_name.to_s <=> b.experiment_name.to_s else a.stopped_at <=> b.stopped_at end else a.experiment_name.to_s <=> b.experiment_name.to_s end end end new(scoped, @combined) end |
#calibrating ⇒ Object
119 120 121 |
# File 'lib/trail_guide/catalog.rb', line 119 def new(to_a.select(&:calibrating?), @combined) end |
#combined_experiment(exp, name) ⇒ Object
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/trail_guide/catalog.rb', line 92 def combined_experiment(exp, name) combo = @combined.find do |cex| cex.experiment_name == name.to_s.underscore.to_sym && cex.parent.experiment_name == exp.experiment_name end return combo if combo.present? combo = self.class.combined_experiment(exp, name) @combined << combo combo end |
#deregister(key, remove_const = false) ⇒ Object
269 270 271 272 273 274 275 276 |
# File 'lib/trail_guide/catalog.rb', line 269 def deregister(key, remove_const=false) klass = find(key) return unless klass.present? experiments.delete(klass) return klass unless remove_const && klass.name.present? Object.send(:remove_const, :"#{klass.name}") return key end |
#ended ⇒ Object
143 144 145 |
# File 'lib/trail_guide/catalog.rb', line 143 def ended new(to_a.select(&:winner?), @combined) end |
#export ⇒ Object
278 279 280 281 282 283 284 285 286 |
# File 'lib/trail_guide/catalog.rb', line 278 def export map do |exp| if exp.combined? [exp.as_json].concat(exp.combined_experiments.map(&:as_json)) else exp.as_json end end.flatten.reduce({}) { |red,exp| red.merge!(exp) } end |
#find(name) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/trail_guide/catalog.rb', line 221 def find(name) if name.is_a?(Class) experiments.find { |exp| exp == name } else experiment = experiments.find do |exp| exp.experiment_name == name.to_s.underscore.to_sym || exp.groups.include?(name.to_s.underscore.to_sym) || exp.name == name.to_s.classify end return experiment if experiment.present? combined = experiments.find do |exp| next unless exp.combined? exp.combined.any? { |combo| combo.to_s.underscore.to_sym == name.to_s.underscore.to_sym } end return nil unless combined.present? return combined_experiment(combined, name) end end |
#groups ⇒ Object
103 104 105 |
# File 'lib/trail_guide/catalog.rb', line 103 def groups experiments.map(&:groups).flatten.uniq end |
#import(state) ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/trail_guide/catalog.rb', line 288 def import(state) state.each do |exp,est| experiment = find(exp) next unless experiment.present? experiment.reset! TrailGuide.redis.hsetnx(experiment.storage_key, 'name', experiment.experiment_name) TrailGuide.redis.hset(experiment.storage_key, 'started_at', DateTime.parse(est['started_at']).to_i) if est['started_at'].present? TrailGuide.redis.hset(experiment.storage_key, 'paused_at', DateTime.parse(est['paused_at']).to_i) if est['paused_at'].present? TrailGuide.redis.hset(experiment.storage_key, 'stopped_at', DateTime.parse(est['stopped_at']).to_i) if est['stopped_at'].present? TrailGuide.redis.hset(experiment.storage_key, 'winner', est['winner']) if est['winner'].present? est['variants'].each do |var,vst| variant = experiment.variants.find { |v| v == var } next unless variant.present? TrailGuide.redis.hincrby(variant.storage_key, 'participants', vst['participants'].to_i) if vst['participants'].to_i > 0 if vst['converted'].is_a?(Hash) vst['converted'].each do |goal,gct| TrailGuide.redis.hincrby(variant.storage_key, goal, gct.to_i) if gct.to_i > 0 end else TrailGuide.redis.hincrby(variant.storage_key, 'converted', vst['converted'].to_i) if vst['converted'].to_i > 0 end end end end |
#missing ⇒ Object
316 317 318 319 320 321 |
# File 'lib/trail_guide/catalog.rb', line 316 def missing TrailGuide.redis.keys.select do |key| exp = key.split(':').first find(exp).nil? end end |
#not_running ⇒ Object
151 152 153 |
# File 'lib/trail_guide/catalog.rb', line 151 def not_running new(to_a.select { |e| !e.running? }, @combined) end |
#orphaned(key, trace) ⇒ Object
323 324 325 326 327 328 329 |
# File 'lib/trail_guide/catalog.rb', line 323 def orphaned(key, trace) added = TrailGuide.redis.sadd("orphans:#{key}", trace) TrailGuide.redis.expire("orphans:#{key}", 15.minutes.seconds) added rescue Errno::ECONNREFUSED, Redis::BaseError, SocketError => e false end |
#orphans ⇒ Object
331 332 333 334 335 336 337 |
# File 'lib/trail_guide/catalog.rb', line 331 def orphans TrailGuide.redis.keys("orphans:*").reduce({}) do |h,key| h.merge({ key.split(':').last => TrailGuide.redis.smembers(key) }) end rescue Errno::ECONNREFUSED, Redis::BaseError, SocketError => e {} end |
#paused ⇒ Object
135 136 137 |
# File 'lib/trail_guide/catalog.rb', line 135 def paused new(to_a.select { |e| e.paused? && !e.winner? }, @combined) end |
#register(klass) ⇒ Object
264 265 266 267 |
# File 'lib/trail_guide/catalog.rb', line 264 def register(klass) experiments << klass unless experiments.any? { |exp| exp == klass } klass end |
#respond_to_missing?(meth, include_private = false) ⇒ Boolean
350 351 352 |
# File 'lib/trail_guide/catalog.rb', line 350 def respond_to_missing?(meth, include_private=false) experiments.respond_to?(meth, include_private) end |
#running ⇒ Object
131 132 133 |
# File 'lib/trail_guide/catalog.rb', line 131 def running new(to_a.select { |e| e.running? && !e.winner? }, @combined) end |
#scheduled ⇒ Object
127 128 129 |
# File 'lib/trail_guide/catalog.rb', line 127 def scheduled new(to_a.select { |e| e.scheduled? && !e.winner? }, @combined) end |
#select(name) ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/trail_guide/catalog.rb', line 242 def select(name) if name.is_a?(Class) selected = experiments.select { |exp| exp == name } else # TODO we can be more efficient than mapping twice here selected = experiments.select do |exp| exp.experiment_name == name.to_s.underscore.to_sym || exp.groups.include?(name.to_s.underscore.to_sym) || exp.name == name.to_s.classify || (exp.combined? && exp.combined.any? { |combo| combo.to_s.underscore.to_sym == name.to_s.underscore.to_sym }) end.map do |exp| if exp.combined? && exp.combined.any? { |combo| combo.to_s.underscore.to_sym == name.to_s.underscore.to_sym } combined_experiment(exp, name) else exp end end end new(selected, @combined) end |
#started ⇒ Object
123 124 125 |
# File 'lib/trail_guide/catalog.rb', line 123 def started new(to_a.select { |e| e.started? && !e.winner? }, @combined) end |
#stopped ⇒ Object
139 140 141 |
# File 'lib/trail_guide/catalog.rb', line 139 def stopped new(to_a.select { |e| e.stopped? && !e.winner? }, @combined) end |
#unstarted ⇒ Object
147 148 149 |
# File 'lib/trail_guide/catalog.rb', line 147 def unstarted new(to_a.select { |e| !e.started? && !e. && !e.scheduled? && !e.winner? }, @combined) end |