Files
spark/src/lib/domain/SecurityCriteriaClause.cpp
T

248 lines
8.0 KiB
C++

#include "SecurityCriteriaClause.h"
#include "lib/api/domain/SecurityCriteria.pb.h"
#include "lib/api/domain/SecurityCriteriaClause.pb.h"
#include "lib/api/domain/SecurityType.h"
#include "lib/api/domain/Venue.h"
#include "lib/collections/Contains.h"
#include "lib/core/FMT.h"
#include "lib/domain/Security.h"
#include "lib/domain_model/Security.pb.h"
#include <range/v3/view.hpp>
#include <regex>
namespace bts::domain {
#define SEC_TYPE(x) bts::api::domain::SECURITY_TYPE_##x
#define VENUE(x) bts::api::domain::VENUE_##x
using bts::api::domain::SecurityType;
using std::regex_search;
using std::smatch;
using std::string;
#define CONTAINS(container, value) \
(std::find(container.begin(), container.end(), value) != container.end())
bool SecurityCriteriaClause::is_defined() const {
return !venues.empty() || !types.empty() || !assets.empty() ||
!groups.empty() || !ids.empty() || !names.empty() ||
!name_regexps.empty();
}
bool SecurityCriteriaClause::includes(const Security &sec) const {
if (!is_defined()) {
return false;
}
if (types.size() && !CONTAINS(types, sec.type())) {
return false;
}
if (assets.size() && !CONTAINS(assets, sec.asset())) {
return false;
}
if (groups.size() && !CONTAINS(groups, sec.group())) {
return false;
}
if (!currencies.empty() &&
!CONTAINS(currencies, sec.security_common.currency)) {
return false;
}
if (ids.size() && !ids.count(sec.id())) {
return false;
}
if (!names.empty() || !name_regexps.empty()) {
if (!names.count(sec.desc()) &&
!std::any_of(
name_regexps.begin(), name_regexps.end(),
[&sec](const auto &rgx) {
smatch match;
return regex_search(sec.desc(), match, rgx.second);
})) {
return false;
}
}
return true;
}
bool SecurityCriteriaClause::includes(
const bts::api::domain::Venue venue,
const SecurityType type,
const string &asset) const {
if (!is_defined()) {
return false;
}
if (venue != VENUE(UNSPECIFIED) && !venues.empty() &&
!CONTAINS(venues, venue)) {
return false;
}
if (type != SEC_TYPE(UNSPECIFIED) && !types.empty() &&
!CONTAINS(types, type)) {
return false;
}
if (!asset.empty() && !assets.empty() && !CONTAINS(assets, asset)) {
return false;
}
return true;
}
std::optional<bool> SecurityCriteriaClause::includes(const string &name) const {
if (names.empty() && name_regexps.empty()) {
return std::nullopt;
}
return names.count(name) > 0 ||
std::any_of(
name_regexps.begin(), name_regexps.end(),
[&name](const auto &rgx) {
smatch match;
return regex_search(name, match, rgx.second);
});
}
SecurityCriteriaClause SecurityCriteriaClause::from_proto(
const api::domain::SecurityCriteriaClause &api_clause) {
// Assert reasonable params ('SecurityCriteriaClause::include' calls should
// be cheap). These limits are somewhat arbitrary. Adjust them as needed.
static const int MAX_VENUES_COUNT = 16;
static const int MAX_TYPES_COUNT = 8;
static const int MAX_SYMBOLS_COUNT = 32;
static const int MAX_NAMES_COUNT = 64;
static const int MAX_NAME_REGEXPS_COUNT = 16;
bts_assert(api_clause.venues().size() <= MAX_VENUES_COUNT);
bts_assert(api_clause.security_types().size() <= MAX_TYPES_COUNT);
bts_assert(api_clause.symbols().size() <= MAX_SYMBOLS_COUNT);
bts_assert(api_clause.names().size() <= MAX_NAMES_COUNT);
bts_assert(api_clause.name_regexps().size() <= MAX_NAME_REGEXPS_COUNT);
SecurityCriteriaClause rv;
for (auto venue : api_clause.venues()) {
rv.venues.push_back(static_cast<api::domain::Venue>(venue));
}
for (auto type : api_clause.security_types()) {
rv.types.push_back(static_cast<api::domain::SecurityType>(type));
}
for (const auto &sym : api_clause.symbols()) {
rv.assets.emplace_back(sym);
rv.groups.emplace_back(sym);
}
for (auto name : api_clause.names()) {
rv.names.insert(std::move(name));
}
for (const auto &name_regexp : api_clause.name_regexps()) {
rv.name_regexps.emplace_back(name_regexp, std::regex{name_regexp});
}
for (auto currency : api_clause.currencies()) {
if (CONTAINS(rv.currencies, currency)) {
continue;
}
rv.currencies.push_back(std::move(currency));
}
return rv;
}
std::string SecurityCriteriaClause::to_expression_str() const {
std::stringstream out;
std::vector<std::string> and_parts;
if (!venues.empty()) {
and_parts.push_back(
FMT("(venue == {})", FMT_JOIN(venues, " || venue == ")));
}
if (!types.empty()) {
and_parts.push_back(FMT("(type == {})", FMT_JOIN(types, " || type == ")));
}
if (!assets.empty()) {
and_parts.push_back(
FMT("(asset == {})", FMT_JOIN(assets, " || asset == ")));
}
if (!groups.empty()) {
and_parts.push_back(
FMT("(group == {})", FMT_JOIN(groups, " || group == ")));
}
if (!currencies.empty()) {
and_parts.push_back(
FMT("(currency == {})", FMT_JOIN(currencies, " || currency == ")));
}
if (!ids.empty()) {
std::vector<std::int64_t> ids_vec;
for (auto id : ids) {
ids_vec.push_back(id.value());
}
and_parts.push_back(FMT("(id == {})", FMT_JOIN(ids_vec, " || id == ")));
}
// Combine names and name_regexps into an 'or' clause. If both fields are
// populated, the security name can match with either.
if (!names.empty() || !name_regexps.empty()) {
std::vector<string> or_parts;
if (!names.empty()) {
or_parts.push_back(
FMT("(name == {})", FMT_JOIN(names, " || name == ")));
}
if (!name_regexps.empty()) {
auto it = name_regexps.begin();
string name_regexps_str{it->first};
while (++it != name_regexps.end()) {
name_regexps_str.append(" || name ~= ");
name_regexps_str.append(it->first);
}
or_parts.push_back(FMT("(name ~= {})", name_regexps_str));
}
if (or_parts.size() == 2) {
and_parts.push_back(FMT("({})", FMT_JOIN(or_parts, " || ")));
} else {
and_parts.push_back(std::move(or_parts.at(0)));
}
}
return FMT_JOIN(and_parts, " && ");
}
std::string SecurityCriteriaClause::to_expression_str_h() const {
std::vector<std::string> and_parts;
if (!venues.empty()) {
and_parts.push_back(FMT("venue IN ({})", FMT_JOIN(venues, ", ")));
}
if (!types.empty()) {
and_parts.push_back(FMT("type IN ({})", FMT_JOIN(types, ", ")));
}
if (!assets.empty()) {
and_parts.push_back(FMT("asset IN ({})", FMT_JOIN(assets, ", ")));
}
if (!groups.empty()) {
and_parts.push_back(FMT("group IN ({})", FMT_JOIN(groups, ", ")));
}
if (!currencies.empty()) {
and_parts.push_back(FMT("currency IN ({})", FMT_JOIN(currencies, ", ")));
}
if (!ids.empty()) {
std::vector<SecurityID> ids_vec{ids.begin(), ids.end()};
and_parts.push_back(FMT("id IN ({})", FMT_JOIN(ids_vec, ", ")));
}
if (!names.empty() || !name_regexps.empty()) {
std::vector<string> or_parts;
if (!names.empty()) {
or_parts.push_back(FMT("name IN ({})", FMT_JOIN(names, ", ")));
}
if (!name_regexps.empty()) {
auto it = name_regexps.begin();
string name_regexps_str{it->first};
while (++it != name_regexps.end()) {
name_regexps_str.append(", ");
name_regexps_str.append(it->first);
}
or_parts.push_back(FMT("name ~IN ({})", name_regexps_str));
}
if (or_parts.size() == 2) {
and_parts.push_back(FMT("({})", FMT_JOIN(or_parts, " OR ")));
} else {
and_parts.push_back(std::move(or_parts.at(0)));
}
}
return FMT_JOIN(and_parts, " AND ");
}
std::ostream &operator<<(
std::ostream &out, const SecurityCriteriaClause &clause) noexcept {
out << clause.to_expression_str_h();
return out;
}
} // namespace bts::domain