Class: TrailGuide::Catalog

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/trail_guide/catalog.rb

Defined Under Namespace

Classes: DSL

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#combinedObject (readonly)

Returns the value of attribute combined.



83
84
85
# File 'lib/trail_guide/catalog.rb', line 83

def combined
  @combined
end

#experimentsObject (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

.catalogObject



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, options|
    expvars = options[:variants].map do |var|
      if var.is_a?(Array)
        [var[0], var[1].symbolize_keys]
      else
        [var]
      end
    end

    expgoals = options[: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                    = options[:control] if options[:control]
      config.groups                     = options[:groups] if options[:groups]
      config.algorithm                  = options[:algorithm] if options[:algorithm]
      config.combined                   = options[:combined] if options[:combined]
      config.reset_manually             = options[:reset_manually] if options.key?(:reset_manually)
      config.start_manually             = options[:start_manually] if options.key?(:start_manually)
      config.store_override             = options[:store_override] if options.key?(:store_override)
      config.track_override             = options[:track_override] if options.key?(:track_override)
      config.allow_multiple_conversions = options[:allow_multiple_conversions] if options.key?(:allow_multiple_conversions)
      config.allow_multiple_goals       = options[:allow_multiple_goals] if options.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

#allObject



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_startedObject



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

#calibratingObject



119
120
121
# File 'lib/trail_guide/catalog.rb', line 119

def calibrating
  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

#endedObject



143
144
145
# File 'lib/trail_guide/catalog.rb', line 143

def ended
  new(to_a.select(&:winner?), @combined)
end

#exportObject



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

#groupsObject



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

#missingObject



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_runningObject



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

#orphansObject



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

#pausedObject



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

Returns:

  • (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

#runningObject



131
132
133
# File 'lib/trail_guide/catalog.rb', line 131

def running
  new(to_a.select { |e| e.running? && !e.winner? }, @combined)
end

#scheduledObject



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

#startedObject



123
124
125
# File 'lib/trail_guide/catalog.rb', line 123

def started
  new(to_a.select { |e| e.started? && !e.winner? }, @combined)
end

#stoppedObject



139
140
141
# File 'lib/trail_guide/catalog.rb', line 139

def stopped
  new(to_a.select { |e| e.stopped? && !e.winner? }, @combined)
end

#unstartedObject



147
148
149
# File 'lib/trail_guide/catalog.rb', line 147

def unstarted
  new(to_a.select { |e| !e.started? && !e.calibrating? && !e.scheduled? && !e.winner? }, @combined)
end