Add active_request_count and active_file_upload_bytes to bot statistics.

This commit is contained in:
levlam 2022-09-18 02:54:22 +03:00
parent 11d27e3e52
commit 27bb4831f6
4 changed files with 45 additions and 4 deletions

View File

@ -245,11 +245,19 @@ void ClientManager::get_stats(td::Promise<td::BufferSlice> 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_) {

View File

@ -135,7 +135,7 @@ void Query::send_response_stat() const {
return;
}
send_closure(stat_actor_, &BotStatActor::add_event<ServerBotStat::Response>,
ServerBotStat::Response{state_ == State::OK, answer_.size()}, now);
ServerBotStat::Response{state_ == State::OK, answer_.size(), files_size()}, now);
}
} // namespace telegram_bot_api

View File

@ -155,9 +155,19 @@ double BotStatActor::get_score(double now) {
if (minute_stat.second != 0) {
result /= minute_stat.second;
}
result += td::max(static_cast<double>(get_active_request_count() - 10), static_cast<double>(0));
result += static_cast<double>(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;
}

View File

@ -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<double>(answer.size_);
response_bytes_ += static_cast<double>(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<EventT>, 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<ServerBotStat> stat_[SIZE];
td::ActorId<BotStatActor> 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