Skip to content

Commit 9589bd5

Browse files
authored
[Sdk 913] (#81)
* namespace added on countly classes * CountlyConfiguration model added * changes * metrics add in config metric integrated metric unit tests added countly singelton instance code change, * countlyconfig pointer -> ref * compile definition issue fixed! * SDK_API_VERSION removed * conflict fixed! * code revert * clearSDK method added in unit tests code move inside subcases struct metrics removed reset renamed to halt enablePost renamed to forcePost * code coment format * clearSDK function added in tests * changes * Test utils added * code formating * constructor added for configuration
1 parent 90e1042 commit 9589bd5

File tree

8 files changed

+254
-85
lines changed

8 files changed

+254
-85
lines changed

CMakeLists.txt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ find_package(Threads)
4646
target_link_libraries(countly Threads::Threads)
4747

4848
target_include_directories(countly PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/vendor/json/include)
49+
if (COUNTLY_BUILD_TESTS)
50+
target_compile_definitions(countly PRIVATE COUNTLY_BUILD_TESTS)
51+
endif()
4952

5053
if (COUNTLY_USE_CUSTOM_SHA256)
5154
target_compile_definitions(countly PRIVATE COUNTLY_USE_CUSTOM_SHA256)
@@ -78,7 +81,10 @@ if(COUNTLY_BUILD_TESTS)
7881
add_executable(countly-tests
7982
${CMAKE_CURRENT_SOURCE_DIR}/tests/main.cpp
8083
${CMAKE_CURRENT_SOURCE_DIR}/tests/views.cpp
81-
${CMAKE_CURRENT_SOURCE_DIR}/tests/event.cpp)
84+
${CMAKE_CURRENT_SOURCE_DIR}/tests/event.cpp
85+
${CMAKE_CURRENT_SOURCE_DIR}/tests/config.cpp)
86+
87+
target_compile_definitions(countly-tests PRIVATE COUNTLY_BUILD_TESTS)
8288
if(COUNTLY_USE_SQLITE)
8389
target_compile_definitions(countly-tests PRIVATE COUNTLY_USE_SQLITE)
8490
target_include_directories(countly-tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/vendor/sqlite)
@@ -98,10 +104,7 @@ endif()
98104
if(COUNTLY_BUILD_SAMPLE)
99105
add_executable(countly-sample
100106
${CMAKE_CURRENT_SOURCE_DIR}/examples/example_integration.cpp)
101-
if(COUNTLY_USE_SQLITE)
102-
target_compile_definitions(countly-tests PRIVATE COUNTLY_USE_SQLITE)
103-
target_include_directories(countly-tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/vendor/sqlite)
104-
endif()
107+
105108
target_include_directories(countly-sample PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/vendor/doctest/doctest)
106109
target_include_directories(countly-sample PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/vendor/json/include)
107110
target_link_libraries(countly-sample countly)

include/countly.hpp

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define COUNTLY_HPP_
33

44
#include "countly/constants.hpp"
5+
#include "countly/countly_configuration.hpp"
56

