RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
core/router/router_types.h
00001 /* rtb_router_types.h                                              -*- C++ -*-
00002    Jeremy Barnes, 1 March 2012
00003    Copyright (c) 2012 Datacratic.  All rights reserved.
00004 
00005    Types for the rtb router.
00006 */
00007 
00008 #ifndef __rtb_router__rtb_router_types_h__
00009 #define __rtb_router__rtb_router_types_h__
00010 
00011 #include "rtbkit/common/bid_request.h"
00012 #include "rtbkit/common/auction.h"
00013 #include "jml/stats/distribution.h"
00014 #include <set>
00015 #include "rtbkit/common/currency.h"
00016 #include "rtbkit/common/bids.h"
00017 
00018 
00019 namespace RTBKIT {
00020 
00021 struct AgentConfig;
00022 
00023 
00024 /*****************************************************************************/
00025 /* BIDDABLE SPOTS                                                            */
00026 /*****************************************************************************/
00027 
00028 typedef ML::compact_vector<std::pair<int, SmallIntVector>, 3, uint32_t >
00029 BiddableSpotsBase;
00030 
00035 struct BiddableSpots : public BiddableSpotsBase {
00036     Json::Value toJson() const;
00037     std::string toJsonStr() const;
00038 };
00039 
00040 struct AgentStats {
00041 
00042     AgentStats();
00043 
00044     Json::Value toJson() const;
00045 
00046     uint64_t auctions;
00047     uint64_t bids;
00048     uint64_t wins;
00049     uint64_t losses;
00050     uint64_t tooLate;
00051     uint64_t invalid;
00052     uint64_t noBudget;
00053 
00054     CurrencyPool totalBid;
00055     CurrencyPool totalBidOnWins;
00056     CurrencyPool totalSpent;
00057 
00058     uint64_t tooManyInFlight;
00059     uint64_t noSpots;
00060     uint64_t skippedBidProbability;
00061     uint64_t urlFiltered;
00062     uint64_t hourOfWeekFiltered;
00063     uint64_t locationFiltered;
00064     uint64_t languageFiltered;
00065     uint64_t userPartitionFiltered;
00066     uint64_t dataProfileFiltered;
00067     uint64_t exchangeFiltered;
00068     uint64_t segmentsMissing;
00069     uint64_t segmentFiltered;
00070     uint64_t augmentationTagsExcluded;
00071     uint64_t userBlacklisted;
00072     uint64_t notEnoughTime;
00073     uint64_t requiredIdMissing;
00074 
00075     uint64_t intoFilters;
00076     uint64_t passedStaticFilters;
00077     uint64_t passedStaticPhase1;
00078     uint64_t passedStaticPhase2;
00079     uint64_t passedStaticPhase3;
00080     uint64_t passedDynamicFilters;
00081     uint64_t bidErrors;
00082  
00083     uint64_t filter1Excluded;
00084     uint64_t filter2Excluded;
00085     uint64_t filternExcluded;
00086 
00087     uint64_t unknownWins;
00088     uint64_t unknownLosses;
00089 
00090     uint64_t requiredAugmentorIsMissing;
00091     uint64_t augmentorValueIsNull;
00092 };
00093 
00094 
00095 struct AgentStatus {
00096     AgentStatus()
00097         : dead(false), numBidsInFlight(0)
00098     {
00099         lastHeartbeat = Date::now();
00100     }
00101 
00102     bool dead;
00103     Date lastHeartbeat;
00104     size_t numBidsInFlight;
00105 };
00106 
00108 struct AgentInfo {
00109     AgentInfo()
00110         : bidRequestFormat(BRF_JSON_RAW),
00111           configured(false),
00112           status(new AgentStatus()),
00113           stats(new AgentStats()),
00114           throttleProbability(1.0)
00115     {
00116     }
00117 
00118     enum BidRequestFormat {
00119         BRF_JSON_RAW,  
00120         BRF_JSON_NORM, 
00121         BRF_BINARY_V1  
00122     } bidRequestFormat;
00123     
00124     bool configured;
00125     std::shared_ptr<const AgentConfig> config;
00126     std::shared_ptr<AgentStatus> status;
00127     std::shared_ptr<AgentStats> stats;
00128     double throttleProbability;
00129 
00131     std::string address;
00132     
00136     const std::string & encodeBidRequest(const BidRequest & br) const;
00137 
00138     const std::string & encodeBidRequest(const Auction & auction) const;
00139     const std::string & getBidRequestEncoding(const Auction & auction) const;
00140 
00142     void setBidRequestFormat(const std::string & val);
00143 
00145     struct PingInfo {
00146         PingInfo()
00147         {
00148         }
00149 
00150         Date lastSent;     
00151         ML::distribution<double> history;  
00152     };
00153 
00154     PingInfo pingInfo[2];
00155 
00156     void gotPong(int level, Date sent, Date received, Date finished)
00157     {
00158         if (level < 0 || level >= 2)
00159             throw ML::Exception("wrong pong level");
00160         PingInfo & info = pingInfo[level];
00161         if (info.lastSent == Date())
00162             throw ML::Exception("got double pong response");
00163         double difference = info.lastSent.secondsSince(sent);
00164         if (difference > 0.001)
00165             throw ML::Exception("sent and lastSent differed by %f seconds",
00166                                 difference);
00167 
00168         double time = finished.secondsSince(sent);
00169         info.history.push_back(time);
00170         if (info.history.size() > 100)
00171             info.history.erase(info.history.begin(),
00172                                info.history.begin() + info.history.size() - 50);
00173         info.lastSent = Date();
00174     }
00175 
00176     bool sendPing(int level, Date & start)
00177     {
00178         if (level < 0 || level >= 2)
00179             throw ML::Exception("wrong pong level");
00180         PingInfo & info = pingInfo[level];
00181 
00182         if (info.lastSent != Date())
00183             return false;
00184         info.lastSent = start;
00185         return true;
00186     }
00187 
00188     Json::Value toJson(bool includeConfig = true,
00189                        bool includeStats = true) const;
00190 
00191     void gotHeartbeat(Date when = Date::now())
00192     {
00193         status->lastHeartbeat = when;
00194         status->dead = false;
00195     }
00196 
00197     template<typename Fn>
00198     void forEachInFlight(const Fn & fn) const
00199     {
00200         for (auto it = bidsInFlight.begin(), end = bidsInFlight.end();
00201              it != end;  ++it) {
00202             fn(it->first, it->second);
00203         }
00204     }
00205 
00206     size_t numBidsInFlight() const
00207     {
00208         // DEBUG
00209         if (status->numBidsInFlight != bidsInFlight.size())
00210             throw ML::Exception("numBidsInFlight is wrong");
00211         return status->numBidsInFlight;
00212     }
00213     
00214     bool expireBidInFlight(const Id & id)
00215     {
00216         bool result = bidsInFlight.erase(id);
00217         status->numBidsInFlight = bidsInFlight.size();
00218         return result;
00219     }
00220 
00221     // Returns true if it was successfully inserted
00222     bool trackBidInFlight(const Id & id, Date date = Date::now())
00223     {
00224         bool result = bidsInFlight.insert(std::make_pair(id, date)).second;
00225         status->numBidsInFlight = bidsInFlight.size();
00226         return result;
00227     }
00228 
00229 private:
00230     std::map<Id, Date> bidsInFlight;  
00231     //std::set<std::pair<Id, Id> > awaitingResult;  ///< Auctions which are awaiting a win/loss result
00232 };
00233 
00235 struct PotentialBidder {
00236     // If inFlightProp == NULL_PROP then the bidder has been filtered out.
00237     enum { NULL_PROP = 1000000 };
00238 
00239     PotentialBidder() : inFlightProp(NULL_PROP) {}
00240 
00241     std::string agent;
00242     float inFlightProp;
00243     BiddableSpots imp;
00244     std::shared_ptr<const AgentConfig> config;
00245     std::shared_ptr<AgentStats> stats;
00246 
00247     bool operator < (const PotentialBidder & other) const
00248     {
00249         return inFlightProp < other.inFlightProp
00250             || (inFlightProp == other.inFlightProp
00251                 && agent < other.agent);
00252     }
00253 };
00254 
00258 struct GroupPotentialBidders : public std::vector<PotentialBidder> {
00259     GroupPotentialBidders()
00260         : totalBidProbability(0.0)
00261     {
00262     }
00263     
00264     double totalBidProbability;
00265 };
00266 
00267 
00268 struct AuctionInfoBase {
00269     AuctionInfoBase() {}
00270     AuctionInfoBase(const std::shared_ptr<Auction> & auction,
00271                     Date lossTimeout)
00272         : auction(auction), lossTimeout(lossTimeout)
00273     {
00274     }
00275 
00276     std::shared_ptr<Auction> auction;   
00277     Date lossTimeout;                     
00278 
00279 };
00280 
00281 struct BidInfo {
00282     Date bidTime;
00283     BiddableSpots imp;
00284     std::shared_ptr<const AgentConfig> agentConfig;  //< config active at auction
00285 };
00286 
00287 // Information about an in-flight auction
00288 struct AuctionInfo : public AuctionInfoBase {
00289     AuctionInfo() {}
00290     AuctionInfo(const std::shared_ptr<Auction> & auction,
00291                 Date lossTimeout)
00292         : AuctionInfoBase(auction, lossTimeout)
00293     {
00294     }
00295 
00296     std::map<std::string, BidInfo> bidders;  
00297 
00298 };
00299 
00300 struct FormatInfo {
00301     FormatInfo()
00302         : numSpots(0), numBids(0)
00303     {
00304     }
00305         
00306     uint64_t numSpots;
00307     uint64_t numBids;
00308 
00309     Json::Value toJson() const;
00310 };
00311 
00312 struct DutyCycleEntry {
00313     DutyCycleEntry()
00314     {
00315         clear();
00316     }
00317 
00318     Date starting, ending;
00319     uint64_t nsSleeping;
00320     uint64_t nsProcessing;
00321     uint64_t nEvents;
00322 
00323     // Time for different parts
00324     uint64_t nsConfig;
00325     uint64_t nsBid;
00326     uint64_t nsAuction;
00327     uint64_t nsStartBidding;
00328     uint64_t nsWin;
00329     uint64_t nsLoss;
00330     uint64_t nsBidResult;
00331     uint64_t nsRemoveInFlightAuction;
00332     uint64_t nsRemoveSubmittedAuction;
00333     uint64_t nsEraseLossTimeout;
00334     uint64_t nsEraseAuction;
00335     uint64_t nsTimeout;
00336     uint64_t nsSubmitted;
00337     uint64_t nsImpression;
00338     uint64_t nsClick;
00339     uint64_t nsExpireInFlight;
00340     uint64_t nsExpireSubmitted;
00341     uint64_t nsExpireFinished;
00342     uint64_t nsExpireBlacklist;
00343     uint64_t nsExpireBanker;
00344     uint64_t nsExpireDebug;
00345 
00346     uint64_t nsOnExpireSubmitted;
00347 
00348     void clear();
00349         
00350     void operator += (const DutyCycleEntry & other);
00351         
00352     Json::Value toJson() const;
00353 };
00354 
00355 
00356 
00357 
00358 } // namespace RTBKIT
00359 
00360 #endif /* __rtb_router__rtb_router_types_h__ */
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator