Class: Conductor::Weights

Inherits:
Object
  • Object
show all
Defined in:
lib/conductor/weights.rb

Class Method Summary collapse

Class Method Details

.compute(group_name, alternatives) ⇒ Object

Computes the weights for a group based on the attribute for weighting and activity for the inclusion period.

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
48
49
50
51
52
53
# File 'lib/conductor/weights.rb', line 34

def compute(group_name, alternatives)
  Conductor::Experiment::Weight.delete_all(:group_name => group_name)
  
  data = []
  equalization_period_data = Conductor::Experiment::Daily.find_equalization_period_stats_for(group_name, alternatives)
  post_equalization_data = Conductor::Experiment::Daily.find_post_equalization_period_stats_for(group_name, alternatives)

  # handle all post_equalization_data
  max_weight = 0
  unless post_equalization_data.empty?
    total = post_equalization_data.sum_it(Conductor.attribute_for_weighting)
    data = (total > 0) ? compute_weights(post_equalization_data, total, max_weight) : assign_equal_weights(post_equalization_data)
  end

  # add weights for recently launched
  weight_recently_launched(data, max_weight, equalization_period_data) unless equalization_period_data.empty?
  
  # add to database
  update_weights_in_db(group_name, data)
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