[SM][Data Usage] Interval used in stats 32/153932/2
authorRadek Kintop <r.kintop@samsung.com>
Tue, 3 Oct 2017 13:30:27 +0000 (15:30 +0200)
committerRadek Kintop <r.kintop@samsung.com>
Mon, 9 Oct 2017 15:47:48 +0000 (17:47 +0200)
- interval is taken into account in sim1, sim2 and wifi
separately when fetching stats

Change-Id: Ia30e9603695470b352ac93af7968b76b2f084879
Signed-off-by: Radek Kintop <r.kintop@samsung.com>
setting-smartmanager/smartmanager-data/include/smartmanager-utils.h
setting-smartmanager/smartmanager-data/src/smartmanager-data.c
setting-smartmanager/smartmanager-data/src/smartmanager-utils.c

index d9b8917f20b7d786b339701cc4c61b234b0922f5..9de3f3738db93b0f7f796961face8111426c6898 100644 (file)
@@ -39,12 +39,71 @@ typedef enum {
        RESTRICTIONS_OTHER_ERROR = 3
 } restrictions_result;
 
-void get_data_amount_str(const char *prefix, int64_t num_bytes, char *txt_out, int len_max);
+void get_data_amount_str(const char *prefix, int64_t num_bytes, char *txt_out,
+                                               int len_max);
+
+/**
+ * @brief Gets total statistics for SIM type interface
+ *
+ * @param[in] stc                      The statistics rule handle
+ * @param[in] stats_cb         Callback to be called asynchronously
+ * @param[in] subscriber_id    The SIM subscriber id to be passed to stats_cb
+ *                                                     in return from STC API calls
+ * @param[in] t_from           The start of the time interval
+ * @param[in] t_to                     The end of the time interval
+ *
+ * @return true on success, otherwise false
+ *
+ */
+bool get_sim_total_stats(stc_h stc, stc_stats_info_cb stats_cb,
+                                               const char *subscriber_id, time_t t_from, time_t t_to);
+
+/**
+ * @brief Gets total statistics for WiFi type interface
+ *
+ * @param[in] stc                      The statistics rule handle
+ * @param[in] stats_cb         Callback to be called asynchronously
+ * @param[in] t_from           The start of the time interval
+ * @param[in] t_to                     The end of the time interval
+ *
+ * @return true on success, otherwise false
+ *
+ */
+bool get_wifi_total_stats(stc_h stc, stc_stats_info_cb stats_cb, time_t t_from,
+                                               time_t t_to);
+
+/**
+ * @brief Gets a list of all apps and their data usage statistics
+ * for SIM type interface.
+ *
+ * @param[in] stc                      The statistics rule handle
+ * @param[in] stats_cb         Callback to be called asynchronously
+ * @param[in] subscriber_id    The SIM subscriber id to be passed to stats_cb
+ *                                                     in return from STC API calls
+ * @param[in] t_from           The start of the time interval
+ * @param[in] t_to                     The end of the time interval
+ *
+ * @return true on success, otherwise false
+ *
+ */
+bool get_sim_apps_list(stc_h stc, stc_stats_info_cb stats_cb,
+                                               const char *subscriber_id, time_t t_from, time_t t_to);
+
+/**
+ * @brief Gets a list of all apps and their data usage statistics
+ * for WiFi type interface.
+ *
+ * @param[in] stc                      The statistics rule handle
+ * @param[in] stats_cb         Callback to be called asynchronously
+ * @param[in] t_from           The start of the time interval
+ * @param[in] t_to                     The end of the time interval
+ *
+ * @return true on success, otherwise false
+ *
+ */
+bool get_wifi_apps_list(stc_h stc, stc_stats_info_cb stats_cb, time_t t_from,
+                                               time_t t_to);
 
