Search in sources :

Example 16 with Campaign

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);
}
Also used : AddCampaign(com.xrtb.commands.AddCampaign) Campaign(com.xrtb.common.Campaign) AddCampaign(com.xrtb.commands.AddCampaign) DeleteCampaign(com.xrtb.commands.DeleteCampaign) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Example 17 with Campaign

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);
}
Also used : User(com.xrtb.db.User) Campaign(com.xrtb.common.Campaign) AddCampaign(com.xrtb.commands.AddCampaign) DeleteCampaign(com.xrtb.commands.DeleteCampaign) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with Campaign

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();
}
Also used : Campaign(com.xrtb.common.Campaign) Creative(com.xrtb.common.Creative) SelectedCreative(com.xrtb.bidder.SelectedCreative) IntNode(com.fasterxml.jackson.databind.node.IntNode) Node(com.xrtb.common.Node) JsonNode(com.fasterxml.jackson.databind.JsonNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) DoubleNode(com.fasterxml.jackson.databind.node.DoubleNode) MissingNode(com.fasterxml.jackson.databind.node.MissingNode) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 19 with Campaign

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;
}
Also used : User(com.xrtb.db.User) Set(java.util.Set) Campaign(com.xrtb.common.Campaign) ArrayList(java.util.ArrayList) List(java.util.List)

Example 20 with Campaign

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);
}
Also used : User(com.xrtb.db.User) Campaign(com.xrtb.common.Campaign) ArrayList(java.util.ArrayList)

Aggregations

Campaign (com.xrtb.common.Campaign)27 User (com.xrtb.db.User)13 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)9 Configuration (com.xrtb.common.Configuration)7 List (java.util.List)7 Creative (com.xrtb.common.Creative)6 RTBServer (com.xrtb.bidder.RTBServer)5 Node (com.xrtb.common.Node)5 BidRequest (com.xrtb.pojo.BidRequest)5 DbTools (com.xrtb.tools.DbTools)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 AddCampaign (com.xrtb.commands.AddCampaign)4 DeleteCampaign (com.xrtb.commands.DeleteCampaign)4 SelectedCreative (com.xrtb.bidder.SelectedCreative)3 BidResponse (com.xrtb.pojo.BidResponse)3 HttpPostGet (com.xrtb.common.HttpPostGet)2 Bid (com.xrtb.pojo.Bid)2