Skip to content

Commit

Permalink
Rpc endpoint for election statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
RickiNano committed Apr 3, 2024
1 parent 697f2ce commit dd07e26
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
39 changes: 39 additions & 0 deletions nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2002,6 +2002,44 @@ void nano::json_handler::confirmation_active ()
response_errors ();
}

void nano::json_handler::election_statistics ()
{
auto active_elections = node.active.list_active ();
unsigned normal_count = 0;
unsigned hinted_count = 0;
unsigned optimistic_count = 0;
unsigned total_count = 0;

for (auto const & election : active_elections)
{
total_count++;
switch (election->behavior ())
{
case election_behavior::normal:
normal_count++;
break;
case election_behavior::hinted:
hinted_count++;
break;
case election_behavior::optimistic:
optimistic_count++;
break;
}
}

auto utilization_percentage = (static_cast<double> (total_count * 100) / node.config.active_elections_size);
std::stringstream stream;
stream << std::fixed << std::setprecision (2) << utilization_percentage;

response_l.put ("normal", normal_count);
response_l.put ("hinted", hinted_count);
response_l.put ("optimistic", optimistic_count);
response_l.put ("total", total_count);
response_l.put ("aec_utilization_percentage", stream.str ());

response_errors ();
}

void nano::json_handler::confirmation_history ()
{
boost::property_tree::ptree elections;
Expand Down Expand Up @@ -5305,6 +5343,7 @@ ipc_json_handler_no_arg_func_map create_ipc_json_handler_no_arg_func_map ()
no_arg_funcs.emplace ("accounts_pending", &nano::json_handler::accounts_pending);
no_arg_funcs.emplace ("accounts_receivable", &nano::json_handler::accounts_receivable);
no_arg_funcs.emplace ("active_difficulty", &nano::json_handler::active_difficulty);
no_arg_funcs.emplace ("election_statistics", &nano::json_handler::election_statistics);
no_arg_funcs.emplace ("available_supply", &nano::json_handler::available_supply);
no_arg_funcs.emplace ("block_info", &nano::json_handler::block_info);
no_arg_funcs.emplace ("block", &nano::json_handler::block_info);
Expand Down
1 change: 1 addition & 0 deletions nano/node/json_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class json_handler : public std::enable_shared_from_this<nano::json_handler>
void accounts_pending ();
void accounts_receivable ();
void active_difficulty ();
void election_statistics ();
void available_supply ();
void block_info ();
void block_confirm ();
Expand Down

0 comments on commit dd07e26

Please sign in to comment.