-bool get_sim_total_stats(stc_h stc, stc_stats_info_cb stats_cb);
-bool get_wifi_total_stats(stc_h stc, stc_stats_info_cb stats_cb);
-bool get_sim_apps_list(stc_h stc, stc_stats_info_cb stats_cb);
-bool get_wifi_apps_list(stc_h stc, stc_stats_info_cb stats_cb);
 bool get_sim_restrictions(stc_h stc, stc_restriction_info_cb restrictions_cb);
 bool get_wifi_restrictions(stc_h stc, stc_restriction_info_cb restrictions_cb);
 bool get_subscriber_id_from_telephony_handle(telephony_h handle,
index 2fa5bb3da9b5ace102a5b727ebbbd30f7f87596d..e12a294ba2dd0658765557ae3fed6a06be3f52e4 100755 (executable)
@@ -35,9 +35,11 @@ static Eina_Bool _delayed_timer_cb(void *data);
 static SmartMgrData ad = {0,};
 
 static bool wifi_stats_checked = false;
-static bool sim_stats_checked = false;
+static bool sim_1_stats_checked = false;
+static bool sim_2_stats_checked = false;
 static bool wifi_total_stats_checked = false;
-static bool sim_total_stats_checked = false;
+static bool sim_1_total_stats_checked = false;
+static bool sim_2_total_stats_checked = false;
 static bool wifi_restrictions_checked = false;
 static bool sim_restrictions_checked = false;
 static bool all_stat_cycles_checked = false;
@@ -58,45 +60,60 @@ static void _lang_changed(app_event_info_h event_info, void *data)
 static Eina_Bool _delayed_timer_cb(void *data)
 {
        data_usage_app_item_t *item = NULL;
-       bool tmp = false;
 
        if (!all_stat_cycles_checked) {
                _read_all_stat_cycles();
                all_stat_cycles_checked = true;
        }
 
-       if (!sim_total_stats_checked) {
+       if (!sim_1_total_stats_checked) {
                ad.sim_1_limits.total_data_used = 0;
-               ad.sim_2_limits.total_data_used = 0;
+               sim_1_total_stats_checked = get_sim_total_stats(ad.stc,
+                                                               _total_stats_cb, ad.subscriber_id_1,
+                                                               ad.sim_1_limits.interval_from,
+                                                               ad.sim_1_limits.interval_to);
+               return ECORE_CALLBACK_RENEW;
+       }
 
-               tmp = get_sim_total_stats(ad.stc, _total_stats_cb);
-               if (tmp)
-                       sim_total_stats_checked = true;
+       if (!sim_2_total_stats_checked) {
+               ad.sim_2_limits.total_data_used = 0;
+               sim_2_total_stats_checked = get_sim_total_stats(ad.stc,
+                                                               _total_stats_cb, ad.subscriber_id_2,
+                                                               ad.sim_2_limits.interval_from,
+                                                               ad.sim_2_limits.interval_to);
                return ECORE_CALLBACK_RENEW;
        }
 
        if (!wifi_total_stats_checked) {
                ad.wifi_limits.total_data_used = 0;
 
-               tmp = get_wifi_total_stats(ad.stc, _total_stats_cb);
-               if (tmp)
-                       wifi_total_stats_checked = true;
+               wifi_total_stats_checked = get_wifi_total_stats(ad.stc, _total_stats_cb,
+                                                                       ad.wifi_limits.interval_from,
+                                                                       ad.wifi_limits.interval_to);
                return ECORE_CALLBACK_RENEW;
        }
 
-       if (!sim_stats_checked) {
+       if (!sim_1_stats_checked) {
                ad.main_view_update_type = MV_UPDATE_CLEAR_ALL_SIM_1_APPS;
                setting_view_update(ad.main_view, &ad);
-               ad.main_view_update_type = MV_UPDATE_CLEAR_ALL_SIM_2_APPS;
-               setting_view_update(ad.main_view, &ad);
                EINA_LIST_FREE(ad.sim_1_apps, item)
                        _delete_stat_item(item);
+               sim_1_stats_checked = get_sim_apps_list(ad.stc,
+                                                               _stc_stats_info_cb, ad.subscriber_id_1,
+                                                               ad.sim_1_limits.interval_from,
+                                                               ad.sim_1_limits.interval_to);
+               return ECORE_CALLBACK_RENEW;
+       }
+
+       if (!sim_2_stats_checked) {
+               ad.main_view_update_type = MV_UPDATE_CLEAR_ALL_SIM_2_APPS;
+               setting_view_update(ad.main_view, &ad);
                EINA_LIST_FREE(ad.sim_2_apps, item)
                        _delete_stat_item(item);
-
-               tmp = get_sim_apps_list(ad.stc, _stc_stats_info_cb);
-               if (tmp)
-                       sim_stats_checked = true;
+               sim_2_stats_checked = get_sim_apps_list(ad.stc,
+                                                               _stc_stats_info_cb, ad.subscriber_id_2,
+                                                               ad.sim_2_limits.interval_from,
+                                                               ad.sim_2_limits.interval_to);
                return ECORE_CALLBACK_RENEW;
        }
 
@@ -106,9 +123,9 @@ static Eina_Bool _delayed_timer_cb(void *data)
                EINA_LIST_FREE(ad.wifi_apps, item)
                        _delete_stat_item(item);
 
-               tmp = get_wifi_apps_list(ad.stc, _stc_stats_info_cb);
-               if (tmp)
-                       wifi_stats_checked = true;
+               wifi_stats_checked = get_wifi_apps_list(ad.stc, _stc_stats_info_cb,
+                                                               ad.wifi_limits.interval_from,
+                                                               ad.wifi_limits.interval_to);
                return ECORE_CALLBACK_RENEW;
        }
 
@@ -117,18 +134,17 @@ static Eina_Bool _delayed_timer_cb(void *data)
                ad.sim_1_limits.warning = 0;
                ad.sim_2_limits.limit = 0;
                ad.sim_2_limits.warning = 0;
-               tmp = get_sim_restrictions(ad.stc, _get_restrictions_cb);
-               if (tmp)
-                       sim_restrictions_checked = true;
+               sim_restrictions_checked = get_sim_restrictions(ad.stc,
+                                                               _get_restrictions_cb);
+
                return ECORE_CALLBACK_RENEW;
        }
 
        if (!wifi_restrictions_checked) {
                ad.wifi_limits.limit = 0;
                ad.wifi_limits.warning = 0;
-               tmp = get_wifi_restrictions(ad.stc, _get_restrictions_cb);
-               if (tmp)
-                       wifi_restrictions_checked = true;
+               wifi_restrictions_checked = get_wifi_restrictions(ad.stc,
+                                                               _get_restrictions_cb);
                return ECORE_CALLBACK_RENEW;
        }
 
@@ -142,9 +158,11 @@ void smartmanager_stc_get_all_data(void)
                return;
 
        wifi_stats_checked = false;
-       sim_stats_checked = false;
+       sim_1_stats_checked = false;
+       sim_2_stats_checked = false;
        wifi_total_stats_checked = false;
-       sim_total_stats_checked = false;
+       sim_1_total_stats_checked = false;
+       sim_2_total_stats_checked = false;
        wifi_restrictions_checked = false;
        sim_restrictions_checked = false;
        all_stat_cycles_checked = false;
@@ -161,7 +179,7 @@ void smartmanager_stc_get_sim_total_and_apps_data(void)
 
        /*sim_stats_checked = false; TODO: figure out way to update without
         * blinking*/
-       sim_total_stats_checked = false;
+       sim_1_total_stats_checked = false;
 
        periodic_update_timer = ecore_timer_add(0.2, _delayed_timer_cb, NULL);
        if (!periodic_update_timer)
@@ -477,11 +495,13 @@ static stc_callback_ret_e _stc_stats_info_cb(stc_error_e result,
 {
        char *app_id = NULL;
        char *subscriber_id = NULL;
+       const char *subscriber_id_to_look_for = user_data;
        int64_t incoming = 0;
        int64_t outgoing = 0;
        stc_iface_type_e iface = STC_IFACE_UNKNOWN;
 
-       SETTING_TRACE_DEBUG("per app cb");
+       SETTING_TRACE_DEBUG("per app cb, looking for subscriber id: %s",
+                                               subscriber_id_to_look_for);
 
        if (STC_ERROR_NONE != result) {
                SETTING_TRACE_ERROR("Error in callback: %s", get_error_message(result));
@@ -522,7 +542,10 @@ static stc_callback_ret_e _stc_stats_info_cb(stc_error_e result,
        }
        if (iface == STC_IFACE_DATACALL) {
                ret = stc_stats_info_get_subscriber_id(info, &subscriber_id);
-               if (STC_ERROR_NONE == ret) {
+               if (STC_ERROR_NONE == ret &&
+                               compare_subscriber_ids(subscriber_id,
+                                                                               subscriber_id_to_look_for)
+                       ) {
                        SETTING_TRACE_DEBUG("Obtained subscriber ID: %s", subscriber_id);
                        if (compare_subscriber_ids(ad.subscriber_id_1, subscriber_id))
                                if (_append_stats_list_item(&ad.sim_1_apps, app_id, incoming + outgoing)) {
@@ -537,8 +560,11 @@ static stc_callback_ret_e _stc_stats_info_cb(stc_error_e result,
                                        setting_view_update(ad.main_view, &ad);
                                }
                } else {
-                       SETTING_TRACE_ERROR("stc_stats_info_get_subscriber_id() error: %s",
-                                                                               get_error_message(ret));
+                       SETTING_TRACE_ERROR("stc_stats_info_get_subscriber_id() error:"\
+                                       " %s , subscriber id: %s , subscriber id to look for: %s",
+                                                                               get_error_message(ret),
+                                                                               subscriber_id,
+                                                                               subscriber_id_to_look_for);
                }
                free(subscriber_id);
        }
@@ -552,10 +578,12 @@ static stc_callback_ret_e _total_stats_cb(stc_error_e result,
        int64_t incoming = 0;
        int64_t outgoing = 0;
        char *subscriber_id = NULL;
+       const char *subscriber_id_to_look_for = user_data;
        int ret = STC_ERROR_NONE;
        stc_iface_type_e iface_type = STC_IFACE_UNKNOWN;
 
-       SETTING_TRACE_DEBUG("total stats cb");
+       SETTING_TRACE_DEBUG("total stats cb, looking for subscriber id: %s",
+                                               subscriber_id_to_look_for);
 
        if (STC_ERROR_NONE != result) {
                SETTING_TRACE_ERROR("Error in cb: %s", get_error_message(result));
@@ -573,7 +601,11 @@ static stc_callback_ret_e _total_stats_cb(stc_error_e result,
        if (STC_ERROR_NONE == ret) {
                if (iface_type == STC_IFACE_DATACALL) {
                        ret = stc_stats_info_get_subscriber_id(info, &subscriber_id);
-                       if (STC_ERROR_NONE == ret) {
+
+                       if (STC_ERROR_NONE == ret &&
+                               compare_subscriber_ids(subscriber_id,
+                                                                               subscriber_id_to_look_for)
+                               ) {
 
                                SETTING_TRACE_DEBUG("Obtained SUBSCRIBER ID: %s",
                                                                        subscriber_id);
index 48bb9c8f3e3f555ea56e9a236695d578f9f00617..ca3b46591f41efd2d55897c95cace3cd77ae066a 100755 (executable)
@@ -7,9 +7,12 @@
 #include <app_preference.h>
 
 static bool _create_stats_list(stc_h stc, stc_iface_type_e iface,
-                                                               stc_stats_info_cb stats_cb);
+                                                               stc_stats_info_cb stats_cb,
+                                                               void *cb_data, time_t t_from,
+                                                               time_t t_to);
 static bool _get_total_stats(stc_h stc, stc_iface_type_e iface_type,
-                                                               stc_stats_info_cb stats_cb);
+                                                               stc_stats_info_cb stats_cb, void *cb_data,
+                                                               time_t t_from, time_t t_to);
 static bool _get_restrictions(stc_h stc, stc_iface_type_e iface_type,
                                                                stc_restriction_info_cb restrictions_cb,
                                                                void *restriction_info_cb_data);
@@ -39,24 +42,31 @@ void get_data_amount_str(const char *prefix, int64_t num_bytes, char *txt_out, i
        }
 }
 
-bool get_sim_total_stats(stc_h stc, stc_stats_info_cb stats_cb)
+bool get_sim_total_stats(stc_h stc, stc_stats_info_cb stats_cb,
+                                               const char *subscriber_id, time_t t_from, time_t t_to)
 {
-       return _get_total_stats(stc, STC_IFACE_DATACALL, stats_cb);
+       return _get_total_stats(stc, STC_IFACE_DATACALL, stats_cb,
+                                                       (void *)subscriber_id, t_from, t_to);
 }
 
-bool get_wifi_total_stats(stc_h stc, stc_stats_info_cb stats_cb)
+bool get_wifi_total_stats(stc_h stc, stc_stats_info_cb stats_cb, time_t t_from,
+                                                       time_t t_to)
 {
-       return _get_total_stats(stc, STC_IFACE_WIFI, stats_cb);
+       return _get_total_stats(stc, STC_IFACE_WIFI, stats_cb, NULL, t_from, t_to);
 }
 
-bool get_sim_apps_list(stc_h stc, stc_stats_info_cb stats_cb)
+bool get_sim_apps_list(stc_h stc, stc_stats_info_cb stats_cb,
+                                               const char *subscriber_id, time_t t_from, time_t t_to)
 {
-       return _create_stats_list(stc, STC_IFACE_DATACALL, stats_cb);
+       return _create_stats_list(stc, STC_IFACE_DATACALL, stats_cb,
+                                                               (void *)subscriber_id, t_from, t_to);
 }
 
-bool get_wifi_apps_list(stc_h stc, stc_stats_info_cb stats_cb)
+bool get_wifi_apps_list(stc_h stc, stc_stats_info_cb stats_cb, time_t t_from,
+                                               time_t t_to)
 {
-       return _create_stats_list(stc, STC_IFACE_WIFI, stats_cb);
+       return _create_stats_list(stc, STC_IFACE_WIFI, stats_cb,
+                                                               NULL, t_from, t_to);
 }
 
 bool get_sim_restrictions(stc_h stc, stc_restriction_info_cb restrictions_cb)
@@ -70,7 +80,9 @@ bool get_wifi_restrictions(stc_h stc, stc_restriction_info_cb restrictions_cb)
 }
 
 static bool _create_stats_list(stc_h stc, stc_iface_type_e iface,
-                                                               stc_stats_info_cb stats_cb)
+                                                               stc_stats_info_cb stats_cb,
+                                                               void *cb_data, time_t t_from,
+                                                               time_t t_to)
 {
        stc_stats_rule_h rule = NULL;
 
@@ -81,10 +93,7 @@ static bool _create_stats_list(stc_h stc, stc_iface_type_e iface,
                return true;
        }
 
-       time_t now = time(NULL);
-
-       ret = stc_stats_rule_set_time_interval(rule,
-                                                                                       now - STC_TIME_PERIOD_MONTH, now);
+       ret = stc_stats_rule_set_time_interval(rule, t_from, t_to);
        if (ret != STC_ERROR_NONE) {
                SETTING_TRACE_ERROR("stc_stats_rule_set_time_interval() error: %s",
                                                        get_error_message(ret));
@@ -99,7 +108,7 @@ static bool _create_stats_list(stc_h stc, stc_iface_type_e iface,
        }
 
        SETTING_TRACE_DEBUG("\033[1;33mObtaining per app data usage for ifce: %d", iface);
-       ret = stc_foreach_stats(stc, rule, stats_cb, NULL);
+       ret = stc_foreach_stats(stc, rule, stats_cb, cb_data);
        if (ret != STC_ERROR_NONE) {
                SETTING_TRACE_ERROR("stc_get_stats() error: %s",
                                                        get_error_message(ret));
@@ -114,10 +123,10 @@ static bool _create_stats_list(stc_h stc, stc_iface_type_e iface,
 }
 
 static bool _get_total_stats(stc_h stc, stc_iface_type_e iface_type,
-                                                               stc_stats_info_cb stats_cb)
+                                                               stc_stats_info_cb stats_cb, void *cb_data,
+                                                               time_t t_from, time_t t_to)
 {
        stc_stats_rule_h rule = NULL;
-       time_t now = time(NULL);
        int ret = STC_ERROR_NONE;
 
        ret = stc_stats_rule_create(stc, &rule);
@@ -127,8 +136,7 @@ static bool _get_total_stats(stc_h stc, stc_iface_type_e iface_type,
                return true;
        }
 
-       ret = stc_stats_rule_set_time_interval(rule,
-                                                                                       now - STC_TIME_PERIOD_MONTH, now);
+       ret = stc_stats_rule_set_time_interval(rule, t_from, t_to);
        if (ret != STC_ERROR_NONE) {
                SETTING_TRACE_ERROR("stc_stats_rule_set_time_interval() error: %s",
                                                        get_error_message(ret));
@@ -145,7 +153,7 @@ static bool _get_total_stats(stc_h stc, stc_iface_type_e iface_type,
        }
 
        SETTING_TRACE_DEBUG("\033[1;34mObtaining total stats for ifce: %d", iface_type);
-       ret = stc_get_total_stats(stc, rule, stats_cb, NULL);
+       ret = stc_get_total_stats(stc, rule, stats_cb, cb_data);
        if (ret != STC_ERROR_NONE) {
                SETTING_TRACE_ERROR("stc_get_total_stats() error: %s",
                                                get_error_message(ret));