Skip to content

Commit cbec460

Browse files
authored
Added checkEQsize and clearEQ (#118)
* checkEQsize and clearEQ * moved * mutex
1 parent 42b8ef8 commit cbec460

File tree

2 files changed

+110
-76
lines changed

2 files changed

+110
-76
lines changed

include/countly.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ class Countly : public cly::CountlyDelegates {
100100

101101
void addEvent(const cly::Event &event);
102102

103+
/*
104+
* Checks and returns the size of the event queue in persistent storage.
105+
*/
106+
int checkEQSize();
107+
108+
/*
109+
* Erases/clears the event queue in persistent storage.
110+
*/
111+
void clearEQ();
112+
103113
void addEventToSqlite(const cly::Event &event);
104114

105115
void setMaxEvents(size_t value);

src/countly.cpp

Lines changed: 100 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -481,45 +481,15 @@ void Countly::addEvent(const cly::Event &event) {
481481
event_queue.push_back(event.serialize());
482482
#else
483483
addEventToSqlite(event);
484-
#endif
485484
mutex->unlock();
486-
}
487-
488-
#ifdef COUNTLY_USE_SQLITE
489-
void Countly::addEventToSqlite(const cly::Event &event) {
490-
log(LogLevel::DEBUG, "[Countly][addEventToSqlite]");
491-
try {
492-
if (database_path.empty()) {
493-
mutex->unlock();
494-
log(LogLevel::FATAL, "Cannot add event, sqlite database path is not set");
495-
return;
496-
}
497-
498-
sqlite3 *database;
499-
int return_value;
500-
char *error_message;
501-
502-
return_value = sqlite3_open(database_path.c_str(), &database);
503-
if (return_value == SQLITE_OK) {
504-
std::ostringstream sql_statement_stream;
505-
// TODO Investigate if we need to escape single quotes in serialized event
506-
sql_statement_stream << "INSERT INTO events (event) VALUES('" << event.serialize() << "');";
507-
std::string sql_statement = sql_statement_stream.str();
508-
509-
return_value = sqlite3_exec(database, sql_statement.c_str(), nullptr, nullptr, &error_message);
510-
if (return_value != SQLITE_OK) {
511-
log(LogLevel::ERROR, error_message);
512-
sqlite3_free(error_message);
513-
}
514-
}
515-
sqlite3_close(database);
516-
} catch (const std::system_error &e) {
517-
std::ostringstream log_message;
518-
log_message << "addEventToSqlite, error: " << e.what();
519-
log(LogLevel::FATAL, log_message.str());
485+
int queueSize = checkEQSize();
486+
mutex->lock();
487+
if (queueSize >= configuration->eventQueueThreshold) {
488+
log(LogLevel::DEBUG, "Event queue threshold is reached");
520489
}
521-
}
522490
#endif
491+
mutex->unlock();
492+
}
523493

524494
void Countly::setMaxEvents(size_t value) {
525495
if (is_sdk_initialized) {
@@ -539,6 +509,8 @@ void Countly::setMaxEvents(size_t value) {
539509
}
540510

541511
void Countly::flushEvents(std::chrono::seconds timeout) {
512+
log(LogLevel::DEBUG, "[Countly][flushEvents] timeout: " + std::to_string(timeout.count()) + " seconds");
513+
542514
try {
543515
auto wait_duration = std::chrono::seconds(1);
544516
bool update_failed;
@@ -553,35 +525,11 @@ void Countly::flushEvents(std::chrono::seconds timeout) {
553525

554526
update_failed = !updateSession();
555527
#else
556-
mutex->lock();
557-
if (database_path.empty()) {
558-
mutex->unlock();
559-
log(LogLevel::FATAL, "Cannot flush events, sqlite database path is not set");
560-
return;
561-
}
562-
563-
sqlite3 *database;
564-
int return_value, row_count, column_count;
565-
char **table;
566-
char *error_message;
567-
568528
update_failed = true;
569-
return_value = sqlite3_open(database_path.c_str(), &database);
570-
mutex->unlock();
571-
if (return_value == SQLITE_OK) {
572-
return_value = sqlite3_get_table(database, "SELECT COUNT(*) FROM events;", &table, &row_count, &column_count, &error_message);
573-
if (return_value == SQLITE_OK) {
574-
int event_count = atoi(table[1]);
575-
if (event_count > 0) {
576-
update_failed = !updateSession();
577-
}
578-
} else {
579-
log(LogLevel::ERROR, error_message);
580-
sqlite3_free(error_message);
581-
}
582-
sqlite3_free_table(table);
529+
int event_count = checkEQSize();
530+
if (event_count > 0) {
531+
update_failed = !updateSession();
583532
}
584-
sqlite3_close(database);
585533
#endif
586534
if (update_failed) {
587535
std::this_thread::sleep_for(wait_duration);
@@ -593,20 +541,8 @@ void Countly::flushEvents(std::chrono::seconds timeout) {
593541
#ifndef COUNTLY_USE_SQLITE
594542
event_queue.clear();
595543
#else
596-
sqlite3 *database;
597-
int return_value;
598-
char *error_message;
599-
600544
update_failed = true;
601-
return_value = sqlite3_open(database_path.c_str(), &database);
602-
if (return_value == SQLITE_OK) {
603-
return_value = sqlite3_exec(database, "DELETE FROM events;", nullptr, nullptr, &error_message);
604-
if (return_value != SQLITE_OK) {
605-
log(LogLevel::FATAL, error_message);
606-
sqlite3_free(error_message);
607-
}
608-
}
609-
sqlite3_close(database);
545+
clearEQ();
610546
#endif
611547
} catch (const std::system_error &e) {
612548
std::ostringstream log_message;
@@ -859,7 +795,95 @@ bool Countly::endSession() {
859795

860796
std::chrono::system_clock::time_point Countly::getTimestamp() { return std::chrono::system_clock::now(); }
861797

798+
// Standalone Sqlite functions
862799
#ifdef COUNTLY_USE_SQLITE
800+
int Countly::checkEQSize() {
801+
log(LogLevel::DEBUG, "[Countly][checkEQSize]");
802+
int event_count = -1;
803+
mutex->lock();
804+
if (database_path.empty()) {
805+
mutex->unlock();
806+
log(LogLevel::FATAL, "[Countly][checkEQSize] Sqlite database path is not set");
807+
return event_count;
808+
}
809+
810+
sqlite3 *database;
811+
int return_value = sqlite3_open(database_path.c_str(), &database);
812+
mutex->unlock();
813+
814+
if (return_value == SQLITE_OK) {
815+
char *error_message;
816+
int row_count, column_count;
817+
char **table;
818+
return_value = sqlite3_get_table(database, "SELECT COUNT(*) FROM events;", &table, &row_count, &column_count, &error_message);
819+
if (return_value == SQLITE_OK) {
820+
event_count = atoi(table[1]);
821+
log(LogLevel::DEBUG, "[Countly][checkEQSize] Fetched event count from database: " + std::to_string(event_count));
822+
} else {
823+
log(LogLevel::ERROR, error_message);
824+
sqlite3_free(error_message);
825+
}
826+
sqlite3_free_table(table);
827+
} else {
828+
log(LogLevel::WARNING, "[Countly][checkEQSize] Could not open database");
829+
}
830+
831+
sqlite3_close(database);
832+
return event_count;
833+
}
834+
835+
void Countly::addEventToSqlite(const cly::Event &event) {
836+
log(LogLevel::DEBUG, "[Countly][addEventToSqlite]");
837+
try {
838+
if (database_path.empty()) {
839+
log(LogLevel::FATAL, "Cannot add event, sqlite database path is not set");
840+
return;
841+
}
842+
843+
sqlite3 *database;
844+
int return_value;
845+
char *error_message;
846+
847+
return_value = sqlite3_open(database_path.c_str(), &database);
848+
if (return_value == SQLITE_OK) {
849+
std::ostringstream sql_statement_stream;
850+
// TODO Investigate if we need to escape single quotes in serialized event
851+
sql_statement_stream << "INSERT INTO events (event) VALUES('" << event.serialize() << "');";
852+
std::string sql_statement = sql_statement_stream.str();
853+
854+
return_value = sqlite3_exec(database, sql_statement.c_str(), nullptr, nullptr, &error_message);
855+
if (return_value != SQLITE_OK) {
856+
log(LogLevel::ERROR, error_message);
857+
sqlite3_free(error_message);
858+
}
859+
}
860+
sqlite3_close(database);
861+
} catch (const std::system_error &e) {
862+
std::ostringstream log_message;
863+
log_message << "addEventToSqlite, error: " << e.what();
864+
log(LogLevel::FATAL, log_message.str());
865+
}
866+
}
867+
868+
void Countly::clearEQ() {
869+
log(LogLevel::DEBUG, "[Countly][clearEQ]");
870+
sqlite3 *database;
871+
int return_value;
872+
char *error_message;
873+
874+
return_value = sqlite3_open(database_path.c_str(), &database);
875+
if (return_value == SQLITE_OK) {
876+
return_value = sqlite3_exec(database, "DELETE FROM events;", nullptr, nullptr, &error_message);
877+
if (return_value != SQLITE_OK) {
878+
log(LogLevel::FATAL, error_message);
879+
sqlite3_free(error_message);
880+
} else {
881+
log(LogLevel::DEBUG, "[Countly][clearEQ] Cleared event queue");
882+
}
883+
}
884+
sqlite3_close(database);
885+
}
886+
863887
void Countly::setDatabasePath(const std::string &path) {
864888
if (is_sdk_initialized) {
865889
log(LogLevel::ERROR, "[Countly][setDatabasePath] You can not set the database path after SDK initialization.");

0 commit comments

Comments
 (0)