Files
spark/src/lib/api/service_helpers/AdminServiceUtils.h
T

360 lines
16 KiB
C++

#pragma once
#include "lib/api/services/AdminService.server.pb.h"
#include "lib/api/services/OrderService.server.pb.h"
#include "lib/domain_model/Security.h"
#include <memory>
namespace bts {
namespace admin {
using namespace bts::api::admin;
using namespace bts::api::orderman;
class AdminServiceUtils {
public:
////////////////////////////////////////////
// connection accessors ////////////////////
////////////////////////////////////////////
// finds the max id out of all the existing connections
// (todo: deleted connections)
static uint32_t find_max_connection_id(const AdminData &data);
// finds a connection by name and returns a mutable pointer to it
static const ExchangeConnection *
find_connection(const AdminData &data, const std::string &connection_name);
// finds the presence of an exchange in a connection
static bool
has_exchange(const ExchangeConnection &conn, bts::api::Venue exchange);
// finds the default connections for an exchange
// in a given account
static const ExchangeConnection *find_default_account_connection(
const AdminData &data,
const BTSAccount &account,
bts::api::Venue exchange);
static const ExchangeConnection *resolve_cme_connection(
const AdminData &data,
const BTSAccount &account,
const api::Security &sec);
static const ExchangeConnection *resolve_ice_connection(
const AdminData &data,
const BTSAccount &account,
const api::Security &sec);
static const ExchangeConnection *resolve_sim_connection(
const AdminData &data,
const BTSAccount &account,
const api::Security &sec);
static const ExchangeConnection *resolve_connection(
const AdminData &data,
const BTSAccount &account,
const bts::api::Security &sec);
////////////////////////////////////////////
// connection mutators /////////////////////
////////////////////////////////////////////
// adds a new connection (does not check for name uniqueness)
static ExchangeConnection *
add_connection(AdminData &data, const ExchangeConnection &new_connection);
// reads the connections from config and fills in the
// admin data (with names only)
static void fill_connections_from_config(AdminData &admin_data);
////////////////////////////////////////////
// account accessors ///////////////////////
////////////////////////////////////////////
// finds the account from the name and returns
// a mutable pointer to the account
static BTSAccount *find_account(
AdminData &data,
const std::string &acct_name,
bool include_deleted = false);
static std::map<std::string, uint32_t>
get_clearing_account_map(const AdminData &);
// finds the account from a clearing account (and its venue)
static BTSAccount const *find_account(
const AdminData &data,
const std::string &clearing_account,
bts::api::Venue);
// same as above for const pointer
static const BTSAccount *find_account(
const AdminData &data,
const std::string &acct_name,
bool include_deleted = false);
// finds the account from the id and returns
// a mutable pointer to the account
static BTSAccount *
find_account(AdminData &data, uint32_t id, bool include_deleted = false);
// like above but returns a const pointer
static const BTSAccount *find_account(
const AdminData &data, uint32_t id, bool include_deleted = false);
// this function is an indirection layer for account access
// so deletions are dealt in a more deliberate manner
static std::vector<const BTSAccount *>
accounts(const AdminData &data, bool include_deleted);
static std::vector<BTSAccount *>
accounts_mutable(AdminData &data, bool include_deleted);
// finds the max id amongts all the existing accounts
static uint32_t find_max_acct_id(const AdminData &data);
// gets the account data for cme (and goes up the hierarchy
// if needed)
static const CMEAccountData *
get_account_cme_data(const BTSAccount &account, const AdminData &data);
static const ICEAccountData *
get_account_ice_data(const BTSAccount &account, const AdminData &data);
static const SIMAccountData *
get_account_sim_data(const BTSAccount &account, const AdminData &data);
// returns true if an account has a connection
bool static account_has_connection(
const BTSAccount &account, const std::string &connection_name);
// finds the external/clearing account associated
// with an account
static std::string find_account_exchange_account(
const AdminData &data,
const BTSAccount &account,
api::Venue exchange,
AdminServiceOperationResult &out_result);
// returns a map of the accounts vs their 'is_active'
static std::map<uint32_t, bool> accounts_map(const AdminData &admin_data);
// validates that the update to the account is valid
static void validate_account_update(
const AdminData &admin_data,
const BTSAccount &updated_account,
AdminServiceOperationResult &result);
// validates that the account exchange changes are valid
// (ex > 12 characters for accounts at CME)
static void validate_account_exchange_data(
const BTSAccount &account, AdminServiceOperationResult &out_result);
static RoutingRecord get_routing_record(
const AdminData &data,
const std::string &account_name,
const std::string &user_name,
const bts::api::Security &sec,
AdminServiceOperationResult &out_result);
static RoutingRecord get_routing_record(
const AdminData &data,
uint32_t account_id,
uint32_t user_id,
const bts::api::Security &sec,
AdminServiceOperationResult &out_result);
////////////////////////////////////////////
// account mutators ////////////////////////
////////////////////////////////////////////
static BTSAccount *create_account(
AdminData &data,
uint32_t parent_account_id,
const std::string &account_name,
const std::string &description,
bool is_active,
AdminServiceCreateAccountResult &result);
// writes over the account data completely
static void update_account(
AdminData &data,
const BTSAccount &updated_account,
AdminServiceOperationResult &result);
static void delete_account(
AdminData &admin_data,
const IDAPIArgument &id,
AdminServiceOperationResult &result);
static void set_account_exchange_data(
BTSAccount *account, const ExchangeAccountData &exchange_data);
// this is not a generally useful function but since
// connection are in config, this cleans up the account's
// connections and syncs it with the list in AdminData
static void
sync_account_connections(const AdminData &data, BTSAccount &account);
////////////////////////////////////////////
// group accessors /////////////////////////
////////////////////////////////////////////
// given an id, finds the mutable pointer to a group
static BTSUserGroup *find_group(AdminData &data, uint32_t id);
// given an id, finds the const pointer to a group
static const BTSUserGroup *const
find_group(const AdminData &data, uint32_t id);
// given a name, finds the mutable pointer to a group
static BTSUserGroup *
find_group(AdminData &data, const std::string &group_name);
// finds the max id out of all the groups
static uint32_t find_max_group_id(const AdminData &data);
// travels up the group tree until it finds a populated cme data
static const CMEUserData *
get_group_cme_data(const BTSUserGroup &group, const AdminData &data);
static const ICEUserData *
get_group_ice_data(const BTSUserGroup &group, const AdminData &data);
static const SIMUserData *
get_group_sim_data(const BTSUserGroup &group, const AdminData &data);
static const UserAccountPermissions *const get_group_user_permissions(
const bts::api::admin::AdminData &admin_data, uint32_t group_id);
////////////////////////////////////////////
// group mutators/ /////////////////////////
////////////////////////////////////////////
static BTSUserGroup *create_group(
AdminData &data,
uint32_t parent_group_id,
const std::string &group_name,
AdminServiceOperationResult &result);
////////////////////////////////////////////
// user accessors //////////////////////////
////////////////////////////////////////////
static BTSUser *find_user(
AdminData &data,
const std::string &user_name,
bool include_deleted = false);
static const BTSUser *find_user(
const AdminData &data,
const std::string &user_name,
bool include_deleted = false);
static BTSUser *
find_user(AdminData &data, uint32_t id, bool include_deleted = false);
static const BTSUser *const
find_user(const AdminData &data, uint32_t id, bool include_deleted = false);
static std::vector<const BTSUser *>
users(const AdminData &data, bool include_deleted);
static std::vector<BTSUser *>
users_mutable(AdminData &data, bool include_deleted);
static uint32_t find_max_user_id(const AdminData &data);
static const CMEUserData *
get_user_cme_data(const BTSUser &user, const AdminData &data);
static const ICEUserData *
get_user_ice_data(const BTSUser &user, const AdminData &data);
static const SIMUserData *
get_user_sim_data(const BTSUser &user, const AdminData &data);
// finds the "exchange user" (ex: "operator_id" at CME or "authorized_trader"
// at ICE) for a user
static std::string find_user_exchange_id(
const AdminData &data,
const BTSUser &user,
api::Venue exchange,
AdminServiceOperationResult &out_result);
// i. uses the exchange and exchange_user to reverse lookup the user(s)
// ii. uses the exchange, connection and exchange_account to reverse lookup
// the account(s) iii. finds the first account from ii that a user from i is
// authorized to trade
static std::string find_user_company_id(
const AdminData &data,
const BTSUser &user,
api::Venue exchange,
AdminServiceOperationResult &out_result);
static std::pair<const BTSAccount *, const BTSUser *>
find_user_account_matches(
const AdminData &admin_data,
const std::string &connection,
const std::string &exchange_account,
const std::string &exchange_user,
api::Venue exchange);
// for a given user finds the account permissions (if permissions are
// derived from the group, it traverses the group hierarcy to find the
// correct permissions.
static const UserAccountPermissions *
get_user_permissions(const AdminData &admin_data, uint32_t user_id);
// for a given user, fills in the associated_accounts ref arg
// with the accounts the user has permission to trade in
static void find_user_trading_accounts(
const AdminData &admin_data,
uint32_t user_id,
std::vector<const BTSAccount *> &associated_accounts);
// for a given account, fills in the associated_users ref arg
// with the users that can trade into the account
static void find_users_for_trading_account(
const AdminData &admin_data,
uint32_t account_id,
std::vector<const BTSUser *> &associated_users);
// validates that changes in user data satisfy all the
// constraints
static void validate_user_update(
const AdminData &admin_data,
const BTSUser &updated_user,
AdminServiceOperationResult &result);
// vaidates the exchange part of user data updates
static void validate_user_exchange_data(
const BTSUser &updated_user, AdminServiceOperationResult &out_result);
static void get_md_entitlement_report(
const AdminData &admin_data,
std::vector<MDEntitlementReportUnit> &entitlements);
// Gets a reporting unit out of CMEMicData
static MDEntitlementReportUnit get_cme_md_entitlement_unit(
const BTSUser &user, const CMEMicData &cme_mic_data);
static MDEntitlementReportUnit get_ice_md_entitlement_unit(
const BTSUser &user, const ICEMicData &ice_mic_data);
static void get_user_symbol_permissions(
const AdminData &data,
const BTSUser &user,
std::vector<UserSymbolMDPermissionUnit> &res);
////////////////////////////////////////////
// user mutators ///////////////////////////
////////////////////////////////////////////
static BTSUser *create_user(
AdminData &data,
uint32_t parent_group_id,
const std::string &name,
const std::string &password,
const std::string &full_name,
const std::string &company,
bool is_active,
const LicenseData &license_data,
const LicenseEntitlements &license_entitlements,
AdminServiceCreateEntityResult &result);
static BTSUser *update_user(
AdminData &admin_data,
const BTSUser &updated_user,
AdminServiceOperationResult &result);
static void delete_user(
AdminData &admin_data,
const IDAPIArgument &id,
AdminServiceOperationResult &result);
static void set_user_exchange_data(
BTSUser *user, const ExchangeUserData &exchange_user_data);
static void clear_user_accounts(BTSUser *user);
static void set_user_accounts(
BTSUser *user, const UserAccountPermissions &user_account_permissions);
////////////////////////////////////////////
// risk limits //////////////////////////////
////////////////////////////////////////////
// sorting function used by sort_limits to
// sort SecurityScope's
static bool
_sort_security_scopes(const SecurityScope &a, const SecurityScope &b);
// sorting function used by sort_limits to
// sort ProductScope's
static bool
_sort_product_scopes(const ProductScope &a, const ProductScope &);
// this sorts the rules such that the most
// specific ones are at the top. this makes it
// easier to match rules with securities/products
static void sort_limits(BTSAccount &account);
// creates a copy of AdminData with sorted rules
static AdminData sort_limits(const AdminData &admin_data);
// find the clip size for a security, account
static const uint32_t find_account_security_clip_size(
const BTSAccount &account, const api::Security &security);
// find the position limit for an account, security
static const AccountSecurityPositionLimitUnit *
find_account_security_position_limit(
const BTSAccount &account, const api::Security &security);
// find the position limit for an account, product
static const AccountProductPositionLimitUnit *
find_account_product_position_limit(
const BTSAccount &account, const api::Product &product);
static bool match_security_scope(
const SecurityScope &scope, const api::Security &security);
static bool
match_product_scope(const ProductScope &scope, const api::Product &product);
static bool
match_product(const api::Security &security, const api::Product &product);
static bool _match_limit_rule_symbol(
const std::string &symbol, const std::string &limit_symbol);
static api::SecurityType security_type(const api::Security &security);
////////////////////////////////////////////
// misc//////// ////////////////////////////
////////////////////////////////////////////
static bool has_id(uint32_t id);
static bool is_symbol_set(const std::string &symbol);
static void bootstrap_data(bts::api::admin::AdminData &data);
static void get_spread_prod_qty(
const api::Security &s,
std::map<std::string, double> &out_map,
double mult = 1.0);
// returns true if this spread effects the position of any outrights
static bool residual_pos_spread(const api::Security &s);
};
} // namespace admin
} // namespace bts