<commit_before>/* standard_adserver_connector.cc
   <NAME>, March 2013
   Copyright (c) 2013 Datacratic.  All rights reserved. */


#include "rtbkit/common/account_key.h"
#include "rtbkit/common/currency.h"
#include "rtbkit/common/json_holder.h"

#include "soa/service/logs.h"

#include "standard_adserver_connector.h"


using namespace std;

std::mutex win_lock;
std::mutex event_lock;

long get_win_id(redisContext *rc) {
    win_lock.lock();
    redisReply *reply;
    reply = (redisReply *)redisCommand(rc,"INCR win_counter");
    long i = reply->integer;
    freeReplyObject(reply);
    win_lock.unlock();
    return i;

}

long get_event_id(redisContext *rc) {
    event_lock.lock();
    redisReply *reply;
    reply = (redisReply *)redisCommand(rc,"INCR event_counter");
    long i = reply->integer;
    freeReplyObject(reply);
    event_lock.unlock();
    return i;

}



void writeFile (std::string s) {

  std::ofstream ofs;
  ofs.open ("win.txt", std::ofstream::out | std::ofstream::app);

  ofs << s << endl;

  ofs.close();

}
using namespace boost::program_options;

using namespace RTBKIT;

Logging::Category adserverTrace("Standard Ad-Server connector");

/* STANDARDADSERVERARGUMENTS */
boost::program_options::options_description
StandardAdServerArguments::
makeProgramOptions()
{
    boost::program_options::options_description stdOptions
        = ServiceProxyArguments::makeProgramOptions();

    verbose = false;

    boost::program_options::options_description
        options("Standard-Ad-Server-Connector");
    options.add_options()
        ("win-port,w", value(&winPort), "listening port for wins")
        ("events-port,e", value(&eventsPort), "listening port for events")
        ("verbose,v", value(&verbose), "verbose mode")
        ("analytics,a", bool_switch(&analyticsOn),
         "Send data to analytics endpoint.")
        ("analytics-connections", value<int>(&analyticsConnections),
         "Number of connections for the analytics publisher.");
    stdOptions.add(options);

    return stdOptions;
}

void
StandardAdServerArguments::
validate()
{
    ExcCheck(winPort > 0, "winPort is not set");
    ExcCheck(eventsPort > 0, "eventsPort is not set");
}

/* STANDARDADSERVERCONNECTOR */

StandardAdServerConnector::
StandardAdServerConnector(std::shared_ptr<ServiceProxies> & proxy,
                           const string & serviceName)
    : HttpAdServerConnector(serviceName, proxy),
      publisher_(proxy->zmqContext)
{
    initEventType(Json::Value());
    rc2 = redisConnect(connection.c_str(), rport);

    if (rc2 == NULL || rc2->err) {
        if (rc2) {
            throw ML::Exception("Error",rc2->errstr);
            // handle error
        } else {
            throw ML::Exception("Can't allocate redis context\n");
        }
    }
    rc3 = redisConnect(connection.c_str(), rport);

    if (rc3 == NULL || rc3->err) {
        if (rc3) {
            throw ML::Exception("Error",rc3->errstr);
            // handle error
        } else {
            throw ML::Exception("Can't allocate redis context\n");
        }
    }
}

void StandardAdServerConnector::record_win(std::string s) const
{
    redisContext* rc7 = redisConnect(connection.c_str(), rport);
    redisReply *reply;
    long i = get_win_id(rc7);
    string z = "win:"+std::to_string(i);
    reply = (redisReply *)redisCommand(rc7,"SET %s %s ", z.c_str(), s.c_str());
    if (reply->type == REDIS_REPLY_ERROR) {
    cerr << reply->str << endl;
    }
    freeReplyObject(reply);



}

void StandardAdServerConnector::record_event(std::string s) const
{

    redisContext* rc8 = redisConnect(connection.c_str(), rport);
    redisReply *reply;
    long i = get_win_id(rc8);
    string z = "event:"+std::to_string(i);
    reply = (redisReply *)redisCommand(rc8,"SET %s %s ", z.c_str(), s.c_str());
    if (reply->type == REDIS_REPLY_ERROR) {
    cerr << reply->str << endl;
    }
    freeReplyObject(reply);



}
StandardAdServerConnector::
StandardAdServerConnector(std::string const & serviceName, std::shared_ptr<ServiceProxies> const & proxies,
                          Json::Value const & json) :
    HttpAdServerConnector(serviceName, proxies),
    publisher_(getServices()->zmqContext) {
    int winPort = json.get("winPort", 17340).asInt();
    int eventsPort = json.get("eventsPort", 17341).asInt();
    verbose = json.get("verbose", false).asBool();
    initEventType(json);
    init(winPort, eventsPort, verbose);
}

