Sunshine v2025.118.151840
Self-hosted game stream host for Moonlight.
stat_trackers.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <chrono>
8#include <functional>
9#include <limits>
10
11#include <boost/format.hpp>
12
13namespace stat_trackers {
14
15 boost::format
16 one_digit_after_decimal();
17
18 boost::format
19 two_digits_after_decimal();
20
21 template <typename T>
23 public:
24 using callback_function = std::function<void(T stat_min, T stat_max, double stat_avg)>;
25
26 void
27 collect_and_callback_on_interval(T stat, const callback_function &callback, std::chrono::seconds interval_in_seconds) {
28 if (data.calls == 0) {
29 data.last_callback_time = std::chrono::steady_clock::now();
30 }
31 else if (std::chrono::steady_clock::now() > data.last_callback_time + interval_in_seconds) {
32 callback(data.stat_min, data.stat_max, data.stat_total / data.calls);
33 data = {};
34 }
35 data.stat_min = std::min(data.stat_min, stat);
36 data.stat_max = std::max(data.stat_max, stat);
37 data.stat_total += stat;
38 data.calls += 1;
39 }
40
41 void
42 reset() {
43 data = {};
44 }
45
46 private:
47 struct {
48 std::chrono::steady_clock::time_point last_callback_time = std::chrono::steady_clock::now();
49 T stat_min = std::numeric_limits<T>::max();
50 T stat_max = std::numeric_limits<T>::min();
51 double stat_total = 0;
52 uint32_t calls = 0;
53 } data;
54 };
55
56} // namespace stat_trackers
Definition stat_trackers.h:22