Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auction module refactor #1644

Merged
merged 2 commits into from
Oct 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions src/auction.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,25 @@ events.on(CONSTANTS.EVENTS.BID_ADJUSTMENT, function (bid) {
*
* @returns {Auction} auction instance
*/
function newAuction({adUnits, adUnitCodes}) {
export function newAuction({adUnits, adUnitCodes, callback, cbTimeout}) {
let _adUnits = adUnits;
let _adUnitCodes = adUnitCodes;
let _bidderRequests = [];
let _bidsReceived = [];
let _auctionStart;
let _auctionId = utils.getUniqueIdentifierStr();
let _auctionStatus;
let _callback;
let _callback = callback;
let _timer;
let _timeout = cbTimeout;

function addBidRequests(bidderRequests) { _bidderRequests = _bidderRequests.concat(bidderRequests) };
function addBidReceived(bidsReceived) { _bidsReceived = _bidsReceived.concat(bidsReceived); }

function startAuctionTimer(callback, cbtimeout) {
_callback = callback;
function startAuctionTimer() {
const timedOut = true;
const timeoutCallback = executeCallback.bind(null, timedOut);
let timer = setTimeout(timeoutCallback, cbtimeout);
let timer = setTimeout(timeoutCallback, _timeout);
_timer = timer;
}

Expand Down Expand Up @@ -301,7 +301,7 @@ function newAuction({adUnits, adUnitCodes}) {
}

function doCallbacksIfNeeded() {
if (bid.timeToRespond > $$PREBID_GLOBAL$$.cbTimeout + $$PREBID_GLOBAL$$.timeoutBuffer) {
if (bid.timeToRespond > _timeout + $$PREBID_GLOBAL$$.timeoutBuffer) {
executeCallback(true);
}
}
Expand Down Expand Up @@ -334,18 +334,19 @@ function newAuction({adUnits, adUnitCodes}) {
}
}

function callBids(cbTimeout) {
function callBids() {
startAuctionTimer();
_auctionStatus = AUCTION_STARTED;
_auctionStart = Date.now();

const auctionInit = {
timestamp: _auctionStart,
auctionId: _auctionId,
timeout: cbTimeout
timeout: _timeout
};
events.emit(CONSTANTS.EVENTS.AUCTION_INIT, auctionInit);

let bidRequests = adaptermanager.makeBidRequests(_adUnits, _auctionStart, _auctionId, cbTimeout);
let bidRequests = adaptermanager.makeBidRequests(_adUnits, _auctionStart, _auctionId, _timeout);
utils.logInfo(`Bids Requested for Auction with id: ${_auctionId}`, bidRequests);
bidRequests.forEach(bidRequest => {
addBidRequests(bidRequest);
Expand All @@ -361,7 +362,6 @@ function newAuction({adUnits, adUnitCodes}) {
getAdUnitCodes: () => _adUnitCodes,
getBidRequests: () => _bidderRequests,
getBidsReceived: () => _bidsReceived,
startAuctionTimer,
callBids
}
}
Expand Down Expand Up @@ -513,7 +513,3 @@ function groupByPlacement(bidsByPlacement, bid) {
bidsByPlacement[bid.adUnitCode].bids.push(bid);
return bidsByPlacement;
}

export function createAuction({adUnits, adUnitCodes}) {
return newAuction({adUnits, adUnitCodes});
}
14 changes: 5 additions & 9 deletions src/auctionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

import { uniques, flatten } from './utils';
import { createAuction, getStandardBidderSettings, AUCTION_COMPLETED } from 'src/auction';
import { newAuction, getStandardBidderSettings, AUCTION_COMPLETED } from 'src/auction';

const CONSTANTS = require('./constants.json');

Expand Down Expand Up @@ -60,8 +60,10 @@ export function newAuctionManager() {
.filter(uniques);
};

_public.createAuction = function({ adUnits, adUnitCodes }) {
return _createAuction({ adUnits, adUnitCodes });
_public.createAuction = function({ adUnits, adUnitCodes, callback, cbTimeout }) {
const auction = newAuction({ adUnits, adUnitCodes, callback, cbTimeout })
_addAuction(auction);
return auction;
};

_public.findBidByAdId = function(adId) {
Expand All @@ -74,12 +76,6 @@ export function newAuctionManager() {
return getStandardBidderSettings()[CONSTANTS.JSON_MAPPING.ADSERVER_TARGETING];
};

function _createAuction({ adUnits, adUnitCodes }) {
const auction = createAuction({ adUnits, adUnitCodes })
_addAuction(auction);
return auction;
}

function _addAuction(auction) {
_auctions.push(auction);
}
Expand Down
12 changes: 3 additions & 9 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ $$PREBID_GLOBAL$$.bidderSettings = $$PREBID_GLOBAL$$.bidderSettings || {};
/** @deprecated - use pbjs.setConfig({ bidderTimeout: <timeout> }) */
$$PREBID_GLOBAL$$.bidderTimeout = $$PREBID_GLOBAL$$.bidderTimeout;

// current timeout set in `requestBids` or to default `bidderTimeout`
$$PREBID_GLOBAL$$.cbTimeout = $$PREBID_GLOBAL$$.cbTimeout || 200;

// timeout buffer to adjust for bidder CDN latency
$$PREBID_GLOBAL$$.timeoutBuffer = 200;

Expand Down Expand Up @@ -308,7 +305,7 @@ $$PREBID_GLOBAL$$.removeAdUnit = function (adUnitCode) {
*/
$$PREBID_GLOBAL$$.requestBids = function ({ bidsBackHandler, timeout, adUnits, adUnitCodes } = {}) {
events.emit('requestBids');
const cbTimeout = $$PREBID_GLOBAL$$.cbTimeout = timeout || config.getConfig('bidderTimeout');
const cbTimeout = timeout || config.getConfig('bidderTimeout');
adUnits = adUnits || $$PREBID_GLOBAL$$.adUnits;

utils.logInfo('Invoking $$PREBID_GLOBAL$$.requestBids', arguments);
Expand Down Expand Up @@ -354,11 +351,8 @@ $$PREBID_GLOBAL$$.requestBids = function ({ bidsBackHandler, timeout, adUnits, a
return;
}

const auction = auctionManager.createAuction({adUnits, adUnitCodes});
if (typeof bidsBackHandler === 'function') {
auction.startAuctionTimer(bidsBackHandler, cbTimeout);
}
auction.callBids(cbTimeout);
const auction = auctionManager.createAuction({adUnits, adUnitCodes, callback: bidsBackHandler, cbTimeout});
auction.callBids();
};

/**
Expand Down
38 changes: 19 additions & 19 deletions test/spec/auctionmanager_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,8 @@ describe('auctionmanager.js', function () {
]
}];
adUnitCodes = ['adUnit-code'];
auction = auctionModule.createAuction({adUnits, adUnitCodes});
createAuctionStub = sinon.stub(auctionModule, 'createAuction');
auction = auctionModule.newAuction({adUnits, adUnitCodes, callback: function() {}, cbTimeout: 3000});
createAuctionStub = sinon.stub(auctionModule, 'newAuction');
createAuctionStub.returns(auction);

spec = {
Expand All @@ -484,7 +484,7 @@ describe('auctionmanager.js', function () {
});

afterEach(() => {
auctionModule.createAuction.restore();
auctionModule.newAuction.restore();
});

it('should return proper price bucket increments for dense mode when cpm is in range 0-3', () => {
Expand All @@ -493,7 +493,7 @@ describe('auctionmanager.js', function () {
spec.buildRequests.returns([{'id': 123, 'method': 'POST'}]);
spec.isBidRequestValid.returns(true);
spec.interpretResponse.returns(bids);
auction.callBids(3000);
auction.callBids();
let registeredBid = auction.getBidsReceived().pop();
assert.equal(registeredBid.pbDg, '1.99', '0 - 3 hits at to 1 cent increment');
});
Expand All @@ -504,7 +504,7 @@ describe('auctionmanager.js', function () {
spec.buildRequests.returns([{'id': 123, 'method': 'POST'}]);
spec.isBidRequestValid.returns(true);
spec.interpretResponse.returns(bids);
auction.callBids(3000);
auction.callBids();
let registeredBid = auction.getBidsReceived().pop();
assert.equal(registeredBid.pbDg, '4.35', '3 - 8 hits at 5 cent increment');
});
Expand All @@ -515,7 +515,7 @@ describe('auctionmanager.js', function () {
spec.buildRequests.returns([{'id': 123, 'method': 'POST'}]);
spec.isBidRequestValid.returns(true);
spec.interpretResponse.returns(bids);
auction.callBids(3000);
auction.callBids();
let registeredBid = auction.getBidsReceived().pop();
assert.equal(registeredBid.pbDg, '19.50', '8 - 20 hits at 50 cent increment');
});
Expand All @@ -526,7 +526,7 @@ describe('auctionmanager.js', function () {
spec.buildRequests.returns([{'id': 123, 'method': 'POST'}]);
spec.isBidRequestValid.returns(true);
spec.interpretResponse.returns(bids);
auction.callBids(3000);
auction.callBids();
let registeredBid = auction.getBidsReceived().pop();
assert.equal(registeredBid.pbDg, '20.00', '20+ caps at 20.00');
});
Expand All @@ -537,7 +537,7 @@ describe('auctionmanager.js', function () {
spec.buildRequests.returns([{'id': 123, 'method': 'POST'}]);
spec.isBidRequestValid.returns(true);
spec.interpretResponse.returns(bids);
auction.callBids(3000);
auction.callBids();
let registeredBid = auction.getBidsReceived().pop();
assert.equal(registeredBid.adserverTargeting[`hb_deal`], 'test deal', 'dealId placed in adserverTargeting');
});
Expand All @@ -549,7 +549,7 @@ describe('auctionmanager.js', function () {
spec.buildRequests.returns([{'id': 123, 'method': 'POST'}]);
spec.isBidRequestValid.returns(true);
spec.interpretResponse.returns(bids);
auction.callBids(3000);
auction.callBids();
let registeredBid = auction.getBidsReceived().pop();
assert.equal(registeredBid.adserverTargeting.hb_bidder, 'sampleBidder');
assert.equal(registeredBid.adserverTargeting.extra, 'stuff');
Expand Down Expand Up @@ -578,7 +578,7 @@ describe('auctionmanager.js', function () {
spec.buildRequests.returns([{'id': 123, 'method': 'POST'}]);
spec.isBidRequestValid.returns(true);
spec.interpretResponse.returns(bids1);
auction.callBids(3000);
auction.callBids();
assert.equal(bidsRecCount + 1, auction.getBidsReceived().length);

utils.getBidRequest.restore();
Expand Down Expand Up @@ -608,7 +608,7 @@ describe('auctionmanager.js', function () {
spec.buildRequests.returns([{'id': 123, 'method': 'POST'}]);
spec.isBidRequestValid.returns(true);
spec.interpretResponse.returns(bids1);
auction.callBids(3000);
auction.callBids();
assert.equal(bidsRecCount, auction.getBidsReceived().length);

utils.getBidRequest.restore();
Expand Down Expand Up @@ -640,7 +640,7 @@ describe('auctionmanager.js', function () {
spec.buildRequests.returns([{'id': 123, 'method': 'POST'}]);
spec.isBidRequestValid.returns(true);
spec.interpretResponse.returns(bids1);
auction.callBids(3000);
auction.callBids();
assert.equal(bidsRecCount + 1, auction.getBidsReceived().length);

utils.getBidRequest.restore();
Expand Down Expand Up @@ -685,7 +685,7 @@ describe('auctionmanager.js', function () {
spec.buildRequests.returns([{'id': 123, 'method': 'POST'}]);
spec.isBidRequestValid.returns(true);
spec.interpretResponse.returns(bids1);
auction.callBids(3000);
auction.callBids();
const addedBid = auction.getBidsReceived().pop();
assert.equal(addedBid.renderer.url, 'renderer.js');
});
Expand Down Expand Up @@ -729,7 +729,7 @@ describe('auctionmanager.js', function () {
spec.buildRequests.returns([{'id': 123, 'method': 'POST'}]);
spec.isBidRequestValid.returns(true);
spec.interpretResponse.returns(bids1);
auction.callBids(3000);
auction.callBids();
const addedBid = auction.getBidsReceived().pop();

assert.equal(addedBid.width, 300);
Expand Down Expand Up @@ -836,8 +836,8 @@ describe('auctionmanager.js', function () {
]
}];
adUnitCodes = ['adUnit-code', 'adUnit-code-1'];
auction = auctionModule.createAuction({adUnits, adUnitCodes});
createAuctionStub = sinon.stub(auctionModule, 'createAuction');
auction = auctionModule.newAuction({adUnits, adUnitCodes, callback: function() {}, cbTimeout: 3000});
createAuctionStub = sinon.stub(auctionModule, 'newAuction');
createAuctionStub.returns(auction);

spec = {
Expand All @@ -858,7 +858,7 @@ describe('auctionmanager.js', function () {
});

afterEach(() => {
auctionModule.createAuction.restore();
auctionModule.newAuction.restore();
});

it('should not alter bid adID', () => {
Expand All @@ -873,7 +873,7 @@ describe('auctionmanager.js', function () {
spec1.isBidRequestValid.returns(true);
spec1.interpretResponse.returns(bids1);

auction.callBids(3000);
auction.callBids();

const addedBid2 = auction.getBidsReceived().pop();
assert.equal(addedBid2.adId, bids1[0].requestId);
Expand All @@ -896,7 +896,7 @@ describe('auctionmanager.js', function () {
spec1.isBidRequestValid.returns(true);
spec1.interpretResponse.returns(bids1);

auction.callBids(3000);
auction.callBids();

let length = auction.getBidsReceived().length;
const addedBid2 = auction.getBidsReceived().pop();
Expand Down
Loading