Skip to content

Commit

Permalink
test1
Browse files Browse the repository at this point in the history
  • Loading branch information
AvinashKapre committed Nov 18, 2024
1 parent ecd9db4 commit e434bbc
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 117 deletions.
71 changes: 15 additions & 56 deletions adapters/pubmatic/pubmatic.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"math"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"

Expand All @@ -24,22 +23,18 @@ import (
const MAX_IMPRESSIONS_PUBMATIC = 30
const MAX_MULTIFLOORS_PUBMATIC = 3

var re = regexp.MustCompile(appLovinMaxImpressionPattern)

const (
ae = "ae"
PUBMATIC = "[PUBMATIC]"
buyId = "buyid"
buyIdTargetingKey = "hb_buyid_"
skAdnetworkKey = "skadn"
rewardKey = "reward"
dctrKeywordName = "dctr"
urlEncodedEqualChar = "%3D"
AdServerKey = "adserver"
PBAdslotKey = "pbadslot"
bidViewability = "bidViewability"
multiFloors = "_mf"
appLovinMaxImpressionPattern = "_mf.*"
ae = "ae"
PUBMATIC = "[PUBMATIC]"
buyId = "buyid"
buyIdTargetingKey = "hb_buyid_"
skAdnetworkKey = "skadn"
rewardKey = "reward"
dctrKeywordName = "dctr"
urlEncodedEqualChar = "%3D"
AdServerKey = "adserver"
PBAdslotKey = "pbadslot"
bidViewability = "bidViewability"
)

type PubmaticAdapter struct {
Expand Down Expand Up @@ -647,7 +642,10 @@ func (a *PubmaticAdapter) MakeBids(internalRequest *openrtb2.BidRequest, externa
bid := sb.Bid[i]

//single bidresp => update, requestdata -> bid.ext.mbmf.floor, trim impid
bid.Ext = updateBidExtWithMultiFloor(bid.ImpID, bid.Ext, externalRequest.Body)
if re.MatchString(bid.ImpID) {
bid.Ext = updateBidExtWithMultiFloor(bid.ImpID, bid.Ext, externalRequest.Body)
trimSuffixWithPattern(bid.ImpID)
}

bid.Ext = renameTransparencyParamsKey(bid.Ext)
// Copy SeatBid Ext to Bid.Ext
Expand Down Expand Up @@ -711,10 +709,6 @@ func (a *PubmaticAdapter) MakeBids(internalRequest *openrtb2.BidRequest, externa
return bidResponse, errs
}

func trimSuffixWithPattern(input string) string {
return re.ReplaceAllString(input, "")
}

func getNativeAdm(adm string) (string, error) {
var err error
nativeAdm := make(map[string]interface{})
Expand Down Expand Up @@ -883,38 +877,3 @@ func getDisplayManagerAndVer(app *openrtb2.App) (string, string) {
}
return "", ""
}

func updateBidExtWithMultiFloor(bidImpID string, bidExt, reqBody []byte) []byte {
bidExtMap := getMapFromJSON(bidExt)
reqBodyMap := getMapFromJSON(reqBody)

if bidExtMap == nil {
bidExtMap = make(map[string]interface{})
}

if reqBodyMap == nil {
return bidExt
}

if imp, ok := reqBodyMap["imp"]; ok {
impMap := imp.([]interface{})
for _, impObj := range impMap {
impObjMap := impObj.(map[string]interface{})
if reqImpID, ok := impObjMap["id"]; ok && re.MatchString(reqImpID.(string)) {
if floor, ok := impObjMap["bidfloor"]; ok && reqImpID.(string) == bidImpID {
floorValue := floor.(float64)
if floorValue > 0 {
bidExtMap["mbmf"] = append(bidExtMap["mbmf"].([]interface{}), floorValue)
}
trimSuffixWithPattern(reqImpID.(string))
}
}
}
}

bidExt, err := json.Marshal(bidExtMap)
if err != nil {
return bidExt
}
return bidExt
}
44 changes: 43 additions & 1 deletion adapters/pubmatic/pubmatic_ow.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"regexp"
"strconv"

"github.com/buger/jsonparser"
Expand All @@ -21,6 +22,13 @@ var (
dsaParamKey = []byte(`"dsaparams"`)
)

var re = regexp.MustCompile(appLovinMaxImpressionPattern)

const (
multiFloors = "_mf"
appLovinMaxImpressionPattern = "_mf.*"
)

func getTargetingKeys(bidExt json.RawMessage, bidderName string) map[string]string {
targets := map[string]string{}
if bidExt != nil {
Expand Down Expand Up @@ -103,6 +111,40 @@ func renameTransparencyParamsKey(bidExt []byte) []byte {
return bidExt
}

func addFloorInBidExt(bidExt []byte, requestBody []byte) []byte {
func trimSuffixWithPattern(input string) string {
return re.ReplaceAllString(input, "")
}

func updateBidExtWithMultiFloor(bidImpID string, bidExt, reqBody []byte) []byte {
bidExtMap := getMapFromJSON(bidExt)
reqBodyMap := getMapFromJSON(reqBody)

if bidExtMap == nil {
bidExtMap = make(map[string]interface{})
}

if reqBodyMap == nil {
return bidExt
}

if imp, ok := reqBodyMap["imp"]; ok {
impMap := imp.([]interface{})
for _, impObj := range impMap {
impObjMap := impObj.(map[string]interface{})
if reqImpID, ok := impObjMap["id"]; ok {
if floor, ok := impObjMap["bidfloor"]; ok && reqImpID.(string) == bidImpID {
floorValue := floor.(float64)
if floorValue > 0 {
bidExtMap["mbmf"] = append(bidExtMap["mbmf"].([]interface{}), floorValue)
}
}
}
}
}

bidExt, err := json.Marshal(bidExtMap)
if err != nil {
return bidExt
}
return bidExt
}
53 changes: 53 additions & 0 deletions adapters/pubmatic/pubmatic_ow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,56 @@ func TestPubmaticMakeBids(t *testing.T) {
})
}
}

func TestTrimSuffixWithPattern(t *testing.T) {
type args struct {
input string
}
tests := []struct {
name string
args args
want string
}{
{
name: "input string is empty",
args: args{
input: "",
},
want: "",
},
{
name: "input string does not contain pattern",
args: args{
input: "div123456789",
},
want: "div123456789",
},
{
name: "input string contains pattern",
args: args{
input: "div123456789_mf1",
},
want: "div123456789",
},
{
name: "input string contains pattern at the end",
args: args{
input: "div123456789_mf1_mf2",
},
want: "div123456789",
},
{
name: "input string contains pattern at the start",
args: args{
input: "mf1_mf2_div123456789",
},
want: "mf1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := trimSuffixWithPattern(tt.args.input)
assert.Equal(t, tt.want, got)
})
}
}
53 changes: 0 additions & 53 deletions adapters/pubmatic/pubmatic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1550,59 +1550,6 @@ func TestPubmaticAdapter_buildAdapterRequest(t *testing.T) {
}
}

func TestTrimSuffixWithPattern(t *testing.T) {
type args struct {
input string
}
tests := []struct {
name string
args args
want string
}{
{
name: "input string is empty",
args: args{
input: "",
},
want: "",
},
{
name: "input string does not contain pattern",
args: args{
input: "div123456789",
},
want: "div123456789",
},
{
name: "input string contains pattern",
args: args{
input: "div123456789_mf1",
},
want: "div123456789",
},
{
name: "input string contains pattern at the end",
args: args{
input: "div123456789_mf1_mf2",
},
want: "div123456789",
},
{
name: "input string contains pattern at the start",
args: args{
input: "mf1_mf2_div123456789",
},
want: "mf1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := trimSuffixWithPattern(tt.args.input)
assert.Equal(t, tt.want, got)
})
}
}

