Sha256: a09db30e86a813828108c7d9abd39cf626861db28143031132a59e8480567302

Contents?: true

Size: 1.05 KB

Versions: 12

Compression:

Stored size: 1.05 KB

Contents

module Neuron
  module Client
    module ZoneCalculations
      # This module expects the following methods to be defined:
      #
      # ad_links (Hash, keys are ad IDs, values are a sub-hash: {'priority' => p, 'weight' => w})
      # find_ad(ad_id) (nil, or an object that responds to :active? and :pressure)

      def ads_by_priority
        calculate_ads_by_priority
      end

      def calculate_ads_by_priority
        entries = {}
        ad_links.each do |ad_id, link|
          next unless ad = find_ad(ad_id)
          next unless ad.active?
          weight   = link['weight'].to_f
          priority = link['priority'].to_f
          entries[priority] ||= []
          entries[priority] << [ad_id, weighted_pressure(weight, ad.pressure)]
        end
        entries.sort_by do |priority, entry|
          priority
        end.map do |priority, entry|
          entry.sort_by(&:first)
        end
      end

      private

      def weighted_pressure(weight, pressure)
        [( [weight, 0.0].max * [pressure, 1.0].max ), 1.0].max.to_f
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
neuron-client-0.5.1 lib/neuron-client/model/zone_calculations.rb
neuron-client-0.5.0 lib/neuron-client/model/zone_calculations.rb
neuron-client-0.4.8 lib/neuron-client/model/zone_calculations.rb
neuron-client-0.4.7 lib/neuron-client/model/zone_calculations.rb
neuron-client-0.4.6 lib/neuron-client/model/zone_calculations.rb
neuron-client-0.4.5 lib/neuron-client/model/zone_calculations.rb
neuron-client-0.4.4 lib/neuron-client/model/zone_calculations.rb
neuron-client-0.4.3 lib/neuron-client/model/zone_calculations.rb
neuron-client-0.4.2 lib/neuron-client/model/zone_calculations.rb
neuron-client-0.4.1 lib/neuron-client/model/zone_calculations.rb
neuron-client-0.4.0 lib/neuron-client/model/zone_calculations.rb
neuron-client-0.3.0 lib/neuron-client/model/zone_calculations.rb