Class: Conductor::Weights
- Inherits:
-
Object
- Object
- Conductor::Weights
- Defined in:
- lib/conductor/weights.rb
Class Method Summary collapse
-
.compute(group_name, alternatives) ⇒ Object
Computes the weights for a group based on the attribute for weighting and activity for the last two weeks.
-
.find_or_create(group_name, alternatives) ⇒ Object
Returns all the weights for a given group.
Class Method Details
.compute(group_name, alternatives) ⇒ Object
Computes the weights for a group based on the attribute for weighting and activity for the last two weeks.
If no conversions have taken place yet for a group, all alternatives are weighted equally.
TODO: add notification table and all notification if there are no conversions and we are out of the equalization period
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/conductor/weights.rb', line 34 def compute(group_name, alternatives) # create the conditions after sanitizing sql. alternative_filter = alternatives.inject([]) {|res,x| res << "alternative = '#{Conductor.sanitize(x)}'"}.join(' OR ') # pull daily data and recompute if daily data group_rows = Conductor::Experiment::Daily.since(14.days.ago).for_group(group_name).find(:all, :conditions => alternative_filter) unless group_rows.empty? Conductor::Experiment::Weight.delete_all(:group_name => group_name) # => remove all old data for group total = group_rows.sum_it(Conductor.attribute_for_weighting) data = total > 0 ? compute_weights_for_group(group_name, group_rows, total) : assign_equal_weights(group_rows) update_weights_in_db(group_name, data) end end |
.find_or_create(group_name, alternatives) ⇒ Object
Returns all the weights for a given group. In the event that the alternatives specified for the group do not match all the alternatives previously computed for the group, new weights are generated. The cache is used to speed up this check
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/conductor/weights.rb', line 8 def find_or_create(group_name, alternatives) weights_for_group = Conductor.cache.read("Conductor::Experiment::#{Conductor.attribute_for_weighting}::#{group_name}::Alternatives") alternatives_array = weights_for_group.map(&:alternative).sort if weights_for_group if alternatives_array.eql?(alternatives.sort) Conductor.log('alternatives equal to cache') return weights_for_group else # Conductor.log('alternatives NOT equal to cache. Need to recompute') compute(group_name, alternatives) # get the new weights weights_for_group = Conductor::Experiment::Weight.find(:all, :conditions => "group_name = '#{group_name}'") Conductor.cache.delete("Conductor::Experiment::#{Conductor.attribute_for_weighting}::#{group_name}::Alternatives") Conductor.cache.write("Conductor::Experiment::#{Conductor.attribute_for_weighting}::#{group_name}::Alternatives", weights_for_group) return weights_for_group end end |