<commit_before>/* auction.h                                                       -*- C++ -*-
   <NAME>, 6 April 2011
   Copyright (c) 2011 Datacratic.  All rights reserved.

   Base class for agents to deal with an auction.
*/

#pragma once

#include "rtbkit/common/bid_request.h"
#include "rtbkit/common/currency.h"
#include "rtbkit/common/account_key.h"
#include "rtbkit/common/augmentation.h"
#include <boost/function.hpp>
#include <boost/enable_shared_from_this.hpp>
#include "soa/jsoncpp/json.h"
#include "soa/types/date.h"
#include "jml/arch/atomic_ops.h"
#include "jml/arch/exception.h"
#include "jml/utils/compact_vector.h"
#include "jml/db/persistent_fwd.h"
#include "rtbkit/common/json_holder.h"


namespace RTBKIT {

/*****************************************************************************/
/* AUCTION                                                                   */
/*****************************************************************************/

/** This is the object that represents an in-flight auction to the router.
    Agents modify this object in order to set their bids.
*/

struct Auction : public std::enable_shared_from_this<Auction> {
    
    /** Callback that's called once the auction is finished. */
    typedef boost::function<void (std::shared_ptr<Auction> auction)>
        HandleAuction;

    Auction();
    
    Auction(HandleAuction handleAuction,
            std::shared_ptr<BidRequest> request,
            const std::string & requestStr,
            const std::string & requestStrFormat,
            Date start,
            Date expiry);
    
    ~Auction();

    bool isZombie;  ///< Auction was externally cancelled

    Date start;
    Date expiry;
    Date lossAssumed;

    // Debug
    Date doneParsing;
    Date inPrepro, outOfPrepro;
    Date doneAugmenting;
    Date inStartBidding;

    Id id;
    std::shared_ptr<BidRequest>  request;
    std::string requestStr;  ///< Stringified version of request
    std::string requestStrFormat;  ///< Format of stringified request
    std::string requestSerialized; ///< Serialized bid request (canonical)

    AugmentationList augmentations; ///< Aggregate aug info for all agents.
    AgentAugmentations agentAugmentations; ///< per agent augmentations.

    /** How much time is still available for the auction (in seconds). */
    double timeAvailable(Date now = Date::now()) const;

    /** How much time has been used by the auction (in seconds). */
    double timeUsed(Date now = Date::now()) const;

    /** If this value is set, then the bid has already been sent of and it's
        too late to modify the object any more.
    */
    bool tooLate();

    struct Price {
        Price(Amount maxPrice = Amount(), float priority = 0.0)
            : maxPrice(maxPrice), priority(priority)
        {
        }

        Amount maxPrice;      ///< Price to bid for this *one* ad
        float  priority;       ///< Bid priority for when multiple campaigns bid

        Json::Value toJson() const;
        std::string toJsonStr() const;
    };

    /** Price to bid if you don't want to bid */
    static const Price NONE;

    /** What happened to the bid at the local and global level? */
    enum WinLoss {
        PENDING,    ///< Bid is pending; unknown if we won or lost
        WIN,        ///< Bid was won
        LOSS,       ///< Bid was lost
        TOOLATE,    ///< Bid was too late and so not accepted
        INVALID,    ///< Bid was invalid and so not accepted
    };

    /** Response to a bid. */
    struct Response {
        Response(Price price = NONE,
                 int tagId = -1,
                 const AccountKey & account = AccountKey(),
                 bool test = true,
                 std::string agent = "",
                 std::string bidData = "",
                 std::string meta = "null",
                 std::shared_ptr<const void> agentConfig
                     = std::shared_ptr<const void>(),
                 const SegmentList& visitChannels = SegmentList())
            : price(price), tagId(tagId),
              account(account),
              test(test), agent(agent),
              bidData(bidData), meta(meta), creativeId(-1),
              localStatus(INVALID),
              agentConfig(agentConfig),
              visitChannels(visitChannels)
        {
        }

        // Information about the actual bid
        Price price;           ///< Price to bid on
        int tagId;             ///< Tag ID (v0.3)
        AccountKey account;    ///< Account we are bidding with
        bool test;             ///< Is this a test bid?

        // Information about the agent who did the bidding
        std::string agent;    ///< Agent ID who's bidding
        std::string bidData;   ///< Data that the bidder wants to keep
        std::string meta;      ///< Free form agent information about the bid
                               ///< (Passed back to agent with notification)

        int creativeId;          ///< Number of the creative/placement
        std::string creativeName; ///< Name of the creative

        // Information about the status of the bid (what happened to it)
        WinLoss localStatus;   ///< What happened in the local auction?

        /** Configuration of the bidder that made the bid

            WARNING: This member will not be serialized and will therefor not be
            available out of process.
        */
        std::shared_ptr<const void> agentConfig;

        // List of channels for which we subscribe to post impression visit
        // events.
        SegmentList visitChannels;

        static std::string print(WinLoss wl);
        Json::Value toJson() const;
        std::string toJsonStr() const;

        void serialize(ML::DB::Store_Writer & store) const;
        void reconstitute(ML::DB::Store_Reader & store);

        /** Is this a valid response? */
        bool valid() const;
    };