67
#include <chrono>
78
#include <functional>
@@ -43,23 +44,16 @@ class Countly : public cly::CountlyDelegates {
4344

4445
void setLogger(void (*fun)(LogLevel level, const std::string &message));
4546

46-
47-
#ifdef COUNTLY_BUILD_TESTS
47+
#ifdef COUNTLY_BUILD_TESTS
4848
/*
4949
This function should not be used as it will be removed in a future release. It is
5050
currently added as a temporary workaround.
5151
*/
5252
inline std::function<void(LogLevel, const std::string &)> getLogger() { return logger->getLogger(); }
53-
#endif
54-
55-
struct HTTPResponse {
56-
bool success;
57-
nlohmann::json data;
58-
};
53+
#endif
5954

6055
void setSha256(cly::SHA256Function fun);
6156

62-
using HTTPClientFunction = std::function<HTTPResponse(bool, const std::string &, const std::string &)>;
6357
void setHTTPClient(HTTPClientFunction fun);
6458

6559
void setMetrics(const std::string &os, const std::string &os_version, const std::string &device, const std::string &resolution, const std::string &carrier, const std::string &app_version);
@@ -193,22 +187,31 @@ class Countly : public cly::CountlyDelegates {
193187
}
194188

195189
/* Provide 'updateInterval' in seconds. */
196-
inline void setAutomaticSessionUpdateInterval(unsigned short updateInterval) { _auto_session_update_interval = updateInterval; }
190+
inline void setAutomaticSessionUpdateInterval(unsigned short updateInterval) { configuration->sessionDuration = updateInterval; }
197191

198192
/**
199193
* Convert event queue into list.
200194
* Warning: This method is for debugging purposes, and it is going to be removed in the future.
201195
* You should not be using this method.
202196
* @return a vector object containing events.
203197
*/
198+
199+
#ifdef COUNTLY_BUILD_TESTS
204200
const std::vector<std::string> debugReturnStateOfEQ() {
201+
205202
#ifdef COUNTLY_USE_SQLITE
206203
return {};
207-
#endif
204+
#else
208205
std::vector<std::string> v(event_queue.begin(), event_queue.end());
209206
return v;
207+
#endif
210208
}
211209

210+
inline const CountlyConfiguration &getConfiguration() { return *configuration.get(); }
211+
212+
static void halt();
213+
#endif
214+
212215
private:
213216
void _deleteThread();
214217
void _sendIndependantLocationRequest();
@@ -236,14 +239,7 @@ class Countly : public cly::CountlyDelegates {
236239

237240
void updateLoop();
238241

239-
cly::SHA256Function sha256_function;
240-
HTTPClientFunction http_client_function;
241-
242-
std::string host;
243-
244-
int port = 0;
245242
bool use_https = false;
246-
bool always_use_post = false;
247243

248244
bool began_session = false;
249245
bool is_being_disposed = false;
@@ -252,10 +248,10 @@ class Countly : public cly::CountlyDelegates {
252248
std::chrono::system_clock::time_point last_sent_session_request;
253249

254250
nlohmann::json session_params;
255-
std::string salt;
256251

257252
std::unique_ptr<std::thread> thread;
258253
std::unique_ptr<cly::ViewsModule> views_module;
254+
std::shared_ptr<cly::CountlyConfiguration> configuration;
259255
std::shared_ptr<cly::LoggerModule> logger;
260256

261257
std::mutex mutex;
@@ -265,7 +261,6 @@ class Countly : public cly::CountlyDelegates {
265261
bool stop_thread = false;
266262
bool running = false;
267263
size_t wait_milliseconds = COUNTLY_KEEPALIVE_INTERVAL;
268-
unsigned short _auto_session_update_interval = 60; // value is in seconds;
269264

270265
size_t max_events = COUNTLY_MAX_EVENTS_DEFAULT;
271266
std::deque<std::string> request_queue;

include/countly/constants.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <random>
1010
#include <sstream>
1111
#include <string>
12+
#include "nlohmann/json.hpp"
1213

1314
#define COUNTLY_SDK_NAME "cpp-native-unknown"
1415
#define COUNTLY_SDK_VERSION "22.06.0"
@@ -17,6 +18,12 @@
1718
#define COUNTLY_MAX_EVENTS_DEFAULT 200
1819

1920
namespace cly {
21+
struct HTTPResponse {
22+
bool success;
23+
nlohmann::json data;
24+
};
25+
26+
using HTTPClientFunction = std::function<HTTPResponse(bool, const std::string &, const std::string &)>;
2027
using SHA256Function = std::function<std::string(const std::string &)>;
2128
namespace utils {
2229
const std::default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count());
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#ifndef COUNTLY_CONFIGURATION_HPP_
2+
#define COUNTLY_CONFIGURATION_HPP_
3+
#include "countly/constants.hpp"
4+
#include <string>
5+
6+
namespace cly {
7+
struct CountlyConfiguration {
8+
/**
9+
* URL of the Countly server to submit data to.
10+
* Mandatory field.
11+
*/
12+
std::string serverUrl;
13+
14+
/**
15+
* App key for the application being tracked.
16+
* Mandatory field.
17+
*/
18+
std::string appKey;
19+
20+
/**
21+
* Unique ID for the device the app is running on.
22+
*/
23+
std::string deviceId;
24+
25+
/**
26+
* Set to prevent parameter tampering.
27+
*/
28+
std::string salt;
29+
30+
/**
31+
* Sets the interval for the automatic update calls
32+
* min value 1 (1 second), max value 600 (10 minutes)
33+
*/
34+
unsigned int sessionDuration = 60;
35+
36+
/**
37+
* Set threshold value for the number of events that can be stored locally.
38+
*/
39+
unsigned int eventQueueThreshold = 100;
40+
41+
/**
42+
* Set limit for the number of requests that can be stored locally.
43+
*/
44+
unsigned int requestQueueThreshold = 1000;
45+
46+
/**
47+
* Set the maximum amount of breadcrumbs.
48+
*/
49+
unsigned int breadcrumbsThreshold = 100;
50+
51+
/**
52+
* Set to send all requests made to the Countly server using HTTP POST.
53+
*/
54+
bool forcePost = false;
55+
56+
unsigned int port = 443;
57+
58+
SHA256Function sha256_function = nullptr;
59+
60+
HTTPClientFunction http_client_function = nullptr;
61+
62+
nlohmann::json metrics;
63+
64+
CountlyConfiguration(std::string appKey, std::string serverUrl) {
65+
this->appKey = appKey;
66+
this->serverUrl = serverUrl;
67+
}
68+
};
69+
} // namespace cly
70+
#endif

0 commit comments

Comments
 (0)