2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include "table-statistics.h"
23 #define VCONF_KEY_DB_ENTRIES_COUNT "db/stc-manager/datausage_timer"
24 #define ENTRY_SIZE 128
27 #define ERASE_TIMER_INTERVAL 3600
29 #define ERASE_INTERVAL 3600 * 24 * 40
31 #define DB_SIZE_THRESHOLD 1048576 * 50
33 static guint erase_timer = 0;
34 static int db_entries = 0;
37 static void __change_db_entries_num_num(int num)
39 __STC_LOG_FUNC_ENTER__;
42 if (vconf_set_int(VCONF_KEY_DB_ENTRIES_COUNT, db_entries))
43 STC_LOGE("Failed to set new db entries number");
45 __STC_LOG_FUNC_EXIT__;
48 static void __check_erase_db_oversize(void)
50 __STC_LOG_FUNC_ENTER__;
52 struct stat db_stat = {0};
55 if (stat(DATABASE_FULL_PATH, &db_stat)) {
56 STC_LOGE("Failed to get statistics for %s errno %d",
57 DATABASE_FULL_PATH, errno);
58 __STC_LOG_FUNC_EXIT__;
62 if (db_stat.st_size < DB_SIZE_THRESHOLD) {
63 STC_LOGD("Db truncation isn't required!");
64 __STC_LOG_FUNC_EXIT__;
68 /* get approximate number of entries for removing */
69 del_entry = (db_stat.st_size - DB_SIZE_THRESHOLD) / ENTRY_SIZE;
71 table_statistics_reset_first_n_entries(del_entry)) {
72 STC_LOGE("Failed to remove first %d entries", del_entry);
73 __STC_LOG_FUNC_EXIT__;
77 __change_db_entries_num_num(-del_entry);
79 __STC_LOG_FUNC_EXIT__;
82 static void __erase_old_entries(void)
84 __STC_LOG_FUNC_ENTER__;
85 char buffer[80] = {0, };
86 table_statistics_reset_rule rule = {
87 .iftype = STC_IFACE_LAST_ELEM,
89 stc_db_tm_interval_s interval;
90 time_t until = time(0);
91 struct tm result = {0, };
93 until -= ERASE_INTERVAL;
97 rule.interval = &interval;
99 strftime(buffer, 80, "%x - %I:%M%p", localtime_r(&until, &result));
100 STC_LOGD("Reset statistics till %s", buffer);
102 if (table_statistics_reset(&rule) != STC_ERROR_NONE)
103 STC_LOGE("Failed to reset statistics");
105 __STC_LOG_FUNC_EXIT__;
108 static gboolean __erase_func_cb(void *user_data)
110 __STC_LOG_FUNC_ENTER__;
112 __check_erase_db_oversize();
113 __erase_old_entries();
115 /* we need to continue the timer */
116 __STC_LOG_FUNC_EXIT__;
121 stc_error_e stc_init_db_guard(void)
123 __STC_LOG_FUNC_ENTER__;
125 erase_timer = g_timeout_add_seconds(ERASE_TIMER_INTERVAL,
126 __erase_func_cb, NULL);
127 if (erase_timer == 0) {
128 STC_LOGE("Failed to create timer"); //LCOV_EXCL_LINE
129 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
130 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
133 __STC_LOG_FUNC_EXIT__;
134 return STC_ERROR_NONE;
137 void stc_deinit_db_guard(void)
139 __STC_LOG_FUNC_ENTER__;
141 if (erase_timer > 0) {
142 g_source_remove(erase_timer);
146 __STC_LOG_FUNC_EXIT__;