    /** Modify the given response.  The boolean return code says whether or
        not this response was accepted (due to it being the maximum-priority
        response).

        Returns the (local) status of the response.

        Thread safe.
    */
    WinLoss setResponse(int spotNum, Response newResponse);
<commit_msg>agent configuration in auction now using correct type; added place to record creative index in auction<commit_after>/* auction.h                                                       -*- C++ -*-
   <NAME>, 6 April 2011
   Copyright (c) 2011 Datacratic.  All rights reserved.

   Base class for agents to deal with an auction.
*/

#pragma once

#include "rtbkit/common/bid_request.h"
#include "rtbkit/common/currency.h"
#include "rtbkit/common/account_key.h"
#include "rtbkit/common/augmentation.h"
#include <boost/function.hpp>
#include <boost/enable_shared_from_this.hpp>
#include "soa/jsoncpp/json.h"
#include "soa/types/date.h"
#include "jml/arch/atomic_ops.h"
#include "jml/arch/exception.h"
#include "jml/utils/compact_vector.h"
#include "jml/db/persistent_fwd.h"
#include "rtbkit/common/json_holder.h"


namespace RTBKIT {

struct AgentConfig;

/*****************************************************************************/
/* AUCTION                                                                   */
/*****************************************************************************/

/** This is the object that represents an in-flight auction to the router.
    Agents modify this object in order to set their bids.
*/

struct Auction : public std::enable_shared_from_this<Auction> {
    
    /** Callback that's called once the auction is finished. */
    typedef boost::function<void (std::shared_ptr<Auction> auction)>
        HandleAuction;

    Auction();
    
    Auction(HandleAuction handleAuction,
            std::shared_ptr<BidRequest> request,
            const std::string & requestStr,
            const std::string & requestStrFormat,
            Date start,
            Date expiry);
    
    ~Auction();

    bool isZombie;  ///< Auction was externally cancelled

    Date start;
    Date expiry;
    Date lossAssumed;

    // Debug
    Date doneParsing;
    Date inPrepro, outOfPrepro;
    Date doneAugmenting;
    Date inStartBidding;

    Id id;
    std::shared_ptr<BidRequest>  request;
    std::string requestStr;  ///< Stringified version of request
    std::string requestStrFormat;  ///< Format of stringified request
    std::string requestSerialized; ///< Serialized bid request (canonical)

    AugmentationList augmentations; ///< Aggregate aug info for all agents.
    AgentAugmentations agentAugmentations; ///< per agent augmentations.

    /** How much time is still available for the auction (in seconds). */
    double timeAvailable(Date now = Date::now()) const;

    /** How much time has been used by the auction (in seconds). */
    double timeUsed(Date now = Date::now()) const;

    /** If this value is set, then the bid has already been sent of and it's
        too late to modify the object any more.
    */
    bool tooLate();

    struct Price {
        Price(Amount maxPrice = Amount(), float priority = 0.0)
            : maxPrice(maxPrice), priority(priority)
        {
        }

        Amount maxPrice;      ///< Price to bid for this *one* ad
        float  priority;       ///< Bid priority for when multiple campaigns bid

        Json::Value toJson() const;
        std::string toJsonStr() const;
    };

    /** Price to bid if you don't want to bid */
    static const Price NONE;

    /** What happened to the bid at the local and global level? */
    enum WinLoss {
        PENDING,    ///< Bid is pending; unknown if we won or lost
        WIN,        ///< Bid was won
        LOSS,       ///< Bid was lost
        TOOLATE,    ///< Bid was too late and so not accepted
        INVALID,    ///< Bid was invalid and so not accepted
    };

    /** Response to a bid. */
    struct Response {
        Response(Price price = NONE,
                 int tagId = -1,
                 const AccountKey & account = AccountKey(),
                 bool test = true,
                 std::string agent = "",
                 std::string bidData = "",
                 std::string meta = "null",
                 std::shared_ptr<const AgentConfig> agentConfig
                     = std::shared_ptr<const AgentConfig>(),
                 const SegmentList& visitChannels = SegmentList(),
                 int agentCreativeIndex = -1)
            : price(price), tagId(tagId),
              account(account),
              test(test), agent(agent),
              bidData(bidData), meta(meta), creativeId(-1),
              localStatus(INVALID),
              agentConfig(agentConfig),
              visitChannels(visitChannels),
              agentCreativeIndex(agentCreativeIndex)
        {
        }

        // Information about the actual bid
        Price price;           ///< Price to bid on
        int tagId;             ///< Tag ID (v0.3)
        AccountKey account;    ///< Account we are bidding with
        bool test;             ///< Is this a test bid?

        // Information about the agent who did the bidding
        std::string agent;    ///< Agent ID who's bidding
        std::string bidData;   ///< Data that the bidder wants to keep
        std::string meta;      ///< Free form agent information about the bid
                               ///< (Passed back to agent with notification)

        int creativeId;          ///< Number of the creative/placement
        std::string creativeName; ///< Name of the creative

        // Information about the status of the bid (what happened to it)
        WinLoss localStatus;   ///< What happened in the local auction?

        /** Configuration of the agent that made the bid

            WARNING: This member will not be serialized and will therefore not
            be available out of process.
        */
        std::shared_ptr<const AgentConfig> agentConfig;

        // List of channels for which we subscribe to post impression visit
        // events.
        SegmentList visitChannels;

        /** Creative index in this agentConfig's creatives array. */
        int agentCreativeIndex;

        static std::string print(WinLoss wl);
        Json::Value toJson() const;
        std::string toJsonStr() const;

        void serialize(ML::DB::Store_Writer & store) const;
        void reconstitute(ML::DB::Store_Reader & store);

        /** Is this a valid response? */
        bool valid() const;
    };

    /** Modify the given response.  The boolean return code says whether or
        not this response was accepted (due to it being the maximum-priority
        response).

        Returns the (local) status of the response.

        Thread safe.
    */
    WinLoss setResponse(int spotNum, Response newResponse);

{'fraction_non_alphanumeric': 0.1187514667918329, 'fraction_numerical': 0.0024250958303997498, 'mean_word_length': 2.063503474718428, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2434726', 'n_tokens_mistral': 3408, 'n_tokens_neox': 3241, 'n_words': 1162}