Skip to content

Commit ff37b49

Browse files
authored
Merge pull request #138 from Countly/2321
feat: 23.2.1
2 parents 7a3ee0e + 7add130 commit ff37b49

File tree

4 files changed

+62
-25
lines changed

4 files changed

+62
-25
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
## XX.XX.XX
2-
- Added manual session control, which can be enabled via "Countly::enableManualSessionControl".
1+
## 23.2.1
2+
- Added manual session control via "Countly::enableManualSessionControl". When enabled, automatic session calls are ignored, while manual calls remain usable for finer control.
33
- Added "checkRQSize" function to return the current number of requests in the queue.
44

55
## 23.2.0

include/countly/constants.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <string>
1414

1515
#define COUNTLY_SDK_NAME "cpp-native-unknown"
16-
#define COUNTLY_SDK_VERSION "23.2.0"
16+
#define COUNTLY_SDK_VERSION "23.2.1"
1717
#define COUNTLY_POST_THRESHOLD 2000
1818
#define COUNTLY_KEEPALIVE_INTERVAL 3000
1919
#define COUNTLY_MAX_EVENTS_DEFAULT 200

src/countly.cpp

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ void Countly::_changeDeviceIdWithoutMerge(const std::string &value) {
346346

347347
// send all event to server and end current session of old user
348348
flushEvents();
349-
if(!configuration->manualSessionControl){
349+
if(configuration->manualSessionControl == false){
350350
endSession();
351351
}
352352

@@ -355,7 +355,7 @@ void Countly::_changeDeviceIdWithoutMerge(const std::string &value) {
355355
mutex->unlock();
356356

357357
// start a new session for new user
358-
if(!configuration->manualSessionControl){
358+
if(configuration->manualSessionControl == false){
359359
beginSession();
360360
}
361361

@@ -403,7 +403,7 @@ void Countly::start(const std::string &app_key, const std::string &host, int por
403403
log(LogLevel::INFO, "[Countly][start] '_WIN32' is not defined");
404404
#endif
405405

406-
enable_automatic_session = start_thread && !configuration->manualSessionControl;
406+
enable_automatic_session = start_thread;
407407
start_thread = true;
408408

409409
if (port < 0 || port > 65535) {
@@ -438,7 +438,7 @@ void Countly::start(const std::string &app_key, const std::string &host, int por
438438

439439
if (!running) {
440440

441-
if(!configuration->manualSessionControl){
441+
if(configuration->manualSessionControl == false){
442442
mutex->unlock();
443443
beginSession();
444444
mutex->lock();
@@ -469,7 +469,7 @@ void Countly::startOnCloud(const std::string &app_key) {
469469

470470
void Countly::stop() {
471471
_deleteThread();
472-
if (!configuration->manualSessionControl) {
472+
if (configuration->manualSessionControl == false) {
473473
endSession();
474474
}
475475
}
@@ -611,16 +611,12 @@ bool Countly::attemptSessionUpdateEQ() {
611611
return false;
612612
}
613613
#endif
614-
bool result;
615-
if(!configuration->manualSessionControl){
616-
result = !updateSession();
614+
if(configuration->manualSessionControl == false){
615+
return !updateSession();
617616
} else {
618-
log(LogLevel::WARNING, "[Countly][attemptSessionUpdateEQ] SDK is in manual session control mode. Please start a session first.");
619-
result = false;
617+
packEvents();
618+
return false;
620619
}
621-
622-
packEvents();
623-
return result;
624620
}
625621

626622
void Countly::clearEQInternal() {
@@ -680,7 +676,7 @@ std::vector<std::string> Countly::debugReturnStateOfEQ() {
680676
bool Countly::beginSession() {
681677
mutex->lock();
682678
log(LogLevel::INFO, "[Countly][beginSession]");
683-
if (began_session) {
679+
if (began_session == true) {
684680
mutex->unlock();
685681
log(LogLevel::DEBUG, "[Countly][beginSession] Session is already active.");
686682
return true;
@@ -736,9 +732,9 @@ bool Countly::updateSession() {
736732
try {
737733
// Check if there was a session, if not try to start one
738734
mutex->lock();
739-
if (!began_session) {
735+
if (began_session == false) {
740736
mutex->unlock();
741-
if(configuration->manualSessionControl){
737+
if(configuration->manualSessionControl == true){
742738
log(LogLevel::WARNING, "[Countly][updateSession] SDK is in manual session control mode and there is no active session. Please start a session first.");
743739
return false;
744740
}
@@ -749,7 +745,28 @@ bool Countly::updateSession() {
749745
mutex->lock();
750746
began_session = true;
751747
}
752-
748+
749+
// events array
750+
nlohmann::json events = nlohmann::json::array();
751+
std::string event_ids;
752+
mutex->unlock();
753+
bool no_events = checkEQSize() > 0 ? false : true;
754+
mutex->lock();
755+
756+
if (!no_events) {
757+
#ifndef COUNTLY_USE_SQLITE
758+
for (const auto &event_json : event_queue) {
759+
events.push_back(nlohmann::json::parse(event_json));
760+
}
761+
#else
762+
// TODO: If database_path was empty there was return false here
763+
mutex->unlock();
764+
fillEventsIntoJson(events, event_ids);
765+
mutex->lock();
766+
#endif
767+
} else {
768+
log(LogLevel::DEBUG, "[Countly][updateSession] EQ empty.");
769+
}
753770
mutex->unlock();
754771
auto duration = std::chrono::duration_cast<std::chrono::seconds>(getSessionDuration());
755772
mutex->lock();
@@ -762,6 +779,22 @@ bool Countly::updateSession() {
762779

763780
last_sent_session_request += duration;
764781
}
782+
783+
// report events if there are any to request queue
784+
if (!no_events) {
785+
sendEventsToRQ(events);
786+
}
787+
788+
// clear event queue
789+
// TODO: check if we want to totally wipe the event queue in memory but not in database
790+
#ifndef COUNTLY_USE_SQLITE
791+
event_queue.clear();
792+
#else
793+
if (!event_ids.empty()) {
794+
// this is a partial clearance, we only remove the events that were sent
795+
removeEventWithId(event_ids);
796+
}
797+
#endif
765798
} catch (const std::system_error &e) {
766799
std::ostringstream log_message;
767800
log_message << "update session, error: " << e.what();
@@ -792,7 +825,7 @@ void Countly::packEvents() {
792825
mutex->lock();
793826
#endif
794827
} else {
795-
log(LogLevel::DEBUG, "[Countly][updateSession] EQ empty.");
828+
log(LogLevel::DEBUG, "[Countly][packEvents] EQ empty.");
796829
}
797830
// report events if there are any to request queue
798831
if (!no_events) {
@@ -811,7 +844,7 @@ void Countly::packEvents() {
811844
#endif
812845
} catch (const std::system_error &e) {
813846
std::ostringstream log_message;
814-
log_message << "update session, error: " << e.what();
847+
log_message << "packEvents, error: " << e.what();
815848
log(LogLevel::FATAL, log_message.str());
816849
}
817850
mutex->unlock();
@@ -826,7 +859,7 @@ void Countly::sendEventsToRQ(const nlohmann::json &events) {
826859

827860
bool Countly::endSession() {
828861
log(LogLevel::INFO, "[Countly][endSession]");
829-
if(!began_session) {
862+
if(began_session == false) {
830863
log(LogLevel::DEBUG, "[Countly][endSession] There is no active session to end.");
831864
return true;
832865
}
@@ -1167,10 +1200,11 @@ void Countly::updateLoop() {
11671200
size_t last_wait_milliseconds = wait_milliseconds;
11681201
mutex->unlock();
11691202
std::this_thread::sleep_for(std::chrono::milliseconds(last_wait_milliseconds));
1170-
if (enable_automatic_session) {
1203+
if (enable_automatic_session == true && configuration->manualSessionControl == false) {
11711204
updateSession();
1205+
} else if (configuration->manualSessionControl == true) {
1206+
packEvents();
11721207
}
1173-
packEvents();
11741208
requestModule->processQueue(mutex);
11751209
}
11761210
mutex->lock();

tests/config.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ TEST_CASE("Validate setting configuration values") {
5151
CHECK(config.breadcrumbsThreshold == 100);
5252
CHECK(config.forcePost == false);
5353
CHECK(config.port == 443);
54+
CHECK(config.manualSessionControl == false);
5455
CHECK(config.sha256_function == nullptr);
5556
CHECK(config.http_client_function == nullptr);
5657
CHECK(config.metrics.empty());
@@ -76,6 +77,7 @@ TEST_CASE("Validate setting configuration values") {
7677
ct.setMaxRequestQueueSize(10);
7778
ct.SetPath(TEST_DATABASE_NAME);
7879
ct.setMaxRQProcessingBatchSize(10);
80+
ct.enableManualSessionControl();
7981
ct.start("YOUR_APP_KEY", "https://try.count.ly", -1, false);
8082

8183
// Get configuration values using Countly getters
@@ -94,6 +96,7 @@ TEST_CASE("Validate setting configuration values") {
9496
CHECK(config.breadcrumbsThreshold == 100);
9597
CHECK(config.forcePost == true);
9698
CHECK(config.port == 443);
99+
CHECK(config.manualSessionControl == true);
97100
CHECK(config.sha256_function("custom SHA256") == customSha_1_returnValue);
98101

99102
HTTPResponse response = config.http_client_function(true, "", "");

0 commit comments

Comments
 (0)