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