Skip to content

Commit dd0b194

Browse files
[SDK 866] Event class move to seprate file (#79)
* Event class move to seprate file * Update CHANGELOG.md Co-authored-by: ArtursKadikis <[email protected]>
1 parent 6db783c commit dd0b194

File tree

8 files changed

+76
-63
lines changed

8 files changed

+76
-63
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
22.06.0
2+
* !! Major breaking change !! We are extracting the 'Event' class out of the 'Countly' class which will change how that class can be referenced.'Countly::Event' will not work, you will have to use 'cly::Event' instead.
3+
14
22.02.0
25
* Added 10-second time-outs for all windows HTTP transactions.
36
* Added ability to record views.

examples/example_integration.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#endif
1111

1212
using namespace std;
13+
using namespace cly;
1314

1415
void printLog(Countly::LogLevel level, const string &msg) {
1516
string lvl = "[DEBUG]";
@@ -73,7 +74,7 @@ int main() {
7374
ct.RecordEvent("Event with count and sum", 644, 13.3);
7475
break;
7576
case 3: {
76-
Countly::Event event("Event with sum, count, duration", 1, 10, 60.5);
77+
Event event("Event with sum, count, duration", 1, 10, 60.5);
7778
ct.addEvent(event);
7879
break;
7980
}

include/countly.hpp

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#ifdef _WIN32
2222
#undef ERROR
2323
#endif
24+
#include "countly/event.hpp"
2425
#include "countly/logger_module.hpp"
2526
#include "countly/views_module.hpp"
2627

@@ -29,7 +30,6 @@ class Countly : public cly::CountlyDelegates {
2930
Countly();
3031

3132
~Countly();
32-
3333
static Countly &getInstance();
3434

3535
// Do not implicitly generate the copy constructor, this is a singleton.
@@ -102,9 +102,7 @@ class Countly : public cly::CountlyDelegates {
102102

103103
void setUpdateInterval(size_t milliseconds);
104104

105-
class Event;
106-
107-
void addEvent(const Event &event);
105+
void addEvent(const cly::Event &event);
108106

109107
void setMaxEvents(size_t value);
110108

@@ -138,33 +136,6 @@ class Countly : public cly::CountlyDelegates {
138136
void setDatabasePath(const std::string &path);
139137
#endif
140138

141-
class Event {
142-
public:
143-
Event(const std::string &key, size_t count = 1);
144-
Event(const std::string &key, size_t count, double sum);
145-
Event(const std::string &key, size_t count, double sum, double duration);
146-
147-
void setTimestamp();
148-
149-
void startTimer();
150-
void stopTimer();
151-
152-
template <typename T> void addSegmentation(const std::string &key, T value) {
153-
if (object.find("segmentation") == object.end()) {
154-
object["segmentation"] = nlohmann::json::object();
155-
}
156-
157-
object["segmentation"][key] = value;
158-
}
159-
160-
std::string serialize() const;
161-
162-
private:
163-
nlohmann::json object;
164-
bool timer_running;
165-
std::chrono::system_clock::time_point timestamp;
166-
};
167-
168139
void SetPath(const std::string &path) {
169140
#ifdef COUNTLY_USE_SQLITE
170141
setDatabasePath(path);
@@ -187,12 +158,12 @@ class Countly : public cly::CountlyDelegates {
187158

188159
inline cly::ViewsModule &views() const { return *views_module.get(); }
189160

190-
void RecordEvent(const std::string &key, int count) override { addEvent(Event(key, count)); }
161+
void RecordEvent(const std::string &key, int count) override { addEvent(cly::Event(key, count)); }
191162

192-
void RecordEvent(const std::string &key, int count, double sum) override { addEvent(Event(key, count, sum)); }
163+
void RecordEvent(const std::string &key, int count, double sum) override { addEvent(cly::Event(key, count, sum)); }
193164

194165
void RecordEvent(const std::string &key, const std::map<std::string, std::string> &segmentation, int count) override {
195-
Event event(key, count);
166+
cly::Event event(key, count);
196167

197168
for (auto key_value : segmentation) {
198169
event.addSegmentation(key_value.first, key_value.second);
@@ -202,7 +173,7 @@ class Countly : public cly::CountlyDelegates {
202173
}
203174

204175
void RecordEvent(const std::string &key, const std::map<std::string, std::string> &segmentation, int count, double sum) override {
205-
Event event(key, count, sum);
176+
cly::Event event(key, count, sum);
206177

207178
for (auto key_value : segmentation) {
208179
event.addSegmentation(key_value.first, key_value.second);
@@ -212,7 +183,7 @@ class Countly : public cly::CountlyDelegates {
212183
}
213184

214185
void RecordEvent(const std::string &key, const std::map<std::string, std::string> &segmentation, int count, double sum, double duration) override {
215-
Event event(key, count, sum, duration);
186+
cly::Event event(key, count, sum, duration);
216187

217188
for (auto key_value : segmentation) {
218189
event.addSegmentation(key_value.first, key_value.second);

include/countly/event.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef EVENT_HPP_
2+
#define EVENT_HPP_
3+
#include "nlohmann/json.hpp"
4+
#include <chrono>
5+
#include <string>
6+
7+
namespace cly {
8+
9+
class Event {
10+
public:
11+
Event(const std::string &key, size_t count = 1);
12+
Event(const std::string &key, size_t count, double sum);
13+
Event(const std::string &key, size_t count, double sum, double duration);
14+
15+
void setTimestamp();
16+
17+
void startTimer();
18+
void stopTimer();
19+
20+
template <typename T> void addSegmentation(const std::string &key, T value) {
21+
if (object.find("segmentation") == object.end()) {
22+
object["segmentation"] = nlohmann::json::object();
23+
}
24+
25+
object["segmentation"][key] = value;
26+
}
27+
28+
std::string serialize() const;
29+
30+
private:
31+
nlohmann::json object;
32+
bool timer_running;
33+
std::chrono::system_clock::time_point timestamp;
34+
};
35+
} // namespace cly
36+
#endif

src/countly.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ void Countly::setUpdateInterval(size_t milliseconds) {
391391
mutex.unlock();
392392
}
393393

394-
void Countly::addEvent(const Event &event) {
394+
void Countly::addEvent(const cly::Event &event) {
395395
mutex.lock();
396396
#ifndef COUNTLY_USE_SQLITE
397397
if (event_queue.size() == max_events) {

src/event.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
1-
#include "countly.hpp"
1+
#include "countly/event.hpp"
22

3-
Countly::Event::Event(const std::string &key, size_t count) : object({}), timer_running(false) {
3+
namespace cly {
4+
Event::Event(const std::string &key, size_t count) : object({}), timer_running(false) {
45
object["key"] = key;
56
object["count"] = count;
67
}
78

8-
Countly::Event::Event(const std::string &key, size_t count, double sum) : object({}), timer_running(false) {
9+
Event::Event(const std::string &key, size_t count, double sum) : object({}), timer_running(false) {
910
object["key"] = key;
1011
object["count"] = count;
1112
object["sum"] = sum;
1213
}
1314

14-
Countly::Event::Event(const std::string &key, size_t count, double sum, double duration) : object({}), timer_running(false) {
15+
Event::Event(const std::string &key, size_t count, double sum, double duration) : object({}), timer_running(false) {
1516
object["key"] = key;
1617
object["count"] = count;
1718
object["sum"] = sum;
1819
object["dur"] = duration;
1920
}
2021

21-
void Countly::Event::setTimestamp() {
22-
timestamp = Countly::getTimestamp();
22+
void Event::setTimestamp() {
23+
timestamp = std::chrono::system_clock::now();
2324
object["timestamp"] = std::chrono::duration_cast<std::chrono::seconds>(timestamp.time_since_epoch()).count();
2425
}
2526

26-
void Countly::Event::startTimer() {
27+
void Event::startTimer() {
2728
setTimestamp();
2829
timer_running = true;
2930
}
3031

31-
void Countly::Event::stopTimer() {
32+
void Event::stopTimer() {
3233
if (timer_running) {
33-
auto now = Countly::getTimestamp();
34+
auto now = std::chrono::system_clock::now();
3435
object["dur"] = std::chrono::duration_cast<std::chrono::seconds>(now - timestamp).count();
3536
timer_running = false;
3637
}
3738
}
3839

39-
std::string Countly::Event::serialize() const { return object.dump(); }
40+
std::string Event::serialize() const { return object.dump(); }
41+
} // namespace cly

tests/event.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,63 @@
44
TEST_CASE("events are serialized correctly") {
55
SUBCASE("without segmentation") {
66
SUBCASE("without sum") {
7-
Countly::Event event("win", 1);
7+
cly::Event event("win", 1);
88
CHECK(event.serialize() == "{\"count\":1,\"key\":\"win\"}");
99
}
1010

1111
SUBCASE("with sum") {
12-
Countly::Event event("buy", 2, 9.99);
12+
cly::Event event("buy", 2, 9.99);
1313
CHECK(event.serialize() == "{\"count\":2,\"key\":\"buy\",\"sum\":9.99}");
1414
}
1515
}
1616

1717
SUBCASE("with segmentation") {
1818
SUBCASE("with signed integer") {
19-
Countly::Event event("lose", 3);
19+
cly::Event event("lose", 3);
2020
event.addSegmentation("points", -144);
2121
CHECK(event.serialize() == "{\"count\":3,\"key\":\"lose\",\"segmentation\":{\"points\":-144}}");
2222
}
2323

2424
SUBCASE("with unsigned integer") {
25-
Countly::Event event("win", 1);
25+
cly::Event event("win", 1);
2626
event.addSegmentation("points", 232U);
2727
CHECK(event.serialize() == "{\"count\":1,\"key\":\"win\",\"segmentation\":{\"points\":232}}");
2828
}
2929

3030
SUBCASE("with boolean") {
31-
Countly::Event event("win", 1);
31+
cly::Event event("win", 1);
3232
event.addSegmentation("alive", true);
3333
CHECK(event.serialize() == "{\"count\":1,\"key\":\"win\",\"segmentation\":{\"alive\":true}}");
3434
}
3535

3636
SUBCASE("with string") {
37-
Countly::Event event("message", 1);
37+
cly::Event event("message", 1);
3838
event.addSegmentation("sender", "TheLegend27");
3939
CHECK(event.serialize() == "{\"count\":1,\"key\":\"message\",\"segmentation\":{\"sender\":\"TheLegend27\"}}");
4040
}
4141

4242
SUBCASE("with multiple values") {
43-
Countly::Event event("buy", 5);
43+
cly::Event event("buy", 5);
4444
event.addSegmentation("quantity", 27);
4545
event.addSegmentation("searchQuery", "cheap cheese");
4646
CHECK(event.serialize() == "{\"count\":5,\"key\":\"buy\",\"segmentation\":{\"quantity\":27,\"searchQuery\":\"cheap cheese\"}}");
4747
}
4848

4949
SUBCASE("with changing values") {
50-
Countly::Event event("lose", 3);
50+
cly::Event event("lose", 3);
5151
event.addSegmentation("points", -144);
5252
event.addSegmentation("points", 2000);
5353
CHECK(event.serialize() == "{\"count\":3,\"key\":\"lose\",\"segmentation\":{\"points\":2000}}");
5454
}
5555

5656
SUBCASE("with count, sum, duration and segmentation") {
57-
Countly::Event event("lose", 3, 10, 100);
57+
cly::Event event("lose", 3, 10, 100);
5858
event.addSegmentation("points", 2000);
5959
CHECK(event.serialize() == "{\"count\":3,\"dur\":100.0,\"key\":\"lose\",\"segmentation\":{\"points\":2000},\"sum\":10.0}");
6060
}
6161

6262
SUBCASE("with multibyte strings") {
63-
Countly::Event event("测试", 1);
63+
cly::Event event("测试", 1);
6464
event.addSegmentation("苹果", "美味");
6565
CHECK(event.serialize() == "{\"count\":1,\"key\":\"测试\",\"segmentation\":{\"苹果\":\"美味\"}}");
6666
}

tests/main.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ TEST_CASE("events are sent correctly") {
217217
}
218218

219219
SUBCASE("single event is sent") {
220-
Countly::Event event("win", 4);
220+
cly::Event event("win", 4);
221221
event.addSegmentation("points", 100);
222222
countly.addEvent(event);
223223
WAIT_FOR_SQLITE(1);
@@ -231,8 +231,8 @@ TEST_CASE("events are sent correctly") {
231231
}
232232

233233
SUBCASE("two events are sent") {
234-
Countly::Event event1("win", 2);
235-
Countly::Event event2("achievement", 1);
234+
cly::Event event1("win", 2);
235+
cly::Event event2("achievement", 1);
236236
countly.addEvent(event1);
237237
countly.addEvent(event2);
238238
WAIT_FOR_SQLITE(1);
@@ -246,7 +246,7 @@ TEST_CASE("events are sent correctly") {
246246
}
247247

248248
SUBCASE("event with count, sum, duration and segmentation is sent") {
249-
Countly::Event event("lose", 3, 10, 100);
249+
cly::Event event("lose", 3, 10, 100);
250250
event.addSegmentation("points", 2000);
251251
countly.addEvent(event);
252252

@@ -262,7 +262,7 @@ TEST_CASE("events are sent correctly") {
262262

263263
SUBCASE("100 events are sent") {
264264
for (int i = 0; i < 100; i++) {
265-
Countly::Event event("click", i);
265+
cly::Event event("click", i);
266266
countly.addEvent(event);
267267
}
268268
WAIT_FOR_SQLITE(1);

0 commit comments

Comments
 (0)