Money file download code complete.

This commit is contained in:
Pedro Pinto committed 2019-08-16 12:34:02 -04:00
1 parent 73d1f67dcd
commit 3d6e6ea7db
11 files changed
+274 -22

No files matched your search

@@ -0,0 +1,179 @@
#include "MoneyFileServiceServer.h"
#include "lib/api/public/ServiceContext.h"
#include "lib/api/services/AdminService.client.pb.h"
#include "lib/api/services/MarginService.client.pb.h"
#include "lib/config/config.h"
#include "lib/config/protos/position_cfg.pb.h"
#include "lib/config/protos/orders_fills_cleanup.pb.h"
#include "lib/logging/logging.h"
#include "lib/utils/PBUtils.h"
#include "lib/utils/bts_assert.h"
#include "lib/utils/datetime.h"
#include "server/sparkd/position/import/parsing/money_parser.h"
#include "server/sparkd/position/import/DownloadCommonStateMachine.h"
#include <boost/algorithm/string/predicate.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <regex>
#include <variant>
namespace bts {
namespace position {
struct MoneyFileServiceServer::PImpl {
PImpl(MoneyFileServiceServer &owner,
bts::api::ServiceContext &ctx)
: ctx(ctx),
admin_service(ctx.locate<api::admin::AdminService>()),
margin_service(ctx.locate<api::position::MarginService>()),
download_sm("Money file downloader",
// File picker
[&owner] (auto const &candidates) {
return owner.pick_most_recent_money_file(candidates);
},
// Download complete handler
[&owner] (auto const&sftp_file, auto const&local_file) {
return owner.on_download_complete(sftp_file,local_file);
},
// Thread Marshaller
[&ctx] (auto const&action) {
ctx.post(action);
}),
admin_data()
{}
bts::api::ServiceContext &ctx;
api::admin::AdminService admin_service;
api::position::MarginService margin_service;
DownloadCommonStateMachine download_sm;
std::shared_ptr<const api::admin::AdminData> admin_data;
};
MoneyFileServiceServer::MoneyFileServiceServer(
bts::api::ServiceContext &ctx)
: MoneyFileServiceBase(ctx), me(new PImpl(*this,ctx)) {
auto const &c = config::get<config::GMI_Import>();
if (c.sftp_host().size() && !c.disable_money_download()) {
LOG(OrderMan, Info, "MoneyFileServiceServer is active.");
on_timer();
me->admin_service.subscribe_admin_data() |
rxcpp::operators::subscribe<api::admin::Admin_dataUpdate>(
[this](auto const &update) {
bts_assert(update.all().size() == 1);
me->admin_data = update.all()[0];
me->download_sm.on_admin(me->admin_data);
});
} else
LOG(OrderMan, Warn,
"Money file import is disabled, since no host is specified in the "
"GMI_Import.sftp_host config key");
}
MoneyFileServiceServer::~MoneyFileServiceServer() {}
void MoneyFileServiceServer::parse_money_csv(
const api::position::Money_CSVBlock &b,
const api::Response<api::position::Money_ParseResult> &reply) {
api::position::Money_ParseResult result;
if (!me->admin_data) {
*result.mutable_failure() = "Server is not ready";
reply(result);
return;
}
std::istringstream in{b.text()};
auto parse_result = parse_money(*me->admin_data, in);
if (!std::holds_alternative<api::position::CashBalanceBatch>(
parse_result)) {
*result.mutable_failure() = std::get<std::string>(parse_result);
reply(result);
return;
}
auto cash_balance =
std::get<api::position::CashBalanceBatch>(parse_result);
*result.mutable_success() = cash_balance;
reply(result);
return;
}
void MoneyFileServiceServer::on_timer() {
me->ctx.post([this]() { on_timer(); }, 7000);
me->download_sm.on_timer();
}
std::optional<std::string> MoneyFileServiceServer::on_download_complete(
std::string const &sftp_file_path, std::string const &local_file_path) {
LOG(OrderMan, Warn,
"Successfully downloaded money file {} to local file "
"{}. Will parse next.",
sftp_file_path, local_file_path);
auto parse_result = parse_money_file(local_file_path);
if (!std::holds_alternative<api::position::CashBalanceBatch>(
parse_result)) {
auto error_msg =
fmt::format("Failed to parse money file at {}. Error msg: {}",
local_file_path, std::get<std::string>(parse_result));
return error_msg;
}
auto cash_balance =
std::get<api::position::CashBalanceBatch>(parse_result);
LOG(OrderMan, Warn,
"Successfully parsed money file at {}. Extracted {} records",
local_file_path, cash_balance.cash_balance_size());
for (auto const &cb : cash_balance.cash_balance())
LOG(OrderMan, Info, "Cash balance for account {} is {}",
cb.account_id(), cb.cash_balance());
me->margin_service.set_cash_balance_batch(
cash_balance,
[this](auto const &rr) {
if (!rr.success())
on_money_set_failure(rr.error_message());
},
5000);
return {};
}
void MoneyFileServiceServer::on_money_set_failure(
std::string const &error) {
LOG(OrderMan, Error,
"Failed to set cash balance through the margin service. The "
"following information is available: {}",
error);
me->download_sm.on_post_handle_failure(error);
}
std::optional<std::string> MoneyFileServiceServer::pick_most_recent_money_file(
std::vector<std::string> const &candidates) {
std::vector<std::string> pos_files;
std::copy_if(candidates.begin(), candidates.end(),
std::back_inserter(pos_files), [](auto const &file_name) {
return boost::algorithm::starts_with(file_name, "btsmny_");
});
if (pos_files.empty())
return {};
std::sort(pos_files.begin(), pos_files.end());
return pos_files.back();
}
std::variant<api::position::CashBalanceBatch, std::string>
MoneyFileServiceServer::parse_money_file(std::string const &f) {
return position::parse_money(*me->admin_data, f);
}
} // namespace position
} // namespace bts