Improve statistics retrieval.

This commit is contained in:
levlam 2022-08-24 15:48:27 +03:00
parent aa9eff357c
commit 5eb24c7e63
3 changed files with 23 additions and 9 deletions

View File

@ -36,7 +36,7 @@
#include "td/utils/StringBuilder.h" #include "td/utils/StringBuilder.h"
#include "td/utils/Time.h" #include "td/utils/Time.h"
#include <map> #include <algorithm>
#include <tuple> #include <tuple>
namespace telegram_bot_api { namespace telegram_bot_api {
@ -182,7 +182,8 @@ void ClientManager::get_stats(td::Promise<td::BufferSlice> promise,
auto now = td::Time::now(); auto now = td::Time::now();
td::int32 active_bot_count = 0; td::int32 active_bot_count = 0;
std::multimap<td::int64, td::uint64> top_bot_ids; td::vector<std::pair<td::int64, td::uint64>> top_bot_ids;
size_t max_bots = 50;
for (auto id : clients_.ids()) { for (auto id : clients_.ids()) {
auto *client_info = clients_.get(id); auto *client_info = clients_.get(id);
CHECK(client_info); CHECK(client_info);
@ -195,15 +196,17 @@ void ClientManager::get_stats(td::Promise<td::BufferSlice> promise,
continue; continue;
} }
auto stats = client_info->stat_.as_vector(now); auto score = static_cast<td::int64>(client_info->stat_.get_score(now) * -1e9);
double score = 0.0; if (score == 0 && top_bot_ids.size() >= max_bots) {
for (auto &stat : stats) { continue;
if (stat.key_ == "update_count" || stat.key_ == "request_count") {
score -= td::to_double(stat.value_);
}
} }
top_bot_ids.emplace(static_cast<td::int64>(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'; sb << stat_.get_description() << '\n';
if (id_filter.empty()) { if (id_filter.empty()) {

View File

@ -149,6 +149,15 @@ td::string BotStatActor::get_description() const {
return res; 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 { bool BotStatActor::is_active(double now) const {
return last_activity_timestamp_ > now - 86400; return last_activity_timestamp_ > now - 86400;
} }

View File

@ -181,6 +181,8 @@ class BotStatActor final : public td::Actor {
td::vector<StatItem> as_vector(double now); td::vector<StatItem> as_vector(double now);
td::string get_description() const; td::string get_description() const;
double get_score(double now);
bool is_active(double now) const; bool is_active(double now) const;
private: private: