forked from mail-ru-im/im-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatistics.h
139 lines (116 loc) · 5.02 KB
/
statistics.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#pragma once
#include "proxy_settings.h"
namespace core
{
class coll_helper;
class async_executer;
namespace tools
{
class binary_stream;
}
const static std::string flurry_url = "https://data.flurry.com/aah.do";
#ifdef DEBUG
const static auto send_interval = std::chrono::minutes(1);
const static auto fetch_ram_usage_interval = std::chrono::seconds(30);
#else
const static auto send_interval = std::chrono::hours(1);
const static auto fetch_ram_usage_interval = std::chrono::hours(1);
#endif // DEBUG
const static auto save_to_file_interval = std::chrono::seconds(10);
const static auto delay_send_on_start = std::chrono::seconds(10);
const static auto fetch_disk_size_interval = std::chrono::hours(24);
namespace stats
{
enum class stats_event_names;
class statistics : public std::enable_shared_from_this<statistics>
{
public:
struct disk_stats
{
disk_stats(size_t _user_folder_size = -1)
: user_folder_size_(_user_folder_size)
{}
bool is_initialized() const { return user_folder_size_ != 0; }
size_t user_folder_size_ = 0;
};
private:
class stats_event
{
public:
std::string to_string(time_t _start_time) const;
stats_event(stats_event_names _name, std::chrono::system_clock::time_point _event_time, int32_t _event_id, const event_props_type& props);
stats_event_names get_name() const;
event_props_type get_props() const;
static void reset_session_event_id();
std::chrono::system_clock::time_point get_time() const;
int32_t get_id() const;
template <typename ValueType>
void increment_prop(event_prop_key_type _key, ValueType _value);
template <typename ValueType>
void set_prop(event_prop_key_type _key, ValueType _value);
bool finished_accumulating();
private:
stats_event_names name_;
int32_t event_id_; // natural serial number, starting from 1
event_props_type props_;
static long long session_event_id_;
std::chrono::system_clock::time_point event_time_;
};
struct stop_objects
{
std::atomic_bool is_stop_;
stop_objects()
{
is_stop_ = false;
}
};
uint64_t last_used_ram_mb_ = 0;
typedef std::vector<stats_event>::const_iterator events_ci;
static std::shared_ptr<stop_objects> stop_objects_;
std::map<std::string, tools::binary_stream> values_;
std::wstring file_name_;
disk_stats disk_stats_;
bool changed_;
uint32_t save_timer_;
uint32_t disk_stats_timer_;
uint32_t ram_stats_timer_;
uint32_t send_timer_;
uint32_t start_send_timer_;
std::unique_ptr<async_executer> stats_thread_;
std::vector<stats_event> events_;
std::vector<stats_event> accumulated_events_;
std::string events_to_json(events_ci begin, events_ci end, time_t _start_time) const;
std::chrono::system_clock::time_point last_sent_time_;
std::vector<std::string> get_post_data() const;
static bool send(const proxy_settings& _user_proxy, const std::string& post_data, const std::wstring& _file_name);
void serialize(tools::binary_stream& _bs) const;
bool unserialize(tools::binary_stream& _bs);
void save_if_needed();
void send_async();
void set_disk_stats(const disk_stats &_stats);
void query_disk_size_async();
bool load();
void start_save();
void start_send();
void insert_event(stats_event_names _event_name, const event_props_type& _props,
std::chrono::system_clock::time_point _event_time, int32_t _event_id);
void insert_accumulated_event(stats_event_names _event_name, core::stats::event_props_type _props);
void clear();
void delayed_start_send();
void start_disk_operations();
void start_ram_usage_monitoring();
void check_ram_usage();
public:
statistics(std::wstring _file_name);
virtual ~statistics();
void init();
void insert_event(stats_event_names _event_name, const event_props_type& _props);
void insert_event(stats_event_names _event_name);
// Inserts if doesn't exist
template <typename ValueType>
void increment_event_prop(stats_event_names _event_name,
event_prop_key_type _prop_key,
ValueType _value);
};
}
}