2 * Copyright (c) 2016 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.
18 * This file implements statistics entity handler methods.
20 * @file table-statistics.c
24 #include "table-statistics.h"
25 #include "db-internal.h"
27 /* DELETE statements */
28 #define DELETE_ALL "delete from statistics where time_stamp between ? and ?"
30 #define DELETE_APP "delete from statistics where binpath=? and " \
31 "time_stamp between ? and ? "
33 #define DELETE_IFACE "delete from statistics where iftype=? and " \
34 "time_stamp between ? and ?"
36 #define DELETE_APP_IFACE "delete from statistics where binpath=? and " \
37 "iftype=? and time_stamp between ? and ?"
39 #define DELETE_FIRST_BY_NUMBER "delete from statistics where time_stamp in " \
40 "(select time_stamp from statistics desc limit ?)"
42 /* SELECT statements */
43 #define SELECT_FOR_PERIOD "select binpath, hw_net_protocol_type, " \
44 "is_roaming, sum(received) as received, " \
45 "sum(sent) as sent, imsi, ground, iftype, ifname from statistics " \
46 "where time_stamp between ? and ? " \
47 "group by binpath, is_roaming, imsi order by received desc"
49 #define SELECT_FOR_PERIOD_IFACE "select binpath, hw_net_protocol_type, " \
50 "is_roaming, sum(received) as received, " \
51 "sum(sent) as sent, imsi, ground, iftype, ifname from statistics " \
52 "where time_stamp between ? and ? " \
53 "and iftype=? group by binpath, is_roaming, imsi order by received desc"
55 #define SELECT_CHUNKS "select binpath, hw_net_protocol_type, " \
56 "is_roaming, sum(received) as received, " \
57 "sum(sent) as sent, imsi, ground, iftype, ifname, " \
58 "time_stamp - time_stamp % ? as timestamp " \
59 "from statistics where time_stamp between ? and ? " \
60 "group by binpath, timestamp, imsi order by timestamp"
62 #define SELECT_CHUNKS_IFACE "select binpath, hw_net_protocol_type, " \
63 "is_roaming, sum(received) as received, " \
64 "sum(sent) as sent, imsi, ground, iftype, ifname, " \
65 "time_stamp - time_stamp % ? as timestamp " \
66 "from statistics where time_stamp between ? and ? and iftype=?" \
67 "group by binpath, timestamp, imsi order by timestamp"
69 #define SELECT_APP_DETAILS "select iftype, hw_net_protocol_type, " \
70 "is_roaming, sum(received) as received, sum(sent) as sent, " \
71 "ifname, imsi, ground from statistics " \
72 "where time_stamp between ? and ? and binpath=? " \
73 "group by binpath, iftype, ifname, imsi, hw_net_protocol_type, " \
75 "order by time_stamp, binpath, iftype, ifname, imsi, " \
76 "hw_net_protocol_type, is_roaming"
78 #define SELECT_APP_DETAILS_IFACE "select iftype, hw_net_protocol_type, " \
79 "is_roaming, sum(received) as received, sum(sent) as sent, " \
80 "ifname, imsi, ground from statistics " \
81 "where time_stamp between ? and ? and binpath=? and iftype=?" \
82 "group by hw_net_protocol_type, is_roaming, iftype, ifname, imsi " \
83 "order by time_stamp, hw_net_protocol_type, is_roaming, iftype, " \
86 #define SELECT_CHUNKS_APP "select iftype, hw_net_protocol_type, " \
87 "is_roaming, sum(received) as received, sum(sent) as sent, " \
88 "ifname, imsi, ground, time_stamp - time_stamp % ? as time_stamp " \
90 "group by iftype, ifname, time_stamp, hw_net_protocol_type, is_roaming " \
91 "order by time_stamp, iftype, ifname, hw_net_protocol_type, is_roaming"
93 #define SELECT_CHUNKS_APP_IFACE "select iftype, hw_net_protocol_type, " \
94 "is_roaming, sum(received) as received, sum(sent) as sent, " \
95 "ifname, imsi, ground, time_stamp - time_stamp % ? as time_stamp " \
96 "from statistics where time_stamp between ? and ? and binpath = ? " \
98 "group by time_stamp, hw_net_protocol_type, is_roaming, " \
99 "iftype, ifname, imsi " \
100 "order by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \
103 #define SELECT_TOTAL "select iftype, hw_net_protocol_type, " \
104 "is_roaming, sum(received) as received, sum(sent) as sent, " \
105 "ifname, imsi, ground from statistics " \
106 " where (time_stamp between ? and ?) and binpath != 'TOTAL_DATACALL' " \
107 "and binpath != 'TOTAL_WIFI' and binpath != 'TOTAL_BLUETOOTH' " \
108 "and binpath != 'TOTAL_IPV4' and binpath != 'TOTAL_IPV6' " \
109 "group by iftype, ifname, imsi, hw_net_protocol_type, is_roaming " \
110 "order by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \
113 #define SELECT_TOTAL_IFACE "select iftype, hw_net_protocol_type, " \
114 "is_roaming, sum(received) as received, sum(sent) as sent, " \
115 "ifname, imsi, ground from statistics " \
116 " where (time_stamp between ? and ?) " \
118 "group by hw_net_protocol_type, is_roaming, " \
119 "iftype, ifname, imsi " \
120 "order by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \
123 #define SELECT_CHUNKS_TOTAL "select iftype, hw_net_protocol_type, " \
124 "is_roaming, sum(received) as received, sum(sent) as sent, " \
125 "ifname, imsi, ground, time_stamp - time_stamp % ? as time_stamp " \
126 "from statistics where time_stamp between ? and ? " \
127 "group by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \
129 "order by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \
132 #define SELECT_CHUNKS_TOTAL_IFACE "select iftype, hw_net_protocol_type, " \
133 "is_roaming, sum(received) as received, sum(sent) as sent, " \
134 "ifname, imsi, ground, time_stamp - time_stamp % ? as time_stamp " \
135 "from statistics where time_stamp between ? and ? " \
137 "group by time_stamp, hw_net_protocol_type, is_roaming, iftype, ifname, imsi " \
138 "order by time_stamp, hw_net_protocol_type, is_roaming, iftype, " \
141 /* INSERT statement */
142 #define INSERT_VALUES "insert into statistics " \
143 "(binpath, received, sent, time_stamp, " \
144 "iftype, is_roaming, hw_net_protocol_type, " \
145 "ifname, imsi, ground) " \
146 "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
148 static void __finalize_delete(void);
150 #define PREPARE_DELETE(stm, query) do { \
151 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
152 if (rc != SQLITE_OK) { \
154 __finalize_delete(); \
155 STC_LOGE("Failed to prepare \"%s\"query" \
161 static void __finalize_select(void);
163 #define PREPARE_SELECT(stm, query) do { \
164 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
165 if (rc != SQLITE_OK) { \
167 __finalize_select(); \
168 STC_LOGE("Failed to prepare \"%s\"query" \
174 static void __finalize_insert(void);
176 #define PREPARE_INSERT(stm, query) do { \
177 rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
178 if (rc != SQLITE_OK) { \
180 __finalize_insert(); \
181 STC_LOGE("Failed to prepare \"%s\"query" \
187 #define FINALIZE(stm) do { \
189 sqlite3_finalize(stm); \
194 /* DELETE statements */
195 /* the following array is strictly ordered
196 * to find required statement the following code will be used:
197 * (app ? 1 : 0) | (iftype ? 2 : 0)
199 static sqlite3_stmt *delete_query[5];
201 /* SELECT statements */
202 static sqlite3_stmt *select_for_period;
203 static sqlite3_stmt *select_for_period_iface;
204 static sqlite3_stmt *select_chunks;
205 static sqlite3_stmt *select_chunks_iface;
206 static sqlite3_stmt *select_app_details;
207 static sqlite3_stmt *select_app_details_iface;
208 static sqlite3_stmt *select_chunks_app;
209 static sqlite3_stmt *select_chunks_app_iface;
210 static sqlite3_stmt *select_total;
211 static sqlite3_stmt *select_total_iface;
212 static sqlite3_stmt *select_chunks_total;
213 static sqlite3_stmt *select_chunks_total_iface;
215 /* INSERT statements */
216 static sqlite3_stmt *update_statistics_query;
218 static int __prepare_delete(sqlite3 *db)
220 __STC_LOG_FUNC_ENTER__;
222 static int initialized;
225 __STC_LOG_FUNC_EXIT__;
229 PREPARE_DELETE(delete_query[0], DELETE_ALL);
230 PREPARE_DELETE(delete_query[1], DELETE_APP);
231 PREPARE_DELETE(delete_query[2], DELETE_IFACE);
232 PREPARE_DELETE(delete_query[3], DELETE_APP_IFACE);
233 PREPARE_DELETE(delete_query[4], DELETE_FIRST_BY_NUMBER);
236 __STC_LOG_FUNC_EXIT__;
240 static void __finalize_delete(void)
242 __STC_LOG_FUNC_ENTER__;
245 for (i = 0; i < sizeof(delete_query) / sizeof(*delete_query); i++)
246 FINALIZE(delete_query[i]);
248 __STC_LOG_FUNC_EXIT__;
251 static int __prepare_select(sqlite3 *db)
253 __STC_LOG_FUNC_ENTER__;
255 static int initialized;
258 __STC_LOG_FUNC_EXIT__;
262 PREPARE_SELECT(select_for_period, SELECT_FOR_PERIOD);
263 PREPARE_SELECT(select_for_period_iface, SELECT_FOR_PERIOD_IFACE);
264 PREPARE_SELECT(select_chunks, SELECT_CHUNKS);
265 PREPARE_SELECT(select_chunks_iface, SELECT_CHUNKS_IFACE);
266 PREPARE_SELECT(select_app_details, SELECT_APP_DETAILS);
267 PREPARE_SELECT(select_app_details_iface, SELECT_APP_DETAILS_IFACE);
268 PREPARE_SELECT(select_chunks_app, SELECT_CHUNKS_APP);
269 PREPARE_SELECT(select_chunks_app_iface, SELECT_CHUNKS_APP_IFACE);
270 PREPARE_SELECT(select_total, SELECT_TOTAL);
271 PREPARE_SELECT(select_total_iface, SELECT_TOTAL_IFACE);
272 PREPARE_SELECT(select_chunks_total, SELECT_CHUNKS_TOTAL);
273 PREPARE_SELECT(select_chunks_total_iface, SELECT_CHUNKS_TOTAL_IFACE);
276 __STC_LOG_FUNC_EXIT__;
280 static void __finalize_select(void)
282 __STC_LOG_FUNC_ENTER__;
284 FINALIZE(select_for_period);
285 FINALIZE(select_for_period_iface);
286 FINALIZE(select_chunks);
287 FINALIZE(select_chunks_iface);
288 FINALIZE(select_app_details);
289 FINALIZE(select_app_details_iface);
290 FINALIZE(select_chunks_app);
291 FINALIZE(select_chunks_app_iface);
292 FINALIZE(select_total);
293 FINALIZE(select_total_iface);
294 FINALIZE(select_chunks_total);
295 FINALIZE(select_chunks_total_iface);
297 __STC_LOG_FUNC_EXIT__;
300 static int __prepare_insert(sqlite3 *db)
302 __STC_LOG_FUNC_ENTER__;
304 static int initialized;
307 __STC_LOG_FUNC_EXIT__;
311 PREPARE_INSERT(update_statistics_query, INSERT_VALUES);
314 __STC_LOG_FUNC_EXIT__;
318 static void __finalize_insert(void)
320 __STC_LOG_FUNC_ENTER__;
322 FINALIZE(update_statistics_query);
324 __STC_LOG_FUNC_EXIT__;
327 static int __is_iftype_defined(const stc_iface_type_e iftype)
329 __STC_LOG_FUNC_ENTER__;
330 __STC_LOG_FUNC_EXIT__;
331 return iftype < STC_IFACE_LAST_ELEM &&
332 iftype > STC_IFACE_UNKNOWN &&
333 iftype != STC_IFACE_ALL;
336 /* the following array is strictly ordered
337 * to find required statement the following code will be used:
338 * (iface ? 1 : 0) | (total ? 2 : 0) | (chunks ? 4 : 0)
340 static sqlite3_stmt **details_stms[] = {
342 &select_app_details_iface,
346 &select_chunks_app_iface,
347 &select_chunks_total,
348 &select_chunks_total_iface
351 static sqlite3_stmt *__select_statement(const char *app_id,
352 const table_statistics_select_rule *rule)
354 __STC_LOG_FUNC_ENTER__;
355 const int stm_index = __is_iftype_defined(rule->iftype) |
356 ((strlen(app_id) > 0) ? 0 : 2) | (rule->granularity ? 4 : 0);
357 STC_LOGD("stm index %d", stm_index);
358 __STC_LOG_FUNC_EXIT__;
359 return *details_stms[stm_index];
362 stc_error_e table_statistics_reset_first_n_entries(int num)
364 __STC_LOG_FUNC_ENTER__;
365 stc_error_e error_code = STC_ERROR_NONE;
368 STC_LOGE("Invalid number of entries");
369 return STC_ERROR_INVALID_PARAMETER;
372 DB_ACTION(sqlite3_bind_int(delete_query[4], 1, num));
374 if (sqlite3_step(delete_query[4]) != SQLITE_DONE) {
375 STC_LOGE("Failed to drop collected statistics.");
376 error_code = STC_ERROR_DB_FAILED;
377 __STC_LOG_FUNC_EXIT__;
380 sqlite3_reset(delete_query[4]);
384 stc_error_e table_statistics_reset(const table_statistics_reset_rule *rule)
386 __STC_LOG_FUNC_ENTER__;
388 stc_error_e error_code = STC_ERROR_NONE;
389 int pos = 1;/* running through positions where to
390 bind parameters in the query */
392 if (!rule || !rule->interval) {
393 __STC_LOG_FUNC_EXIT__;
394 return STC_ERROR_INVALID_PARAMETER;
397 /* pick a statement depending on parameters.
398 See comment for delete_query */
399 stmt = delete_query[(rule->app_id ? 1 : 0) |
400 (rule->iftype != STC_IFACE_UNKNOWN &&
401 rule->iftype != STC_IFACE_LAST_ELEM ? 2 : 0)];
404 DB_ACTION(sqlite3_bind_text(stmt, pos++, rule->app_id, -1,
407 if (rule->iftype != STC_IFACE_LAST_ELEM &&
408 rule->iftype != STC_IFACE_UNKNOWN)
409 DB_ACTION(sqlite3_bind_int(stmt, pos++, rule->iftype));
411 DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->interval->from));
412 DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->interval->to));
414 if (sqlite3_step(stmt) != SQLITE_DONE) {
415 STC_LOGE("Failed to drop collected statistics.");
416 error_code = STC_ERROR_DB_FAILED;
417 __STC_LOG_FUNC_EXIT__;
421 STC_LOGD("Entry deleted successfully.");
427 stc_error_e table_statistics_foreach_app(const table_statistics_select_rule *rule,
428 table_statistics_info_cb info_cb,
431 __STC_LOG_FUNC_ENTER__;
432 table_statistics_info data;
434 stc_error_e error_code = STC_ERROR_NONE;
436 int pos = 1;/* running through positions where to
437 bind parameters in the query */
438 stc_db_tm_interval_s interval;
440 memset(&data, 0, sizeof(data));
442 if (!rule || !info_cb) {
443 __STC_LOG_FUNC_EXIT__;
444 return STC_ERROR_INVALID_PARAMETER;
447 /* pick a statement depending on parameters */
448 if (rule->granularity) {
449 stmt = __is_iftype_defined(rule->iftype) ?
450 select_chunks_iface : select_chunks;
452 DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->granularity));
453 data.interval = &interval;
455 stmt = __is_iftype_defined(rule->iftype)
456 ? select_for_period_iface : select_for_period;
459 DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->from));
460 DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->to));
462 if (__is_iftype_defined(rule->iftype)) {
463 data.iftype = rule->iftype;
464 DB_ACTION(sqlite3_bind_int(stmt, pos++, rule->iftype));
468 rc = sqlite3_step(stmt);
471 data.app_id = (char *)sqlite3_column_text(stmt, 0);
472 data.hw_net_protocol_type = sqlite3_column_int(stmt, 1);
473 data.roaming = sqlite3_column_int(stmt, 2);
474 data.cnt.in_bytes = sqlite3_column_int64(stmt, 3);
475 data.cnt.out_bytes = sqlite3_column_int64(stmt, 4);
476 data.imsi = (char *)sqlite3_column_text(stmt, 5);
477 data.ground = sqlite3_column_int(stmt, 6);
478 data.iftype = sqlite3_column_int(stmt, 7);
479 data.ifname = (char *)sqlite3_column_text(stmt, 8);
481 if (rule->granularity) {
482 interval.from = sqlite3_column_int64(stmt, 9);
483 interval.to = interval.from + rule->granularity;
486 if (info_cb(&data, user_data) == STC_CANCEL)
487 rc = SQLITE_DONE;/* emulate end of data */
488 __STC_LOG_FUNC_EXIT__;
491 __STC_LOG_FUNC_EXIT__;
495 error_code = STC_ERROR_DB_FAILED;
496 __STC_LOG_FUNC_EXIT__;
499 } while (rc == SQLITE_ROW);
503 __STC_LOG_FUNC_EXIT__;
507 stc_error_e table_statistics_per_app(const char *app_id,
508 table_statistics_select_rule *rule,
509 table_statistics_info_cb info_cb,
512 __STC_LOG_FUNC_ENTER__;
513 table_statistics_info data;
515 stc_error_e error_code = STC_ERROR_NONE;
517 int pos = 1;/* running through positions
518 where to bind parameters in the query */
519 stc_db_tm_interval_s interval;
521 memset(&data, 0, sizeof(data));
523 if (!rule || !info_cb) {
524 __STC_LOG_FUNC_EXIT__;
525 return STC_ERROR_INVALID_PARAMETER;
528 /* pick a statement depending on parameters.
529 See comment for details_stms */
530 stmt = __select_statement(app_id, rule);
532 if (rule->granularity) {
533 DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->granularity));
534 data.interval = &interval;
537 DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->from));
538 DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->to));
540 if (strlen(app_id) > 0) {
541 DB_ACTION(sqlite3_bind_text(stmt, pos++, app_id, -1,
543 data.app_id = (char *)app_id;
546 if (__is_iftype_defined(rule->iftype))
547 DB_ACTION(sqlite3_bind_int(stmt, pos++, rule->iftype));
550 rc = sqlite3_step(stmt);
553 data.iftype = sqlite3_column_int(stmt, 0);
554 data.hw_net_protocol_type = sqlite3_column_int(stmt, 1);
555 data.roaming = sqlite3_column_int(stmt, 2);
556 data.cnt.in_bytes = sqlite3_column_int64(stmt, 3);
557 data.cnt.out_bytes = sqlite3_column_int64(stmt, 4);
558 data.ifname = (char *)sqlite3_column_text(stmt, 5);
559 data.imsi = (char *)sqlite3_column_text(stmt, 6);
560 data.ground = sqlite3_column_int(stmt, 7);
562 if (rule->granularity) {
563 interval.from = sqlite3_column_int64(stmt, 8);
564 interval.to = interval.from + rule->granularity;
567 if (info_cb(&data, user_data) == STC_CANCEL)
568 rc = SQLITE_DONE; /* emulate end of data */
569 __STC_LOG_FUNC_EXIT__;
572 __STC_LOG_FUNC_EXIT__;
576 error_code = STC_ERROR_DB_FAILED;
577 __STC_LOG_FUNC_EXIT__;
580 } while (rc == SQLITE_ROW);
584 __STC_LOG_FUNC_EXIT__;
588 stc_error_e table_statistics_insert(stc_db_classid_iftype_key *stat_key,
589 stc_db_app_stats *stat,
590 time_t last_touch_time)
592 stc_error_e error_code = STC_ERROR_NONE;
593 sqlite3_stmt *stmt = update_statistics_query;
594 stc_hw_net_protocol_type_e hw_net_protocol_type = STC_PROTOCOL_NONE;
596 if (!stat->rcv_count && !stat->snd_count) {
597 error_code = STC_ERROR_INVALID_PARAMETER;
601 DB_ACTION(sqlite3_bind_text(stmt, 1, stat->app_id, -1,
603 DB_ACTION(sqlite3_bind_int(stmt, 2, stat->rcv_count));
604 DB_ACTION(sqlite3_bind_int(stmt, 3, stat->snd_count));
605 DB_ACTION(sqlite3_bind_int64(stmt, 4,
606 (sqlite3_int64)(last_touch_time)));
607 DB_ACTION(sqlite3_bind_int(stmt, 5, (int)(stat_key->iftype)));
608 DB_ACTION(sqlite3_bind_int(stmt, 6, (int)(stat->is_roaming)));
609 DB_ACTION(sqlite3_bind_int(stmt, 7, (int)hw_net_protocol_type));
610 DB_ACTION(sqlite3_bind_text(stmt, 8, stat_key->ifname, -1,
612 DB_ACTION(sqlite3_bind_text(stmt, 9,
613 stat_key->imsi ? stat_key->imsi : "" , -1,
615 DB_ACTION(sqlite3_bind_int(stmt, 10, (int)stat->ground));
617 /*we want to reuse tree*/
620 if (sqlite3_step(stmt) != SQLITE_DONE) {
621 STC_LOGE("Failed to record appstat. %s",
622 sqlite3_errmsg(stc_db_get_database()));
623 error_code = STC_ERROR_DB_FAILED;
624 __STC_LOG_FUNC_EXIT__;
628 STC_LOGD("App stat recorded [%s]", stat->app_id);
636 * This function will be somewhere consumer and will not be placed in this file.
639 stc_error_e table_statistics_store_result(app_stat_tree *stats)
643 pthread_rwlock_rdlock(&stats->guard);
644 WALK_TREE(stats->tree, print_appstat);
645 pthread_rwlock_unlock(&stats->guard);
648 stats->last_touch_time = current_time;
650 /* it's reader only, we don't modify tree, don't reduce it,
651 * due we want to reuse it in next iteration */
652 pthread_rwlock_rdlock(&stats->guard);
653 g_tree_foreach((GTree *) stats->tree, __store_application_stat,
654 &stats->last_touch_time);
656 pthread_rwlock_unlock(&stats->guard);
658 change_db_entries_num_num(g_tree_nnodes((GTree *)stats->tree));
660 return STC_ERROR_NONE;
664 stc_error_e table_statistics_prepare(sqlite3 *db)
666 __STC_LOG_FUNC_ENTER__;
668 stc_error_e error_code = STC_ERROR_NONE;
671 __STC_LOG_FUNC_EXIT__;
672 return STC_ERROR_DB_FAILED;
675 DB_ACTION(__prepare_delete(db));
676 DB_ACTION(__prepare_select(db));
677 DB_ACTION(__prepare_insert(db));
681 __STC_LOG_FUNC_EXIT__;
685 void table_statistics_finalize(void)
687 __STC_LOG_FUNC_ENTER__;
691 __STC_LOG_FUNC_EXIT__;