Skip to content

Commit c108da5

Browse files
Merge pull request #28 from Countly/sdk-652
[SDK 652] Duration and event bug fixed while updating session.
2 parents f908361 + d9335a0 commit c108da5

File tree

4 files changed

+43
-36
lines changed

4 files changed

+43
-36
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
21.11.1
2+
* ! Minor breaking change ! Default automatic session update duration changed to 60 seconds.
3+
* Added a call to change the automatic session update duration.
4+
* Fixed a bug where session duration was reported wrong.
5+
16
21.11.0
27
* Fixed session duration issue.
38
* Added functionality to report event duration manually.

examples/example_integration.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ int main() {
9393
}
9494
}
9595

96-
ct.updateSession();
97-
ct.stop();
96+
//ct.updateSession();
97+
ct.endSession();
9898

9999
return 0;
100100
}

include/countly.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ class Countly {
207207

208208
addEvent(event);
209209
}
210+
211+
/* Provide 'updateInterval' in seconds. */
212+
inline void setAutomaticSessionUpdateInterval(unsigned short updateInterval) {
213+
_auto_session_update_interval = updateInterval;
214+
}
210215
private:
211216
void log(LogLevel level, const std::string& message);
212217

@@ -225,7 +230,7 @@ class Countly {
225230
int port;
226231
bool use_https;
227232
bool always_use_post;
228-
std::chrono::system_clock::time_point last_sent;
233+
std::chrono::system_clock::time_point last_sent_session_request;
229234
bool began_session;
230235

231236
json session_params;
@@ -236,6 +241,7 @@ class Countly {
236241
bool stop_thread;
237242
bool running;
238243
size_t wait_milliseconds;
244+
unsigned short _auto_session_update_interval = 60; // value is in seconds;
239245

240246
size_t max_events;
241247
#ifndef COUNTLY_USE_SQLITE

src/countly.cpp

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -202,19 +202,21 @@ void Countly::start(const std::string& app_key, const std::string& host, int por
202202
}
203203

204204
if (!running) {
205+
206+
mutex.unlock();
207+
beginSession();
208+
mutex.lock();
209+
205210
if (start_thread) {
206211
stop_thread = false;
212+
207213
try {
208214
thread = new std::thread(&Countly::updateLoop, this);
209215
} catch(const std::system_error& e) {
210216
std::ostringstream log_message;
211217
log_message << "Could not create thread: " << e.what();
212218
log(Countly::LogLevel::FATAL, log_message.str());
213219
}
214-
} else {
215-
mutex.unlock();
216-
beginSession();
217-
mutex.lock();
218220
}
219221
}
220222
mutex.unlock();
@@ -400,7 +402,7 @@ bool Countly::beginSession() {
400402
}
401403

402404
if (sendHTTP("/i", Countly::serializeForm(data)).success) {
403-
last_sent = Countly::getTimestamp();
405+
last_sent_session_request = Countly::getTimestamp();
404406
began_session = true;
405407
}
406408

@@ -415,8 +417,6 @@ bool Countly::beginSession() {
415417
}
416418

417419
bool Countly::updateSession() {
418-
log(Countly::LogLevel::INFO, "[Countly][updateSession]");
419-
420420
mutex.lock();
421421
if (!began_session) {
422422
mutex.unlock();
@@ -484,39 +484,35 @@ bool Countly::updateSession() {
484484
auto duration = std::chrono::duration_cast<std::chrono::seconds>(getSessionDuration());
485485
mutex.lock();
486486

487-
if (no_events) {
488-
if (duration.count() > COUNTLY_KEEPALIVE_INTERVAL) {
489-
std::map<std::string, std::string> data = {
490-
{"app_key", session_params["app_key"].get<std::string>()},
491-
{"device_id", session_params["device_id"].get<std::string>()},
492-
{"session_duration", std::to_string(duration.count())}
493-
};
494-
if (!sendHTTP("/i", Countly::serializeForm(data)).success) {
495-
mutex.unlock();
496-
return false;
497-
}
498-
499-
last_sent += duration;
487+
if (duration.count() >= _auto_session_update_interval) {
488+
log(Countly::LogLevel::DEBUG, "[Countly][updateSession] sending session update.");
489+
std::map<std::string, std::string> data = {
490+
{"app_key", session_params["app_key"].get<std::string>()},
491+
{"device_id", session_params["device_id"].get<std::string>()},
492+
{"session_duration", std::to_string(duration.count())}
493+
};
494+
if (!sendHTTP("/i", Countly::serializeForm(data)).success) {
495+
mutex.unlock();
496+
return false;
500497
}
501498

502-
mutex.unlock();
503-
return true;
499+
last_sent_session_request += duration;
504500
}
505501

506-
std::map<std::string, std::string> data = {
502+
if (!no_events) {
503+
log(Countly::LogLevel::DEBUG, "[Countly][updateSession] sending event.");
504+
std::map<std::string, std::string> data = {
507505
{"app_key", session_params["app_key"].get<std::string>()},
508506
{"device_id", session_params["device_id"].get<std::string>()},
509-
{"session_duration", std::to_string(duration.count())},
510507
{"events", events.dump()}
511-
};
508+
};
512509

513-
if (!sendHTTP("/i", Countly::serializeForm(data)).success) {
514-
mutex.unlock();
515-
return false;
510+
if (!sendHTTP("/i", Countly::serializeForm(data)).success) {
511+
mutex.unlock();
512+
return false;
513+
}
516514
}
517515

518-
last_sent += duration;
519-
520516
#ifndef COUNTLY_USE_SQLITE
521517
event_queue.clear();
522518
#else
@@ -554,7 +550,7 @@ bool Countly::endSession() {
554550
{"end_session", "1"}
555551
};
556552
if (sendHTTP("/i", Countly::serializeForm(data)).success) {
557-
last_sent = now;
553+
last_sent_session_request = now;
558554
began_session = false;
559555
mutex.unlock();
560556
return true;
@@ -804,7 +800,7 @@ log(Countly::LogLevel::DEBUG, "[Countly][sendHTTP] data: "+ data);
804800

805801
std::chrono::system_clock::duration Countly::getSessionDuration(std::chrono::system_clock::time_point now) {
806802
mutex.lock();
807-
std::chrono::system_clock::duration duration = now - last_sent;
803+
std::chrono::system_clock::duration duration = now - last_sent_session_request;
808804
mutex.unlock();
809805
return duration;
810806
}
@@ -827,8 +823,8 @@ void Countly::updateLoop() {
827823
}
828824
size_t last_wait_milliseconds = wait_milliseconds;
829825
mutex.unlock();
830-
updateSession();
831826
std::this_thread::sleep_for(std::chrono::milliseconds(last_wait_milliseconds));
827+
updateSession();
832828
}
833829
mutex.lock();
834830
running = false;

0 commit comments

Comments
 (0)