From 6bfd8b32a9ca14d81f58d397aeb16a30c03d832f Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 5 Feb 2023 21:56:40 +0100 Subject: Update stats every 5 seconds via websocket --- debian/changelog | 1 + whiteboard.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/debian/changelog b/debian/changelog index 7c0064f..5dda00e 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,6 +1,7 @@ whiteboard (1.6) UNRELEASED; urgency=medium * Added stats.html + * Use boost strands instead of mutex -- Roland Reichwein Tue, 31 Jan 2023 18:22:42 +0100 diff --git a/whiteboard.cpp b/whiteboard.cpp index 922bd37..2c8a47b 100644 --- a/whiteboard.cpp +++ b/whiteboard.cpp @@ -251,6 +251,45 @@ public: }); } + std::string stats_xml() + { + return make_xml({ + {"type", "stats" }, + {"dbsizegross", std::to_string(m_storage.dbsize_gross()) }, + {"dbsizenet", std::to_string(m_storage.dbsize_net()) }, + {"numberofdocuments", std::to_string(m_storage.getNumberOfDocuments()) }, + {"numberofconnections", std::to_string(m_registry.number_of_connections()) }, + }); + } + + void on_write_stats(std::shared_ptr data, std::shared_ptr buffer, boost::beast::error_code ec, std::size_t bytes_transferred) + { + boost::ignore_unused(bytes_transferred); + + if (ec) { + std::cerr << "Error on session write stats: " << ec.message() << std::endl; + } + } + + void stats_callback(const boost::system::error_code&) + { + auto data{std::make_shared(stats_xml())}; + auto buffer{std::make_shared(data->data(), data->size())}; + m_ws->async_write(*buffer, boost::asio::bind_executor(m_ws->next_layer().get_executor(), + boost::beast::bind_front_handler(&session::on_write_stats, shared_from_this(), data, buffer))); + + m_stats_timer->expires_at(m_stats_timer->expires_at() + boost::asio::chrono::seconds(5)); + m_stats_timer->async_wait(boost::beast::bind_front_handler(&session::stats_callback, this)); + } + + void setup_stats_timer() + { + if (!m_stats_timer) { + m_stats_timer = std::make_shared(m_ws->next_layer().get_executor(), boost::asio::chrono::seconds(5)); + m_stats_timer->async_wait(boost::beast::bind_front_handler(&session::stats_callback, this)); + } + } + std::string handle_request(const std::string& request) { try { @@ -339,13 +378,8 @@ public: {"version", WHITEBOARD_VERSION } }); } else if (command == "getstats") { - return make_xml({ - {"type", "stats" }, - {"dbsizegross", std::to_string(m_storage.dbsize_gross()) }, - {"dbsizenet", std::to_string(m_storage.dbsize_net()) }, - {"numberofdocuments", std::to_string(m_storage.getNumberOfDocuments()) }, - {"numberofconnections", std::to_string(m_registry.number_of_connections()) }, - }); + setup_stats_timer(); + return stats_xml(); } else { throw std::runtime_error("Bad command: "s + command); } @@ -365,6 +399,7 @@ private: boost::beast::http::request m_req; boost::beast::flat_buffer m_buffer; + std::shared_ptr m_stats_timer{}; }; void Whiteboard::do_accept() -- cgit v1.2.3