void
StandardAdServerConnector::
initEventType(const Json::Value &json) {<commit_msg>event bug<commit_after>/* standard_adserver_connector.cc
   <NAME>, March 2013
   Copyright (c) 2013 Datacratic.  All rights reserved. */


#include "rtbkit/common/account_key.h"
#include "rtbkit/common/currency.h"
#include "rtbkit/common/json_holder.h"

#include "soa/service/logs.h"

#include "standard_adserver_connector.h"


using namespace std;

std::mutex win_lock;
std::mutex event_lock;

long get_win_id(redisContext *rc) {
    win_lock.lock();
    redisReply *reply;
    reply = (redisReply *)redisCommand(rc,"INCR win_counter");
    long i = reply->integer;
    freeReplyObject(reply);
    win_lock.unlock();
    return i;

}

long get_event_id(redisContext *rc) {
    event_lock.lock();
    redisReply *reply;
    reply = (redisReply *)redisCommand(rc,"INCR event_counter");
    long i = reply->integer;
    freeReplyObject(reply);
    event_lock.unlock();
    return i;

}



void writeFile (std::string s) {

  std::ofstream ofs;
  ofs.open ("win.txt", std::ofstream::out | std::ofstream::app);

  ofs << s << endl;

  ofs.close();

}
using namespace boost::program_options;

using namespace RTBKIT;

Logging::Category adserverTrace("Standard Ad-Server connector");

/* STANDARDADSERVERARGUMENTS */
boost::program_options::options_description
StandardAdServerArguments::
makeProgramOptions()
{
    boost::program_options::options_description stdOptions
        = ServiceProxyArguments::makeProgramOptions();

    verbose = false;

    boost::program_options::options_description
        options("Standard-Ad-Server-Connector");
    options.add_options()
        ("win-port,w", value(&winPort), "listening port for wins")
        ("events-port,e", value(&eventsPort), "listening port for events")
        ("verbose,v", value(&verbose), "verbose mode")
        ("analytics,a", bool_switch(&analyticsOn),
         "Send data to analytics endpoint.")
        ("analytics-connections", value<int>(&analyticsConnections),
         "Number of connections for the analytics publisher.");
    stdOptions.add(options);

    return stdOptions;
}

void
StandardAdServerArguments::
validate()
{
    ExcCheck(winPort > 0, "winPort is not set");
    ExcCheck(eventsPort > 0, "eventsPort is not set");
}

/* STANDARDADSERVERCONNECTOR */

StandardAdServerConnector::
StandardAdServerConnector(std::shared_ptr<ServiceProxies> & proxy,
                           const string & serviceName)
    : HttpAdServerConnector(serviceName, proxy),
      publisher_(proxy->zmqContext)
{
    initEventType(Json::Value());
    rc2 = redisConnect(connection.c_str(), rport);

    if (rc2 == NULL || rc2->err) {
        if (rc2) {
            throw ML::Exception("Error",rc2->errstr);
            // handle error
        } else {
            throw ML::Exception("Can't allocate redis context\n");
        }
    }
    rc3 = redisConnect(connection.c_str(), rport);

    if (rc3 == NULL || rc3->err) {
        if (rc3) {
            throw ML::Exception("Error",rc3->errstr);
            // handle error
        } else {
            throw ML::Exception("Can't allocate redis context\n");
        }
    }
}

void StandardAdServerConnector::record_win(std::string s) const
{
    redisContext* rc7 = redisConnect(connection.c_str(), rport);
    redisReply *reply;
    long i = get_win_id(rc7);
    string z = "win:"+std::to_string(i);
    reply = (redisReply *)redisCommand(rc7,"SET %s %s ", z.c_str(), s.c_str());
    if (reply->type == REDIS_REPLY_ERROR) {
    cerr << reply->str << endl;
    }
    freeReplyObject(reply);



}

void StandardAdServerConnector::record_event(std::string s) const
{

    redisContext* rc8 = redisConnect(connection.c_str(), rport);
    redisReply *reply;
    long i = get_event_id(rc8);
    string z = "event:"+std::to_string(i);
    reply = (redisReply *)redisCommand(rc8,"SET %s %s ", z.c_str(), s.c_str());
    if (reply->type == REDIS_REPLY_ERROR) {
    cerr << reply->str << endl;
    }
    freeReplyObject(reply);



}
StandardAdServerConnector::
StandardAdServerConnector(std::string const & serviceName, std::shared_ptr<ServiceProxies> const & proxies,
                          Json::Value const & json) :
    HttpAdServerConnector(serviceName, proxies),
    publisher_(getServices()->zmqContext) {
    int winPort = json.get("winPort", 17340).asInt();
    int eventsPort = json.get("eventsPort", 17341).asInt();
    verbose = json.get("verbose", false).asBool();
    initEventType(json);
    init(winPort, eventsPort, verbose);
}

void
StandardAdServerConnector::
initEventType(const Json::Value &json) {
{'fraction_non_alphanumeric': 0.1414990050851205, 'fraction_numerical': 0.007959319036038028, 'mean_word_length': 3.6394871794871797, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4888378', 'n_tokens_mistral': 2954, 'n_tokens_neox': 2881, 'n_words': 583}