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

Emit array of objects from BID_TIMEOUT event #1824

Merged
merged 4 commits into from
Nov 21, 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
60 changes: 42 additions & 18 deletions src/auction.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
* @property {function(): void} callBids - sends requests to all adapters for bids
*/

import { uniques, timestamp, adUnitsFilter, delayExecution, getBidderRequest } from './utils';
import { uniques, flatten, timestamp, adUnitsFilter, delayExecution, getBidderRequest } from './utils';
import { getPriceBucketString } from './cpmBucketManager';
import { NATIVE_KEYS } from './native';
import { getCacheUrl, store } from './videoCache';
Expand Down Expand Up @@ -88,7 +88,7 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels})
let _bidderRequests = [];
let _bidsReceived = [];
let _auctionStart;
let _auctionId = utils.getUniqueIdentifierStr();
let _auctionId = utils.generateUUID();
let _auctionStatus;
let _callback = callback;
let _timer;
Expand Down Expand Up @@ -132,28 +132,14 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels})

if (timedOut) {
utils.logMessage(`Auction ${_auctionId} timedOut`);
const timedOutBidders = getTimedOutBidders();
const timedOutBidders = getTimedOutBids(_bidderRequests, _bidsReceived);
if (timedOutBidders.length) {
events.emit(CONSTANTS.EVENTS.BID_TIMEOUT, { timedOutBidders, auctionId: _auctionId });
events.emit(CONSTANTS.EVENTS.BID_TIMEOUT, timedOutBidders);
}
}
}
}

function getTimedOutBidders() {
return _bidderRequests
.map((bidSet) => {
return bidSet.bidderCode;
})
.filter(uniques)
.filter(bidder => _bidsReceived
.map((bid) => {
return bid.bidder;
})
.filter(uniques)
.indexOf(bidder) < 0);
};

function done(bidRequestId) {
var innerBidRequestId = bidRequestId;
return delayExecution(function() {
Expand Down Expand Up @@ -458,3 +444,41 @@ function groupByPlacement(bidsByPlacement, bid) {
bidsByPlacement[bid.adUnitCode].bids.push(bid);
return bidsByPlacement;
}

/**
* Returns a list of bids that we haven't received a response yet
* @param {BidRequest[]} bidderRequests List of bids requested for auction instance
* @param {BidReceived[]} bidsReceived List of bids received for auction instance
*
* @typedef {Object} TimedOutBid
* @property {string} bidId The id representing the bid
* @property {string} bidder The string name of the bidder
* @property {string} adUnitCode The code used to uniquely identify the ad unit on the publisher's page
* @property {string} auctionId The id representing the auction
*
* @return {Array<TimedOutBid>} List of bids that Prebid hasn't received a response for
*/
function getTimedOutBids(bidderRequests, bidsReceived) {
const bidRequestedCodes = bidderRequests
.map(bid => bid.bidderCode)
.filter(uniques);

const bidReceivedCodes = bidsReceived
.map(bid => bid.bidder)
.filter(uniques);

const timedOutBidderCodes = bidRequestedCodes
.filter(bidder => !bidReceivedCodes.includes(bidder));

const timedOutBids = bidderRequests
.map(bid => (bid.bids || []).filter(bid => timedOutBidderCodes.includes(bid.bidder)))
.reduce(flatten, [])
.map(bid => ({
bidId: bid.bidId,
bidder: bid.bidder,
adUnitCode: bid.adUnitCode,
auctionId: bid.auctionId,
}));

return timedOutBids;
}
2 changes: 1 addition & 1 deletion test/spec/unit/pbjs_api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ describe('Unit: Prebid Module', function () {
};

$$PREBID_GLOBAL$$.requestBids(requestObj);
let re = new RegExp('^Auction [0-9A-Za-z]+ timedOut$');
let re = new RegExp('^Auction [a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12} timedOut$');
clock.tick(requestObj.timeout - 1);
assert.ok(logMessageSpy.neverCalledWith(sinon.match(re)), 'executeCallback not called');

Expand Down