Add support of app_id in GetAll method call on statistics interface
[platform/core/connectivity/stc-manager.git] / src / database / tables / table-statistics.c
old mode 100644 (file)
new mode 100755 (executable)
index 6355f60..7d13a3f
@@ -327,8 +327,6 @@ static void __finalize_insert(void)
 
 static int __is_iftype_defined(const stc_iface_type_e iftype)
 {
-       __STC_LOG_FUNC_ENTER__;
-       __STC_LOG_FUNC_EXIT__;
        return iftype < STC_IFACE_LAST_ELEM &&
                iftype > STC_IFACE_UNKNOWN &&
                iftype != STC_IFACE_ALL;
@@ -352,11 +350,9 @@ static sqlite3_stmt **details_stms[] = {
 static sqlite3_stmt *__select_statement(const char *app_id,
                                        const table_statistics_select_rule *rule)
 {
-       __STC_LOG_FUNC_ENTER__;
        const int stm_index = __is_iftype_defined(rule->iftype) |
                ((strlen(app_id) > 0) ? 0 : 2) | (rule->granularity ? 4 : 0);
        STC_LOGD("stm index %d", stm_index);
-       __STC_LOG_FUNC_EXIT__;
        return *details_stms[stm_index];
 }
 
@@ -379,7 +375,9 @@ stc_error_e table_statistics_reset_first_n_entries(int num)
                __STC_LOG_FUNC_EXIT__;
        }
 handle_error:
-       sqlite3_reset(delete_query[4]);
+       if (sqlite3_reset(delete_query[4]) != SQLITE_OK)
+               error_code = STC_ERROR_DB_FAILED;
+
        return error_code;
 }
 //LCOV_EXCL_STOP
@@ -423,7 +421,9 @@ stc_error_e table_statistics_reset(const table_statistics_reset_rule *rule)
 
        STC_LOGD("Entry deleted successfully.");
 handle_error:
-       sqlite3_reset(stmt);
+       if (sqlite3_reset(stmt) != SQLITE_OK)
+               error_code = STC_ERROR_DB_FAILED;
+
        return error_code;
 }
 
@@ -440,13 +440,19 @@ stc_error_e table_statistics_foreach_app(const table_statistics_select_rule *rul
                       bind parameters in the query */
        stc_db_tm_interval_s interval;
 
-       memset(&data, 0, sizeof(data));
-
        if (!rule || !info_cb) {
                __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
                return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
        }
 
+       memset(&data, 0, sizeof(data));
+
+       if (rule->app_id) {
+               int ret = table_statistics_per_app(rule->app_id, rule, info_cb, user_data);
+               __STC_LOG_FUNC_EXIT__;
+               return ret;
+       }
+
        /* pick a statement depending on parameters */
        if (rule->granularity) {
                stmt = __is_iftype_defined(rule->iftype) ?
@@ -488,27 +494,27 @@ stc_error_e table_statistics_foreach_app(const table_statistics_select_rule *rul
 
                        if (info_cb(&data, user_data) == STC_CANCEL)
                                rc = SQLITE_DONE; //LCOV_EXCL_LINE
-                       __STC_LOG_FUNC_EXIT__;
                        break;
                case SQLITE_DONE:
-                       __STC_LOG_FUNC_EXIT__;
                        break;
                case SQLITE_ERROR:
                default:
                        error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
-                       __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
                        break; //LCOV_EXCL_LINE
                }
        } while (rc == SQLITE_ROW);
 
 handle_error:
-       sqlite3_reset(stmt);
+       rc = sqlite3_reset(stmt);
+       if (rc != SQLITE_OK)
+               error_code = STC_ERROR_DB_FAILED;
+
        __STC_LOG_FUNC_EXIT__;
        return error_code;
 }
 
-stc_error_e table_statistics_per_app(const char *app_id,
-                                    table_statistics_select_rule *rule,
+API stc_error_e table_statistics_per_app(const char *app_id,
+                                    const table_statistics_select_rule *rule,
                                     table_statistics_info_cb info_cb,
                                     void *user_data)
 {
@@ -569,32 +575,34 @@ stc_error_e table_statistics_per_app(const char *app_id,
 
                        if (info_cb(&data, user_data) == STC_CANCEL)
                                rc = SQLITE_DONE; //LCOV_EXCL_LINE
-                       __STC_LOG_FUNC_EXIT__;
                        break;
                case SQLITE_DONE:
-                       __STC_LOG_FUNC_EXIT__;
                        break;
                case SQLITE_ERROR:
                default:
                        error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
-                       __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
                        break; //LCOV_EXCL_LINE
                }
        } while (rc == SQLITE_ROW);
 
 handle_error:
-       sqlite3_reset(stmt);
+       rc = sqlite3_reset(stmt);
+       if (rc != SQLITE_OK)
+               error_code = STC_ERROR_DB_FAILED;
+
        __STC_LOG_FUNC_EXIT__;
        return error_code;
 }
 
-stc_error_e table_statistics_insert(stc_db_classid_iftype_key *stat_key,
+API stc_error_e table_statistics_insert(stc_db_classid_iftype_key *stat_key,
                                    stc_db_app_stats *stat,
                                    time_t last_touch_time)
 {
        stc_error_e error_code = STC_ERROR_NONE;
        sqlite3_stmt *stmt = update_statistics_query;
        stc_hw_net_protocol_type_e hw_net_protocol_type = STC_PROTOCOL_UNKNOWN;
+       long long int rcv;
+       long long int snd;
 
        if (!stat->rcv_count && !stat->snd_count) {
                error_code = STC_ERROR_INVALID_PARAMETER;
@@ -609,14 +617,16 @@ stc_error_e table_statistics_insert(stc_db_classid_iftype_key *stat_key,
        DB_ACTION(sqlite3_bind_int(stmt, 5, (int)(stat_key->iftype)));
        DB_ACTION(sqlite3_bind_int(stmt, 6, (int)(stat->is_roaming)));
        DB_ACTION(sqlite3_bind_int(stmt, 7, (int)hw_net_protocol_type));
-       DB_ACTION(sqlite3_bind_text(stmt, 8, stat_key->ifname, -1,
-                                   SQLITE_STATIC));
+       DB_ACTION(sqlite3_bind_text(stmt, 8, stat_key->ifname ? stat_key->ifname : "",
+                                       -1, SQLITE_STATIC));
        DB_ACTION(sqlite3_bind_text(stmt, 9,
-                                   stat_key->subscriber_id ? stat_key->subscriber_id : "" , -1,
-                                   SQLITE_STATIC));
+                                   stat_key->subscriber_id ? stat_key->subscriber_id : "" ,
+                                   -1, SQLITE_STATIC));
        DB_ACTION(sqlite3_bind_int(stmt, 10, (int)stat->ground));
 
        /*we want to reuse tree*/
+       rcv = stat->rcv_count;
+       snd = stat->snd_count;
        stat->rcv_count = 0;
        stat->snd_count = 0;
        if (sqlite3_step(stmt) != SQLITE_DONE) {
@@ -627,11 +637,14 @@ stc_error_e table_statistics_insert(stc_db_classid_iftype_key *stat_key,
                goto handle_error; //LCOV_EXCL_LINE
        }
 
-       if (STC_DEBUG_LOG)
-               STC_LOGD("App stat recorded [%s]", stat->app_id);
+       if (STC_STAT_LOG)
+               STC_LOGD("App stat recorded [\033[0;34m%s\033[0;m] "
+                       "rcv[%lld] snd[%lld]", stat->app_id, rcv, snd);
 
 handle_error:
-       sqlite3_reset(stmt);
+       if (sqlite3_reset(stmt) != SQLITE_OK)
+               error_code = STC_ERROR_DB_FAILED;
+
        return error_code;
 }