-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathconstants.hpp
More file actions
100 lines (80 loc) · 2.85 KB
/
constants.hpp
File metadata and controls
100 lines (80 loc) · 2.85 KB
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
#ifndef COUNTLY_CONSTANTS_HPP_
#define COUNTLY_CONSTANTS_HPP_
#include "nlohmann/json.hpp"
#include <cassert>
#include <chrono>
#include <climits>
#include <functional>
#include <map>
#include <memory>
#include <random>
#include <sstream>
#include <string>
#define COUNTLY_SDK_NAME "cpp-native-unknown"
#define COUNTLY_SDK_VERSION "23.2.3"
#define COUNTLY_POST_THRESHOLD 2000
#define COUNTLY_KEEPALIVE_INTERVAL 3000
#define COUNTLY_MAX_EVENTS_DEFAULT 200
namespace cly {
struct HTTPResponse {
bool success;
nlohmann::json data;
};
using HTTPClientFunction = std::function<HTTPResponse(bool, const std::string &, const std::string &)>;
using SHA256Function = std::function<std::string(const std::string &)>;
namespace utils {
const std::default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count());
const std::uniform_int_distribution<int> distribution(1, INT_MAX);
/**
* Formats the given arguments into a string buffer.
*
* @param format formatting string
* @param args arguments to be formatted
* @return a string object holding the formatted result.
*/
template <typename... Args> static std::string format_string(const std::string &format, Args... args) {
int length = std::snprintf(nullptr, 0, format.c_str(), args...);
assert(length >= 0);
std::unique_ptr<char[]> buf(new char[length + 1]);
std::snprintf(buf.get(), length + 1, format.c_str(), args...);
std::string str(buf.get());
return str;
}
/**
* Gives a string representation of the size of a map.
*
* @param m a map containing key-value pairs
* @return a string object holding size of map.
* TODO: In the future, this function will be improved.
*/
static std::string mapToString(const std::map<std::string, std::string> &m) {
int lenght = m.size();
return std::to_string(lenght);
}
/**
* Generate a random UUID.
*
* @return a string object holding a UUID.
*/
static std::string generateEventID() {
auto dice = std::bind(distribution, generator);
int random = dice();
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
const auto timestamp = now.time_since_epoch();
std::stringstream r;
r << std::to_string(random);
r << "_";
r << std::to_string(timestamp.count());
return r.str();
}
} // namespace utils
class CountlyDelegates {
public:
virtual void RecordEvent(const std::string &key, int count) = 0;
virtual void RecordEvent(const std::string &key, int count, double sum) = 0;
virtual void RecordEvent(const std::string &key, const std::map<std::string, std::string> &segmentation, int count) = 0;
virtual void RecordEvent(const std::string &key, const std::map<std::string, std::string> &segmentation, int count, double sum) = 0;
virtual void RecordEvent(const std::string &key, const std::map<std::string, std::string> &segmentation, int count, double sum, double duration) = 0;
};
} // namespace cly
#endif