Files
spark/src/lib/md/Diag/ShmDumper.cpp
T

212 lines
6.6 KiB
C++

#ifndef NO_CPP0X
#include "ShmDumper.h"
#include "../Shm/MDStructure.h"
#include "lib/io/JsonDumper.h"
#include "lib/logging/logging.h"
#include "lib/utils/SharedList.h"
#include <iomanip>
#include <iostream>
namespace bts {
namespace md {
using namespace utils;
using namespace std;
using namespace api;
using bts::io::JsonDumper;
ShmDumper::ShmDumper(const string &name) : _name(name) {}
ShmDumper::~ShmDumper() {}
double dump(TimeStamp time) { return (TimeStamp::now() - time).seconds(); }
string dump(IntPrice price) {
stringstream res;
res << std::setprecision(11) << price.price();
return res.str();
}
string dump(PriceSize ps) {
stringstream res;
res << "{";
res << "'price': " << std::setprecision(11) << ps.price();
if (!ps.is_nan()) {
res << " (^" << (int)ps.exponent() << ")";
}
res << ", 'size': " << ps.size() << " ";
// res << "'mant': " << ps.mantissa() << ", ";
// res << "'exp': " << (int)ps.exponent() << " ";
res << "}";
return res.str();
}
string dump(const AtomicPriceSize &pss) {
return dump(std::get<PriceSize>(pss.load()));
}
string dump_header(const ColdHeader &header) {
return JsonDumper::dump(header);
}
void write_date_string(
uint32_t year, uint32_t month, uint32_t day, std::ostringstream &result);
string ShmDumper::dump_security(const FixedSecurity &security) {
ostringstream res;
res << "{";
res << "'key': " << security.key << ", ";
res << "'symbol': '" << security.symbol() << "', ";
res << "'type': '" << api::SecurityType_Name(security.type) << "', ";
res << "'exchange': '" << api::Venue_Name(security.exchange) << "', ";
res << "'alt_symbol': '" << security.alt_symbol() << "', ";
res << "'cfi_code': '" << security.cfi_code() << "', ";
res << "'security_desc': '" << security.security_desc() << "', ";
res << "'platform_id': '" << security.platform_id() << "', ";
res << "'display_factor': " << (int)security.display_factor << ", ";
res << "'numeric_id': " << security.numeric_id << ", ";
res << "'cboe_class_key': " << security.cboe_class_key << ", ";
res << "'main_frac': " << (int)security.cme_main_fraction << ", ";
res << "'sub_fract': " << (int)security.cme_sub_fraction << ", ";
res << "'price_format': " << (int)security.cme_price_display_format << ", ";
res << "'market_segment': " << (int)security.cme_market_segment_id << ", ";
res << "'price_break': " << security.price_break << ", ";
res << "'low_tick': " << security.low_tick << ", ";
res << "'high_tick': " << security.high_tick << ", ";
res << "'cpp': " << security.cash_per_point << ", ";
res << "'market_description ': " << security.ice_market_description()
<< ", ";
res << "'cleared_alias ': " << security.ice_cleared_alias() << ", ";
res << "'product_description ': " << security.ice_product_name() << ", ";
res << "'hub_alias ': " << security.ice_hub_alias() << ", ";
res << "'strip_name': " << security.ice_strip_name() << ", ";
res << "'mic_code': " << security.mic_code() << ", ";
switch (security.type) {
case SECURITYTYPE_SPOT:
break;
case SECURITYTYPE_FUTURE: {
auto *psec = static_cast<const FixedFutureSecurity *>(&security);
res << "'expiration': '";
write_date_string(
psec->expiration_year, psec->expiration_month, psec->expiration_day,
res);
res << "' ";
break;
}
case SECURITYTYPE_OPTION: {
auto *psec = static_cast<const FixedOptionSecurity *>(&security);
res << "'expiration': '";
write_date_string(
psec->expiration_year, psec->expiration_month, psec->expiration_day,
res);
res << "', ";
res << "'strike': " << psec->strike << ", ";
res << "'callput': '" << api::PutOrCall_Name(psec->call_put) << "' ";
break;
}
case SECURITYTYPE_SPREAD: {
auto *psec = static_cast<const FixedSpreadSecurityXL *>(&security);
res << "'legs': [";
for (size_t x = 0; x < psec->leg_count(); ++x) {
auto &leg = psec->legs[x];
if (x > 0)
res << ",";
res << "{ 'key': " << leg.key << ", 'exkey': " << leg.exchange_key
<< ", 'quantity': " << leg.leg_quantity
<< ", 'leg_price': " << leg.leg_price
<< ", 'delta': " << leg.leg_delta << ", 'side': '"
<< api::Side_Name(leg.leg_side) << "' }";
}
res << "]";
break;
}
default:
break;
}
res << "}";
return res.str();
}
string dump(datetime::Date date) {
stringstream res;
res << "{";
res << "'year': " << date.year << ", ";
res << "'month': " << (int)date.month << ", ";
res << "'day': " << (int)date.day;
res << "}";
return res.str();
}
bool has_value(PriceSize p) { return p.size() > 0; }
bool has_value(const AtomicPriceSize &p) {
return has_value(std::get<PriceSize>(p.load()));
}
string dump_entry(const ColdEntry &entry) { return JsonDumper::dump(entry); }
string dump_entry(const HotDepthEntry &entry) {
return JsonDumper::dump(entry);
}
string dump_entry(const HotEntry &entry) { return JsonDumper::dump(entry); }
void ShmDumper::dump_cold() {
SharedListReader<ColdHeader, ColdEntry> reader(_name + "_cold");
if (!reader.is_initialized()) {
LOG(md, Info, "Unable to initialize section {}", _name);
return;
}
const auto &header = reader.header();
vector<const ColdEntry *> entries;
reader.all_entries(
[&](const ColdEntry &entry) { entries.push_back(&entry); });
nlohmann::json j = {{"header", header}, {"entries", entries}};
std::cout << j.dump(2) << std::endl;
}
template <class TEntry>
void dump_hot_t(SharedListReader<HotHeader, TEntry> &reader) {
if (!reader.is_initialized()) {
LOG(md, Info, "Unable to initialize section ");
return;
}
auto &header = reader.header();
vector<const TEntry *> entries;
reader.all_entries([&](const TEntry &entry) { entries.push_back(&entry); });
nlohmann::json j = {
{"header", header},
{"entries", entries},
};
std::cout << j.dump(2) << std::endl;
}
void ShmDumper::dump_hot() {
SharedListReader<ColdHeader, ColdEntry> cold_reader(_name + "_cold");
if (!cold_reader.is_initialized()) {
LOG(md, Info,
"Unable to initialize section {}_cold - needed to "
"determine metadata.",
_name);
return;
}
const auto &header = cold_reader.header();
if (header.has_depth) {
SharedListReader<HotHeader, HotDepthEntry> reader(_name + "_hot");
dump_hot_t(reader);
} else {
SharedListReader<HotHeader, HotEntry> reader(_name + "_hot");
dump_hot_t(reader);
}
}
} // namespace md
} // namespace bts
#endif