Adjust coding rule and remove compile warning messages
[platform/core/appfw/librua.git] / src / rua_stat.c
1 /*
2  * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /*
18  * @file    rua_stat.c
19  * @author  Hyunho Kang (hhstark.kang@samsung.com)
20  * @version 0.1
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <db-util.h>
28
29 /* For multi-user support */
30 #include <tzplatform_config.h>
31
32 #include "rua_stat.h"
33 #include "db-schema.h"
34 #include "perf-measure.h"
35
36 #include <dlog.h>
37
38 #ifdef LOG_TAG
39 #undef LOG_TAG
40 #endif
41
42 #define LOG_TAG "RUA"
43
44 #define RUA_STAT_DB_NAME        ".rua_stat.db"
45 #define QUERY_MAXLEN    4096
46 #define WIN_SCORE 100
47 #define LOSE_SCORE_RATE 0.7f
48
49 static sqlite3 *_db = NULL;
50 static int __exec(sqlite3 *db, char *query);
51 static sqlite3 *__db_init(char *root, int flags);
52
53 int __rua_stat_init(int flags)
54 {
55         char defname[FILENAME_MAX];
56         const char *rua_stat_db_path = tzplatform_getenv(TZ_USER_DB);
57
58         if (_db)
59                 return 0;
60
61         snprintf(defname, sizeof(defname), "%s/%s", rua_stat_db_path, RUA_STAT_DB_NAME);
62         _db = __db_init(defname, flags);
63
64         if (_db == NULL) {
65                 LOGW("__rua_stat_init error");
66                 return -1;
67         }
68
69         return 0;
70 }
71
72 int __rua_stat_fini(void)
73 {
74         if (_db) {
75                 db_util_close(_db);
76                 _db = NULL;
77         }
78
79         return 0;
80 }
81
82 int __rua_stat_insert(char *caller, char *rua_stat_tag)
83 {
84         int r;
85         char query[QUERY_MAXLEN];
86         sqlite3_stmt *stmt = NULL;
87
88         sqlite3_snprintf(QUERY_MAXLEN, query,
89                 "INSERT INTO rua_panel_stat (caller_panel, rua_stat_tag, score) VALUES (?,?,?)");
90
91         r = sqlite3_prepare(_db, query, sizeof(query), &stmt, NULL);
92         if (r != SQLITE_OK) {
93                 LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(_db), sqlite3_errmsg(_db));
94                 goto out;
95         }
96
97         r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC);
98         if (r != SQLITE_OK) {
99                 LOGE("caller bind error(%d) \n", r);
100                 goto out;
101         }
102
103         r = sqlite3_bind_text(stmt, 2, rua_stat_tag, strlen(rua_stat_tag), SQLITE_STATIC);
104         if (r != SQLITE_OK) {
105                 LOGE("rua_stat_tag bind error(%d) \n", r);
106                 goto out;
107         }
108
109         r = sqlite3_bind_int(stmt, 3, WIN_SCORE);
110         if (r != SQLITE_OK) {
111                 LOGE("arg bind error(%d) \n", r);
112                 goto out;
113         }
114
115         r = sqlite3_step(stmt);
116         if (r != SQLITE_DONE) {
117                 LOGE("step error(%d) \n", r);
118                 goto out;
119         }
120
121 out:
122         if (stmt)
123                 sqlite3_finalize(stmt);
124
125         return r;
126 }
127
128 int __rua_stat_lose_score_update(char *caller, char *rua_stat_tag)
129 {
130         int r;
131         char query[QUERY_MAXLEN];
132         sqlite3_stmt *stmt = NULL;
133
134         sqlite3_snprintf(QUERY_MAXLEN, query,
135                 "UPDATE rua_panel_stat SET score = score * %f WHERE caller_panel = ? AND rua_stat_tag != ?",
136                 LOSE_SCORE_RATE);
137
138         LOGD("lose score update sql : %s", query);
139         r = sqlite3_prepare(_db, query, sizeof(query), &stmt, NULL);
140         if (r != SQLITE_OK) {
141                 LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(_db), sqlite3_errmsg(_db));
142                 goto out;
143         }
144
145         r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC);
146         if (r != SQLITE_OK) {
147                 LOGE("caller bind error(%d) \n", r);
148                 goto out;
149         }
150
151         r = sqlite3_bind_text(stmt, 2, rua_stat_tag, strlen(rua_stat_tag), SQLITE_STATIC);
152         if (r != SQLITE_OK) {
153                 LOGE("rua_stat_tag bind error(%d) \n", r);
154                 goto out;
155         }
156
157         r = sqlite3_step(stmt);
158         if (r != SQLITE_DONE) {
159                 LOGE("step error(%d) \n", r);
160                 goto out;
161         }
162
163 out:
164         if (stmt)
165                 sqlite3_finalize(stmt);
166
167         return r;
168 }
169
170 int __rua_stat_win_score_update(char *caller, char *rua_stat_tag)
171 {
172         int r;
173         char query[QUERY_MAXLEN];
174         sqlite3_stmt *stmt = NULL;
175
176         sqlite3_snprintf(QUERY_MAXLEN, query,
177                 "UPDATE rua_panel_stat SET score = score + %d WHERE caller_panel = ? AND rua_stat_tag = ?",
178                 WIN_SCORE);
179
180         LOGD("win score update sql : %s", query);
181
182         r = sqlite3_prepare(_db, query, sizeof(query), &stmt, NULL);
183         if (r != SQLITE_OK) {
184                 LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(_db), sqlite3_errmsg(_db));
185                 goto out;
186         }
187
188         r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC);
189         if (r != SQLITE_OK) {
190                 LOGE("caller bind error(%d) \n", r);
191                 goto out;
192         }
193
194         r = sqlite3_bind_text(stmt, 2, rua_stat_tag, strlen(rua_stat_tag), SQLITE_STATIC);
195         if (r != SQLITE_OK) {
196                 LOGE("rua_stat_tag bind error(%d) \n", r);
197                 goto out;
198         }
199
200         r = sqlite3_step(stmt);
201         if (r != SQLITE_DONE) {
202                 LOGE("step error(%d) \n", r);
203                 goto out;
204         }
205
206 out:
207         if (stmt)
208                 sqlite3_finalize(stmt);
209
210         return r;
211 }
212
213 int rua_stat_update(char *caller, char *rua_stat_tag)
214 {
215         int r;
216         int affected_rows = 0;
217
218         LOGD("rua_stat_update start");
219
220         r = __rua_stat_init(SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE);
221         if (r == -1) {
222                 LOGE("__rua_stat_init fail");
223                 return -1;
224         }
225
226         if (_db == NULL) {
227                 LOGE("rua_stat is not initialized");
228                 return -1;
229         }
230
231         if (caller == NULL) {
232                 LOGE("caller is null");
233                 return -1;
234         }
235
236         if (rua_stat_tag == NULL) {
237                 LOGE("rua_stat_tag is null");
238                 return -1;
239         }
240
241
242         r = __rua_stat_lose_score_update(caller, rua_stat_tag);
243         if (r != SQLITE_DONE) {
244                 LOGE("__rua_stat_lose_score_insert fail.");
245                 return -1;
246         }
247
248         r = __rua_stat_win_score_update(caller, rua_stat_tag);
249         affected_rows = sqlite3_changes(_db);
250         if ((r != SQLITE_DONE) || (affected_rows == 0)) {
251                 r = __rua_stat_insert(caller, rua_stat_tag);
252
253                 if (r != SQLITE_DONE) {
254                         LOGE("__rua_stat_insert fail.");
255                         return -1;
256                 }
257         }
258
259         __rua_stat_fini();
260         LOGD("rua_stat_update done");
261
262         return r;
263
264 }
265
266
267 int rua_stat_get_stat_tags(char *caller,
268                 int (*rua_stat_tag_iter_fn)(const char *rua_stat_tag, void *data),
269                 void *data)
270 {
271         int r;
272         sqlite3_stmt *stmt;
273         char query[QUERY_MAXLEN];
274         const unsigned char *ct;
275
276         r = __rua_stat_init(SQLITE_OPEN_READONLY);
277         if (r == -1) {
278                 LOGE("__rua_stat_init fail");
279                 return -1;
280         }
281
282         sqlite3_snprintf(QUERY_MAXLEN, query,
283                 "SELECT rua_stat_tag FROM rua_panel_stat WHERE caller_panel = ? ORDER BY score DESC");
284
285         if (!_db)
286                 return -1;
287
288         r = sqlite3_prepare(_db, query, sizeof(query), &stmt, NULL);
289         if (r != SQLITE_OK) {
290                 LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(_db), sqlite3_errmsg(_db));
291                 goto out;
292         }
293
294         r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC);
295         if (r != SQLITE_OK) {
296                 LOGE("caller bind error(%d) \n", r);
297                 goto out;
298         }
299
300         while (sqlite3_step(stmt) == SQLITE_ROW) {
301                 ct = sqlite3_column_text(stmt, 0);
302                 if (ct == NULL || ct[0] == '\0')
303                         LOGW("sqlite3_column_text null");
304
305                 rua_stat_tag_iter_fn((const char *)ct, data);
306         }
307
308 out:
309         if (stmt)
310                 sqlite3_finalize(stmt);
311
312         __rua_stat_fini();
313
314         return r;
315 }
316
317 static int __exec(sqlite3 *db, char *query)
318 {
319         int r;
320         char *errmsg = NULL;
321
322         if (db == NULL)
323                 return -1;
324
325         r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
326         if (r != SQLITE_OK) {
327                 SECURE_LOGE("query(%s) exec error(%s)", query, errmsg);
328                 sqlite3_free(errmsg);
329                 return -1;
330         }
331
332         return 0;
333 }
334
335 static int __create_table(sqlite3 *db)
336 {
337         int r;
338
339         r = __exec(db, CREATE_RUA_STAT_TABLE);
340         if (r == -1) {
341                 LOGE("create table error");
342                 return -1;
343         }
344
345         return 0;
346 }
347
348 static sqlite3 *__db_init(char *root, int flags)
349 {
350         int r;
351         sqlite3 *db = NULL;
352
353         r = db_util_open_with_options(root, &db, flags, NULL);
354         if (r) {
355                 LOGE("db util open error(%d/%d/%d/%s)", r,
356                         sqlite3_errcode(db),
357                         sqlite3_extended_errcode(db),
358                         sqlite3_errmsg(db));
359                 return NULL;
360
361         }
362         r = __create_table(db);
363         if (r) {
364                 db_util_close(db);
365                 return NULL;
366         }
367
368         return db;
369 }