RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
core/router/router_types.cc
00001 /* rtb_router_types.cc
00002    Jeremy Barnes, 1 March 2012
00003    Copyright (c) 2012 Datacratic.  All rights reserved.
00004 
00005    Types for the RTB router.
00006 */
00007 
00008 #include "router_types.h"
00009 #include "rtbkit/core/agent_configuration/agent_config.h"
00010 #include "jml/db/persistent.h"
00011 
00012 using namespace std;
00013 using namespace ML;
00014 
00015 namespace RTBKIT {
00016 
00017 void
00018 DutyCycleEntry::
00019 clear()
00020 {
00021     starting = Date::now();
00022     nsSleeping = nsProcessing =  nEvents = 0;
00023     nsConfig = nsBid = nsAuction = nsWin = nsLoss = nsBidResult = 0;
00024     nsRemoveInFlightAuction = nsRemoveSubmittedAuction = 0;
00025     nsEraseLossTimeout = nsEraseAuction = 0;
00026     nsTimeout = nsSubmitted = nsImpression = nsClick = 0;
00027     nsStartBidding = 0;
00028     nsExpireInFlight = nsExpireSubmitted
00029         = nsExpireFinished = nsExpireBlacklist = nsExpireBanker
00030         = nsExpireDebug = nsOnExpireSubmitted = 0;
00031 }
00032         
00033 void
00034 DutyCycleEntry::
00035 operator += (const DutyCycleEntry & other)
00036 {
00037     starting = std::min(starting, other.starting);
00038     ending = std::max(ending, other.ending);
00039     nsSleeping += other.nsSleeping;
00040     nsProcessing += other.nsProcessing;
00041     nEvents += other.nEvents;
00042     nsConfig += other.nsConfig;
00043     nsBid += other.nsBid;
00044     nsAuction += other.nsAuction;
00045     nsStartBidding += other.nsStartBidding;
00046     nsWin += other.nsWin;
00047     nsLoss += other.nsLoss;
00048     nsBidResult += other.nsBidResult;
00049     nsRemoveInFlightAuction += other.nsRemoveInFlightAuction;
00050     nsTimeout += other.nsTimeout;
00051     nsSubmitted += other.nsSubmitted;
00052     nsImpression += other.nsImpression;
00053     nsClick += other.nsClick;
00054     throw ML::Exception("not finished");
00055 }
00056 
00057 Json::Value
00058 DutyCycleEntry::
00059 toJson() const
00060 {
00061     Json::Value result;
00062 
00063     double elapsedTime = ending.secondsSince(starting);
00064 
00065     Json::Value & times = result["times"];
00066     Json::Value & duty = result["duty"];
00067 
00068     result["elapsedTime"] = elapsedTime;
00069     result["events"] = nEvents;
00070     result["eventsPerSecond"] = nEvents / elapsedTime;
00071     result["nsProcessing"] = (double)nsProcessing;
00072     result["nsSleeping"] = (double)nsSleeping;
00073     result["dutyCycle"] = nsProcessing / (nsSleeping + nsProcessing + 0.0);
00074 
00075     auto doTime = [&] (const char * metric, uint64_t val)
00076         {
00077             times[metric] = val;
00078             duty[metric] = 100.0 * (double)val / (double)nsProcessing;
00079         };
00080 
00081     doTime("config", nsConfig);
00082     doTime("bid", nsBid);
00083     doTime("auction", nsAuction);
00084     doTime("win", nsWin);
00085     doTime("loss", nsLoss);
00086     doTime("bidResult", nsBidResult);
00087     doTime("removeInFlightAuction", nsRemoveInFlightAuction);
00088     doTime("eraseLossTimeout", nsEraseLossTimeout);
00089     doTime("eraseAuction", nsEraseAuction);
00090     doTime("expireInFlight", nsExpireInFlight);
00091     doTime("expireSubmitted", nsExpireSubmitted);
00092     doTime("expireFinished", nsExpireFinished);
00093     doTime("expireBlacklist", nsExpireBlacklist);
00094     doTime("expireBanker", nsExpireBanker);
00095     doTime("expireDebug", nsExpireDebug);
00096     doTime("onExpireSubmitted", nsOnExpireSubmitted);
00097 
00098     return result;
00099 }
00100 
00101 Json::Value
00102 AgentInfo::
00103 toJson(bool includeConfig, bool includeStats) const
00104 {
00105     Json::Value result;
00106     result["configured"] = configured;
00107     result["lastHeartbeat"]
00108         = status->lastHeartbeat.print(4);
00109     result["numInFlight"] = status->numBidsInFlight;
00110     if (config && includeConfig) result["config"] = config->toJson(false);
00111     if (stats && includeStats) result["stats"] = stats->toJson();
00112     
00113     return result;
00114 }
00115 
00116 const std::string &
00117 AgentInfo::
00118 encodeBidRequest(const BidRequest & br) const
00119 {
00120     throw ML::Exception("encodeBidRequest not there yet");
00121 }
00122 
00123 const std::string &
00124 AgentInfo::
00125 encodeBidRequest(const Auction & auction) const
00126 {
00127     return auction.requestStr;
00128 }
00129 
00130 const std::string &
00131 AgentInfo::
00132 getBidRequestEncoding(const Auction & auction) const
00133 {
00134     return auction.requestStrFormat;
00135 }
00136 
00137 void
00138 AgentInfo::
00139 setBidRequestFormat(const std::string & val)
00140 {
00141     // TODO
00142 }
00143 
00144 AgentStats::
00145 AgentStats()
00146     : auctions(0), bids(0), wins(0), losses(0), tooLate(0),
00147       invalid(0), noBudget(0),
00148       tooManyInFlight(0), noSpots(0), skippedBidProbability(0),
00149       urlFiltered(0), hourOfWeekFiltered(0),
00150       locationFiltered(0), languageFiltered(0),
00151       userPartitionFiltered(0),
00152       dataProfileFiltered(0),
00153       exchangeFiltered(0),
00154       segmentsMissing(0), segmentFiltered(0),
00155       augmentationTagsExcluded(0), userBlacklisted(0), notEnoughTime(0),
00156       requiredIdMissing(0),
00157       intoFilters(0), passedStaticFilters(0),
00158       passedStaticPhase1(0), passedStaticPhase2(0), passedStaticPhase3(0),
00159       passedDynamicFilters(0),
00160       bidErrors(0),
00161       filter1Excluded(0),
00162       filter2Excluded(0),
00163       filternExcluded(0),
00164       unknownWins(0), unknownLosses(0),
00165       requiredAugmentorIsMissing(0), augmentorValueIsNull(0)
00166 {
00167 }
00168 
00169 Json::Value
00170 AgentStats::
00171 toJson() const
00172 {
00173     Json::Value result;
00174     result["auctions"] = auctions;
00175     result["bids"] = bids;
00176     result["wins"] = wins;
00177     result["losses"] = losses;
00178     result["tooLate"] = tooLate;
00179     result["invalid"] = invalid;
00180     result["noBudget"] = noBudget;
00181     result["totalBid"] = totalBid.toJson();
00182     result["totalBidOnWins"] = totalBidOnWins.toJson();
00183     result["totalSpent"] = totalSpent.toJson();
00184     result["tooManyInFlight"] = tooManyInFlight;
00185     result["requiredIdMissing"] = requiredIdMissing;
00186     result["notEnoughTime"] = notEnoughTime;
00187 
00188     result["filter_noSpots"] = noSpots;
00189     result["filter_skippedBidProbability"] = skippedBidProbability;
00190     result["filter_urlFiltered"] = urlFiltered;
00191     result["filter_hourOfWeekFiltered"] = hourOfWeekFiltered;
00192     result["filter_locationFiltered"] = locationFiltered;
00193     result["filter_languageFiltered"] = languageFiltered;
00194     result["filter_userPartitionFiltered"] = userPartitionFiltered;
00195     result["filter_dataProfileFiltered"] = dataProfileFiltered;
00196     result["filter_exchangeFiltered"] = exchangeFiltered;
00197     result["filter_augmentationTagsExcluded"] = augmentationTagsExcluded;
00198     result["filter_userBlacklisted"] = userBlacklisted;
00199     result["filter_segmentsMissing"] = segmentsMissing;
00200     result["filter_segmentFiltered"] = segmentFiltered;
00201 
00202     result["bidErrors"] = bidErrors;
00203     result["unknownWins"] = unknownWins;
00204     result["unknownLosses"] = unknownLosses;
00205     result["intoFilters"] = intoFilters;
00206     result["passedStaticFilters"] = passedStaticFilters;
00207     result["passedStaticFiltersPhase1"] = passedStaticPhase1;
00208     result["passedStaticFiltersPhase2"] = passedStaticPhase2;
00209     result["passedStaticFiltersPhase3"] = passedStaticPhase3;
00210     result["passedDynamicFilters"] = passedDynamicFilters;
00211 
00212     result["filter1Excluded"] = filter1Excluded;
00213     result["filter2Excluded"] = filter2Excluded;
00214     result["filternExcluded"] = filternExcluded;
00215 
00216     result["requiredAugmentorIsMissing"] = requiredAugmentorIsMissing;
00217     result["augmentorValueIsNull"] = augmentorValueIsNull;
00218 
00219     return result;
00220 }
00221 
00222 Json::Value
00223 FormatInfo::
00224 toJson() const
00225 {
00226     Json::Value result;
00227     result["numSpots"] = numSpots;
00228     result["numBids"] = numBids;
00229     return result;
00230 }
00231 
00232 
00233 /*****************************************************************************/
00234 /* BIDDABLE SPOTS                                                            */
00235 /*****************************************************************************/
00236 
00237 Json::Value
00238 BiddableSpots::
00239 toJson() const
00240 {
00241     /* Convert to JSON to send it on. */
00242     Json::Value result;
00243 
00244     for (unsigned i = 0;  i < size();  ++i) {
00245         Json::Value & val = result[i];
00246         val["spot"] = (*this)[i].first;
00247         Json::Value & creatives = val["creatives"];
00248         for (unsigned j = 0;  j < (*this)[i].second.size();
00249              ++j)
00250             creatives[j] = (*this)[i].second[j];
00251     }
00252 
00253     return result;
00254 }
00255 
00256 std::string
00257 BiddableSpots::
00258 toJsonStr() const
00259 {
00260     /* Convert to JSON to send it on. */
00261     std::string result = "[";
00262     result.reserve(512);
00263 
00264     for (unsigned i = 0;  i < size();  ++i) {
00265         if (i != 0) result += ",";
00266         result += format("{\"spot\":%d,\"creatives\":[", (*this)[i].first);
00267         for (unsigned j = 0;  j < (*this)[i].second.size();
00268              ++j) {
00269             if (j > 0) result += ",";
00270             result += format("%d", (*this)[i].second[j]);
00271         }
00272         result += "]}";
00273     }
00274     result += "]";
00275 
00276     return result;
00277 }
00278 
00279 } // namespace RTBKIT
00280 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator