Fix 'frequency' statistics for the trigger to emit proper results
[platform/core/context/statistics-context-provider.git] / src / app / db_init.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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 #include <sstream>
18 #include <json.h>
19 #include <types_internal.h>
20 #include <db_mgr.h>
21 #include <zone_util.h>
22 #include "app_stats_types.h"
23 #include "db_init.h"
24
25 #define EMPTY_CHECKER_QID 999
26
27 ctx::app_db_initializer::app_db_initializer()
28 {
29         create_table();
30         check_app_list();
31 }
32
33 ctx::app_db_initializer::~app_db_initializer()
34 {
35 }
36
37 void ctx::app_db_initializer::create_table()
38 {
39         static bool done = false;
40         IF_FAIL_VOID(!done);
41
42         db_manager::create_table(0, APP_TABLE_USAGE_LOG, APP_TABLE_USAGE_LOG_COLUMNS, NULL, NULL);
43         db_manager::create_table(0, APP_TABLE_REMOVABLE_APP, APP_TABLE_REMOVABLE_APP_COLUMNS, NULL, NULL);
44         db_manager::execute(0, APP_TEMP_USAGE_FREQ_SQL, NULL);
45
46         done = true;
47 }
48
49 void ctx::app_db_initializer::check_app_list()
50 {
51         db_manager::execute(EMPTY_CHECKER_QID, "SELECT * FROM " APP_TABLE_REMOVABLE_APP " LIMIT 1", this);
52 }
53
54 void ctx::app_db_initializer::duplicate_app_list()
55 {
56         int err;
57         package_manager_filter_h filter;
58
59         scope_zone_joiner sz(zone_util::default_zone());
60
61         err = package_manager_filter_create(&filter);
62         IF_FAIL_VOID_TAG(err == PACKAGE_MANAGER_ERROR_NONE, _E, "package_manager_filter_create() failed");
63
64         err = package_manager_filter_add_bool(filter, PACKAGE_MANAGER_PKGINFO_PROP_REMOVABLE, true);
65         IF_FAIL_CATCH_TAG(err == PACKAGE_MANAGER_ERROR_NONE, _E, "package_manager_filter_add_bool() failed");
66
67         err = package_manager_filter_foreach_package_info(filter, package_info_cb, this);
68         IF_FAIL_CATCH_TAG(err == PACKAGE_MANAGER_ERROR_NONE, _E, "package_manager_filter_foreach_package_info() failed");
69
70 CATCH:
71         if (filter)
72                 package_manager_filter_destroy(filter);
73 }
74
75 bool ctx::app_db_initializer::package_info_cb(package_info_h package_info, void *user_data)
76 {
77         int err = package_info_foreach_app_from_package(package_info, PACKAGE_INFO_UIAPP, app_info_cb, user_data);
78         IF_FAIL_RETURN_TAG(err == PACKAGE_MANAGER_ERROR_NONE, false, _E, "package_info_foreach_app_from_package() failed");
79         return true;
80 }
81
82 bool ctx::app_db_initializer::app_info_cb(package_info_app_component_type_e comp_type, const char *app_id, void *user_data)
83 {
84         json data;
85         data.set(NULL, STATS_APP_ID, app_id);
86         return db_manager::insert(0, APP_TABLE_REMOVABLE_APP, data, NULL);
87 }
88
89 void ctx::app_db_initializer::on_creation_result_received(unsigned int query_id, int error)
90 {
91 }
92
93 void ctx::app_db_initializer::on_insertion_result_received(unsigned int query_id, int error, int64_t row_id)
94 {
95 }
96
97 void ctx::app_db_initializer::on_query_result_received(unsigned int query_id, int error, std::vector<json>& records)
98 {
99         if (query_id != EMPTY_CHECKER_QID) {
100                 _E("Unknown Query ID: %d", query_id);
101                 delete this;
102                 return;
103         }
104
105         if (records.empty())
106                 duplicate_app_list();
107
108         delete this;
109 }