-
Notifications
You must be signed in to change notification settings - Fork 748
/
auction.go
365 lines (332 loc) · 11.3 KB
/
auction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package exchange
import (
"context"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"regexp"
"strings"
"time"
uuid "github.com/gofrs/uuid"
"github.com/mxmCherry/openrtb/v16/openrtb2"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/openrtb_ext"
"github.com/prebid/prebid-server/prebid_cache_client"
)
const (
DebugOverrideHeader string = "x-pbs-debug-override"
)
type DebugLog struct {
Enabled bool
CacheType prebid_cache_client.PayloadType
Data DebugData
TTL int64
CacheKey string
CacheString string
Regexp *regexp.Regexp
DebugOverride bool
//little optimization, it stores value of debugLog.Enabled || debugLog.DebugOverride
DebugEnabledOrOverridden bool
}
type DebugData struct {
Request string
Headers string
Response string
}
func (d *DebugLog) BuildCacheString() {
if d.Regexp != nil {
d.Data.Request = fmt.Sprintf(d.Regexp.ReplaceAllString(d.Data.Request, ""))
d.Data.Headers = fmt.Sprintf(d.Regexp.ReplaceAllString(d.Data.Headers, ""))
d.Data.Response = fmt.Sprintf(d.Regexp.ReplaceAllString(d.Data.Response, ""))
}
d.Data.Request = fmt.Sprintf("<Request>%s</Request>", d.Data.Request)
d.Data.Headers = fmt.Sprintf("<Headers>%s</Headers>", d.Data.Headers)
d.Data.Response = fmt.Sprintf("<Response>%s</Response>", d.Data.Response)
d.CacheString = fmt.Sprintf("%s<Log>%s%s%s</Log>", xml.Header, d.Data.Request, d.Data.Headers, d.Data.Response)
}
func IsDebugOverrideEnabled(debugHeader, configOverrideToken string) bool {
return configOverrideToken != "" && debugHeader == configOverrideToken
}
func (d *DebugLog) PutDebugLogError(cache prebid_cache_client.Client, timeout int, errors []error) error {
if len(d.Data.Response) == 0 && len(errors) == 0 {
d.Data.Response = "No response or errors created"
}
if len(errors) > 0 {
errStrings := []string{}
for _, err := range errors {
errStrings = append(errStrings, err.Error())
}
d.Data.Response = fmt.Sprintf("%s\nErrors:\n%s", d.Data.Response, strings.Join(errStrings, "\n"))
}
d.BuildCacheString()
if len(d.CacheKey) == 0 {
rawUUID, err := uuid.NewV4()
if err != nil {
return err
}
d.CacheKey = rawUUID.String()
}
data, err := json.Marshal(d.CacheString)
if err != nil {
return err
}
toCache := []prebid_cache_client.Cacheable{
{
Type: d.CacheType,
Data: data,
TTLSeconds: d.TTL,
Key: "log_" + d.CacheKey,
},
}
if cache != nil {
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Duration(timeout)*time.Millisecond))
defer cancel()
cache.PutJson(ctx, toCache)
}
return nil
}
func newAuction(seatBids map[openrtb_ext.BidderName]*pbsOrtbSeatBid, numImps int, preferDeals bool) *auction {
winningBids := make(map[string]*pbsOrtbBid, numImps)
winningBidsByBidder := make(map[string]map[openrtb_ext.BidderName]*pbsOrtbBid, numImps)
for bidderName, seatBid := range seatBids {
if seatBid != nil {
for _, bid := range seatBid.bids {
cpm := bid.bid.Price
wbid, ok := winningBids[bid.bid.ImpID]
if !ok || isNewWinningBid(bid.bid, wbid.bid, preferDeals) {
winningBids[bid.bid.ImpID] = bid
}
if bidMap, ok := winningBidsByBidder[bid.bid.ImpID]; ok {
bestSoFar, ok := bidMap[bidderName]
if !ok || cpm > bestSoFar.bid.Price {
bidMap[bidderName] = bid
}
} else {
winningBidsByBidder[bid.bid.ImpID] = make(map[openrtb_ext.BidderName]*pbsOrtbBid)
winningBidsByBidder[bid.bid.ImpID][bidderName] = bid
}
}
}
}
return &auction{
winningBids: winningBids,
winningBidsByBidder: winningBidsByBidder,
}
}
// isNewWinningBid calculates if the new bid (nbid) will win against the current winning bid (wbid) given preferDeals.
func isNewWinningBid(bid, wbid *openrtb2.Bid, preferDeals bool) bool {
if preferDeals {
if len(wbid.DealID) > 0 && len(bid.DealID) == 0 {
return false
}
if len(wbid.DealID) == 0 && len(bid.DealID) > 0 {
return true
}
}
return bid.Price > wbid.Price
}
func (a *auction) setRoundedPrices(priceGranularity openrtb_ext.PriceGranularity) {
roundedPrices := make(map[*pbsOrtbBid]string, 5*len(a.winningBids))
for _, topBidsPerImp := range a.winningBidsByBidder {
for _, topBidPerBidder := range topBidsPerImp {
roundedPrices[topBidPerBidder] = GetPriceBucket(topBidPerBidder.bid.Price, priceGranularity)
}
}
a.roundedPrices = roundedPrices
}
func (a *auction) doCache(ctx context.Context, cache prebid_cache_client.Client, targData *targetData, evTracking *eventTracking, bidRequest *openrtb2.BidRequest, ttlBuffer int64, defaultTTLs *config.DefaultTTLs, bidCategory map[string]string, debugLog *DebugLog) []error {
var bids, vast, includeBidderKeys, includeWinners bool = targData.includeCacheBids, targData.includeCacheVast, targData.includeBidderKeys, targData.includeWinners
if !((bids || vast) && (includeBidderKeys || includeWinners)) {
return nil
}
var errs []error
expectNumBids := valOrZero(bids, len(a.roundedPrices))
expectNumVast := valOrZero(vast, len(a.roundedPrices))
bidIndices := make(map[int]*openrtb2.Bid, expectNumBids)
vastIndices := make(map[int]*openrtb2.Bid, expectNumVast)
toCache := make([]prebid_cache_client.Cacheable, 0, expectNumBids+expectNumVast)
expByImp := make(map[string]int64)
competitiveExclusion := false
var hbCacheID string
if len(bidCategory) > 0 {
// assert: category of winning bids never duplicated
if rawUuid, err := uuid.NewV4(); err == nil {
hbCacheID = rawUuid.String()
competitiveExclusion = true
} else {
errs = append(errs, errors.New("failed to create custom cache key"))
}
}
// Grab the imp TTLs
for _, imp := range bidRequest.Imp {
expByImp[imp.ID] = imp.Exp
}
for _, topBidsPerImp := range a.winningBidsByBidder {
for bidderName, topBidPerBidder := range topBidsPerImp {
impID := topBidPerBidder.bid.ImpID
isOverallWinner := a.winningBids[impID] == topBidPerBidder
if !includeBidderKeys && !isOverallWinner {
continue
}
var customCacheKey string
var catDur string
useCustomCacheKey := false
if competitiveExclusion && isOverallWinner || includeBidderKeys {
// set custom cache key for winning bid when competitive exclusion applies
catDur = bidCategory[topBidPerBidder.bid.ID]
if len(catDur) > 0 {
customCacheKey = fmt.Sprintf("%s_%s", catDur, hbCacheID)
useCustomCacheKey = true
}
}
if bids {
if jsonBytes, err := json.Marshal(topBidPerBidder.bid); err == nil {
jsonBytes, err = evTracking.modifyBidJSON(topBidPerBidder, bidderName, jsonBytes)
if err != nil {
errs = append(errs, err)
}
if useCustomCacheKey {
// not allowed if bids is true; log error and cache normally
errs = append(errs, errors.New("cannot use custom cache key for non-vast bids"))
}
toCache = append(toCache, prebid_cache_client.Cacheable{
Type: prebid_cache_client.TypeJSON,
Data: jsonBytes,
TTLSeconds: cacheTTL(expByImp[impID], topBidPerBidder.bid.Exp, defTTL(topBidPerBidder.bidType, defaultTTLs), ttlBuffer),
})
bidIndices[len(toCache)-1] = topBidPerBidder.bid
} else {
errs = append(errs, err)
}
}
if vast && topBidPerBidder.bidType == openrtb_ext.BidTypeVideo {
vastXML := makeVAST(topBidPerBidder.bid)
if jsonBytes, err := json.Marshal(vastXML); err == nil {
if useCustomCacheKey {
toCache = append(toCache, prebid_cache_client.Cacheable{
Type: prebid_cache_client.TypeXML,
Data: jsonBytes,
TTLSeconds: cacheTTL(expByImp[impID], topBidPerBidder.bid.Exp, defTTL(topBidPerBidder.bidType, defaultTTLs), ttlBuffer),
Key: customCacheKey,
})
} else {
toCache = append(toCache, prebid_cache_client.Cacheable{
Type: prebid_cache_client.TypeXML,
Data: jsonBytes,
TTLSeconds: cacheTTL(expByImp[impID], topBidPerBidder.bid.Exp, defTTL(topBidPerBidder.bidType, defaultTTLs), ttlBuffer),
})
}
vastIndices[len(toCache)-1] = topBidPerBidder.bid
} else {
errs = append(errs, err)
}
}
}
}
if len(toCache) > 0 && debugLog != nil && debugLog.DebugEnabledOrOverridden {
debugLog.CacheKey = hbCacheID
debugLog.BuildCacheString()
if jsonBytes, err := json.Marshal(debugLog.CacheString); err == nil {
toCache = append(toCache, prebid_cache_client.Cacheable{
Type: debugLog.CacheType,
Data: jsonBytes,
TTLSeconds: debugLog.TTL,
Key: "log_" + debugLog.CacheKey,
})
}
}
ids, err := cache.PutJson(ctx, toCache)
if err != nil {
errs = append(errs, err...)
}
if bids {
a.cacheIds = make(map[*openrtb2.Bid]string, len(bidIndices))
for index, bid := range bidIndices {
if ids[index] != "" {
a.cacheIds[bid] = ids[index]
}
}
}
if vast {
a.vastCacheIds = make(map[*openrtb2.Bid]string, len(vastIndices))
for index, bid := range vastIndices {
if ids[index] != "" {
if competitiveExclusion && strings.HasSuffix(ids[index], hbCacheID) {
// omit the pb_cat_dur_ portion of cache ID
a.vastCacheIds[bid] = hbCacheID
} else {
a.vastCacheIds[bid] = ids[index]
}
}
}
}
return errs
}
// makeVAST returns some VAST XML for the given bid. If AdM is defined,
// it takes precedence. Otherwise the Nurl will be wrapped in a redirect tag.
func makeVAST(bid *openrtb2.Bid) string {
if bid.AdM == "" {
return `<VAST version="3.0"><Ad><Wrapper>` +
`<AdSystem>prebid.org wrapper</AdSystem>` +
`<VASTAdTagURI><![CDATA[` + bid.NURL + `]]></VASTAdTagURI>` +
`<Impression></Impression><Creatives></Creatives>` +
`</Wrapper></Ad></VAST>`
}
return bid.AdM
}
func valOrZero(useVal bool, val int) int {
if useVal {
return val
}
return 0
}
func cacheTTL(impTTL int64, bidTTL int64, defTTL int64, buffer int64) (ttl int64) {
if impTTL <= 0 && bidTTL <= 0 {
// Only use default if there is no imp nor bid TTL provided. We don't want the default
// to cut short a requested longer TTL.
return addBuffer(defTTL, buffer)
}
if impTTL <= 0 {
// Use <= to handle the case of someone sending a negative ttl. We treat it as zero
return addBuffer(bidTTL, buffer)
}
if bidTTL <= 0 {
return addBuffer(impTTL, buffer)
}
if impTTL < bidTTL {
return addBuffer(impTTL, buffer)
}
return addBuffer(bidTTL, buffer)
}
func addBuffer(base int64, buffer int64) int64 {
if base <= 0 {
return 0
}
return base + buffer
}
func defTTL(bidType openrtb_ext.BidType, defaultTTLs *config.DefaultTTLs) (ttl int64) {
switch bidType {
case openrtb_ext.BidTypeBanner:
return int64(defaultTTLs.Banner)
case openrtb_ext.BidTypeVideo:
return int64(defaultTTLs.Video)
case openrtb_ext.BidTypeNative:
return int64(defaultTTLs.Native)
case openrtb_ext.BidTypeAudio:
return int64(defaultTTLs.Audio)
}
return 0
}
type auction struct {
// winningBids is a map from imp.id to the highest overall CPM bid in that imp.
winningBids map[string]*pbsOrtbBid
// winningBidsByBidder stores the highest bid on each imp by each bidder.
winningBidsByBidder map[string]map[openrtb_ext.BidderName]*pbsOrtbBid
// roundedPrices stores the price strings rounded for each bid according to the price granularity.
roundedPrices map[*pbsOrtbBid]string
// cacheIds stores the UUIDs from Prebid Cache for fetching the full bid JSON.
cacheIds map[*openrtb2.Bid]string
// vastCacheIds stores UUIDS from Prebid cache for fetching the VAST markup to video bids.
vastCacheIds map[*openrtb2.Bid]string
}