github.com/prebid/prebid-server/v2@v2.18.0/openrtb_ext/floors.go (about) 1 package openrtb_ext 2 3 import ( 4 "github.com/prebid/prebid-server/v2/util/maputil" 5 "github.com/prebid/prebid-server/v2/util/ptrutil" 6 "github.com/prebid/prebid-server/v2/util/sliceutil" 7 ) 8 9 // Defines strings for FetchStatus 10 const ( 11 FetchSuccess = "success" 12 FetchTimeout = "timeout" 13 FetchError = "error" 14 FetchInprogress = "inprogress" 15 FetchNone = "none" 16 ) 17 18 // Defines strings for PriceFloorLocation 19 const ( 20 NoDataLocation = "noData" 21 RequestLocation = "request" 22 FetchLocation = "fetch" 23 ) 24 25 // PriceFloorRules defines the contract for bidrequest.ext.prebid.floors 26 type PriceFloorRules struct { 27 FloorMin float64 `json:"floormin,omitempty"` 28 FloorMinCur string `json:"floormincur,omitempty"` 29 SkipRate int `json:"skiprate,omitempty"` 30 Location *PriceFloorEndpoint `json:"floorendpoint,omitempty"` 31 Data *PriceFloorData `json:"data,omitempty"` 32 Enforcement *PriceFloorEnforcement `json:"enforcement,omitempty"` 33 Enabled *bool `json:"enabled,omitempty"` 34 Skipped *bool `json:"skipped,omitempty"` 35 FloorProvider string `json:"floorprovider,omitempty"` 36 FetchStatus string `json:"fetchstatus,omitempty"` 37 PriceFloorLocation string `json:"location,omitempty"` 38 } 39 40 // GetEnforcePBS will check if floors enforcement is enabled in request 41 func (Floors *PriceFloorRules) GetEnforcePBS() bool { 42 if Floors != nil && Floors.Enforcement != nil && Floors.Enforcement.EnforcePBS != nil { 43 return *Floors.Enforcement.EnforcePBS 44 } 45 return true 46 } 47 48 // GetFloorsSkippedFlag will return floors skipped flag 49 func (Floors *PriceFloorRules) GetFloorsSkippedFlag() bool { 50 if Floors != nil && Floors.Skipped != nil { 51 return *Floors.Skipped 52 } 53 return false 54 } 55 56 // GetEnforceRate will return enforcement rate in request 57 func (Floors *PriceFloorRules) GetEnforceRate() int { 58 if Floors != nil && Floors.Enforcement != nil { 59 return Floors.Enforcement.EnforceRate 60 } 61 return 0 62 } 63 64 // GetEnforceDealsFlag will return FloorDeals flag in request 65 func (Floors *PriceFloorRules) GetEnforceDealsFlag() bool { 66 if Floors != nil && Floors.Enforcement != nil && Floors.Enforcement.FloorDeals != nil { 67 return *Floors.Enforcement.FloorDeals 68 } 69 return false 70 } 71 72 // GetEnabled will check if floors is enabled in request 73 func (Floors *PriceFloorRules) GetEnabled() bool { 74 if Floors != nil && Floors.Enabled != nil { 75 return *Floors.Enabled 76 } 77 return true 78 } 79 80 type PriceFloorEndpoint struct { 81 URL string `json:"url,omitempty"` 82 } 83 84 type PriceFloorData struct { 85 Currency string `json:"currency,omitempty"` 86 SkipRate int `json:"skiprate,omitempty"` 87 FloorsSchemaVersion int `json:"floorsschemaversion,omitempty"` 88 ModelTimestamp int `json:"modeltimestamp,omitempty"` 89 ModelGroups []PriceFloorModelGroup `json:"modelgroups,omitempty"` 90 FloorProvider string `json:"floorprovider,omitempty"` 91 FetchRate *int `json:"fetchrate,omitempty"` 92 } 93 94 type PriceFloorModelGroup struct { 95 Currency string `json:"currency,omitempty"` 96 ModelWeight *int `json:"modelweight,omitempty"` 97 ModelVersion string `json:"modelversion,omitempty"` 98 SkipRate int `json:"skiprate,omitempty"` 99 Schema PriceFloorSchema `json:"schema,omitempty"` 100 Values map[string]float64 `json:"values,omitempty"` 101 Default float64 `json:"default,omitempty"` 102 } 103 104 func (mg PriceFloorModelGroup) Copy() PriceFloorModelGroup { 105 newMg := new(PriceFloorModelGroup) 106 newMg.Currency = mg.Currency 107 newMg.ModelVersion = mg.ModelVersion 108 newMg.SkipRate = mg.SkipRate 109 newMg.Default = mg.Default 110 if mg.ModelWeight != nil { 111 newMg.ModelWeight = new(int) 112 *newMg.ModelWeight = *mg.ModelWeight 113 } 114 115 newMg.Schema.Delimiter = mg.Schema.Delimiter 116 newMg.Schema.Fields = make([]string, len(mg.Schema.Fields)) 117 copy(newMg.Schema.Fields, mg.Schema.Fields) 118 newMg.Values = make(map[string]float64, len(mg.Values)) 119 for key, val := range mg.Values { 120 newMg.Values[key] = val 121 } 122 return *newMg 123 } 124 125 type PriceFloorSchema struct { 126 Fields []string `json:"fields,omitempty"` 127 Delimiter string `json:"delimiter,omitempty"` 128 } 129 130 type PriceFloorEnforcement struct { 131 EnforceJS *bool `json:"enforcejs,omitempty"` 132 EnforcePBS *bool `json:"enforcepbs,omitempty"` 133 FloorDeals *bool `json:"floordeals,omitempty"` 134 BidAdjustment *bool `json:"bidadjustment,omitempty"` 135 EnforceRate int `json:"enforcerate,omitempty"` 136 } 137 138 type ImpFloorExt struct { 139 FloorRule string `json:"floorrule,omitempty"` 140 FloorRuleValue float64 `json:"floorrulevalue,omitempty"` 141 FloorValue float64 `json:"floorvalue,omitempty"` 142 } 143 type Price struct { 144 FloorMin float64 `json:"floormin,omitempty"` 145 FloorMinCur string `json:"floormincur,omitempty"` 146 } 147 148 type ExtImp struct { 149 Prebid *ImpExtPrebid `json:"prebid,omitempty"` 150 } 151 152 type ImpExtPrebid struct { 153 Floors Price `json:"floors,omitempty"` 154 } 155 156 func (pf *PriceFloorRules) DeepCopy() *PriceFloorRules { 157 if pf == nil { 158 return nil 159 } 160 161 newRules := *pf 162 newRules.Enabled = ptrutil.Clone(pf.Enabled) 163 newRules.Skipped = ptrutil.Clone(pf.Skipped) 164 newRules.Location = ptrutil.Clone(pf.Location) 165 newRules.Data = pf.Data.DeepCopy() 166 newRules.Enforcement = pf.Enforcement.DeepCopy() 167 168 return &newRules 169 } 170 171 func (data *PriceFloorData) DeepCopy() *PriceFloorData { 172 if data == nil { 173 return nil 174 } 175 176 newData := *data 177 newModelGroups := make([]PriceFloorModelGroup, len(data.ModelGroups)) 178 179 for i := range data.ModelGroups { 180 var eachGroup PriceFloorModelGroup 181 eachGroup.Currency = data.ModelGroups[i].Currency 182 eachGroup.ModelWeight = ptrutil.Clone(data.ModelGroups[i].ModelWeight) 183 eachGroup.ModelVersion = data.ModelGroups[i].ModelVersion 184 eachGroup.SkipRate = data.ModelGroups[i].SkipRate 185 eachGroup.Values = maputil.Clone(data.ModelGroups[i].Values) 186 eachGroup.Default = data.ModelGroups[i].Default 187 eachGroup.Schema = PriceFloorSchema{ 188 Fields: sliceutil.Clone(data.ModelGroups[i].Schema.Fields), 189 Delimiter: data.ModelGroups[i].Schema.Delimiter, 190 } 191 newModelGroups[i] = eachGroup 192 } 193 newData.ModelGroups = newModelGroups 194 195 return &newData 196 } 197 198 func (enforcement *PriceFloorEnforcement) DeepCopy() *PriceFloorEnforcement { 199 if enforcement == nil { 200 return nil 201 } 202 203 newEnforcement := *enforcement 204 newEnforcement.EnforceJS = ptrutil.Clone(enforcement.EnforceJS) 205 newEnforcement.EnforcePBS = ptrutil.Clone(enforcement.EnforcePBS) 206 newEnforcement.FloorDeals = ptrutil.Clone(enforcement.FloorDeals) 207 newEnforcement.BidAdjustment = ptrutil.Clone(enforcement.BidAdjustment) 208 209 return &newEnforcement 210 }