From 5eb24c7e63db35baf3515d8d953ce502570fefc1 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 24 Aug 2022 15:48:27 +0300 Subject: [PATCH] Improve statistics retrieval. --- telegram-bot-api/ClientManager.cpp | 21 ++++++++++++--------- telegram-bot-api/Stats.cpp | 9 +++++++++ telegram-bot-api/Stats.h | 2 ++ 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/telegram-bot-api/ClientManager.cpp b/telegram-bot-api/ClientManager.cpp index 8a58b22..582549c 100644 --- a/telegram-bot-api/ClientManager.cpp +++ b/telegram-bot-api/ClientManager.cpp @@ -36,7 +36,7 @@ #include "td/utils/StringBuilder.h" #include "td/utils/Time.h" -#include +#include #include namespace telegram_bot_api { @@ -182,7 +182,8 @@ void ClientManager::get_stats(td::Promise promise, auto now = td::Time::now(); td::int32 active_bot_count = 0; - std::multimap top_bot_ids; + td::vector> top_bot_ids; + size_t max_bots = 50; for (auto id : clients_.ids()) { auto *client_info = clients_.get(id); CHECK(client_info); @@ -195,15 +196,17 @@ void ClientManager::get_stats(td::Promise promise, continue; } - auto stats = client_info->stat_.as_vector(now); - double score = 0.0; - for (auto &stat : stats) { - if (stat.key_ == "update_count" || stat.key_ == "request_count") { - score -= td::to_double(stat.value_); - } + auto score = static_cast(client_info->stat_.get_score(now) * -1e9); + if (score == 0 && top_bot_ids.size() >= max_bots) { + continue; } - top_bot_ids.emplace(static_cast(score * 1e9), id); + top_bot_ids.emplace_back(score, id); } + if (top_bot_ids.size() < max_bots) { + max_bots = top_bot_ids.size(); + } + std::partial_sort(top_bot_ids.begin(), top_bot_ids.begin() + max_bots, top_bot_ids.end()); + top_bot_ids.resize(max_bots); sb << stat_.get_description() << '\n'; if (id_filter.empty()) { diff --git a/telegram-bot-api/Stats.cpp b/telegram-bot-api/Stats.cpp index 59ca63e..52d0f0b 100644 --- a/telegram-bot-api/Stats.cpp +++ b/telegram-bot-api/Stats.cpp @@ -149,6 +149,15 @@ td::string BotStatActor::get_description() const { return res; } +double BotStatActor::get_score(double now) { + auto minute_stat = stat_[2].stat_duration(now); + double result = minute_stat.first.request_count_ + minute_stat.first.update_count_; + if (minute_stat.second != 0) { + result /= minute_stat.second; + } + return result; +} + bool BotStatActor::is_active(double now) const { return last_activity_timestamp_ > now - 86400; } diff --git a/telegram-bot-api/Stats.h b/telegram-bot-api/Stats.h index 3b317fc..efc6287 100644 --- a/telegram-bot-api/Stats.h +++ b/telegram-bot-api/Stats.h @@ -181,6 +181,8 @@ class BotStatActor final : public td::Actor { td::vector as_vector(double now); td::string get_description() const; + double get_score(double now); + bool is_active(double now) const; private: