Added data limits monitoring for daily, weekly and monthly
[platform/core/connectivity/stc-manager.git] / src / database / tables / table-counters.c
old mode 100755 (executable)
new mode 100644 (file)
index 070ef46..9aec545
 
 #define DELETE_COUNTER "DELETE FROM counters WHERE restriction_id=?"
 
-#define SELECT_COUNTER "SELECT sent_bytes, rcv_bytes FROM counters WHERE restriction_id=?"
+#define SELECT_RESTRICTION_ID "SELECT restriction_id FROM counters " \
+       " WHERE restriction_id = ?"
 
-#define INSERT_COUNTER "INSERT INTO counters (restriction_id, sent_bytes, rcv_bytes) " \
-       " VALUES (?, ?, ?)"
+#define SELECT_COUNTER "SELECT data_counter, warn_counter, monthly_counter, weekly_counter, daily_counter " \
+       " FROM counters WHERE restriction_id = ?"
 
-#define UPDATE_COUNTER "REPLACE INTO counters (restriction_id, sent_bytes, rcv_bytes) " \
-       " VALUES (?, ?, ?)"
+#define UPDATE_COUNTER "UPDATE counters " \
+       " SET data_counter = ?, warn_counter = ?, monthly_counter = ?, weekly_counter = ?, daily_counter = ? " \
+       " WHERE restriction_id = ?"
+
+#define INSERT_COUNTER "INSERT INTO counters " \
+       " (restriction_id, data_counter, warn_counter, monthly_counter, weekly_counter, daily_counter) " \
+       " VALUES (?, ?, ?, ?, ?, ?)"
+
+#define SELECT_TIMESTAMP "SELECT month_start_date, month_start_ts, week_start_ts, day_start_ts " \
+       " FROM counters WHERE restriction_id = ?"
+
+#define UPDATE_TIMESTAMP "UPDATE counters " \
+       " SET month_start_date = ?, month_start_ts = ?, week_start_ts = ?, day_start_ts = ? " \
+       " WHERE restriction_id = ?"
+
+#define INSERT_TIMESTAMP "INSERT INTO counters " \
+       " (restriction_id, month_start_date, month_start_ts, week_start_ts, day_start_ts) " \
+       " VALUES (?, ?, ?, ?, ?)"
 
 static void __finalize_delete(void);
 
@@ -73,6 +90,21 @@ static void __finalize_update(void);
        } \
 } while (0)
 
+static void __finalize_insert(void);
+
+#define PREPARE_INSERT(stm, query) do { \
+               rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
+               if (rc != SQLITE_OK) { \
+                       stm = NULL; \
+                       __finalize_insert(); \
+                       STC_LOGE("Failed to prepare \"%s\"query" \
+                                , query); \
+                       return rc; \
+               } \
+       } while (0)
+
+
+
 #define FINALIZE(stm) do { \
        if (stm) { \
                sqlite3_finalize(stm); \
@@ -81,8 +113,13 @@ static void __finalize_update(void);
 } while (0)
 
 static sqlite3_stmt *delete_counter;
+static sqlite3_stmt *select_restriction_id;
 static sqlite3_stmt *select_counter;
 static sqlite3_stmt *update_counter;
+static sqlite3_stmt *insert_counter;
+static sqlite3_stmt *select_timestamp;
+static sqlite3_stmt *update_timestamp;
+static sqlite3_stmt *insert_timestamp;
 
 static int __prepare_delete(sqlite3 *db)
 {
@@ -91,8 +128,8 @@ static int __prepare_delete(sqlite3 *db)
        static int initialized;
 
        if (initialized) {
-               __STC_LOG_FUNC_EXIT__;
-               return SQLITE_OK;
+               __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
+               return SQLITE_OK; //LCOV_EXCL_LINE
        }
 
        PREPARE_DELETE(delete_counter, DELETE_COUNTER);
@@ -118,11 +155,13 @@ static int __prepare_select(sqlite3 *db)
        static int initialized;
 
        if (initialized) {
-               __STC_LOG_FUNC_EXIT__;
-               return SQLITE_OK;
+               __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
+               return SQLITE_OK; //LCOV_EXCL_LINE
        }
 
        PREPARE_SELECT(select_counter, SELECT_COUNTER);
+       PREPARE_SELECT(select_timestamp, SELECT_TIMESTAMP);
+       PREPARE_SELECT(select_restriction_id, SELECT_RESTRICTION_ID);
 
        initialized = 1;
        __STC_LOG_FUNC_EXIT__;
@@ -134,6 +173,8 @@ static void __finalize_select(void)
        __STC_LOG_FUNC_ENTER__;
 
        FINALIZE(select_counter);
+       FINALIZE(select_timestamp);
+       FINALIZE(select_restriction_id);
 
        __STC_LOG_FUNC_EXIT__;
 }
