From 2bc145e7a5ec58f1ecb6c738625bca5e99057621 Mon Sep 17 00:00:00 2001 From: hyunho kang Date: Tue, 17 Nov 2015 11:55:21 +0900 Subject: [PATCH] Sync rua_stat feature with tizen 2.4 Change-Id: I1ae6825fb29e739b08e7dc54004b8e184a94bd93 Signed-off-by: hyunho kang --- CMakeLists.txt | 1 + include/db-schema.h | 19 +++ include/rua_stat.h | 64 +++++++++ src/rua.c | 17 ++- src/rua_stat.c | 379 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 478 insertions(+), 2 deletions(-) create mode 100755 include/rua_stat.h create mode 100755 src/rua_stat.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 6200a85..ffbe4e0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,7 @@ SET(VERSION 0.1.0) SET(SRCS src/perf-measure.c src/rua.c + src/rua_stat.c ) INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) diff --git a/include/db-schema.h b/include/db-schema.h index df05dbd..87d34ca 100644 --- a/include/db-schema.h +++ b/include/db-schema.h @@ -33,6 +33,16 @@ CREATE TABLE IF NOT EXISTS rua_history ( \ launch_time INTEGER \ );" + +#define CREATE_RUA_STAT_TABLE " \ +CREATE TABLE if not exists rua_panel_stat ( \ +caller_panel TEXT NOT NULL, \ +rua_stat_tag TEXT NOT NULL, \ +score INTEGER DEFAULT 0, \ +PRIMARY KEY(rua_stat_tag, caller_panel) \ +);" + + /* table index */ enum { RUA_COL_ID = 0x00, @@ -41,4 +51,13 @@ enum { RUA_COL_ARG, RUA_COL_LAUNCHTIME }; + +enum { + RUA_STAT_COL_CALLER_PANEL = 0x00, + RUA_STAT_COL_RUA_STAT_TAG, + RUA_SATA_COL_SCORE +}; + + + #endif /* __RUA_SCHEMA_H__ */ diff --git a/include/rua_stat.h b/include/rua_stat.h new file mode 100755 index 0000000..a20ffd7 --- /dev/null +++ b/include/rua_stat.h @@ -0,0 +1,64 @@ +/* + * RUA STAT + * + * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * @file rua_stat.h + * @brief RUA STATUS API declaration header file. + * @author Hyunho Kang (hhstark.kang@samsung.com) + * @version 0.1 + * @history 0.1: RUA STAT API Declarations, structure declaration + */ + +#ifndef __RUA_STAT_H__ +#define __RUA_STAT_H__ + +#include +#include + +#ifndef API +#define API __attribute__ ((visibility("default"))) +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Add application launch status. + * @param[in] caller, rua_stat_tag + * @return 0 on success, otherwise a nagative error value + * @retval 0 on successful + * @retval -1 on failed + */ +API int rua_stat_update(char *caller, char *rua_stat_tag); + +/** + * @brief Get rua status tag list + * @param[in] caller, callback, user data + * @return 0 on success, otherwise a nagative error value + * @retval 0 on successful + * @retval -1 on failed + */ +API int rua_stat_get_stat_tags(char *caller, + int (*rua_stat_tag_iter_fn)(const char *rua_stat_tag, void *data), void *data); + +#ifdef __cplusplus +} +#endif +#endif /*__RUA_STAT_H__*/ diff --git a/src/rua.c b/src/rua.c index 2e2e64c..f806ad6 100644 --- a/src/rua.c +++ b/src/rua.c @@ -32,6 +32,14 @@ /* For multi-user support */ #include +#include + +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_TAG "RUA" + #include "rua.h" #include "db-schema.h" @@ -113,11 +121,15 @@ int rua_add_history(struct rua_rec *rec) unsigned int timestamp; timestamp = PERF_MEASURE_START("RUA"); - if (_db == NULL) + if (_db == NULL) { + LOGE("Error db null"); return -1; + } - if (rec == NULL) + if (rec == NULL) { + LOGE("Error rec null"); return -1; + } snprintf(query, QUERY_MAXLEN, "select count(*) from %s where pkg_name = '%s';", RUA_HISTORY, @@ -125,6 +137,7 @@ int rua_add_history(struct rua_rec *rec) r = sqlite3_prepare(_db, query, sizeof(query), &stmt, NULL); if (r != SQLITE_OK) { + LOGE("Error sqlite3_prepare fail"); return -1; } diff --git a/src/rua_stat.c b/src/rua_stat.c new file mode 100755 index 0000000..094e39c --- /dev/null +++ b/src/rua_stat.c @@ -0,0 +1,379 @@ +/* + * RUA + * + * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/* + * @file rua_stat.c + * @author Hyunho Kang (hhstark.kang@samsung.com) + * @version 0.1 + */ + +#include +#include +#include + +#include + +/* For multi-user support */ +#include + +#include "rua_stat.h" +#include "db-schema.h" +#include "perf-measure.h" + +#include + +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_TAG "RUA" + +#define RUA_STAT_DB_NAME ".rua_stat.db" +#define QUERY_MAXLEN 4096 +#define WIN_SCORE 100 +#define LOSE_SCORE_RATE 0.7f + +static sqlite3 *_db = NULL; +static int __exec(sqlite3 *db, char *query); +static sqlite3 *__db_init(char *root, int flags); + + +int __rua_stat_insert(char *caller, char *rua_stat_tag) { + + int r; + char query[QUERY_MAXLEN]; + sqlite3_stmt *stmt = NULL; + sqlite3_snprintf(QUERY_MAXLEN, query, + "INSERT INTO rua_panel_stat (caller_panel, rua_stat_tag, score) VALUES (?,?,?)"); + + r = sqlite3_prepare(_db, query, sizeof(query), &stmt, NULL); + if (r != SQLITE_OK) { + LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(_db), sqlite3_errmsg(_db)); + goto out; + } + + r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC); + if(r != SQLITE_OK) { + LOGE("caller bind error(%d) \n", r); + goto out; + } + + r = sqlite3_bind_text(stmt, 2, rua_stat_tag, strlen(rua_stat_tag), SQLITE_STATIC); + if(r != SQLITE_OK) { + LOGE("rua_stat_tag bind error(%d) \n", r); + goto out; + } + + r = sqlite3_bind_int(stmt, 3, WIN_SCORE); + if(r != SQLITE_OK) { + LOGE("arg bind error(%d) \n", r); + goto out; + } + + r = sqlite3_step(stmt); + if (r != SQLITE_DONE) { + LOGE("step error(%d) \n", r); + goto out; + } + + +out : + if(stmt) + sqlite3_finalize(stmt); + + return r; +} + +int __rua_stat_lose_score_update(char *caller, char *rua_stat_tag) { + + int r; + char query[QUERY_MAXLEN]; + sqlite3_stmt *stmt = NULL; + sqlite3_snprintf(QUERY_MAXLEN, query, + "UPDATE rua_panel_stat SET score = score * %f WHERE caller_panel = ? AND rua_stat_tag != ?", + LOSE_SCORE_RATE); + + LOGD("lose score update sql : %s", query); + + r = sqlite3_prepare(_db, query, sizeof(query), &stmt, NULL); + if (r != SQLITE_OK) { + LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(_db), sqlite3_errmsg(_db)); + goto out; + } + + r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC); + if(r != SQLITE_OK) { + LOGE("caller bind error(%d) \n", r); + goto out; + } + + r = sqlite3_bind_text(stmt, 2, rua_stat_tag, strlen(rua_stat_tag), SQLITE_STATIC); + if(r != SQLITE_OK) { + LOGE("rua_stat_tag bind error(%d) \n", r); + goto out; + } + + r = sqlite3_step(stmt); + if (r != SQLITE_DONE) { + LOGE("step error(%d) \n", r); + goto out; + } + + +out : + if(stmt) + sqlite3_finalize(stmt); + + return r; + +} + +int __rua_stat_win_score_update(char *caller, char *rua_stat_tag) { + + int r; + char query[QUERY_MAXLEN]; + sqlite3_stmt *stmt = NULL; + sqlite3_snprintf(QUERY_MAXLEN, query, + "UPDATE rua_panel_stat SET score = score + %d WHERE caller_panel = ? AND rua_stat_tag = ?", + WIN_SCORE); + + LOGD("win score update sql : %s", query); + + r = sqlite3_prepare(_db, query, sizeof(query), &stmt, NULL); + if (r != SQLITE_OK) { + LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(_db), sqlite3_errmsg(_db)); + goto out; + } + + r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC); + if(r != SQLITE_OK) { + LOGE("caller bind error(%d) \n", r); + goto out; + } + + r = sqlite3_bind_text(stmt, 2, rua_stat_tag, strlen(rua_stat_tag), SQLITE_STATIC); + if(r != SQLITE_OK) { + LOGE("rua_stat_tag bind error(%d) \n", r); + goto out; + } + + r = sqlite3_step(stmt); + if (r != SQLITE_DONE) { + LOGE("step error(%d) \n", r); + goto out; + } + + +out : + if(stmt) + sqlite3_finalize(stmt); + + return r; + +} + +int rua_stat_update(char *caller, char *rua_stat_tag) +{ + int r; + int affected_rows = 0; + sqlite3_stmt *stmt = NULL; + + LOGD("rua_stat_update start"); + + r = __rua_stat_init(SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE); + if (r == -1) { + LOGE("__rua_stat_init fail"); + return -1; + } + + if (_db == NULL) { + LOGE("rua_stat is not initialized"); + return -1; + } + + if (caller == NULL) { + LOGE("caller is null"); + return -1; + } + + if (rua_stat_tag == NULL) { + LOGE("rua_stat_tag is null"); + return -1; + } + + + r = __rua_stat_lose_score_update(caller, rua_stat_tag); + if (r != SQLITE_DONE) { + LOGE("__rua_stat_lose_score_insert fail."); + return -1; + } + + r = __rua_stat_win_score_update(caller, rua_stat_tag); + affected_rows = sqlite3_changes(_db); + if ((r != SQLITE_DONE) || (affected_rows == 0)) { + r = __rua_stat_insert(caller, rua_stat_tag); + + if (r != SQLITE_DONE) { + LOGE("__rua_stat_insert fail."); + return -1; + } + } + + __rua_stat_fini(); + LOGD("rua_stat_update done"); + + return r; + +} + + +int rua_stat_get_stat_tags(char *caller, int (*rua_stat_tag_iter_fn)(const char *rua_stat_tag, void *data), + void *data) { + + int r; + sqlite3_stmt *stmt; + char query[QUERY_MAXLEN]; + const unsigned char *ct; + + r = __rua_stat_init(SQLITE_OPEN_READONLY); + if (r == -1) { + LOGE("__rua_stat_init fail"); + return -1; + } + + sqlite3_snprintf(QUERY_MAXLEN, query, + "SELECT rua_stat_tag FROM rua_panel_stat WHERE caller_panel = ? ORDER BY score DESC"); + + if (!_db) + return -1; + + r = sqlite3_prepare(_db, query, sizeof(query), &stmt, NULL); + if (r != SQLITE_OK) { + LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(_db), sqlite3_errmsg(_db)); + goto out; + } + + r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC); + if (r != SQLITE_OK) { + LOGE("caller bind error(%d) \n", r); + goto out; + } + + while(sqlite3_step(stmt) == SQLITE_ROW) { + + ct = sqlite3_column_text(stmt, 0); + if (ct == NULL || ct[0] == '\0') { + LOGW("sqlite3_column_text null"); + } + rua_stat_tag_iter_fn(ct, data); + } + +out: + if (stmt) + sqlite3_finalize(stmt); + + __rua_stat_fini(); + + return r; +} + +int __rua_stat_init(int flags) +{ + + if (_db) { + return 0; + } + + char defname[FILENAME_MAX]; + const char *rua_stat_db_path = tzplatform_getenv(TZ_USER_DB); + snprintf(defname, sizeof(defname), "%s/%s", rua_stat_db_path, RUA_STAT_DB_NAME); + _db = __db_init(defname, flags); + + if (_db == NULL) { + LOGW("__rua_stat_init error"); + return -1; + } + + return 0; +} + +int __rua_stat_fini(void) +{ + + if (_db) { + db_util_close(_db); + _db = NULL; + } + + return 0; +} + +static int __exec(sqlite3 *db, char *query) +{ + int r; + char *errmsg = NULL; + + if (db == NULL) + return -1; + + r = sqlite3_exec(db, query, NULL, NULL, &errmsg); + if (r != SQLITE_OK) { + SECURE_LOGE("query(%s) exec error(%s)", query, errmsg); + sqlite3_free(errmsg); + return -1; + } + + return 0; +} + +static int __create_table(sqlite3 *db) +{ + int r; + + r = __exec(db, CREATE_RUA_STAT_TABLE); + if (r == -1) { + LOGE("create table error"); + return -1; + } + + return 0; +} + +static sqlite3 *__db_init(char *root, int flags) +{ + int r; + sqlite3 *db = NULL; + + r = db_util_open_with_options(root, &db, flags, NULL); + if (r) { + LOGE("db util open error(%d/%d/%d/%s)", r, + sqlite3_errcode(db), + sqlite3_extended_errcode(db), + sqlite3_errmsg(db)); + return NULL; + + } + r = __create_table(db); + if (r) { + db_util_close(db); + return NULL; + } + + return db; +} -- 2.7.4