use of com.xrtb.common.Campaign in project XRTB by benmfaul.
the class WebCampaign method updateCampaign.
/**
* Updates a command in the database (NOT in the currently running list)
*
* @param cmd
* Map. The web user command map.
* @return String. JSON representation of the running campaigns.
*/
public String updateCampaign(Map cmd) throws Exception {
Map response = new HashMap();
try {
String name = (String) cmd.get("username");
String id = getString(cmd.get("id"));
id = id.replaceAll("\"", "");
String data = (String) cmd.get("campaign");
Campaign c = new Campaign(data);
System.out.println(data);
db.editCampaign(name, c);
List<String> deletions = (List<String>) cmd.get("deletions");
for (String s : deletions) {
db.deleteCampaign(name, s);
}
dumpFile(cmd);
if (Configuration.getInstance().isRunning(name, id)) {
AddCampaign command = new AddCampaign(null, name, id);
command.to = "*";
command.from = Configuration.getInstance().instanceName;
}
c = db.getCampaign(name, id);
Controller.getInstance().addCampaign(c);
Controller.getInstance().sendLog(3, "WebAccess-Update-Campaign", name + " Modified campaign: " + id);
Controller.getInstance().sendLog(3, "WebAccess-Start-Campaign", "Campaign start: " + id);
} catch (Exception error) {
error.printStackTrace();
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
error.printStackTrace(pw);
response.put("message", "Incomplete construction, failed: " + sw.toString());
response.put("error", true);
}
response.put("running", Configuration.getInstance().getLoadedCampaignNames());
return getString(response);
}
use of com.xrtb.common.Campaign in project XRTB by benmfaul.
the class WebCampaign method doNewCampaign.
/**
* Adds a new campaign to the cache/aerospike.
*
* @param m
* Map. The command parameters.
* @return JSON. The results of the add.
* @throws Exception
* on JSON or cache errors.
*/
private String doNewCampaign(Map m) throws Exception {
Map response = new HashMap();
String who = (String) m.get("username");
User u = db.getUser(who);
if (u == null) {
response.put("message", "No user " + who);
return getString(response);
}
String name = (String) m.get("username");
String id = (String) m.get("campaign");
Controller.getInstance().sendLog(3, "WebAccess-New-Campaign", who + " added a new campaign: " + id);
try {
if (db.getCampaign(name, id) != null) {
response.put("error", true);
response.put("message", "Error, campaign by that name is already defined");
return getString(response);
}
Campaign c = db.createStub(name, id);
db.editCampaign(name, c);
response.put("campaign", c);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
response.put("error", true);
response.put("message", "Error creating campaign: " + e.toString());
}
return getString(response);
}
use of com.xrtb.common.Campaign in project XRTB by benmfaul.
the class BidRequest method compile.
/**
* Take the union of all campaign attributes and place them into the static
* mapp. This way the JSON is queried once and the query becomes the key,
* and the JSON value becomes the map value. With multiple campaigns it is
* important to not be traversing the JSON tree for each campaign.
*
* The compiled attributes are stored in mapp. In setup, the compiled list
* of key/values is then put in the 'database' object for the bidrequest.
*/
public static synchronized void compile() throws Exception {
RTB4FREE = false;
/**
* Stop the bidder, if it is running
*/
stopBidder();
keys.clear();
mapp.clear();
List<Campaign> list = Configuration.getInstance().campaignsList;
for (int i = 0; i < list.size(); i++) {
Campaign c = list.get(i);
Controller.getInstance().sendLog(5, "BidRequest:compile", ("Compiling for domain: : " + c.adomain));
for (int j = 0; j < c.attributes.size(); j++) {
Node node = c.attributes.get(j);
if (mapp.containsKey(keys) == false) {
Controller.getInstance().sendLog(5, "BidRequest:compile", ("Compile unit: " + c.adomain + ":" + node.hierarchy) + ", values: " + node.bidRequestValues);
if (node.hierarchy.equals("") == false) {
keys.add(node.hierarchy);
mapp.put(node.hierarchy, node.bidRequestValues);
} else {
if (node.operator != Node.OR) {
startBidder();
throw new Exception("Malformed OR processing in campaign " + c.adId);
}
List<Node> nodes = (List<Node>) node.value;
for (int nc = 0; nc < nodes.size(); nc++) {
Object x = nodes.get(nc);
Node n = null;
if (x instanceof LinkedHashMap) {
Map map = (Map) x;
n = new Node(map);
} else
n = (Node) x;
n.setValues();
if (mapp.get(n.hierarchy) == null) {
keys.add(n.hierarchy);
mapp.put(n.hierarchy, n.bidRequestValues);
}
}
}
}
}
for (Creative creative : c.creatives) {
// Handle creative specific
// attributes
Controller.getInstance().sendLog(5, "BidRequest:compile", "Compiling creatives for: " + c.adomain + ":" + creative.impid);
for (Node node : creative.attributes) {
if (mapp.containsKey(keys) == false) {
Controller.getInstance().sendLog(5, "BidRequest:compile", ("Compile unit: " + c.adomain + ":" + creative.impid + ":" + node.hierarchy) + ", values: " + node.bidRequestValues);
if (mapp.get(node.hierarchy) == null) {
keys.add(node.hierarchy);
mapp.put(node.hierarchy, node.bidRequestValues);
}
}
}
// Now frequency caps */
if (creative.capSpecification != null) {
String spec = creative.capSpecification;
if (mapp.containsKey(spec) == false) {
addMap(spec);
}
}
}
}
compileBuiltIns();
/**
* Restart the bidder
*/
startBidder();
}
use of com.xrtb.common.Campaign in project XRTB by benmfaul.
the class DbTools method read.
/**
* Read the database.json file into this object.
*
* @param db
* String. The JSON string database to load into Redis.
* @return List. A list of users in the database file.
* @throws Exception
* on file errors.
*/
public List<User> read(String db) throws Exception {
String content = new String(Files.readAllBytes(Paths.get(db)), StandardCharsets.UTF_8);
System.out.println(content);
List<User> users = mapper.readValue(content, mapper.getTypeFactory().constructCollectionType(List.class, User.class));
Set set = map.keySet();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
User u = map.get(it.next());
for (Campaign c : u.campaigns) {
c.owner = u.name;
}
dbo.put(u);
}
return users;
}
use of com.xrtb.common.Campaign in project XRTB by benmfaul.
the class DbTools method write.
/**
* Write the database object to the database.json file.
*
* @param dbName
* String. The filename to contain the Redis database.
* @throws Exception
* on file errors.
*/
public void write(String dbName) throws Exception {
List<User> list = new ArrayList();
List<String> users = dbo.listUsers();
for (String user : users) {
User u = dbo.get(user);
for (Campaign c : u.campaigns) {
c.owner = u.name;
}
list.add(u);
}
String content = mapper.writer().withDefaultPrettyPrinter().writeValueAsString(list);
Files.write(Paths.get(dbName), content.getBytes());
System.out.println(content);
}
Aggregations