Skip to content

Commit 92f795e

Browse files
revert: some changes
1 parent 4434b54 commit 92f795e

File tree

1 file changed

+47
-13
lines changed

1 file changed

+47
-13
lines changed

src/countly.cpp

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {
@@ -611,16 +611,12 @@ bool Countly::attemptSessionUpdateEQ() {
611611
return false;
612612
}
613613
#endif
614-
bool result;
615614
if(!configuration->manualSessionControl){
616-
result = !updateSession();
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() {
@@ -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();
@@ -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 && !configuration->manualSessionControl) {
11711204
updateSession();
1205+
} else {
1206+
packEvents();
11721207
}
1173-
packEvents();
11741208
requestModule->processQueue(mutex);
11751209
}
11761210
mutex->lock();

0 commit comments

Comments
 (0)