SPARK-1679 get_order_summary done

This commit is contained in:
Taha Afzal committed 2018-09-04 22:13:25 -04:00
1 parent 1f4c91a344
commit 2b2fe02315
5 files changed
+109 -17

No files matched your search

+62 -5
View File
@@ -2,8 +2,10 @@
#include "lib/persistence/SqliteStore.h"
#include "lib/logging/logging.h"
#include "lib/utils/bts_assert.h"
#include "lib/utils/PBUtils.h"
#include "lib/api/service_helpers/AdminServiceUtils.h"
#include "lib/orderman/orderman_utils/orderman_utils.h"
#include <boost/tokenizer.hpp>
#include <fmt/format.h>
@@ -22,7 +24,9 @@ AdminServiceServer::AdminServiceServer(bts::api::ServiceContext& context)
_unique_key("0"),
_state(),
_store(new bts::persistence::SqliteStore(_db_name)),
_publisher(make_admin_data_stateful_publisher())
_publisher(make_admin_data_stateful_publisher()),
_order_service(context.locate<bts::api::orderman::OrderService>()),
_order_map()
{
LOG(Gen, Info, "AdminServiceServer: service started");
std::map<std::string,std::map<std::string,std::string>> all_data = _store->retrieve_all();
@@ -48,7 +52,19 @@ AdminServiceServer::AdminServiceServer(bts::api::ServiceContext& context)
AdminServiceUtils::fill_connections_from_config(_state);
AdminServiceUtils::bootstrap_data(_state);
_update_and_publish(true);
}
}
_order_service.subscribe_orders() |
rxcpp::operators::subscribe<api::orderman::OrdersUpdate>(
[this](auto const& update) {
for (const auto& o : update.removed()){
const auto& it = _order_map.find(o->key());
if(it != _order_map.end())
_order_map.erase(it);
}
for (const auto& o : update.added()){
_order_map[o->key()]=o;
}
});
}
AdminServiceServer::~AdminServiceServer() {}
void AdminServiceServer::create_account(const api::admin::AccountCreateAPIArgument& request,
@@ -113,10 +129,51 @@ void AdminServiceServer::_update_and_publish(bool is_insert){
else
LOG(Gen, Error, "Could not serialize admin data, saving failed");
}
void AdminServiceServer::get_order_summary(const api::admin::OrderSummaryRequest&,
const api::Response<::bts::api::admin::OrdersSummary>&){
void AdminServiceServer::get_order_summary(const bts::api::VoidMessage&,
const api::Response<::bts::api::admin::OrdersSummary>& reply){
std::map<std::string,std::map<std::string,OrdersSummaryUnit>> agg;
for(auto it=_order_map.begin();it!=_order_map.end();it++){
const OrderRecord& ord=*(it->second);
if(bts::orderman::order_in_working_state(ord.state().status()) &&
bts::orderman::is_parent_order(ord))
_add_live_order(agg,ord);
}
OrdersSummary rv;
for (auto acct_it=agg.begin();acct_it!=agg.end();acct_it++){
const std::map<std::string,OrdersSummaryUnit>& user_map=acct_it->second;
for(auto user_it=user_map.begin();user_it!=user_map.end();user_it++){
const OrdersSummaryUnit& u=user_it->second;
OrdersSummaryUnit* nv=rv.add_units();
nv->CopyFrom(u);
}
}
reply(rv);
}
void AdminServiceServer::cancel_orders(const api::admin::OrderUsersCancelRequest&,
const api::Response<bts::api::VoidMessage>&){
const api::Response<bts::api::VoidMessage>&){
}
void AdminServiceServer::_add_live_order(std::map<std::string,std::map<std::string,OrdersSummaryUnit>>& agg,
const OrderRecord& ord){
const std::string& acct=ord.def().account();
const std::string& user=ord.def().user();
auto acct_it=agg.find(acct);
if(acct_it==agg.end())
agg[acct]=std::map<std::string,OrdersSummaryUnit>();
acct_it=agg.find(acct);
bts_assert(acct_it!=agg.end());
std::map<std::string,OrdersSummaryUnit>& user_map=acct_it->second;
auto user_it=user_map.find(user);
if(user_it==user_map.end()){
OrdersSummaryUnit nu;
nu.set_account(acct);
nu.set_user(user);
user_map[user]=nu;
}
user_it=user_map.find(user);
bts_assert(user_it!=user_map.end());
OrdersSummaryUnit& u=user_it->second;
u.set_total_orders(u.total_orders()+1);
u.set_total_contracts(u.total_contracts()+ord.state().quantity());
}
}}