2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
25 ctx::contact_log_aggregator::contact_log_aggregator()
29 timer_id = timer_manager::set_at(3, 0, timer_manager::EVERYDAY, this, NULL);
32 ctx::contact_log_aggregator::~contact_log_aggregator()
34 timer_manager::remove(timer_id);
37 void ctx::contact_log_aggregator::create_table()
39 static bool done = false;
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);
48 bool ctx::contact_log_aggregator::on_timer_expired(int timer, void* user_data)
50 aggregate_contact_log();
54 void ctx::contact_log_aggregator::aggregate_contact_log()
56 db_manager::execute(0,
57 "SELECT IFNULL(MAX(" STATS_UNIV_TIME "),0) AS " STATS_LAST_TIME \
58 " FROM " SOCIAL_TABLE_CONTACT_LOG, this);
61 void ctx::contact_log_aggregator::on_query_result_received(unsigned int query_id, int error, std::vector<json>& records)
63 IF_FAIL_VOID_TAG(!records.empty(), _E, "Invalid query result");
66 records[0].get(NULL, STATS_LAST_TIME, &last_time);
67 _D("Last Comm Time: %d", last_time);
69 scope_zone_joiner sz(zone_util::default_zone());
71 contacts_list_h list = NULL;
73 get_updated_contact_log_list(last_time, &list);
76 insert_contact_log_list(list);
77 destroy_contact_log_list(list);
80 void ctx::contact_log_aggregator::get_updated_contact_log_list(int last_time, contacts_list_h *list)
82 contacts_filter_h filter = NULL;
83 contacts_query_h query = NULL;
85 int err = contacts_connect();
86 IF_FAIL_VOID_TAG(err == CONTACTS_ERROR_NONE, _E, "contacts_connect() failed");
88 err = contacts_filter_create(_contacts_phone_log._uri, &filter);
89 IF_FAIL_CATCH_TAG(err == CONTACTS_ERROR_NONE, _E, "contacts_filter_create() failed");
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);
98 err = contacts_query_create(_contacts_phone_log._uri, &query);
99 IF_FAIL_CATCH_TAG(err == CONTACTS_ERROR_NONE, _E, "contacts_query_create() failed");
101 contacts_query_set_filter(query, filter);
102 contacts_query_set_sort(query, _contacts_phone_log.log_time, true);
104 contacts_db_get_records_with_query(query, 0, 0, list);
108 contacts_filter_destroy(filter);
110 contacts_query_destroy(query);
113 void ctx::contact_log_aggregator::destroy_contact_log_list(contacts_list_h list)
116 contacts_list_destroy(list, true);
118 contacts_disconnect();
121 void ctx::contact_log_aggregator::insert_contact_log_list(contacts_list_h list)
123 IF_FAIL_VOID(contacts_list_first(list) == CONTACTS_ERROR_NONE);
126 contacts_record_h record = NULL;
127 contacts_list_get_current_record_p(list, &record);
128 if (record == NULL) break;
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);
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);
147 /* TODO: Local Time */
149 db_manager::insert(0, SOCIAL_TABLE_CONTACT_LOG, data, NULL);
151 } while(contacts_list_next(list) == CONTACTS_ERROR_NONE);