Fix 'frequency' statistics for the trigger to emit proper results
[platform/core/context/statistics-context-provider.git] / src / social / log_aggregator.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 <json.h>
18 #include <db_mgr.h>
19 #include <timer_mgr.h>
20 #include <zone_util.h>
21 #include <types_internal.h>
22 #include "social_stats_types.h"
23 #include "log_aggregator.h"
24
25 ctx::contact_log_aggregator::contact_log_aggregator()
26         : timer_id(-1)
27 {
28         create_table();
29         timer_id = timer_manager::set_at(3, 0, timer_manager::EVERYDAY, this, NULL);
30 }
31
32 ctx::contact_log_aggregator::~contact_log_aggregator()
33 {
34         timer_manager::remove(timer_id);
35 }
36
37 void ctx::contact_log_aggregator::create_table()
38 {
39         static bool done = false;
40         IF_FAIL_VOID(!done);
41
42         db_manager::create_table(0, SOCIAL_TABLE_CONTACT_LOG, SOCIAL_TABLE_CONTACT_LOG_COLUMNS, NULL, NULL);
43         db_manager::execute(0, SOCIAL_TEMP_CONTACT_FREQ_SQL, NULL);
44
45         done = true;
46 }
47
48 bool ctx::contact_log_aggregator::on_timer_expired(int timer, void* user_data)
49 {
50         aggregate_contact_log();
51         return true;
52 }
53
54 void ctx::contact_log_aggregator::aggregate_contact_log()
55 {
56         db_manager::execute(0,
57                         "SELECT IFNULL(MAX(" STATS_UNIV_TIME "),0) AS " STATS_LAST_TIME \
58                         " FROM " SOCIAL_TABLE_CONTACT_LOG, this);
59 }
60
61 void ctx::contact_log_aggregator::on_query_result_received(unsigned int query_id, int error, std::vector<json>& records)
62 {
63         IF_FAIL_VOID_TAG(!records.empty(), _E, "Invalid query result");
64
65         int last_time = 0;
66         records[0].get(NULL, STATS_LAST_TIME, &last_time);
67         _D("Last Comm Time: %d", last_time);
68
69         scope_zone_joiner sz(zone_util::default_zone());
70
71         contacts_list_h list = NULL;
72
73         get_updated_contact_log_list(last_time, &list);
74         IF_FAIL_VOID(list);
75
76         insert_contact_log_list(list);
77         destroy_contact_log_list(list);
78 }
79
80 void ctx::contact_log_aggregator::get_updated_contact_log_list(int last_time, contacts_list_h *list)
81 {
82         contacts_filter_h filter = NULL;
83         contacts_query_h query = NULL;
84
85         int err = contacts_connect();
86         IF_FAIL_VOID_TAG(err == CONTACTS_ERROR_NONE, _E, "contacts_connect() failed");
87
88         err = contacts_filter_create(_contacts_phone_log._uri, &filter);
89         IF_FAIL_CATCH_TAG(err == CONTACTS_ERROR_NONE, _E, "contacts_filter_create() failed");
90
91         contacts_filter_add_int(filter, _contacts_phone_log.log_type, CONTACTS_MATCH_GREATER_THAN_OR_EQUAL, CONTACTS_PLOG_TYPE_VOICE_INCOMMING);
92         contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_AND);
93         contacts_filter_add_int(filter, _contacts_phone_log.log_type, CONTACTS_MATCH_LESS_THAN_OR_EQUAL, CONTACTS_PLOG_TYPE_MMS_BLOCKED);
94         contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_AND);
95         contacts_filter_add_int(filter, _contacts_phone_log.log_time , CONTACTS_MATCH_GREATER_THAN, last_time);
96         contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_AND);
97
98         err = contacts_query_create(_contacts_phone_log._uri, &query);
99         IF_FAIL_CATCH_TAG(err == CONTACTS_ERROR_NONE, _E, "contacts_query_create() failed");
100
101         contacts_query_set_filter(query, filter);
102         contacts_query_set_sort(query, _contacts_phone_log.log_time, true);
103
104         contacts_db_get_records_with_query(query, 0, 0, list);
105
106 CATCH:
107         if (filter)
108                 contacts_filter_destroy(filter);
109         if (query)
110                 contacts_query_destroy(query);
111 }
112
113 void ctx::contact_log_aggregator::destroy_contact_log_list(contacts_list_h list)
114 {
115         if (list)
116                 contacts_list_destroy(list, true);
117
118         contacts_disconnect();
119 }
120
121 void ctx::contact_log_aggregator::insert_contact_log_list(contacts_list_h list)
122 {
123         IF_FAIL_VOID(contacts_list_first(list) == CONTACTS_ERROR_NONE);
124
125         do {
126                 contacts_record_h record = NULL;
127                 contacts_list_get_current_record_p(list, &record);
128                 if (record == NULL) break;
129
130                 ctx::json data;
131
132                 char* address;
133                 int log_type;
134                 int duration = 0;
135                 int accesstime = 0;
136
137                 contacts_record_get_str_p(record, _contacts_phone_log.address, &address);
138                 contacts_record_get_int(record, _contacts_phone_log.log_type, &log_type);
139                 contacts_record_get_int(record, _contacts_phone_log.extra_data1, &duration);
140                 contacts_record_get_int(record, _contacts_phone_log.log_time, &accesstime);
141
142                 data.set(NULL, SOCIAL_ADDRESS, address);
143                 data.set(NULL, SOCIAL_PHONE_LOG_TYPE, log_type);
144                 data.set(NULL, STATS_DURATION, duration);
145                 data.set(NULL, STATS_UNIV_TIME, accesstime);
146
147                 /* TODO: Local Time */
148
149                 db_manager::insert(0, SOCIAL_TABLE_CONTACT_LOG, data, NULL);
150
151         } while(contacts_list_next(list) == CONTACTS_ERROR_NONE);
152 }