Class: Sinatra::AbingoObject::Experiment

Inherits:
Object
  • Object
show all
Includes:
Abingo::ConversionRate, Abingo::Statistics, DataMapper::Resource
Defined in:
lib/sinmetrics/abingo/experiment.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.start_experiment!(abingo, test_name, alternatives_array) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sinmetrics/abingo/experiment.rb', line 41

def self.start_experiment!(abingo, test_name, alternatives_array)
  cloned_alternatives_array = alternatives_array.clone
  Abingo::Experiment.transaction do |txn|
    experiment = Abingo::Experiment.first_or_create(:test_name => test_name)
    experiment.alternatives.destroy  #Blows away alternatives for pre-existing experiments.
    while (cloned_alternatives_array.size > 0)
      alt = cloned_alternatives_array[0]
      weight = cloned_alternatives_array.size - (cloned_alternatives_array - [alt]).size
      experiment.alternatives << Abingo::Alternative.new(:content => alt, :weight => weight,
        :lookup => abingo.calculate_alternative_lookup(test_name, alt))
      cloned_alternatives_array -= [alt]
    end
    experiment.status = "Live"
    experiment.save
    experiment
  end
end

Instance Method Details

#before_destroyObject



19
20
21
22
23
24
# File 'lib/sinmetrics/abingo/experiment.rb', line 19

def before_destroy
  cache_keys.each do |key|
    Abingo.cache.delete key
  end
  true
end

#best_alternativeObject



34
35
36
37
38
39
# File 'lib/sinmetrics/abingo/experiment.rb', line 34

def best_alternative
  alts = Array.new alternatives.each { |a| a }
  alts.max do |a,b|
    a.conversion_rate <=> b.conversion_rate
  end
end

#cache_keysObject



12
13
14
15
16
17
# File 'lib/sinmetrics/abingo/experiment.rb', line 12

def cache_keys
["Abingo::Experiment::exists(#{test_name})".gsub(" ", "_"),
  "Abingo::Experiment::#{test_name}::alternatives".gsub(" ","_"),
  "Abingo::Experiment::short_circuit(#{test_name})".gsub(" ", "_")
]
end

#conversionsObject



30
31
32
# File 'lib/sinmetrics/abingo/experiment.rb', line 30

def conversions
  alternatives.sum(:conversions)
end

#end_experiment!(abingo, final_alternative) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/sinmetrics/abingo/experiment.rb', line 59

def end_experiment!(abingo, final_alternative)
  Abingo::Experiment.transaction do
    alternatives.each do |alternative|
      alternative.lookup = "Experiment completed.  #{alternative.id}"
      alternative.save!
    end
    update(:status => "Finished", :short_circuit => final_alternative)
  end
end

#participantsObject



26
27
28
# File 'lib/sinmetrics/abingo/experiment.rb', line 26

def participants
  alternatives.sum(:participants)
end