func TestGetDisplayManagerAndVer(t *testing.T) {
type args struct {
app *openrtb2.App
Expand Down
12 changes: 10 additions & 2 deletions modules/pubmatic/openwrap/defaultbids.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,11 @@ func (m *OpenWrap) addDefaultBidsForMultiFloorsConfig(rctx *models.RequestCtx, b
for impID, impCtx := range rctx.ImpBidCtx {
count := len(rctx.AppLovinMax.MultiFloorsConfig.Config[impCtx.TagID]) - seatBidsMultiFloor[impID]
if _, ok := defaultBids[impID][models.BidderPubMatic]; ok {
count = count - len(defaultBids[impID][models.BidderPubMatic])
defaultBids[impID][models.BidderPubMatic] = make([]openrtb2.Bid, 0, count)
}
for i := 0; i < count; i++ {
uuid, _ := m.uuidGenerator.Generate()
bidExt := newDefaultBidExt(*rctx, impID, models.BidderPubMatic, bidResponseExt)
bidExt := newDefaultBidExtMultiFloors(*rctx, impID, i)
bidExtJson, _ := json.Marshal(bidExt)

defaultBids[impID][models.BidderPubMatic] = append(defaultBids[impID][models.BidderPubMatic], openrtb2.Bid{
Expand Down Expand Up @@ -258,6 +258,14 @@ func newDefaultBidExt(rctx models.RequestCtx, impID, bidder string, bidResponseE
return &bidExt
}

func newDefaultBidExtMultiFloors(rctx models.RequestCtx, impID string, index int) *models.BidExt {
return &models.BidExt{
NetECPM: 0,
Nbr: exchange.ErrorGeneral.Ptr(),
MultiBidMultiFloor: rctx.AppLovinMax.MultiFloorsConfig.Config[rctx.ImpBidCtx[impID].TagID][index],
}
}

// TODO : Check if we need this?
// func newDefaultVastTagBidExt(rctx models.RequestCtx, impID, bidder, vastTag string, bidResponseExt openrtb_ext.ExtBidResponse) *models.BidExt {
// bidExt := models.BidExt{
Expand Down
11 changes: 6 additions & 5 deletions modules/pubmatic/openwrap/models/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,12 @@ func GetBidLevelFloorsDetails(bidExt BidExt, impCtx ImpCtx,
var floorCurrency string
frv = NotSet

//Set the fv from bid.ext.mbmf if it is set
if bidExt.MultiBidMultiFloor > 0 {
fv = bidExt.MultiBidMultiFloor
return
}

if bidExt.Prebid != nil && bidExt.Prebid.Floors != nil {
floorCurrency = bidExt.Prebid.Floors.FloorCurrency
fv = RoundToTwoDigit(bidExt.Prebid.Floors.FloorValue)
Expand Down Expand Up @@ -405,11 +411,6 @@ func GetBidLevelFloorsDetails(bidExt BidExt, impCtx ImpCtx,
frv = 0 // set it back to 0
}

//Set the fv from bid.ext.mbmf if it is set
if bidExt.MultiBidMultiFloor > 0 {
fv = bidExt.MultiBidMultiFloor
}

return
}

Expand Down

0 comments on commit e434bbc

Please sign in to comment.