Skip to content

Commit 96646e4

Browse files
Merged in yhhuang@refactor-is-timeout-helper-function (pull request prebid#22)
refactor: extract timeout logic to a helper function
2 parents 1312827 + 59ac5fc commit 96646e4

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

modules/appierAnalyticsAdapter.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ const cacheManager = {
9090
const bidsCacheSection = this.cache[auctionId].bids.adUnits;
9191
Object.assign(bidsCacheSection[adUnitCode][bidderCode], newObject)
9292
},
93-
getStatus(auctionId, adUnitCode) {
93+
getAuctionStatus(auctionId, adUnitCode) {
9494
return this.cache[auctionId].status[adUnitCode];
9595
},
96-
setStatus(auctionId, adUnitCode, newValue) {
96+
setAuctionStatus(auctionId, adUnitCode, newValue) {
9797
this.cache[auctionId].status[adUnitCode] = newValue;
9898
},
9999
setAdCache(auctionId, adUnitCode, bidderCode, newObject) {
@@ -105,6 +105,11 @@ const cacheManager = {
105105
const bidsWonCacheSection = this.cache[auctionId].bidsWon.adUnits;
106106
bidsWonCacheSection[adUnitCode] = bidsWonCacheSection[adUnitCode] || {};
107107
bidsWonCacheSection[adUnitCode][bidderCode] = newObject;
108+
},
109+
isBidderTimeout(auctionId, adUnitCode, bidderCode) {
110+
const bid = cacheManager.getBidsCache(auctionId, adUnitCode, bidderCode);
111+
return this.getAuctionStatus(auctionId, adUnitCode) === AUCTION_STATUS.COMPLETED ||
112+
bid.status === BIDDER_STATUS.TIMEOUT
108113
}
109114
};
110115

@@ -180,7 +185,7 @@ export const appierAnalyticsAdapter = Object.assign(adapter({DEFAULT_SERVER, ana
180185
bidderRequest.bids.forEach(function (bid) {
181186
const adUnitCode = parseAdUnitCode(bid);
182187
cacheManager.initBidsCache(auction.auctionId, adUnitCode, bidderCode);
183-
cacheManager.setStatus(auction.auctionId, adUnitCode, AUCTION_STATUS.IN_PROGRESS);
188+
cacheManager.setAuctionStatus(auction.auctionId, adUnitCode, AUCTION_STATUS.IN_PROGRESS);
184189
});
185190
});
186191
},
@@ -198,27 +203,24 @@ export const appierAnalyticsAdapter = Object.assign(adapter({DEFAULT_SERVER, ana
198203
const bidderCode = parseBidderCode(bidMessage);
199204
bidMessage.bids.forEach(function (bid) {
200205
const adUnitCode = parseAdUnitCode(bid);
201-
const cachedBid = cacheManager.getBidsCache(bid.auctionId, adUnitCode, bidderCode);
202206
cacheManager.updateBidsCache(bid.auctionId, adUnitCode, bidderCode, {
203-
'isTimeout': cacheManager.getStatus(bid.auctionId, adUnitCode) === AUCTION_STATUS.COMPLETED ||
204-
cachedBid.status === BIDDER_STATUS.TIMEOUT
207+
'isTimeout': cacheManager.isBidderTimeout(bid.auctionId, adUnitCode, bidderCode)
205208
});
206209
});
207210
},
208211
handleBidResponseMessage(bid) {
209212
const adUnitCode = parseAdUnitCode(bid);
210213
const bidderCode = parseBidderCode(bid);
211-
const cachedBid = cacheManager.getBidsCache(bid.auctionId, adUnitCode, bidderCode);
214+
const isBidderTimeout = cacheManager.isBidderTimeout(bid.auctionId, adUnitCode, bidderCode);
212215
cacheManager.updateBidsCache(bid.auctionId, adUnitCode, bidderCode, {
213216
'time': bid.timeToRespond,
214-
'status': (cachedBid.status === BIDDER_STATUS.TIMEOUT) ? BIDDER_STATUS.TIMEOUT : BIDDER_STATUS.BID,
217+
'status': (isBidderTimeout) ? BIDDER_STATUS.TIMEOUT : BIDDER_STATUS.BID,
215218
'cpm': bid.cpm,
216219
'currency': bid.currency,
217220
'originalCpm': bid.originalCpm || bid.cpm,
218221
'cpmUsd': getCpmInUsd(bid),
219222
'originalCurrency': bid.originalCurrency || bid.currency,
220-
'isTimeout': cacheManager.getStatus(bid.auctionId, adUnitCode) === AUCTION_STATUS.COMPLETED ||
221-
cachedBid.status === BIDDER_STATUS.TIMEOUT,
223+
'isTimeout': isBidderTimeout,
222224
'prebidWon': false
223225
});
224226
},
@@ -257,15 +259,13 @@ export const appierAnalyticsAdapter = Object.assign(adapter({DEFAULT_SERVER, ana
257259
// Update cached auction status
258260
auction.adUnits.forEach(function (adUnit) {
259261
const adUnitCode = adUnit.code.toLowerCase();
260-
cacheManager.setStatus(auction.auctionId, adUnitCode, AUCTION_STATUS.COMPLETED);
262+
cacheManager.setAuctionStatus(auction.auctionId, adUnitCode, AUCTION_STATUS.COMPLETED);
261263
});
262264
// Update no-bids in bidCache
263265
auction.noBids.forEach(function (noBid) {
264266
const adUnitCode = parseAdUnitCode(noBid);
265267
const bidderCode = parseBidderCode(noBid);
266-
const cachedBid = cacheManager.getBidsCache(auction.auctionId, adUnitCode, bidderCode);
267-
if (cachedBid.status === BIDDER_STATUS.TIMEOUT ||
268-
cacheManager.getStatus(auction.auctionId, adUnitCode) === AUCTION_STATUS.COMPLETED) {
268+
if (cacheManager.isBidderTimeout(auction.auctionId, adUnitCode, bidderCode)) {
269269
cacheManager.updateBidsCache(auction.auctionId, adUnitCode, bidderCode, {
270270
'status': BIDDER_STATUS.TIMEOUT,
271271
'isTimeout': true

test/spec/modules/appierAnalyticsAdapter_spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ describe('Appier Prebid AnalyticsAdapter', function () {
504504
'adUnits': {
505505
'/12345678/adunit_1': {
506506
'appier': {
507-
'status': 'bid',
507+
'status': 'timeout',
508508
'isTimeout': true,
509509
'time': 600,
510510
'cpm': 0.59878,

0 commit comments

Comments
 (0)