Created
August 24, 2012 15:35
-
-
Save infectious/3452057 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local XB = 100000 -- give up bid threshold = 10p | |
local BL = 1 | |
local ad = tonumber(ARGV[1]) | |
local weight = 0 | |
-- Check if there is a weight | |
if tonumber(redis.call('zscore', 'ad:weights', ad)) == nil then | |
return nil | |
end | |
-- Retrieve attributes | |
local bid, bids, wins, stake = unpack(redis.call('hmget', "ad::" .. ad, "bid", "bids", "wins", "stake")) | |
-- Pure paranoia: ensure we get number back | |
bid = tonumber(bid) or 0 | |
bids = tonumber(bids) or 0 | |
wins = tonumber(wins) or 0 | |
stake = tonumber(stake) or 0 | |
if stake < 0 then stake = 0 end | |
if bids < 1 then bids = 1 end | |
-- Calculate weight for pacing | |
if bid > XB then | |
weight = math.floor( BL * stake/(bid) * bids/(wins+0.0001) + 0.5 ) | |
end | |
-- Store weight | |
redis.call('zadd', 'ad:weights', weight, ad) | |
return weight |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment