Factored storage/retrieval of admin data out of service.

This commit is contained in:
Pedro Pinto committed 2020-04-17 18:37:05 -04:00
1 parent 991a199d06
commit 39db9b1bd7
4 files changed
+96 -46

No files matched your search

+18 -40
View File
@@ -8,6 +8,7 @@
#include "lib/orderman/orderman_utils/orderman_utils.h"
#include "lib/utils/TimeStamp.h"
#include "lib/utils/AuthToken.h"
#include "server/sparkd/user/AdminServiceDB.h"
#include <boost/container/flat_map.hpp>
#include <boost/tokenizer.hpp>
@@ -22,11 +23,8 @@ namespace admin{
AdminServiceServer::AdminServiceServer(bts::api::ServiceContext& context)
:
bts::api::admin::AuthenticatedAdminServiceBase (context),
_db_name("admin"),
_table_name("admin_data"),
_unique_key("0"),
_db(),
_state(),
_store(new bts::persistence::SqliteStore(_db_name)),
_publisher(make_admin_data_stateful_publisher()),
_md_entit_publisher(make_md_entitlement_data_stateful_publisher()),
_md_perm_publisher(make_md_permissions_data_stateful_publisher()),
@@ -34,29 +32,15 @@ AdminServiceServer::AdminServiceServer(bts::api::ServiceContext& context)
_order_map()
{
LOG(Gen, Info, "AdminServiceServer: service started");
map<string,map<string,string>> all_data = _store->retrieve_all();
auto table = all_data.find(_table_name);
bool data_found = false;
if (table != all_data.end())
{
auto row = table->second.find(_unique_key);
if (row != table->second.end())
{
std::string& ser_data = row->second;
if (_state.ParseFromString(ser_data))
{
data_found = true;
_check_data_on_start();
_update_and_publish(false);
}
}
}
if (!data_found)
{
auto admin = _db.retrieve();
if (admin.has_value()) {
_check_data_on_start();
_update_and_publish();
} else {
LOG(Gen, Info, "AdminServiceServer: data created for the first time");
AdminServiceUtils::fill_connections_from_config(_state);
AdminServiceUtils::bootstrap_data(_state);
_update_and_publish(true);
_update_and_publish();
}
_order_service.subscribe_orders() |
rxcpp::operators::subscribe<api::orderman::OrdersUpdate>(
@@ -103,7 +87,7 @@ void AdminServiceServer::create_account(
AdminServiceUtils::update_user(_state, update, res);
}
}
_update_and_publish(false);
_update_and_publish();
reply(result);
}
void AdminServiceServer::update_account(
@@ -112,7 +96,7 @@ void AdminServiceServer::update_account(
const api::Response<AdminServiceOperationResult>& reply){
bts::api::admin::AdminServiceOperationResult result;
AdminServiceUtils::update_account(_state, request.complete_data(), result);
_update_and_publish(false);
_update_and_publish();
reply(result);
}
void AdminServiceServer::create_user(
@@ -130,7 +114,7 @@ void AdminServiceServer::create_user(
request.license_data(),
request.license_entitlements(),
result);
_update_and_publish(false);
_update_and_publish();
reply(result);
}
void AdminServiceServer::update_user(
@@ -139,7 +123,7 @@ void AdminServiceServer::update_user(
const api::Response<AdminServiceOperationResult>& reply){
AdminServiceOperationResult result;
AdminServiceUtils::update_user(_state, request.complete_data(), result);
_update_and_publish(false);
_update_and_publish();
reply(result);
}
void AdminServiceServer::get_admin_data(
@@ -230,7 +214,7 @@ void AdminServiceServer::delete_user(
const api::Response<AdminServiceOperationResult>& reply){
AdminServiceOperationResult result; result.set_is_success(true);
AdminServiceUtils::delete_user(_state, id, result);
_update_and_publish(false);
_update_and_publish();
reply(result);
}
void AdminServiceServer::delete_account(
@@ -239,23 +223,16 @@ void AdminServiceServer::delete_account(
const api::Response<AdminServiceOperationResult>& reply){
AdminServiceOperationResult result; result.set_is_success(true);
bts::admin::AdminServiceUtils::delete_account(_state, id, result);
_update_and_publish(false);
_update_and_publish();
reply(result);
}
void AdminServiceServer::_update_and_publish(bool is_insert){
std::string ser_state;
void AdminServiceServer::_update_and_publish(){
LOG(Gen, Info, "Saving admin data");
auto start = utils::TimeStamp::now();
if (_state.SerializeToString(&ser_state))
{
LOG(Gen, Info, "Saving admin data");
if(is_insert)
_store->insert(_table_name, _unique_key, ser_state);
else
_store->update(_table_name, _unique_key, ser_state);
if (_db.update(_state) ) {
_publisher.add_or_update(_state);
_publisher.flush();
_publish_md_entitlements();
// _publish_md_permissions();
auto elapsed = (utils::TimeStamp::now() - start).milliseconds();
LOG(Gen, Info, "Saved admin data ({}ms)", elapsed);
}
@@ -263,6 +240,7 @@ void AdminServiceServer::_update_and_publish(bool is_insert){
LOG(Gen, Error, "Could not serialize admin data, saving failed");
}
void AdminServiceServer::get_order_summary(
const api::AuthToken&,
const bts::api::VoidMessage&,