@@ -145,11 +186,12 @@ static int __prepare_update(sqlite3 *db)
        static int initialized;
 
        if (initialized) {
-               __STC_LOG_FUNC_EXIT__;
-               return SQLITE_OK;
+               __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
+               return SQLITE_OK; //LCOV_EXCL_LINE
        }
 
        PREPARE_UPDATE(update_counter, UPDATE_COUNTER);
+       PREPARE_UPDATE(update_timestamp, UPDATE_TIMESTAMP);
 
        initialized = 1;
        __STC_LOG_FUNC_EXIT__;
@@ -161,10 +203,74 @@ static void __finalize_update(void)
        __STC_LOG_FUNC_ENTER__;
 
        FINALIZE(update_counter);
+       FINALIZE(update_timestamp);
 
        __STC_LOG_FUNC_EXIT__;
 }
 
+static int __prepare_insert(sqlite3 *db)
+{
+       __STC_LOG_FUNC_ENTER__;
+       int rc;
+       static int initialized;
+
+       if (initialized) {
+               __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
+               return SQLITE_OK; //LCOV_EXCL_LINE
+       }
+
+       PREPARE_INSERT(insert_counter, INSERT_COUNTER);
+       PREPARE_INSERT(insert_timestamp, INSERT_TIMESTAMP);
+
+       initialized = 1;
+       __STC_LOG_FUNC_EXIT__;
+       return rc;
+}
+
+static void __finalize_insert(void)
+{
+       __STC_LOG_FUNC_ENTER__;
+
+       FINALIZE(insert_counter);
+       FINALIZE(insert_timestamp);
+
+       __STC_LOG_FUNC_EXIT__;
+}
+
+static bool __table_counters_is_entry_present(uint64_t restriction_id)
+{
+       bool ret = FALSE;
+       int rc;
+       uint64_t l_restriction_id = -1;
+       sqlite3_stmt *stmt = select_restriction_id;
+
+       if (sqlite3_bind_int(stmt, 1, restriction_id) != SQLITE_OK) {
+               ret = FALSE;
+               __STC_LOG_FUNC_EXIT__;
+               goto handle_error;
+       }
+
+       rc = sqlite3_step(stmt);
+
+       switch (rc) {
+       case SQLITE_DONE:
+               break;
+       case SQLITE_ROW:
+               l_restriction_id = sqlite3_column_int64(stmt, 0);
+               STC_LOGD("restriction id [%llu]", l_restriction_id);
+               ret = TRUE;
+               break;
+       case SQLITE_ERROR:
+       default:
+               STC_LOGE("Failed to get restriction id : %s",
+                       sqlite3_errmsg(stc_db_get_database()));
+       }
+
+handle_error:
+       sqlite3_reset(stmt);
+       return ret;
+}
+
 stc_error_e table_counters_get(uint64_t restriction_id,
                               table_counters_info *info)
 {
@@ -175,21 +281,26 @@ stc_error_e table_counters_get(uint64_t restriction_id,
        if (info == NULL)
                goto handle_error;
 
+       info->restriction_id = restriction_id;
+
        DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
 
        do {
                rc = sqlite3_step(stmt);
 
+                //LCOV_EXCL_START
                switch (rc) {
                case SQLITE_DONE:
                        break;
                case SQLITE_ROW:
-                       info->sent_bytes = sqlite3_column_int64(stmt, 0);
-                       info->rcv_bytes = sqlite3_column_int64(stmt, 1);
-
-                       STC_LOGD("rstn_id [%llu] counters [sent = %llu rcv = %llu]",
-                                restriction_id, info->sent_bytes,
-                                info->rcv_bytes);
+                       info->data_counter = sqlite3_column_int64(stmt, 0);
+                       info->warn_counter = sqlite3_column_int64(stmt, 1);
+                       info->monthly_counter = sqlite3_column_int64(stmt, 2);
+                       info->weekly_counter = sqlite3_column_int64(stmt, 3);
+                       info->daily_counter = sqlite3_column_int64(stmt, 4);
+
+                       STC_LOGD("rstn_id [%llu] data_counters [%lld] bytes",
+                                restriction_id, info->data_counter);
                        break;
                case SQLITE_ERROR:
                default:
@@ -197,7 +308,9 @@ stc_error_e table_counters_get(uint64_t restriction_id,
                                 sqlite3_errmsg(stc_db_get_database()));
 
                        error_code = STC_ERROR_DB_FAILED;
+                       __STC_LOG_FUNC_EXIT__;
                }
+                //LCOV_EXCL_STOP
        } while (rc == SQLITE_ROW);
 
 handle_error:
@@ -205,19 +318,26 @@ handle_error:
        return error_code;
 }
 
+//LCOV_EXCL_START
 stc_error_e table_counters_update_counters(const table_counters_info *info)
 {
        stc_error_e error_code = STC_ERROR_NONE;
        sqlite3_stmt *stmt = update_counter;
 
-       if (!info->rcv_bytes && !info->sent_bytes) {
+       if (!info->data_counter) {
                error_code = STC_ERROR_INVALID_PARAMETER;
                goto handle_error;
        }
 
+       if (__table_counters_is_entry_present(info->restriction_id) == FALSE)
+               stmt = insert_counter;
+
        DB_ACTION(sqlite3_bind_int64(stmt, 1, info->restriction_id));
-       DB_ACTION(sqlite3_bind_int64(stmt, 2, info->sent_bytes));
-       DB_ACTION(sqlite3_bind_int64(stmt, 3, info->rcv_bytes));
+       DB_ACTION(sqlite3_bind_int64(stmt, 2, info->data_counter));
+       DB_ACTION(sqlite3_bind_int64(stmt, 3, info->warn_counter));
+       DB_ACTION(sqlite3_bind_int64(stmt, 4, info->monthly_counter));
+       DB_ACTION(sqlite3_bind_int64(stmt, 5, info->weekly_counter));
+       DB_ACTION(sqlite3_bind_int64(stmt, 6, info->daily_counter));
 
        if (sqlite3_step(stmt) != SQLITE_DONE) {
                STC_LOGE("Failed to update counter: %s\n",
@@ -234,21 +354,106 @@ handle_error:
        sqlite3_reset(stmt);
        return error_code;
 }
+//LCOV_EXCL_STOP
 
-stc_error_e table_counters_delete(uint64_t restriction_id)
+stc_error_e table_counters_get_timestamps(uint64_t restriction_id,
+                                                                               table_counters_info *info)
 {
        stc_error_e error_code = STC_ERROR_NONE;
-       sqlite3_stmt *stmt = delete_counter;
+       sqlite3_stmt *stmt = select_timestamp;
+       int rc;
+
+       if (info == NULL) {
+               __STC_LOG_FUNC_EXIT__;
+               goto handle_error;
+       }
+
+       info->restriction_id = restriction_id;
 
        DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
 
+       do {
+               rc = sqlite3_step(stmt);
+
+                //LCOV_EXCL_START
+               switch (rc) {
+               case SQLITE_DONE:
+                       break;
+               case SQLITE_ROW:
+                       info->month_start_date = sqlite3_column_int(stmt, 0);
+                       info->month_start_ts = sqlite3_column_int64(stmt, 1);
+                       info->week_start_ts = sqlite3_column_int64(stmt, 2);
+                       info->day_start_ts = sqlite3_column_int64(stmt, 3);
+
+                       STC_LOGD("rstn_id [%llu] month_start_date [%d], "
+                               "month_start_ts [%lld], week_start_ts [%lld], "
+                               "day_start_ts [%lld]", restriction_id,
+                               info->month_start_date, info->month_start_ts,
+                               info->week_start_ts, info->day_start_ts);
+                       break;
+               case SQLITE_ERROR:
+               default:
+                       STC_LOGE("Failed to enumerate counters: %s\n",
+                                sqlite3_errmsg(stc_db_get_database()));
+
+                       error_code = STC_ERROR_DB_FAILED;
+                       __STC_LOG_FUNC_EXIT__;
+               }
+                //LCOV_EXCL_STOP
+       } while (rc == SQLITE_ROW);
+
+handle_error:
+       sqlite3_reset(stmt);
+       return error_code;
+}
+
+//LCOV_EXCL_START
+stc_error_e table_counters_update_timestamps(const table_counters_info *info)
+{
+       stc_error_e error_code = STC_ERROR_NONE;
+       sqlite3_stmt *stmt = update_timestamp;
+
+       if (__table_counters_is_entry_present(info->restriction_id) == FALSE)
+               stmt = insert_timestamp;
+
+       DB_ACTION(sqlite3_bind_int64(stmt, 1, info->restriction_id));
+       DB_ACTION(sqlite3_bind_int(stmt, 2, info->month_start_date));
+       DB_ACTION(sqlite3_bind_int64(stmt, 3, info->month_start_ts));
+       DB_ACTION(sqlite3_bind_int64(stmt, 4, info->week_start_ts));
+       DB_ACTION(sqlite3_bind_int64(stmt, 5, info->day_start_ts));
+
        if (sqlite3_step(stmt) != SQLITE_DONE) {
-               STC_LOGE("Failed to delete counter: %s\n",
+               STC_LOGE("Failed to update timestamps: %s\n",
                         sqlite3_errmsg(stc_db_get_database()));
                error_code = STC_ERROR_DB_FAILED;
+               __STC_LOG_FUNC_EXIT__;
                goto handle_error;
        }
 
+       STC_LOGD("Timestamps updated for restriction_id [%llu]",
+                info->restriction_id);
+
+handle_error:
+       sqlite3_reset(stmt);
+       return error_code;
+}
+//LCOV_EXCL_STOP
+
+
+stc_error_e table_counters_delete(uint64_t restriction_id)
+{
+       stc_error_e error_code = STC_ERROR_NONE;
+       sqlite3_stmt *stmt = delete_counter;
+
+       DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id));
+
+       if (sqlite3_step(stmt) != SQLITE_DONE) {
+               STC_LOGE("Failed to delete counter: %s\n", //LCOV_EXCL_LINE
+                        sqlite3_errmsg(stc_db_get_database()));
+               error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
+               goto handle_error; //LCOV_EXCL_LINE
+       }
+
        STC_LOGD("Counter deleted for restriction_id [%llu]", restriction_id);
 
 handle_error:
@@ -263,13 +468,14 @@ stc_error_e table_counters_prepare(sqlite3 *db)
        stc_error_e error_code = STC_ERROR_NONE;
 
        if (db == NULL) {
-               __STC_LOG_FUNC_EXIT__;
-               return STC_ERROR_FAIL;
+               __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
+               return STC_ERROR_FAIL; //LCOV_EXCL_LINE
        }
 
        DB_ACTION(__prepare_delete(db));
        DB_ACTION(__prepare_select(db));
        DB_ACTION(__prepare_update(db));
+       DB_ACTION(__prepare_insert(db));
 
 handle_error:
        __STC_LOG_FUNC_EXIT__;
@@ -282,5 +488,6 @@ void table_counters_finalize(void)
        __finalize_delete();
        __finalize_select();
        __finalize_update();
+       __finalize_insert();
        __STC_LOG_FUNC_EXIT__;
 }