cli: Add cmd to print security info from the db to console

Usage: sparkd md dump --db
This commit is contained in:
John Colvin committed 2025-01-03 16:59:07 +00:00
1 parent 4a285fa581
commit 73356e0d61
4 files changed
+141 -4

No files matched your search

+25 -2
View File
@@ -1,15 +1,22 @@
#pragma once
#include "lib/domain_model/Security.pb.h"
#include "lib/core/FMT_IMPL.h"
#include "lib/core/EnumMacros.h"
#include "lib/core/FMT_IMPL.h"
#include "lib/domain_model/Security.pb.h"
#include "lib/io/JsonReader.h"
#include "lib/logging/logging.h"
#include "lib/utils/string_utils.h"
#include <nlohmann/json.hpp>
namespace bts::api {
/**
* Parses Venue from a string. Unlike `bts::api::SecurityType_Parse`, this implementation is case
* insensitive, and makes the 'SECURITYTYPE_' prefix optional.
*/
bool security_type_parse_smart(const std::string &str, SecurityType *result);
PB_ENUM_FROM_JSON(SecurityType)
PB_ENUM_TO_JSON(SecurityType)
@@ -19,6 +26,22 @@ std::ostream &operator<<(std::ostream &out, const SecurityType &type) noexcept;
// Definition
//////////////////////////////////////////////////
inline bool security_type_parse_smart(
const std::string &str, SecurityType *result) {
if (SecurityType_Parse(str, result)) {
return true;
}
std::string s = bts::system::string_utils::copy_to_upper(str);
if (SecurityType_Parse(s, result)) {
return true;
}
const char *prefix = "SECURITYTYPE_";
if (!s.starts_with(prefix)) {
s.insert(0, prefix);
}
return SecurityType_Parse(s, result);
}
inline std::ostream &operator<<(
std::ostream &out, const SecurityType &type) noexcept {
out << SecurityType_Name(type);
+22
View File
@@ -5,11 +5,18 @@
#include "lib/domain_model/Security.pb.h"
#include "lib/io/JsonReader.h"
#include "lib/logging/logging.h"
#include "lib/utils/string_utils.h"
#include <nlohmann/json.hpp>
namespace bts::api {
/**
* Parses Venue from a string. Unlike `bts::api::Venue_Parse`, this implementation is case
* insensitive, and makes the 'VENUE_' prefix optional.
*/
bool venue_parse_smart(const std::string &str, Venue *result);
PB_ENUM_FROM_JSON(Venue)
PB_ENUM_TO_JSON(Venue)
@@ -19,6 +26,21 @@ std::ostream &operator<<(std::ostream &out, Venue venue) noexcept;
// Definition
//////////////////////////////////////////////////
inline bool venue_parse_smart(const std::string &str, Venue *result) {
if (Venue_Parse(str, result)) {
return true;
}
std::string s = bts::system::string_utils::copy_to_upper(str);
if (Venue_Parse(s, result)) {
return true;
}
const char *prefix = "VENUE_";
if (!s.starts_with(prefix)) {
s.insert(0, prefix);
}
return Venue_Parse(s, result);
}
inline std::ostream &operator<<(std::ostream &out, Venue venue) noexcept {
out << Venue_Name(venue);
return out;
+18
View File
@@ -14,6 +14,9 @@ bool copy_to_array(
template <class UIntType>
bool parse_uint(const std::string &s, UIntType &i);
void to_upper(std::string &str);
std::string copy_to_upper(const std::string &str);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
@@ -83,4 +86,19 @@ inline bool parse_uint(const std::string &s, UIntType &i) {
return true;
}
inline void to_upper(std::string &str) {
for (char &c : str) {
c = toupper(c);
}
}
inline std::string copy_to_upper(const std::string &str) {
std::string s;
s.reserve(str.size());
for (char c : str) {
s.push_back(toupper(c));
}
return s;
}
} // namespace bts::system::string_utils
@@ -1,6 +1,9 @@
#include "DumpSecurity.h"
#include "lib/config/protos/market_data.pb.h"
#include "lib/core/TableFormatter.h"
#include "lib/domain_model/Security.h"
#include "lib/domain_model/Security.pb.h"
#include "lib/domain_model/SecurityType.h"
#include "lib/md/Diag/ShmDumper.h"
#include "lib/md/SecurityDB.h"
#include "lib/md/SymbolLoader.h"
@@ -10,7 +13,11 @@
namespace bts::cmds::md::cmds {
using bts::api::Security;
using bts::api::SecurityType_Name;
using bts::md::SymbolLoader;
using bts::securities::security_type;
using std::make_tuple;
template <class EnumT>
using EnumNameMap = std::map<std::string, EnumT>;
@@ -36,8 +43,40 @@ void print_securities(std::function<bool(const api::Security &)> predicate) {
});
}
void print_db_securities(std::function<bool(const api::Security &)> predicate) {
std::vector<bts::api::Security> secs;
const bts::core::TableFormatter::Schema<bts::api::Security> schema{
make_tuple(
"id", 12, [](const Security &s) { return std::to_string(s.id()); }),
make_tuple(
"type", 12,
[](const Security &s) {
return SecurityType_Name(security_type(s)).substr(13);
}),
make_tuple(
"venue", 18,
[](const Security &s) {
return Venue_Name(s.exchange()).substr(6);
}),
make_tuple(
"name", 32,
[](const Security &s) { return bts::securities::descriptor(s); }),
};
auto on_sec = [&](const bts::api::Security &sec) {
if (!predicate(sec)) {
return;
}
secs.push_back(sec);
};
bts::md::db::load_securities([](auto) { return true; }, on_sec);
bts::core::TableFormatter::format(std::cout, secs, schema);
}
struct DumpSecurity::Args {
bool all;
bool db = false;
std::vector<std::string> types;
std::vector<std::string> venues;
std::vector<uint32_t> security_ids;
};
@@ -45,14 +84,49 @@ DumpSecurity::DumpSecurity()
: CLI::App("Print information about securities", "dump-security"),
_args(new DumpSecurity::Args()) {
add_flag("-a,--all", _args->all, "Print information about all securities");
add_flag(
"--db", _args->db,
"Print information about securities in the db (on disk)");
add_option("--types", _args->types, "The types of securities to show");
add_option("--venues", _args->venues, "The venues to show");
add_option(
"--ids,--security-ids", _args->security_ids,
"Print information about specific securities");
callback([&] {
if (_args->all)
if (_args->all) {
print_securities([](const auto &) { return true; });
else {
} else if (_args->db) {
std::vector<api::SecurityType> types;
for (const auto &t : _args->types) {
if (!api::security_type_parse_smart(t, &types.emplace_back())) {
types.pop_back();
}
}
std::vector<api::Venue> venues;
for (const auto &t : _args->venues) {
if (!api::venue_parse_smart(t, &venues.emplace_back())) {
venues.pop_back();
}
}
print_db_securities([&](const auto &s) {
if (!types.empty()) {
auto st = bts::securities::security_type(s);
if (!std::any_of(
types.begin(), types.end(),
[st](api::SecurityType _st) { return _st == st; })) {
return false;
}
}
if (!venues.empty() &&
!std::any_of(
venues.begin(), venues.end(),
[v = s.exchange()](api::Venue _v) { return _v == v; })) {
return false;
}
return true;
});
} else {
std::unordered_set<uint32_t> ids;
for (auto id : _args->security_ids)
ids.insert(id);