2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
2019-02-12 21:48:16 +01:00
|
|
|
#include "td/utils/common.h"
|
2020-06-24 13:47:36 +02:00
|
|
|
#include "td/utils/optional.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-06-26 01:24:13 +02:00
|
|
|
#include <functional>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
template <class StatT>
|
|
|
|
class TimedStat {
|
|
|
|
public:
|
|
|
|
TimedStat(double duration, double now)
|
2022-06-21 19:06:39 +02:00
|
|
|
: duration_(duration), current_(), current_timestamp_(now - 1), next_(), next_timestamp_(now) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
TimedStat() : TimedStat(0, 0) {
|
|
|
|
}
|
|
|
|
template <class EventT>
|
|
|
|
void add_event(const EventT &e, double now) {
|
|
|
|
update(now);
|
|
|
|
current_.on_event(e);
|
|
|
|
next_.on_event(e);
|
|
|
|
}
|
|
|
|
const StatT &get_stat(double now) {
|
|
|
|
update(now);
|
|
|
|
return current_;
|
|
|
|
}
|
|
|
|
std::pair<StatT, double> stat_duration(double now) {
|
|
|
|
update(now);
|
|
|
|
return std::make_pair(current_, now - current_timestamp_);
|
|
|
|
}
|
|
|
|
void clear_events() {
|
|
|
|
current_.clear();
|
|
|
|
next_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
double duration_;
|
|
|
|
StatT current_;
|
|
|
|
double current_timestamp_;
|
|
|
|
StatT next_;
|
|
|
|
double next_timestamp_;
|
|
|
|
|
2018-02-20 01:03:28 +01:00
|
|
|
void update(double &now) {
|
|
|
|
if (now < next_timestamp_) {
|
2019-02-12 17:17:20 +01:00
|
|
|
// LOG_CHECK(now >= next_timestamp_ * (1 - 1e-14)) << now << " " << next_timestamp_;
|
2018-02-20 01:03:28 +01:00
|
|
|
now = next_timestamp_;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (duration_ == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (next_timestamp_ + 2 * duration_ < now) {
|
|
|
|
current_ = StatT();
|
2022-11-01 12:03:20 +01:00
|
|
|
current_timestamp_ = now - duration_;
|
2018-12-31 20:04:05 +01:00
|
|
|
next_ = StatT();
|
|
|
|
next_timestamp_ = now;
|
|
|
|
} else if (next_timestamp_ + duration_ < now) {
|
|
|
|
current_ = next_;
|
|
|
|
current_timestamp_ = next_timestamp_;
|
|
|
|
next_ = StatT();
|
|
|
|
next_timestamp_ = now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-26 01:24:13 +02:00
|
|
|
namespace detail {
|
2020-06-24 13:47:36 +02:00
|
|
|
template <class T, class Cmp>
|
|
|
|
struct MinMaxStat {
|
|
|
|
using Event = T;
|
|
|
|
void on_event(Event event) {
|
|
|
|
if (!best_ || Cmp()(event, best_.value())) {
|
|
|
|
best_ = event;
|
|
|
|
}
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
optional<T> get_stat() const {
|
2020-06-24 13:47:36 +02:00
|
|
|
return best_.copy();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-06-26 01:24:13 +02:00
|
|
|
optional<T> best_;
|
2020-06-24 13:47:36 +02:00
|
|
|
};
|
2020-06-26 01:24:13 +02:00
|
|
|
} // namespace detail
|
2020-06-24 13:47:36 +02:00
|
|
|
|
|
|
|
template <class T>
|
2021-01-01 13:59:53 +01:00
|
|
|
using MinStat = detail::MinMaxStat<T, std::less<void>>;
|
2020-06-24 13:47:36 +02:00
|
|
|
|
|
|
|
template <class T>
|
2021-01-01 13:59:53 +01:00
|
|
|
using MaxStat = detail::MinMaxStat<T, std::greater<void>>;
|
2020-06-24 13:47:36 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|