Created
August 17, 2012 15:34
-
-
Save infectious/3379947 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 ad = tonumber(ARGV[1]) | |
local BL = 1 | |
local weight = 0 | |
-- bid price constant will be 50p : 500000 /1000 = 500 | |
-- give up bid price constant will be 10p : 100000/1000 = 100 | |
local give_up_bid = 100 | |
-- 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 | |
-- Calculate weight for pacing | |
if bid > give_up_bid 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