RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
common/auction.h
00001 /* auction.h                                                       -*- C++ -*-
00002    Jeremy Barnes, 6 April 2011
00003    Copyright (c) 2011 Datacratic.  All rights reserved.
00004 
00005    Base class for agents to deal with an auction.
00006 */
00007 
00008 #pragma once
00009 
00010 #include "rtbkit/common/bid_request.h"
00011 #include "rtbkit/common/currency.h"
00012 #include "rtbkit/common/account_key.h"
00013 #include "rtbkit/common/augmentation.h"
00014 #include <boost/function.hpp>
00015 #include <boost/enable_shared_from_this.hpp>
00016 #include "soa/jsoncpp/json.h"
00017 #include "soa/types/date.h"
00018 #include "jml/arch/atomic_ops.h"
00019 #include "jml/arch/exception.h"
00020 #include "jml/utils/compact_vector.h"
00021 #include "jml/db/persistent_fwd.h"
00022 #include "rtbkit/common/json_holder.h"
00023 
00024 
00025 namespace RTBKIT {
00026 
00027 struct AgentConfig;
00028 struct ExchangeConnector;
00029 
00030 
00031 /*****************************************************************************/
00032 /* AUCTION                                                                   */
00033 /*****************************************************************************/
00034 
00039 struct Auction : public std::enable_shared_from_this<Auction> {
00040     
00042     typedef boost::function<void (std::shared_ptr<Auction> auction)>
00043         HandleAuction;
00044 
00045     Auction();
00046     
00047     Auction(ExchangeConnector * exchangeConnector,
00048             HandleAuction handleAuction,
00049             std::shared_ptr<BidRequest> request,
00050             const std::string & requestStr,
00051             const std::string & requestStrFormat,
00052             Date start,
00053             Date expiry);
00054     
00055     ~Auction();
00056 
00057     bool isZombie;  
00058 
00059     Date start;
00060     Date expiry;
00061     Date lossAssumed;
00062 
00063     // Debug
00064     Date doneParsing;
00065     Date inPrepro, outOfPrepro;
00066     Date doneAugmenting;
00067     Date inStartBidding;
00068 
00069     Id id;
00070     std::shared_ptr<BidRequest>  request;
00071     std::string requestStr;  
00072     std::string requestStrFormat;  
00073     std::string requestSerialized; 
00074 
00075     AugmentationList augmentations; 
00076     AgentAugmentations agentAugmentations; 
00077 
00079     double timeAvailable(Date now = Date::now()) const;
00080 
00082     double timeUsed(Date now = Date::now()) const;
00083 
00087     bool tooLate();
00088 
00089     struct Price {
00090         Price(Amount maxPrice = Amount(), float priority = 0.0)
00091             : maxPrice(maxPrice), priority(priority)
00092         {
00093         }
00094 
00095         Amount maxPrice;      
00096         float  priority;       
00097 
00098         Json::Value toJson() const;
00099         std::string toJsonStr() const;
00100         static Price fromJson(const Json::Value&);
00101     };
00102 
00104     static const Price NONE;
00105 
00107     enum WinLoss {
00108         PENDING,    
00109         WIN,        
00110         LOSS,       
00111         TOOLATE,    
00112         INVALID,    
00113     };
00114 
00116     struct Response {
00117         Response(Price price = NONE,
00118                  int tagId = -1,
00119                  const AccountKey & account = AccountKey(),
00120                  bool test = true,
00121                  std::string agent = "",
00122                  std::string bidData = "",
00123                  std::string meta = "null",
00124                  std::shared_ptr<const AgentConfig> agentConfig
00125                      = std::shared_ptr<const AgentConfig>(),
00126                  const SegmentList& visitChannels = SegmentList(),
00127                  int agentCreativeIndex = -1)
00128             : price(price), tagId(tagId),
00129               account(account),
00130               test(test), agent(agent),
00131               bidData(bidData), meta(meta), creativeId(-1),
00132               localStatus(INVALID),
00133               agentConfig(agentConfig),
00134               visitChannels(visitChannels),
00135               agentCreativeIndex(agentCreativeIndex)
00136         {
00137         }
00138 
00139         // Information about the actual bid
00140         Price price;           
00141         int tagId;             
00142         AccountKey account;    
00143         bool test;             
00144 
00145         // Information about the agent who did the bidding
00146         std::string agent;    
00147         std::string bidData;   
00148         std::string meta;      
00149 
00150 
00151         int creativeId;          
00152         std::string creativeName; 
00153 
00154         // Information about the status of the bid (what happened to it)
00155         WinLoss localStatus;   
00156 
00162         std::shared_ptr<const AgentConfig> agentConfig;
00163 
00164         // List of channels for which we subscribe to post impression visit
00165         // events.
00166         SegmentList visitChannels;
00167 
00169         int agentCreativeIndex;
00170 
00171         static std::string print(WinLoss wl);
00172         Json::Value toJson() const;
00173         std::string toJsonStr() const;
00174 
00175         void serialize(ML::DB::Store_Writer & store) const;
00176         void reconstitute(ML::DB::Store_Reader & store);
00177 
00179         bool valid() const;
00180     };
00181 
00190     WinLoss setResponse(int spotNum, Response newResponse);
00191 
00197     void addDataSources(const std::set<std::string> & sources);
00198     const std::set<std::string> & getDataSources() const;
00199 
00201     std::string status() const;
00202 
00206     bool setError(const std::string & error, const std::string & details = "");
00207 
00212     bool finish();
00213 
00215     size_t numSpots() const
00216     {
00217         return request->imp.size();
00218     }
00219 
00221     Json::Value getResponseJson(int spotNum) const;
00222     
00224     Json::Value getResponsesJson() const;
00225 
00229     const std::vector<std::vector<Response> > & getResponses() const;
00230 
00231     ExchangeConnector * exchangeConnector; 
00232     HandleAuction handleAuction;   
00233 
00234     struct Data {
00235         Data()
00236             : tooLate(false), oldData(0)
00237         {
00238             responses.reserve(8);
00239         }
00240 
00241         Data(int numSpots)
00242             : tooLate(false), responses(numSpots), oldData(0)
00243         {
00244         }
00245 
00246         bool hasValidResponse(int spotNum) const
00247         {
00248             if (spotNum >= responses.size())
00249                 throw ML::Exception("invalid spot number");
00250             return !responses[spotNum].empty();
00251         }
00252         
00253         bool hasError() const
00254         {
00255             return !error.empty();
00256         }
00257 
00258         const Response & winningResponse(int spotNum) const
00259         {
00260             if (spotNum >= responses.size())
00261                 throw ML::Exception("invalid spot number");
00262             if (responses[spotNum].empty())
00263                 throw ML::Exception("empty responses");
00264             return responses[spotNum][0];
00265         }
00266 
00267         bool tooLate;
00268         std::vector<std::vector<Response> > responses;  
00269         std::set<std::string> dataSources; // data sources used to make the bid decissions.
00270         Data * oldData;  
00271         std::string error, details;
00272     };
00273 
00274     const Data * getCurrentData() const
00275     {
00276         return data;
00277     }
00278 
00279 private:
00280     Data * data;
00281 
00282 public:
00284     static long long created;
00285     static long long destroyed;
00286 };
00287 
00288 } // namespace RTBKIT
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator