Adding report API to analytics server

This commit is contained in:
Taha Afzal committed 2022-02-21 11:10:15 -05:00
1 parent 41a072d4d2
commit f568691731
5 files changed
+209 -21

No files matched your search

@@ -9,6 +9,7 @@
#include "lib/utils/PBUtils.h"
#include "lib/utils/Puid.h"
#include "lib/utils/bts_assert.h"
#include "server/sparkd/algo/AlgoDefinitionServiceServer.h"
#include <boost/algorithm/string.hpp>
#include <boost/tokenizer.hpp>
#include <chrono>
@@ -21,6 +22,37 @@ using namespace bts::api::analytics;
namespace bts::analytics {
// assumes key are ints
int get_max_id(IStore *const store, const std::string &ns) {
auto ns_map = store->retrieve_all();
auto it = ns_map.find(ns);
if (it == ns_map.end()) {
return 0;
}
int max_id = -1;
for (const auto &kv : it->second) {
int key = std::stoi(kv.first);
max_id = key > max_id ? key : max_id;
}
return max_id;
}
void add_or_update_to_table(
persistence::IStore *store,
const std::string &ns,
const std::string &key,
const std::string &val) {
auto [exists, existing_val] = store->retrieve_one(ns, key);
LOG(Gen, Info,
"add_or_update ns: {} key: {} val: {} exists: {} existing_val: {}", ns,
key, val, exists, existing_val);
if (exists) {
store->update(ns, key, val);
} else {
store->insert(ns, key, val);
}
}
// parses out the vs_id and vs_name from the key
std::tuple<int, std::string> parse_vs_id_name(
const std::string &ns, const std::string &sep_str) {
@@ -35,27 +67,33 @@ struct AnalyticsServiceServer::PImpl {
const std::string vs_ns_sep = ".";
const std::string exp_params_ns = "analytics_expiration_params";
const std::string vs_usage_ns = "volsurface_usage";
const std::string reports_ns = "reports";
std::unique_ptr<persistence::IStore> vs_store;
std::unique_ptr<persistence::IStore> exp_params_store;
std::unique_ptr<persistence::IStore> vs_usage_store;
std::unique_ptr<persistence::IStore> reports_store;
AnalyticsServiceBase::VolsurfacesStatefulPublisher vs_publisher;
AnalyticsServiceBase::Exp_parametersStatefulPublisher exp_params_publisher;
AnalyticsServiceBase::Volsurface_usageStatefulPublisher vs_usage_publisher;
AnalyticsServiceBase::ReportsStatefulPublisher reports_publisher;
PImpl(
bts::api::ServiceContext &context,
const AnalyticsServiceBase::VolsurfacesStatefulPublisher &vs_publisher,
const AnalyticsServiceBase::Exp_parametersStatefulPublisher
&exp_params_publisher,
const AnalyticsServiceBase::Volsurface_usageStatefulPublisher
&vs_usage_publisher)
&vs_usage_publisher,
const AnalyticsServiceBase::ReportsStatefulPublisher &reports_publisher)
: context(context),
vs_store(std::make_unique<persistence::SqliteStore>("volsurfaces")),
exp_params_store(std::make_unique<persistence::SqliteStore>(
"analytics_expiration_params")),
vs_usage_store(
std::make_unique<persistence::SqliteStore>("volsurface_usage")),
reports_store(std::make_unique<persistence::SqliteStore>("reports")),
vs_publisher(vs_publisher), exp_params_publisher(exp_params_publisher),
vs_usage_publisher(vs_usage_publisher) {}
vs_usage_publisher(vs_usage_publisher),
reports_publisher(reports_publisher) {}
};
AnalyticsServiceServer::AnalyticsServiceServer(
@@ -65,7 +103,8 @@ AnalyticsServiceServer::AnalyticsServiceServer(
context,
make_volsurfaces_stateful_publisher(),
make_exp_parameters_stateful_publisher(),
make_volsurface_usage_stateful_publisher())) {
make_volsurface_usage_stateful_publisher(),
make_reports_stateful_publisher())) {
// publishing all vol surfaces
LOG(Gen, Info, "Starting AnalyticsServiceServer");
auto vs_table = me->vs_store->retrieve_all();
@@ -109,6 +148,19 @@ AnalyticsServiceServer::AnalyticsServiceServer(
}
}
me->vs_usage_publisher.flush();
// reports
{
auto ns_table = me->reports_store->retrieve_all();
bts_assert(ns_table.size() == 0 || ns_table.size() == 1);
for (const auto &ns_vals : ns_table) {
for (const auto &kvp : ns_vals.second) {
Report report;
utils::pb::from_string(kvp.second, &report);
me->reports_publisher.add_or_update(report);
}
}
me->reports_publisher.flush();
}
}
void AnalyticsServiceServer::get_new_volsurface_id(
@@ -199,20 +251,6 @@ void AnalyticsServiceServer::clean_up(
// TODO
}
void _add_or_update(
persistence::IStore *store,
const std::string &ns,
const std::string &key,
const std::string &val) {
auto [exists, existing_val] = store->retrieve_one(ns, key);
if (exists) {
LOG(Gen, Info, "_add_or_update key:{} val:{} exists", key, existing_val);
store->update(ns, key, val);
} else {
store->insert(ns, key, val);
}
}
void AnalyticsServiceServer::set_exp_parameters(
const bts::api::analytics::ExpirationParameterEntryList &exp_params_args,
const api::Response<::bts::api::ResultMessage> &response) {
@@ -221,8 +259,7 @@ void AnalyticsServiceServer::set_exp_parameters(
std::string ns = me->exp_params_ns;
std::string key = std::to_string(1);
std::string value = utils::pb::to_string(exp_params);
_add_or_update(me->exp_params_store.get(), ns, key, value);
me->exp_params_store->update(ns, key, value);
add_or_update_to_table(me->exp_params_store.get(), ns, key, value);
me->exp_params_publisher.add_or_update(exp_params);
me->exp_params_publisher.flush();
api::ResultMessage res;
@@ -246,6 +283,61 @@ void AnalyticsServiceServer::set_volsurface_usage_list(
response(res);
}
void AnalyticsServiceServer::get_new_report_id(
const ::bts::api::VoidMessage &,
const bts::api::Response<::bts::api::analytics::AnalyticsServiceCreateReply>
&response) {
auto max_id = get_max_id(me->reports_store.get(), me->reports_ns);
auto new_id = max_id + 1;
bts::api::analytics::AnalyticsServiceCreateReply rep;
rep.set_ok(true);
rep.set_new_id(new_id);
response(rep);
}
void AnalyticsServiceServer::save_report(
const ::bts::api::analytics::Report &report_arg,
const api::Response<::bts::api::ResultMessage> &resp) {
Report report = report_arg;
std::string key = std::to_string(report.id());
auto [present, val] = me->reports_store->retrieve_one(me->reports_ns, key);
if (!present) {
int new_id = get_max_id(me->reports_store.get(), me->reports_ns) + 1;
report.set_id(new_id);
} else {
bts_assert(!val.empty());
}
std::string report_str = utils::pb::to_string(report);
add_or_update_to_table(
me->reports_store.get(), me->reports_ns, std::to_string(report.id()),
report_str);
me->reports_publisher.add_or_update(report);
me->reports_publisher.flush();
api::ResultMessage res;
res.set_success(true);
resp(res);
}
void AnalyticsServiceServer::delete_report(
const bts::api::analytics::IntWrapper &id,
const api::Response<::bts::api::ResultMessage> &resp) {
std::string key = std::to_string(id.data());
auto [present, val] = me->reports_store->retrieve_one(me->reports_ns, key);
api::ResultMessage res;
res.set_success(false);
if (!present) {
res.set_success(false);
res.set_error_message("Could not find report");
resp(res);
return;
}
bts_assert(!val.empty());
me->reports_publisher.erase(id.data());
me->reports_publisher.flush();
me->reports_store->remove(me->reports_ns, {key});
resp(res);
}
AnalyticsServiceServer::~AnalyticsServiceServer() {}
} // namespace bts::analytics