Skip to content

Commit

Permalink
Emit array of objects from BID_TIMEOUT event (#1824)
Browse files Browse the repository at this point in the history
* Emit array of objects from BID_TIMEOUT event

* requestId is now auctionId

* Use v4 UUID to match previous requestId format

* Move function
  • Loading branch information
matthewlane authored and jaiminpanchal27 committed Nov 21, 2017
1 parent 45dc8aa commit 6b387f3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
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 @@ -860,7 +860,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

0 comments on commit 6b387f3

Please sign in to comment.