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;
36 static void __change_db_entries_num_num(int num)
38 __STC_LOG_FUNC_ENTER__;
41 if (vconf_set_int(VCONF_KEY_DB_ENTRIES_COUNT, db_entries))
42 STC_LOGE("Failed to set new db entries number");
44 __STC_LOG_FUNC_EXIT__;
47 static void __check_erase_db_oversize(void)
49 __STC_LOG_FUNC_ENTER__;
51 struct stat db_stat = {0};
54 if (stat(DATABASE_FULL_PATH, &db_stat)) {
55 STC_LOGE("Failed to get statistics for %s errno %d",
56 DATABASE_FULL_PATH, errno);
57 __STC_LOG_FUNC_EXIT__;
61 if (db_stat.st_size < DB_SIZE_THRESHOLD) {
62 STC_LOGD("Db truncation isn't required!");
63 __STC_LOG_FUNC_EXIT__;
67 /* get approximate number of entries for removing */
68 del_entry = (db_stat.st_size - DB_SIZE_THRESHOLD) / ENTRY_SIZE;
70 table_statistics_reset_first_n_entries(del_entry)) {
71 STC_LOGE("Failed to remove first %d entries", del_entry);
72 __STC_LOG_FUNC_EXIT__;
76 __change_db_entries_num_num(-del_entry);
78 __STC_LOG_FUNC_EXIT__;
81 static void __erase_old_entries(void)
83 __STC_LOG_FUNC_ENTER__;
84 char buffer[80] = {0, };
85 table_statistics_reset_rule rule = {
86 .iftype = STC_IFACE_LAST_ELEM,
88 stc_db_tm_interval_s interval;
89 time_t until = time(0);
90 struct tm result = {0, };
92 until -= ERASE_INTERVAL;
96 rule.interval = &interval;
98 strftime(buffer, 80, "%x - %I:%M%p", localtime_r(&until, &result));
99 STC_LOGD("Reset statistics till %s", buffer);
101 if (table_statistics_reset(&rule) != STC_ERROR_NONE)
102 STC_LOGE("Failed to reset statistics");
104 __STC_LOG_FUNC_EXIT__;
107 static gboolean __erase_func_cb(void *user_data)
109 __STC_LOG_FUNC_ENTER__;
111 __check_erase_db_oversize();
112 __erase_old_entries();
114 /* we need to continue the timer */
115 __STC_LOG_FUNC_EXIT__;
119 stc_error_e stc_init_db_guard(void)
121 __STC_LOG_FUNC_ENTER__;
123 erase_timer = g_timeout_add_seconds(ERASE_TIMER_INTERVAL,
124 __erase_func_cb, NULL);
125 if (erase_timer == 0) {
126 STC_LOGE("Failed to create timer");
127 __STC_LOG_FUNC_EXIT__;
128 return STC_ERROR_FAIL;
131 __STC_LOG_FUNC_EXIT__;
132 return STC_ERROR_NONE;
135 void stc_deinit_db_guard(void)
137 __STC_LOG_FUNC_ENTER__;
139 if (erase_timer > 0) {
140 g_source_remove(erase_timer);
144 __STC_LOG_FUNC_EXIT__;