github.com/prebid/prebid-server@v0.275.0/exchange/price_granularity.go (about) 1 package exchange 2 3 import ( 4 "github.com/prebid/openrtb/v19/openrtb2" 5 "github.com/prebid/prebid-server/openrtb_ext" 6 "math" 7 "strconv" 8 ) 9 10 // GetPriceBucket is the externally facing function for computing CPM buckets 11 func GetPriceBucket(bid openrtb2.Bid, targetingData targetData) string { 12 cpmStr := "" 13 bucketMax := 0.0 14 bucketMin := 0.0 15 increment := 0.0 16 17 config := targetingData.priceGranularity //assign default price granularity 18 19 if bidType, err := getMediaTypeForBid(bid); err == nil { 20 if bidType == openrtb_ext.BidTypeBanner && targetingData.mediaTypePriceGranularity.Banner != nil { 21 config = *targetingData.mediaTypePriceGranularity.Banner 22 } else if bidType == openrtb_ext.BidTypeVideo && targetingData.mediaTypePriceGranularity.Video != nil { 23 config = *targetingData.mediaTypePriceGranularity.Video 24 } else if bidType == openrtb_ext.BidTypeNative && targetingData.mediaTypePriceGranularity.Native != nil { 25 config = *targetingData.mediaTypePriceGranularity.Native 26 } 27 } 28 29 precision := *config.Precision 30 31 cpm := bid.Price 32 for i := 0; i < len(config.Ranges); i++ { 33 if config.Ranges[i].Max > bucketMax { 34 bucketMax = config.Ranges[i].Max 35 } 36 // find what range cpm is in 37 if cpm >= config.Ranges[i].Min && cpm <= config.Ranges[i].Max { 38 increment = config.Ranges[i].Increment 39 bucketMin = config.Ranges[i].Min 40 } 41 } 42 43 if cpm > bucketMax { 44 // We are over max, just return that 45 cpmStr = strconv.FormatFloat(bucketMax, 'f', precision, 64) 46 } else if increment > 0 { 47 // If increment exists, get cpm string value 48 cpmStr = getCpmTarget(cpm, bucketMin, increment, precision) 49 } 50 51 return cpmStr 52 } 53 54 func getCpmTarget(cpm float64, bucketMin float64, increment float64, precision int) string { 55 roundedCPM := math.Floor((cpm-bucketMin)/increment)*increment + bucketMin 56 return strconv.FormatFloat(roundedCPM, 'f', precision, 64) 57 }