RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 /* bid_request.h -*- C++ -*- 00002 Jeremy Barnes, 1 February 2012 00003 Copyright (c) 2012 Datacratic. All rights reserved. 00004 00005 Bid request class. 00006 */ 00007 00008 #ifndef __rtb__bid_request_h__ 00009 #define __rtb__bid_request_h__ 00010 00011 #include "soa/jsoncpp/json.h" 00012 #include "soa/types/date.h" 00013 #include "soa/types/string.h" 00014 #include "jml/arch/exception.h" 00015 #include "jml/utils/compact_vector.h" 00016 #include "jml/utils/less.h" 00017 #include <boost/function.hpp> 00018 #include "soa/types/id.h" 00019 #include "soa/types/url.h" 00020 #include "rtbkit/common/segments.h" 00021 #include <set> 00022 #include "rtbkit/common/currency.h" 00023 #include "tags.h" 00024 #include "openrtb/openrtb.h" 00025 00026 00027 namespace RTBKIT { 00028 00029 using namespace Datacratic; 00030 00031 typedef ML::compact_vector<uint16_t, 5, uint16_t> SmallIntVector; 00032 typedef ML::compact_vector<uint32_t, 3, uint32_t> IntVector; 00033 00034 00035 /*****************************************************************************/ 00036 /* FORMAT */ 00037 /*****************************************************************************/ 00038 00041 struct Format { 00042 Format(int16_t width = -1, int16_t height = -1) 00043 : width(width), height(height) 00044 { 00045 } 00046 00047 bool valid() const 00048 { 00049 return width > 0 && height > 0; 00050 } 00051 00052 Json::Value getWidthsJson() const; 00053 Json::Value getHeightsJson() const; 00054 void fromJson(const Json::Value & val); 00055 void fromString(const std::string &val); 00056 Json::Value toJson() const; 00057 std::string toJsonStr() const; 00058 std::string print() const; 00059 00060 bool operator == (const Format & other) const 00061 { 00062 return width == other.width && height == other.height; 00063 } 00064 00065 bool operator != (const Format & other) const 00066 { 00067 return ! operator == (other); 00068 } 00069 00070 bool operator < (const Format & other) const 00071 { 00072 return ML::less_all(width, other.width, 00073 height, other.height); 00074 } 00075 00076 int16_t width; 00077 int16_t height; 00078 00079 void serialize(ML::DB::Store_Writer & store) const; 00080 void reconstitute(ML::DB::Store_Reader & store); 00081 }; 00082 00083 IMPL_SERIALIZE_RECONSTITUTE(Format); 00084 00085 00086 /*****************************************************************************/ 00087 /* FORMAT SET */ 00088 /*****************************************************************************/ 00089 00090 struct FormatSet : public ML::compact_vector<Format, 3, uint16_t> { 00091 00092 bool compatible(const Format & format) const 00093 { 00094 for (auto it = begin(), end = this->end(); it != end; ++it) 00095 if (*it == format) return true; 00096 return false; 00097 } 00098 00099 void fromJson(const Json::Value & val); 00100 Json::Value toJson() const; 00101 std::string toJsonStr() const; 00102 std::string print() const; 00103 00104 void sort(); 00105 00106 void serialize(ML::DB::Store_Writer & store) const; 00107 void reconstitute(ML::DB::Store_Reader & store); 00108 }; 00109 00110 IMPL_SERIALIZE_RECONSTITUTE(FormatSet); 00111 00112 ValueDescriptionT<RTBKIT::FormatSet> * 00113 getDefaultDescription(RTBKIT::FormatSet * = 0); 00114 00115 00116 00117 /*****************************************************************************/ 00118 /* AD SPOT */ 00119 /*****************************************************************************/ 00120 00123 struct AdSpot: public OpenRTB::Impression { 00124 AdSpot() 00125 { 00126 } 00127 00128 AdSpot(OpenRTB::Impression && imp) 00129 : OpenRTB::Impression(std::move(imp)) 00130 { 00131 } 00132 00133 void fromJson(const Json::Value & val); 00134 Json::Value toJson() const; 00135 std::string toJsonStr() const; 00136 static AdSpot createFromJson(const Json::Value & json); 00137 00138 std::string format() const; 00139 std::string firstFormat() const; 00140 00142 FormatSet formats; 00143 00145 OpenRTB::AdPosition position; 00146 00148 Amount reservePrice; 00149 00151 Tags tags; 00152 00154 TagFilter tagFilter; 00155 00156 void serialize(ML::DB::Store_Writer & store) const; 00157 void reconstitute(ML::DB::Store_Reader & store); 00158 }; 00159 00160 IMPL_SERIALIZE_RECONSTITUTE(AdSpot); 00161 00162 00163 /*****************************************************************************/ 00164 /* ID DOMAIN */ 00165 /*****************************************************************************/ 00166 00168 enum IdDomain { 00169 ID_PROVIDER, 00170 ID_EXCHANGE, 00171 ID_MAX 00172 }; 00173 00174 00175 /*****************************************************************************/ 00176 /* USER IDS */ 00177 /*****************************************************************************/ 00178 00181 struct UserIds : public std::map<std::string, Id> { 00182 00183 void add(const Id & id, IdDomain domain); 00184 void add(const Id & id, const std::string & domain); 00185 void add(const Id & id, const std::string & domain, IdDomain domain2); 00186 00187 void set(const Id & id, const std::string & domain); 00188 00189 // These are always present 00190 Id exchangeId; 00191 Id providerId; 00192 00194 Json::Value toJson() const; 00195 00197 std::string toJsonStr() const; 00198 00199 std::string toString() const 00200 { 00201 return toJsonStr(); 00202 } 00203 00204 static UserIds createFromJson(const Json::Value & json); 00205 00207 void setStatic(const Id & id, const std::string & domain); 00208 void setStatic(const Id & id, IdDomain domain); 00209 00210 static const char * domainToString(IdDomain domain); 00211 00212 std::string serializeToString() const; 00213 static UserIds createFromString(const std::string & str); 00214 00215 void serialize(ML::DB::Store_Writer & store) const; 00216 void reconstitute(ML::DB::Store_Reader & store); 00217 }; 00218 00219 IMPL_SERIALIZE_RECONSTITUTE(UserIds); 00220 00221 ValueDescriptionT<RTBKIT::UserIds> * 00222 getDefaultDescription(RTBKIT::UserIds * = 0); 00223 00224 00225 00226 /*****************************************************************************/ 00227 /* LOCATION */ 00228 /*****************************************************************************/ 00229 00230 struct Location { 00231 Location() 00232 : cityName(""),dma(-1), timezoneOffsetMinutes(-1) 00233 { 00234 } 00235 00236 std::string countryCode; 00237 std::string regionCode; 00238 Utf8String cityName; 00239 std::string postalCode; 00240 00241 int dma; 00242 int timezoneOffsetMinutes; 00243 00244 static Location createFromJson(const Json::Value & json); 00245 00247 Utf8String fullLocationString() const; 00248 00250 Json::Value toJson() const; 00251 00253 std::string toJsonStr() const; 00254 00255 void serialize(ML::DB::Store_Writer & store) const; 00256 void reconstitute(ML::DB::Store_Reader & store); 00257 }; 00258 00259 IMPL_SERIALIZE_RECONSTITUTE(Location); 00260 00261 00262 using OpenRTB::AuctionType; 00263 00264 /*****************************************************************************/ 00265 /* BID REQUEST */ 00266 /*****************************************************************************/ 00267 00268 struct BidRequest { 00269 BidRequest() 00270 : auctionType(AuctionType::SECOND_PRICE), timeAvailableMs(0.0), 00271 isTest(false) 00272 { 00273 } 00274 00275 Id auctionId; 00276 AuctionType auctionType; 00277 double timeAvailableMs; 00278 Date timestamp; 00279 bool isTest; 00280 00281 std::string protocolVersion; 00282 std::string exchange; 00283 std::string provider; 00284 00285 /* The following fields indicate the contents of the OpenRTB bid request 00286 that is being processed. 00287 */ 00288 00292 OpenRTB::Optional<OpenRTB::Site> site; 00293 00297 OpenRTB::Optional<OpenRTB::App> app; 00298 00300 OpenRTB::Optional<OpenRTB::Device> device; 00301 00303 OpenRTB::Optional<OpenRTB::User> user; 00304 00306 std::vector<AdSpot> imp; 00307 00308 00309 /* The following fields are all mirrored from the information in the rest 00310 of the bid request. They provide a way for the bid request parser to 00311 indicate the value of commonly used values in such a way that any 00312 optimization algorithm can make use of them. 00313 */ 00314 00315 std::string language; 00316 Location location; 00317 Url url; 00318 std::string ipAddress; 00319 std::string userAgent; 00320 00325 UserIds userIds; 00326 00327 SegmentsBySource restrictions; 00328 00329 00333 SegmentsBySource segments; 00334 00335 Json::Value meta; 00336 00341 Json::Value unparseable; 00342 00344 std::vector<CurrencyCode> bidCurrency; 00345 00349 LineItems winSurcharges; 00350 00352 Json::Value toJson() const; 00353 00355 std::string toJsonStr() const; 00356 00358 static BidRequest createFromJson(const Json::Value & json); 00359 00361 Id getUserId(IdDomain domain) const; 00362 Id getUserId(const std::string & domain) const; 00363 00365 int findAdSpotIndex(const Id & adSpotId) const 00366 { 00367 for (unsigned i = 0; i < imp.size(); ++i) 00368 if (imp[i].id == adSpotId) 00369 return i; 00370 return -1; 00371 } 00372 00374 SegmentResult 00375 segmentPresent(const std::string & source, 00376 const std::string & segment) const; 00377 00379 SegmentResult 00380 segmentPresent(const std::string & source, int segment) const; 00381 00382 void sortAll(); 00383 00384 typedef boost::function<BidRequest * (const std::string)> Parser; 00385 00389 static void registerParser(const std::string & source, 00390 Parser parser); 00391 00395 static BidRequest * 00396 parse(const std::string & source, const std::string & bidRequest); 00397 00398 static BidRequest * 00399 parse(const std::string & source, const Utf8String & bidRequest); 00400 00401 void serialize(ML::DB::Store_Writer & store) const; 00402 void reconstitute(ML::DB::Store_Reader & store); 00403 00404 std::string serializeToString() const; 00405 static BidRequest createFromString(const std::string & str); 00406 }; 00407 00408 IMPL_SERIALIZE_RECONSTITUTE(BidRequest); 00409 00410 } // namespace RTBKIT 00411 00412 namespace Datacratic { 00413 00414 template<> 00415 struct DefaultDescription<RTBKIT::Location> 00416 : public StructureDescription<RTBKIT::Location> { 00417 00418 DefaultDescription(); 00419 }; 00420 00421 template<> 00422 struct DefaultDescription<RTBKIT::Format> 00423 : public StructureDescription<RTBKIT::Format> { 00424 00425 DefaultDescription(); 00426 }; 00427 00428 template<> 00429 struct DefaultDescription<RTBKIT::AdSpot> 00430 : public StructureDescription<RTBKIT::AdSpot> { 00431 00432 DefaultDescription(); 00433 }; 00434 00435 template<> 00436 struct DefaultDescription<RTBKIT::BidRequest> 00437 : public StructureDescription<RTBKIT::BidRequest> { 00438 00439 DefaultDescription(); 00440 }; 00441 00442 00443 } // namespace Datacratic 00444 00445 #endif /* __rtb__auction_h__ */