diff --git a/telegram-bot-api/ClientManager.cpp b/telegram-bot-api/ClientManager.cpp index 69dba8f..074d874 100644 --- a/telegram-bot-api/ClientManager.cpp +++ b/telegram-bot-api/ClientManager.cpp @@ -245,11 +245,19 @@ void ClientManager::get_stats(td::Promise promise, CHECK(client_info); auto bot_info = client_info->client_->get_actor_unsafe()->get_bot_info(); + auto active_request_count = client_info->stat_.get_active_request_count(); + auto active_file_upload_bytes = client_info->stat_.get_active_file_upload_bytes(); sb << '\n'; sb << "id\t" << bot_info.id_ << '\n'; sb << "uptime\t" << now - bot_info.start_time_ << '\n'; sb << "token\t" << bot_info.token_ << '\n'; sb << "username\t" << bot_info.username_ << '\n'; + if (active_request_count != 0) { + sb << "active_request_count\t" << active_request_count << '\n'; + } + if (active_file_upload_bytes != 0) { + sb << "active_file_upload_bytes\t" << active_file_upload_bytes << '\n'; + } if (!bot_info.webhook_.empty()) { sb << "webhook\t" << bot_info.webhook_ << '\n'; if (bot_info.has_webhook_certificate_) { diff --git a/telegram-bot-api/Query.cpp b/telegram-bot-api/Query.cpp index e2a7b08..32ff8b4 100644 --- a/telegram-bot-api/Query.cpp +++ b/telegram-bot-api/Query.cpp @@ -135,7 +135,7 @@ void Query::send_response_stat() const { return; } send_closure(stat_actor_, &BotStatActor::add_event, - ServerBotStat::Response{state_ == State::OK, answer_.size()}, now); + ServerBotStat::Response{state_ == State::OK, answer_.size(), files_size()}, now); } } // namespace telegram_bot_api diff --git a/telegram-bot-api/Stats.cpp b/telegram-bot-api/Stats.cpp index 52d0f0b..54a6236 100644 --- a/telegram-bot-api/Stats.cpp +++ b/telegram-bot-api/Stats.cpp @@ -155,9 +155,19 @@ double BotStatActor::get_score(double now) { if (minute_stat.second != 0) { result /= minute_stat.second; } + result += td::max(static_cast(get_active_request_count() - 10), static_cast(0)); + result += static_cast(get_active_file_upload_bytes()) * 1e-8; return result; } +td::int64 BotStatActor::get_active_request_count() const { + return active_request_count_; +} + +td::int64 BotStatActor::get_active_file_upload_bytes() const { + return active_file_upload_bytes_; +} + 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 efc6287..a4d7a07 100644 --- a/telegram-bot-api/Stats.h +++ b/telegram-bot-api/Stats.h @@ -115,15 +115,16 @@ struct ServerBotStat { struct Response { bool ok_; size_t size_; + td::int64 files_size_; }; - void on_event(const Response &answer) { + void on_event(const Response &response) { response_count_++; - if (answer.ok_) { + if (response.ok_) { response_count_ok_++; } else { response_count_error_++; } - response_bytes_ += static_cast(answer.size_); + response_bytes_ += static_cast(response.size_); } struct Request { @@ -173,6 +174,7 @@ class BotStatActor final : public td::Actor { for (auto &stat : stat_) { stat.add_event(event, now); } + on_event(event); if (!parent_.empty()) { send_closure(parent_, &BotStatActor::add_event, event, now); } @@ -183,6 +185,10 @@ class BotStatActor final : public td::Actor { double get_score(double now); + td::int64 get_active_request_count() const; + + td::int64 get_active_file_upload_bytes() const; + bool is_active(double now) const; private: @@ -193,6 +199,23 @@ class BotStatActor final : public td::Actor { td::TimedStat stat_[SIZE]; td::ActorId parent_; double last_activity_timestamp_ = -1e9; + td::int64 active_request_count_ = 0; + td::int64 active_file_upload_bytes_ = 0; + + void on_event(const ServerBotStat::Update &update) { + } + + void on_event(const ServerBotStat::Response &response) { + active_request_count_--; + active_file_upload_bytes_ -= response.files_size_; + CHECK(active_request_count_ >= 0); + CHECK(active_file_upload_bytes_ >= 0); + } + + void on_event(const ServerBotStat::Request &request) { + active_request_count_++; + active_file_upload_bytes_ += request.files_size_; + } }; } // namespace telegram_bot_api