Skip to content

Commit

Permalink
blocking by creative ids (#53)
Browse files Browse the repository at this point in the history
* blocking by creative ids

* update blocking list
  • Loading branch information
shunj-nb authored Jul 25, 2023
1 parent fdf52af commit 45d84ac
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pkg/modules/blocking/core.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"encoding/json"

blocking "github.com/ParticleMedia/msp/pkg/modules/blocking/module"
"github.com/prebid/prebid-server/modules/moduledeps"
)

type builder struct{}

func (b builder) Build(rawConfig json.RawMessage, deps moduledeps.ModuleDeps) (interface{}, error) {
return blocking.New(), nil
}

var Builder builder
32 changes: 32 additions & 0 deletions pkg/modules/blocking/module/hook_raw_bidder_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package blocking

import (
"fmt"
"time"

"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/hooks/hookstage"
)

func handleRawBidderResponseHook(
creativeBlockingList map[string]int,
payload hookstage.RawBidderResponsePayload,
moduleCtx hookstage.ModuleContext,
) (result hookstage.HookResult[hookstage.RawBidderResponsePayload], err error) {
allowedBids := make([]*adapters.TypedBid, 0)
for _, bid := range payload.Bids {
if _, ok := creativeBlockingList[bid.Bid.CrID]; !ok {
allowedBids = append(allowedBids, bid)
} else if time.Now().Second()%100 < 20 {
fmt.Printf("blocking by createive id: %s, bidder: %s, sampled adm: %s \n", bid.Bid.CrID, payload.Bidder, bid.Bid.AdM)
}
}

changeSet := hookstage.ChangeSet[hookstage.RawBidderResponsePayload]{}
if len(payload.Bids) != len(allowedBids) {
changeSet.RawBidderResponse().Bids().Update(allowedBids)
result.ChangeSet = changeSet
}

return result, err
}
54 changes: 54 additions & 0 deletions pkg/modules/blocking/module/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package blocking

import (
"context"
"encoding/csv"
"log"
"os"

"github.com/golang/glog"
"github.com/prebid/prebid-server/hooks/hookstage"
)

const (
blocking_creative_file_path = "./static/blocking_creatives.csv"
)

type Module struct {
creativeBlockingList map[string]int
}

func New() *Module {
return &Module{
creativeBlockingList: parseBlockingCreativeMap(blocking_creative_file_path),
}
}

func parseBlockingCreativeMap(path string) map[string]int {
f, err := os.Open(path)
if err != nil {
glog.Fatal("Unable to read input file "+path, err)
}
defer f.Close()

csvReader := csv.NewReader(f)
records, err := csvReader.ReadAll()
if err != nil {
log.Fatal("Unable to parse file as CSV for "+path, err)
}

result := map[string]int{}
for _, col := range records {
result[col[0]] = 1
}

return result
}

func (m Module) HandleRawBidderResponseHook(
_ context.Context,
miCtx hookstage.ModuleInvocationContext,
payload hookstage.RawBidderResponsePayload,
) (hookstage.HookResult[hookstage.RawBidderResponsePayload], error) {
return handleRawBidderResponseHook(m.creativeBlockingList, payload, miCtx.ModuleContext)
}
2 changes: 2 additions & 0 deletions static/msp/blocking_creatives.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<adv>6bf6dff898144f7cb9326d1a1cd9029a<crid>4083164
4083164

0 comments on commit 45d84ac

Please sign in to comment.