Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New basic functionalities added #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/com/jacamars/dsp/rtb/bidder/CampaignProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public class CampaignProcessor implements Runnable {
/** The logging object */
static final Logger logger = LoggerFactory.getLogger(CampaignProcessor.class);

/** Map to check creative caps */
Map<String, String> capSpecs;

/**
* Constructor.
*
Expand All @@ -79,12 +82,12 @@ public class CampaignProcessor implements Runnable {
* @param br
* . BidRequest. The bid request to apply to this campaign.
*/
public CampaignProcessor(Campaign camp, BidRequest br, CountDownLatch flag,
AbortableCountDownLatch latch) {
public CampaignProcessor(Campaign camp, BidRequest br, Map<String, String> capSpecs, CountDownLatch flag, AbortableCountDownLatch latch) {
this.camp = camp;
this.br = br;
this.latch = latch;
this.flag = flag;
this.capSpecs = capSpecs;

if (latch != null)
start();
Expand Down Expand Up @@ -181,7 +184,7 @@ public void run() {
for (int i=0; i<creatives.size();i++) {
Creative create = creatives.get(i);
SelectedCreative sc = null;
if ((sc = create.process(br, camp.adId, err, probe)) != null) {
if ((sc = create.process(br, camp.adId, err, probe, capSpecs)) != null) {
sc.campaign = this.camp;
probe.process(br.getExchange(), camp.adId, sc.impid);
selected.add(sc);
Expand Down
8 changes: 7 additions & 1 deletion src/com/jacamars/dsp/rtb/bidder/CampaignSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public BidResponse getMaxConnections(BidRequest br) throws Exception {
SelectionWorker w = workers.get(i);
candidates.addAll(w.candidates);
frequencyCap = w.frequencyCap;
// TODO POSSIBLE BUG. W.FREQUENCYCAP SHOULD BE ADDALL INSTEAD OF =
}


Expand Down Expand Up @@ -268,7 +269,7 @@ public void run() {
e.printStackTrace();
}
} else {
CampaignProcessor p = new CampaignProcessor(test, br, null, null);
CampaignProcessor p = new CampaignProcessor(test, br, capSpecs, null, null);
p.run();

select = p.getSelectedCreative();
Expand All @@ -290,6 +291,11 @@ public void run() {

for (int ii = 0; ii < select.size(); ii++) {
candidates.add(select.get(ii));
if(select.get(ii).creative.frequencyCap != null){
FrequencyCap f = select.get(ii).creative.frequencyCap.copy();
f.capKey = capSpecs.get(select.get(ii).campaign.adId.concat("B").concat(select.get(ii).creative.impid).concat("_"));
frequencyCap.add(f);
}
}
if (!(br.multibid || test.weights == null)) {
flag.setValue(true);
Expand Down
43 changes: 29 additions & 14 deletions src/com/jacamars/dsp/rtb/common/Creative.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.io.JsonStringEncoder;
Expand Down Expand Up @@ -143,6 +144,8 @@ public class Creative {

/** A sorter for the campaign/creative attributes, who is most likely to cause a false will bubble up */
private SortNodesFalseCount nodeSorter = new SortNodesFalseCount();

public FrequencyCap frequencyCap = null;


/**
Expand Down Expand Up @@ -178,6 +181,7 @@ public Creative copy() {
c.imageurl = imageurl;
c.dimensions = dimensions;
c.categories = categories;
c.frequencyCap = frequencyCap;

c.encodeUrl();
return c;
Expand Down Expand Up @@ -303,17 +307,6 @@ void encodeUrl() {
//strH = Integer.toString(h);
strPrice = Double.toString(price);

if (extensions != null) {
String cat = extensions.get("categories");
if (cat != null) {
categories = new ArrayList();
String[] cats = cat.split(",");
for (String c : cats) {
categories.add(c.trim());
}
}
}

/**
* Always contain these!
*/
Expand Down Expand Up @@ -523,7 +516,7 @@ public String getUnencodedNativeAdm(BidRequest br) {
* @return boolean. Returns true of this campaign matches the bid request,
* ie eligible to bid
*/
public SelectedCreative process(BidRequest br, String adId, StringBuilder errorString , Probe probe) throws Exception {
public SelectedCreative process(BidRequest br, String adId, StringBuilder errorString , Probe probe, Map<String, String> capSpecs) throws Exception {

/**
* Fixed nodes do not access deals or the br impressions
Expand All @@ -544,7 +537,7 @@ public SelectedCreative process(BidRequest br, String adId, StringBuilder errorS

for (int i=0; i<n;i++) {
imp = br.getImpression(i);
SelectedCreative cr = xproc(br,adId,imp,errorString, probe);
SelectedCreative cr = xproc(br,adId,imp,errorString, probe, capSpecs);
if (cr != null) {
cr.setImpression(imp);
return cr;
Expand All @@ -553,7 +546,7 @@ public SelectedCreative process(BidRequest br, String adId, StringBuilder errorS
return null;
}

public SelectedCreative xproc(BidRequest br, String adId, Impression imp, StringBuilder errorString, Probe probe) throws Exception {
public SelectedCreative xproc(BidRequest br, String adId, Impression imp, StringBuilder errorString, Probe probe, Map<String, String> capSpecs) throws Exception {
//List<Deal> newDeals = null;
String dealId = null;
double xprice = price;
Expand Down Expand Up @@ -616,6 +609,15 @@ public SelectedCreative xproc(BidRequest br, String adId, Impression imp, String
}
}

// Creative frequency cap check
if (isCapped(br, capSpecs,adId)) {
if (errorString != null) {
errorString.append("Creative capped by frequency cap ");
}
probe.process(br.getExchange(), adId, impid, Probe.FREQUENCY_CAPPED_CREATIVE);
return null;
}

return new SelectedCreative(this, dealId, xprice, impid);
}

Expand Down Expand Up @@ -697,4 +699,17 @@ public Node findAttribute(String hierarchy) {
}
return null;
}

/**
* Is this creative capped on the item in this bid request?
* @param br BidRequest. The bid request to query.
* @param capSpecs Map. The current cap spec.
* @return boolean. Returns true if the item is capped, else false.
*/
public boolean isCapped(BidRequest br, Map<String, String> capSpecs, String adId) {
if (frequencyCap == null)
return false;
return frequencyCap.isCapped(br,capSpecs,adId.concat("B").concat(impid).concat("_"));
}

}
14 changes: 13 additions & 1 deletion src/com/jacamars/dsp/rtb/pojo/BidResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -718,8 +718,20 @@ public void makeResponse(double price) throws Exception {
} else {
response.append(getTemplate());
}
response.append('"');

// Categories
if (creat.categories != null && creat.categories.size() > 0) {
response.append(",\"cat\":[");
for (int i=0;i<creat.categories.size();i++) {
response.append("\"").append(creat.categories.get(i)).append("\"");
if (i+1 < creat.categories.size())
response.append(",");
}
response.append("]");
}

response.append("\"}]}],");
response.append("}]}],");
response.append("\"id\":\"");
response.append(oidStr); // backwards?
response.append("\",\"bidid\":\"");
Expand Down
1 change: 1 addition & 0 deletions src/com/jacamars/dsp/rtb/probe/Probe.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class Probe {
public static final String VIDEO_MIME = new String("Video Creative mime type mismatch\n");
public static final String CREATIVE_MISMATCH = new String("Creative mismatch: ");
public static final String FREQUENCY_CAPPED = new String("Frequency capped\n");
public static final String FREQUENCY_CAPPED_CREATIVE = new String("Creative frequency capped\n");
public static final String FREQUENCY_GOVERNED = new String("Frequency governed\n");
public static final String CREATIVE_NOTACTIVE = new String("Creative is not in active state\n");
public static final String WRONG_EXCHANGE = new String("Wrong exchange\n");
Expand Down
4 changes: 2 additions & 2 deletions src/com/jacamars/dsp/rtb/redisson/RedissonClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.slf4j.LoggerFactory;

/**
* A Replacement for the Redisson object. This class is a serialized (JSON) interface to the Aerospike/Cache2k database.
* A Replacement for the JEDIS object. This class is a serialized (JSON) interface to the Aerospike/Cache2k database.
*
* @author Ben M. Faul
*/
Expand Down Expand Up @@ -321,7 +321,7 @@ public Map hgetAll(String id) {
}

/**
* Mimic a REDIS mhset operation.
* Mimic a REDIS hmset operation.
*
* @param id String. The key of the map.
* @param m Map. The map to set.
Expand Down
41 changes: 36 additions & 5 deletions src/com/jacamars/dsp/rtb/tools/MacroProcessing.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ public class MacroProcessing {
macroList.add("%7Bcreative_ad_width%7D");
macroList.add("{creative_ad_width_enc}");

macroList.add("{creative_ad_height}");
macroList.add("%7Bcreative_ad_height%7D");
macroList.add("{creative_ad_height_enc}");


macroList.add("{impression_width}");
macroList.add("%7Bimpression_width%7D");
macroList.add("{impression_width_enc}");
Expand Down Expand Up @@ -211,6 +206,14 @@ public class MacroProcessing {
macroList.add("{publisher_enc}");
macroList.add("%7Bpublisher%7D");

macroList.add("{publisher_id}");
macroList.add("{publisher_id_enc}");
macroList.add("%7Bpublisher_id%7D");

macroList.add("{publisher_name}");
macroList.add("{publisher_name_enc}");
macroList.add("%7Bpublisher_name%7D");

macroList.add("{adsize}");
macroList.add("{adsize_enc}");
macroList.add("%7Badsize%7D");
Expand Down Expand Up @@ -660,6 +663,34 @@ public static void replace(List<String> list, BidRequest br, Creative creat, Imp
replaceAll(sb, item, value);
break;

case "{publisher_id}":
case "{publisher_id_enc}":
case "%7Bpublisher_id%7D":
o = br.interrogate("site.publisher.id");
if (o == null)
o = br.interrogate("app.publisher.id");
if (o != null)
value = BidRequest.getStringFrom(o);
else
value = "";
value = encodeIfRequested(item, value);
replaceAll(sb, item, value);
break;

case "{publisher_name}":
case "{publisher_name_enc}":
case "%7Bpublisher_name%7D":
o = br.interrogate("site.publisher.name");
if (o == null)
o = br.interrogate("app.publisher.name");
if (o != null)
value = BidRequest.getStringFrom(o);
else
value = "";
value = encodeIfRequested(item, value);
replaceAll(sb, item, value);
break;

case "{adsize}":
case "{adsize_enc}":
case "%7Badsize%7D":
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/TestCampaignProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void testNoCampaigns() throws Exception {

AbortableCountDownLatch latch = new AbortableCountDownLatch(1,1);
CountDownLatch flag = new CountDownLatch(1);
CampaignProcessor proc = new CampaignProcessor(null,request,flag,latch);
CampaignProcessor proc = new CampaignProcessor(null,request, null,flag,latch);
flag.countDown();
proc.run();
List<SelectedCreative> resp = proc.getSelectedCreative();
Expand Down Expand Up @@ -95,7 +95,7 @@ public void testOneMatching() throws Exception {

AbortableCountDownLatch latch = new AbortableCountDownLatch(1,1);
CountDownLatch flag = new CountDownLatch(1);
CampaignProcessor proc = new CampaignProcessor(c,request, flag, latch);
CampaignProcessor proc = new CampaignProcessor(c,request, null, flag, latch);
flag.countDown();
latch.await();
List<SelectedCreative> resp = proc.getSelectedCreative();
Expand Down