Skip to content

Commit 7645f7e

Browse files
[SDK 864] Views features added (#68)
* files added * view module changes * View features finalized * logger module pointer changed to shared_ptr * views uuid implementation * oopen view mehtod return type updated map to string method added. * - Methods documentation added - event id added - EQ into list, get method added - Utils methods added * Adding unit tests * Unit tests refactor Unit tests added "close view with name and id" * changes, power shortage * memory leak fixed PR changes added * view id added in class rando no generator updated * changes back to struct * randome generator function compile issue fixed view moduel udated * struct -> shared_ptr * shared_ptr redundant copy removed. * code revert * PR Changes stable state * View module restructure RecordEvent signatures updates * random engined costructed from time base seed. * MISRA rules applied * MISRA rules applied * Update countly.hpp * typo fixed! * changelog added * Update CHANGELOG.md * Unit tests added! * - redudant code moved to a common function - event size variable move into subcases - "start" field logic updated * test improved * Duration calculation logic updated Sample app updated Unit tests updated Chagne log updated * comments added! * sample app updated * event size check after every view insertion Co-authored-by: ArtursKadikis <[email protected]>
1 parent 90b9b5f commit 7645f7e

File tree

12 files changed

+652
-131
lines changed

12 files changed

+652
-131
lines changed

.clang-format

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
Language: Cpp
3+
BasedOnStyle: LLVM
4+
5+
ColumnLimit: 300
6+
7+

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
22.02.0
22
* Added 10-second time-outs for all windows HTTP transactions.
3+
* Added ability to record views.
34

45
21.11.3
56
* Added functionality to set custom SHA256.

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ endif()
2020

2121
add_library(countly
2222
${CMAKE_CURRENT_SOURCE_DIR}/src/countly.cpp
23+
${CMAKE_CURRENT_SOURCE_DIR}/src/views_module.cpp
2324
${CMAKE_CURRENT_SOURCE_DIR}/src/logger_module.cpp
2425
${CMAKE_CURRENT_SOURCE_DIR}/src/event.cpp)
2526

@@ -67,6 +68,7 @@ endif()
6768
if(COUNTLY_BUILD_TESTS)
6869
add_executable(countly-tests
6970
${CMAKE_CURRENT_SOURCE_DIR}/tests/main.cpp
71+
${CMAKE_CURRENT_SOURCE_DIR}/tests/views.cpp
7072
${CMAKE_CURRENT_SOURCE_DIR}/tests/event.cpp)
7173
if(COUNTLY_USE_SQLITE)
7274
target_compile_definitions(countly-tests PRIVATE COUNTLY_USE_SQLITE)

examples/example_integration.cpp

Lines changed: 125 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,142 @@
1-
#include <thread>
1+
#include "countly.hpp"
22
#include <chrono>
3-
#include <random>
43
#include <iostream>
5-
#include "countly.hpp"
4+
#include <random>
5+
#include <thread>
6+
#ifdef _WIN32
7+
#include <Windows.h>
8+
#else
9+
#include <unistd.h>
10+
#endif
611

712
using namespace std;
813

9-
void printLog(Countly::LogLevel level, const string& msg) {
10-
string lvl = "[DEBUG]";
11-
switch (level) {
12-
case Countly::LogLevel::DEBUG:
13-
lvl = "[Debug]";
14-
break;
15-
case Countly::LogLevel::INFO:
16-
lvl = "[INFO]";
17-
break;
18-
case Countly::LogLevel::WARNING:
19-
lvl = "[WARNING]";
20-
break;
21-
case Countly::LogLevel::ERROR:
22-
lvl = "[ERROR]";
23-
break;
24-
case Countly::LogLevel::FATAL:
25-
lvl = "[FATAL]";
26-
break;
14+
void printLog(Countly::LogLevel level, const string &msg) {
15+
string lvl = "[DEBUG]";
16+
switch (level) {
17+
case Countly::LogLevel::DEBUG:
18+
lvl = "[Debug]";
19+
break;
20+
case Countly::LogLevel::INFO:
21+
lvl = "[INFO]";
22+
break;
23+
case Countly::LogLevel::WARNING:
24+
lvl = "[WARNING]";
25+
break;
26+
case Countly::LogLevel::FATAL:
27+
lvl = "[FATAL]";
28+
break;
29+
default:
30+
lvl = "[ERROR]";
31+
break;
32+
}
2733

28-
default:
29-
break;
30-
}
31-
32-
cout << lvl << msg << endl;
34+
cout << lvl << msg << endl;
3335
}
3436

37+
int main() {
38+
cout << "Sample App" << endl;
39+
Countly &ct = Countly::getInstance();
40+
ct.alwaysUsePost(true);
41+
ct.setDeviceID("test-device-id");
3542

43+
ct.setLogger(printLog);
44+
// OS, OS_version, device, resolution, carrier, app_version);
45+
ct.SetMetrics("Windows 10", "10.22", "Mac", "800x600", "Carrier", "1.0");
46+
// Server and port
47+
ct.start("YOUR_APP_KEY", "https://try.count.ly", 443, true);
48+
ct.SetMaxEventsPerMessage(10);
49+
ct.setAutomaticSessionUpdateInterval(5);
3650

37-
int main() {
38-
cout << "Sample App" << endl;
39-
Countly& ct = Countly::getInstance();
40-
ct.alwaysUsePost(true);
41-
ct.setDeviceID("test-device-id");
51+
bool flag = true;
52+
while (flag) {
53+
cout << "Choose your option:" << endl;
54+
cout << "1) Basic Event" << endl;
55+
cout << "2) Event with count and sum" << endl;
56+
cout << "3) Event with count, sum, duration" << endl;
57+
cout << "4) Event with sum, count, duration and segmentation" << endl;
58+
cout << "5) Update Session" << endl;
59+
cout << "6) Download remote config" << endl;
60+
cout << "7) Send user detail to server" << endl;
61+
cout << "8) Change device id with server merge" << endl;
62+
cout << "9) Change device id without server merge" << endl;
63+
cout << "10) Set user location" << endl;
64+
cout << "11) Record a view" << endl;
65+
cout << "0) Exit" << endl;
66+
int a;
67+
cin >> a;
68+
switch (a) {
69+
case 1:
70+
ct.RecordEvent("[CLY]_view", 123);
71+
break;
72+
case 2:
73+
ct.RecordEvent("Event with count and sum", 644, 13.3);
74+
break;
75+
case 3: {
76+
Countly::Event event("Event with sum, count, duration", 1, 10, 60.5);
77+
ct.addEvent(event);
78+
break;
79+
}
80+
case 4: {
81+
std::map<std::string, std::string> segmentation;
82+
segmentation["name"] = "start and end";
83+
ct.RecordEvent("Event with segmentation, count and sum", segmentation, 1, 0, 10);
84+
break;
85+
}
86+
case 5:
87+
ct.updateSession();
88+
break;
89+
case 6: {
90+
ct.updateRemoteConfig();
91+
break;
92+
}
93+
case 7: {
94+
std::map<std::string, std::string> userdetail = {
95+
{"name", "Full name"}, {"username", "username123"}, {"email", "[email protected]"}, {"phone", "222-222-222"}, {"phone", "222-222-222"}, {"picture", "http://webresizer.com/images2/bird1_after.jpg"}, {"gender", "M"}, {"byear", "1991"}, {"organization", "Organization"},
96+
};
97+
98+
ct.getInstance().setUserDetails(userdetail);
99+
} break;
100+
case 8:
101+
ct.setDeviceID("new-device-id", true);
102+
break;
103+
case 9:
104+
ct.setDeviceID("new-device-id", false);
105+
break;
106+
case 10: {
107+
string countryCode = "us";
108+
string city = "Houston";
109+
string latitude = "29.634933";
110+
string longitude = "-95.220255";
111+
string ipAddress = "192.168.0.1";
112+
113+
ct.setLocation(countryCode, city, latitude + "," + longitude, ipAddress);
114+
} break;
42115

43-
ct.setLogger(printLog);
44-
// OS, OS_version, device, resolution, carrier, app_version);
45-
ct.SetMetrics("Windows 10", "10.22", "Mac", "800x600", "Carrier", "1.0");
46-
// Server and port
47-
ct.start("APP_KEY", "https://try.count.ly", 443, true);
48-
ct.SetMaxEventsPerMessage(10);
49-
ct.setAutomaticSessionUpdateInterval(5);
116+
case 11: {
117+
std::map<std::string, std::string> segmentation = {
118+
{"platform", "ubuntu"},
119+
{"time", "60"},
120+
};
50121

51-
bool flag = true;
52-
while (flag) {
53-
cout << "Choose your option:" << endl;
54-
cout << "1) Basic Event" << endl;
55-
cout << "2) Event with count and sum" << endl;
56-
cout << "3) Event with count, sum, duration" << endl;
57-
cout << "4) Event with sum, count, duration and segmentation" << endl;
58-
cout << "5) Update Session" << endl;
59-
cout << "6) Download remote config" << endl;
60-
cout << "7) Send user detail to server" << endl;
61-
cout << "8) Change device id with server merge" << endl;
62-
cout << "9) Change device id without server merge" << endl;
63-
cout << "10) Set user location" << endl;
64-
cout << "0) Exit" << endl;
65-
int a;
66-
cin >> a;
67-
switch (a) {
68-
case 1:
69-
ct.RecordEvent("[CLY]_view", 123);
70-
break;
71-
case 2:
72-
ct.RecordEvent("Event with count and sum", 644, 13.3);
73-
break;
74-
case 3: {
75-
Countly::Event event("Event with sum, count, duration", 1, 10, 60.5);
76-
ct.addEvent(event);
77-
break;
78-
}
79-
case 4: {
80-
std::map<std::string, std::string> segmentation;
81-
segmentation["name"] = "start and end";
82-
ct.RecordEvent("Event with segmentation, count and sum", segmentation, 1, 0, 10);
83-
break;
84-
}
85-
case 5:
86-
ct.updateSession();
87-
break;
88-
case 6: {
89-
ct.updateRemoteConfig();
90-
break;
91-
}
92-
case 7: {
93-
std::map<std::string, std::string> userdetail = {
94-
{"name", "Full name"},
95-
{"username", "username123"},
96-
{"email", "[email protected]"},
97-
{"phone", "222-222-222"},
98-
{"phone", "222-222-222"},
99-
{"picture", "http://webresizer.com/images2/bird1_after.jpg"},
100-
{"gender", "M"},
101-
{"byear", "1991"},
102-
{"organization", "Organization"},
103-
};
122+
// Open a view
123+
std::string viewID = ct.views().openView("Main view", segmentation);
104124

105-
ct.getInstance().setUserDetails(userdetail);
106-
}
107-
break;
108-
case 8:
109-
ct.setDeviceID("new-device-id", true);
110-
break;
111-
case 9:
112-
ct.setDeviceID("new-device-id", false);
113-
break;
114-
case 10: {
115-
string countryCode = "us";
116-
string city = "Houston";
117-
string latitude = "29.634933";
118-
string longitude = "-95.220255";
119-
string ipAddress = "192.168.0.1";
125+
Sleep(2000); // two seconds
120126

121-
ct.setLocation(countryCode, city, latitude + "," + longitude, ipAddress);
122-
}
123-
break;
124-
case 0:
125-
flag = false;
126-
break;
127-
default:
128-
cout << "Option not found!" << endl;
129-
break;
130-
}
131-
}
127+
// Close an opened view
128+
ct.views().closeViewWithID(viewID);
129+
} break;
130+
case 0:
131+
flag = false;
132+
break;
133+
default:
134+
cout << "Option not found!" << endl;
135+
break;
136+
}
137+
}
132138

133-
ct.stop();
139+
ct.stop();
134140

135-
return 0;
141+
return 0;
136142
}

include/countly.hpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
#undef ERROR
2323
#endif
2424
#include "countly/logger_module.hpp"
25+
#include "countly/views_module.hpp"
2526

26-
class Countly {
27+
class Countly : public cly::CountlyDelegates {
2728
public:
2829
Countly();
2930

@@ -196,15 +197,19 @@ class Countly {
196197
stop();
197198
}
198199

199-
void RecordEvent(const std::string key, int count) {
200+
inline cly::ViewsModule& views() const {
201+
return *views_module.get();
202+
}
203+
204+
void RecordEvent(const std::string& key, int count) override {
200205
addEvent(Event(key, count));
201206
}
202207

203-
void RecordEvent(const std::string key, int count, double sum) {
208+
void RecordEvent(const std::string& key, int count, double sum) override {
204209
addEvent(Event(key, count, sum));
205210
}
206211

207-
void RecordEvent(const std::string key, std::map<std::string, std::string> segmentation, int count) {
212+
void RecordEvent(const std::string &key, const std::map<std::string, std::string> &segmentation, int count) override {
208213
Event event(key, count);
209214

210215
for (auto key_value: segmentation) {
@@ -214,7 +219,7 @@ class Countly {
214219
addEvent(event);
215220
}
216221

217-
void RecordEvent(const std::string key, std::map<std::string, std::string> segmentation, int count, double sum) {
222+
void RecordEvent(const std::string &key, const std::map<std::string, std::string> &segmentation, int count, double sum) override {
218223
Event event(key, count, sum);
219224

220225
for (auto key_value: segmentation) {
@@ -224,7 +229,7 @@ class Countly {
224229
addEvent(event);
225230
}
226231

227-
void RecordEvent(const std::string key, std::map<std::string, std::string> segmentation, int count, double sum, double duration) {
232+
void RecordEvent(const std::string &key, const std::map<std::string, std::string> &segmentation, int count, double sum, double duration) override {
228233
Event event(key, count, sum, duration);
229234

230235
for (auto key_value: segmentation) {
@@ -238,6 +243,18 @@ class Countly {
238243
inline void setAutomaticSessionUpdateInterval(unsigned short updateInterval) {
239244
_auto_session_update_interval = updateInterval;
240245
}
246+
247+
/**
248+
* Convert event queue into list.
249+
* Warning: This method is for debugging purposes, and it is going to be removed in the future.
250+
* You should not be using this method.
251+
* @return a vector object containing events.
252+
*/
253+
const std::vector<std::string> debugReturnStateOfEQ() {
254+
std::vector<std::string> v(event_queue.begin(), event_queue.end());
255+
return v;
256+
}
257+
241258
private:
242259
void _deleteThread();
243260
void _sendIndependantLocationRequest();
@@ -275,7 +292,8 @@ class Countly {
275292
std::string salt;
276293

277294
std::unique_ptr<std::thread> thread;
278-
std::unique_ptr<cly::LoggerModule> logger;
295+
std::unique_ptr<cly::ViewsModule> views_module;
296+
std::shared_ptr<cly::LoggerModule> logger;
279297

280298
std::mutex mutex;
281299
bool stop_thread = false;

0 commit comments

Comments
 (0)