P2PServer: add local api data

This commit is contained in:
SChernykh 2023-03-27 11:04:02 +02:00
parent 645de31fe3
commit 2e6041dcf5
3 changed files with 53 additions and 3 deletions

View file

@ -26,6 +26,7 @@
#include "json_rpc_request.h"
#include "json_parsers.h"
#include "block_template.h"
#include "p2pool_api.h"
#include <rapidjson/document.h>
#include <fstream>
#include <numeric>
@ -1032,6 +1033,7 @@ void P2PServer::on_timer()
update_peer_connections();
check_zmq();
check_block_template();
api_update_local_stats();
}
void P2PServer::flush_cache()
@ -1220,6 +1222,52 @@ void P2PServer::on_shutdown()
uv_close(reinterpret_cast<uv_handle_t*>(&m_showPeersAsync), nullptr);
}
void P2PServer::api_update_local_stats()
{
if (!m_pool->api() || !m_pool->params().m_localStats || ((m_timerCounter % 30) != 5)) {
return;
}
m_pool->api()->set(p2pool_api::Category::LOCAL, "p2p",
[this](log::Stream& s)
{
const uint64_t cur_time = seconds_since_epoch();
s << "{\"connections\":" << m_numConnections.load()
<< ",\"incoming_connections\":" << m_numIncomingConnections.load()
<< ",\"peer_list_size\":" << m_peerList.size()
<< ",\"peers\":[";
bool first = true;
for (P2PClient* client = static_cast<P2PClient*>(m_connectedClientsList->m_next); client != m_connectedClientsList; client = static_cast<P2PClient*>(client->m_next)) {
if (client->m_listenPort >= 0) {
char buf[32] = {};
log::Stream s1(buf);
if (client->m_SoftwareVersion) {
s1 << client->software_name() << " v" << (client->m_SoftwareVersion >> 16) << '.' << (client->m_SoftwareVersion & 0xFFFF);
}
if (!first) {
s << ',';
}
s << '"'
<< (client->m_isIncoming ? "I," : "O,")
<< (cur_time - client->m_connectedTime) << ','
<< client->m_pingTime << ','
<< static_cast<const char*>(buf) << ','
<< client->m_broadcastMaxHeight << ','
<< static_cast<char*>(client->m_addrString)
<< '"';
first = false;
}
}
s << "],\"uptime\":" << cur_time - m_pool->start_time() << '}';
});
}
P2PServer::P2PClient::~P2PClient()
{
}

View file

@ -259,6 +259,8 @@ private:
void show_peers() const;
void on_shutdown() override;
void api_update_local_stats();
};
} // namespace p2pool

View file

@ -1306,10 +1306,10 @@ void StratumServer::api_update_local_stats(uint64_t timestamp)
double current_effort = static_cast<double>(hashes_since_last_share) * 100.0 / m_pool->side_chain().difficulty().to_double();
int connections = m_numConnections;
int incoming_connections = m_numIncomingConnections;
uint32_t connections = m_numConnections;
uint32_t incoming_connections = m_numIncomingConnections;
m_pool->api()->set(p2pool_api::Category::LOCAL, "stats",
m_pool->api()->set(p2pool_api::Category::LOCAL, "stratum",
[hashrate_15m, hashrate_1h, hashrate_24h, total_hashes, shares_found, shares_failed, average_effort, current_effort, connections, incoming_connections](log::Stream& s)
{
s << "{\"hashrate_15m\":" << hashrate_15m