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
-
#experiments ⇒ Object
readonly
Returns the value of attribute experiments.
Class Method Summary collapse
- .catalog ⇒ Object
- .combined_experiment(combined, name) ⇒ Object
- .load_experiments! ⇒ Object
- .load_yaml_experiments(file) ⇒ Object
Instance Method Summary collapse
- #adopted(key) ⇒ Object
- #all ⇒ Object
- #by_started ⇒ Object
- #calibrating ⇒ Object
- #deregister(key) ⇒ Object
- #each(&block) ⇒ Object
- #ended ⇒ Object
- #export ⇒ Object
- #find(name) ⇒ Object
- #groups ⇒ Object
- #import(state) ⇒ Object
-
#initialize(experiments = []) ⇒ Catalog
constructor
A new instance of Catalog.
- #method_missing(meth, *args, &block) ⇒ 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 = []) ⇒ Catalog
Returns a new instance of Catalog.
82 83 84 |
# File 'lib/trail_guide/catalog.rb', line 82 def initialize(experiments=[]) @experiments = experiments end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
302 303 304 305 |
# File 'lib/trail_guide/catalog.rb', line 302 def method_missing(meth, *args, &block) return experiments.send(meth, *args, &block) if experiments.respond_to?(meth, true) super end |
Instance Attribute Details
#experiments ⇒ Object (readonly)
Returns the value of attribute experiments.
80 81 82 |
# File 'lib/trail_guide/catalog.rb', line 80 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
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/trail_guide/catalog.rb', line 66 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! ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/trail_guide/catalog.rb', line 10 def load_experiments! @catalog = nil # Load experiments from YAML configs if any exists load_yaml_experiments(Rails.root.join("config/experiments.yml")) Dir[Rails.root.join("config/experiments/**/*.yml")].each { |f| load_yaml_experiments(f) } # Load experiments from ruby configs if any exist DSL.instance_eval(File.read(Rails.root.join("config/experiments.rb"))) if File.exists?(Rails.root.join("config/experiments.rb")) Dir[Rails.root.join("config/experiments/**/*.rb")].each { |f| DSL.instance_eval(File.read(f)) } # Load any experiment classes defined in the app Dir[Rails.root.join("app/experiments/**/*.rb")].each { |f| load f } end |
.load_yaml_experiments(file) ⇒ Object
25 26 27 28 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 |
# File 'lib/trail_guide/catalog.rb', line 25 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 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
296 297 298 299 300 |
# File 'lib/trail_guide/catalog.rb', line 296 def adopted(key) TrailGuide.redis.del("orphans:#{key}") rescue Errno::ECONNREFUSED, Redis::BaseError, SocketError => e false end |
#all ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/trail_guide/catalog.rb', line 94 def all exploded = experiments.map do |exp| if exp.combined? exp.combined.map { |name| combined_experiment(exp, name) } else exp end end.flatten self.class.new(exploded) end |
#by_started ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 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 |
# File 'lib/trail_guide/catalog.rb', line 142 def by_started scoped = to_a.sort do |a,b| 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? a.started_at <=> b.started_at elsif a.paused? && !b.paused? -1 elsif !a.paused? && b.paused? 1 elsif a.paused? && b.paused? a.paused_at <=> b.paused_at elsif a.scheduled? && !b.scheduled? -1 elsif !a.scheduled? && b.scheduled? 1 elsif a.scheduled? && b.scheduled? a.started_at <=> b.started_at elsif a.stopped? && !b.stopped? -1 elsif !a.stopped? && b.stopped? 1 elsif a.stopped? && b.stopped? a.stopped_at <=> b.stopped_at else a.experiment_name.to_s <=> b.experiment_name.to_s end end end self.class.new(scoped) end |
#calibrating ⇒ Object
106 107 108 |
# File 'lib/trail_guide/catalog.rb', line 106 def self.class.new(to_a.select(&:calibrating?)) end |
#deregister(key) ⇒ Object
238 239 240 |
# File 'lib/trail_guide/catalog.rb', line 238 def deregister(key) # TODO (mostly only useful for engine specs) end |
#each(&block) ⇒ Object
86 87 88 |
# File 'lib/trail_guide/catalog.rb', line 86 def each(&block) experiments.each(&block) end |
#ended ⇒ Object
130 131 132 |
# File 'lib/trail_guide/catalog.rb', line 130 def ended self.class.new(to_a.select(&:winner?)) end |
#export ⇒ Object
242 243 244 245 246 247 248 249 250 |
# File 'lib/trail_guide/catalog.rb', line 242 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
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/trail_guide/catalog.rb', line 190 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
90 91 92 |
# File 'lib/trail_guide/catalog.rb', line 90 def groups experiments.map(&:groups).flatten.uniq end |
#import(state) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/trail_guide/catalog.rb', line 252 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', est['started_at']) if est['started_at'].present? TrailGuide.redis.hset(experiment.storage_key, 'paused_at', est['paused_at']) if est['paused_at'].present? TrailGuide.redis.hset(experiment.storage_key, 'stopped_at', est['stopped_at']) 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 |
#not_running ⇒ Object
138 139 140 |
# File 'lib/trail_guide/catalog.rb', line 138 def not_running self.class.new(to_a.select { |e| !e.running? }) end |
#orphaned(key, trace) ⇒ Object
280 281 282 283 284 285 286 |
# File 'lib/trail_guide/catalog.rb', line 280 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
288 289 290 291 292 293 294 |
# File 'lib/trail_guide/catalog.rb', line 288 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
122 123 124 |
# File 'lib/trail_guide/catalog.rb', line 122 def paused self.class.new(to_a.select { |e| e.paused? && !e.winner? }) end |
#register(klass) ⇒ Object
233 234 235 236 |
# File 'lib/trail_guide/catalog.rb', line 233 def register(klass) experiments << klass unless experiments.any? { |exp| exp == klass } klass end |
#respond_to_missing?(meth, include_private = false) ⇒ Boolean
307 308 309 |
# File 'lib/trail_guide/catalog.rb', line 307 def respond_to_missing?(meth, include_private=false) experiments.respond_to?(meth, include_private) end |
#running ⇒ Object
118 119 120 |
# File 'lib/trail_guide/catalog.rb', line 118 def running self.class.new(to_a.select { |e| e.running? && !e.winner? }) end |
#scheduled ⇒ Object
114 115 116 |
# File 'lib/trail_guide/catalog.rb', line 114 def scheduled self.class.new(to_a.select { |e| e.scheduled? && !e.winner? }) end |
#select(name) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/trail_guide/catalog.rb', line 211 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 self.class.new(selected) end |
#started ⇒ Object
110 111 112 |
# File 'lib/trail_guide/catalog.rb', line 110 def started self.class.new(to_a.select { |e| e.started? && !e.winner? }) end |
#stopped ⇒ Object
126 127 128 |
# File 'lib/trail_guide/catalog.rb', line 126 def stopped self.class.new(to_a.select { |e| e.stopped? && !e.winner? }) end |
#unstarted ⇒ Object
134 135 136 |
# File 'lib/trail_guide/catalog.rb', line 134 def unstarted self.class.new(to_a.select { |e| !e.started? && !e. && !e.scheduled? && !e.winner? }) end |