Ignoring values of TOTAL_* details when fetching interface data.
[platform/core/connectivity/stc-manager.git] / src / database / tables / table-statistics.c
1 /*
2  * Copyright (c) 2016 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 /**
18  * This file implements statistics entity handler methods.
19  *
20  * @file        table-statistics.c
21  */
22
23 #include "stc-db.h"
24 #include "table-statistics.h"
25 #include "db-internal.h"
26
27 /* DELETE statements */
28 #define DELETE_ALL "delete from statistics where time_stamp between ? and ?"
29
30 #define DELETE_APP "delete from statistics where binpath=? and " \
31         "time_stamp between ? and ? "
32
33 #define DELETE_IFACE "delete from statistics where iftype=? and " \
34         "time_stamp between ? and ?"
35
36 #define DELETE_APP_IFACE "delete from statistics where binpath=? and " \
37         "iftype=? and time_stamp between ? and ?"
38
39 #define DELETE_FIRST_BY_NUMBER "delete from statistics where time_stamp in " \
40         "(select time_stamp from statistics desc limit ?)"
41
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"
48
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"
54
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"
61
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"
68
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, " \
74         "is_roaming " \
75         "order by time_stamp, binpath, iftype, ifname, imsi, " \
76         "hw_net_protocol_type, is_roaming"
77
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, " \
84         "ifname, imsi"
85
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 " \
89         "from statistics " \
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"
92
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 = ? " \
97         "and iftype = ? " \
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, " \
101         "is_roaming"
102
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 ?) " \
107         "and binpath NOT LIKE 'TOTAL_%' " \
108         "group by iftype, ifname, imsi, hw_net_protocol_type, is_roaming " \
109         "order by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \
110         "is_roaming"
111
112 #define SELECT_TOTAL_IFACE "select iftype, hw_net_protocol_type, " \
113         "is_roaming, sum(received) as received, sum(sent) as sent, " \
114         "ifname, imsi, ground from statistics " \
115         "where (time_stamp between ? and ?) and iftype=? " \
116         "and binpath NOT LIKE 'TOTAL_%' " \
117         "group by hw_net_protocol_type, is_roaming, " \
118         "iftype, ifname, imsi " \
119         "order by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \
120         "is_roaming"
121
122 #define SELECT_CHUNKS_TOTAL "select iftype, hw_net_protocol_type, " \
123         "is_roaming, sum(received) as received, sum(sent) as sent, " \
124         "ifname, imsi, ground, time_stamp - time_stamp % ? as time_stamp " \
125         "from statistics where time_stamp between ? and ? " \
126         "and binpath NOT LIKE 'TOTAL_%' " \
127         "group by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \
128         "is_roaming " \
129         "order by time_stamp, iftype, ifname, imsi, hw_net_protocol_type, " \
130         "is_roaming"
131
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 ? " \
136         "and iftype = ? " \
137         "and binpath NOT LIKE 'TOTAL_%' " \
138         "group by time_stamp, hw_net_protocol_type, is_roaming, iftype, ifname, imsi " \
139         "order by time_stamp, hw_net_protocol_type, is_roaming, iftype, " \
140         "ifname, imsi"
141
142 /* INSERT statement */
143 #define INSERT_VALUES "insert into statistics " \
144         "(binpath, received, sent, time_stamp, " \
145         "iftype, is_roaming, hw_net_protocol_type, " \
146         "ifname, imsi, ground) " \
147         "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
148
149 static void __finalize_delete(void);
150
151 #define PREPARE_DELETE(stm, query) do {                         \
152         rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL);     \
153         if (rc != SQLITE_OK) {                                  \
154                 stm = NULL;                                     \
155                 __finalize_delete();                            \
156                 STC_LOGE("Failed to prepare \"%s\"query"        \
157                          , query);                              \
158                 return rc;                                      \
159         }                                                       \
160 } while (0)
161
162 static void __finalize_select(void);
163
164 #define PREPARE_SELECT(stm, query) do {                         \
165         rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL);     \
166         if (rc != SQLITE_OK) {                                  \
167                 stm = NULL;                                     \
168                 __finalize_select();                            \
169                 STC_LOGE("Failed to prepare \"%s\"query"        \
170                          , query);                              \
171                 return rc;                                      \
172         }                                                       \
173 } while (0)
174
175 static void __finalize_insert(void);
176
177 #define PREPARE_INSERT(stm, query) do {                         \
178         rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL);     \
179         if (rc != SQLITE_OK) {                                  \
180                 stm = NULL;                                     \
181                 __finalize_insert();                            \
182                 STC_LOGE("Failed to prepare \"%s\"query"        \
183                          , query);                              \
184                 return rc;                                      \
185         }                                                       \
186 } while (0)
187
188 #define FINALIZE(stm) do {                                      \
189         if (stm) {                                              \
190                 sqlite3_finalize(stm);                          \
191                 stm = NULL;                                     \
192         }                                                       \
193 } while (0)
194
195 /* DELETE statements */
196 /* the following array is strictly ordered
197  * to find required statement the following code will be used:
198  * (app ? 1 : 0) | (iftype ? 2 : 0)
199  */
200 static sqlite3_stmt *delete_query[5];
201
202 /* SELECT statements */
203 static sqlite3_stmt *select_for_period;
204 static sqlite3_stmt *select_for_period_iface;
205 static sqlite3_stmt *select_chunks;
206 static sqlite3_stmt *select_chunks_iface;
207 static sqlite3_stmt *select_app_details;
208 static sqlite3_stmt *select_app_details_iface;
209 static sqlite3_stmt *select_chunks_app;
210 static sqlite3_stmt *select_chunks_app_iface;
211 static sqlite3_stmt *select_total;
212 static sqlite3_stmt *select_total_iface;
213 static sqlite3_stmt *select_chunks_total;
214 static sqlite3_stmt *select_chunks_total_iface;
215
216 /* INSERT statements */
217 static sqlite3_stmt *update_statistics_query;
218
219 static int __prepare_delete(sqlite3 *db)
220 {
221         __STC_LOG_FUNC_ENTER__;
222         int rc;
223         static int initialized;
224
225         if (initialized) {
226                 __STC_LOG_FUNC_EXIT__;
227                 return SQLITE_OK;
228         }
229
230         PREPARE_DELETE(delete_query[0], DELETE_ALL);
231         PREPARE_DELETE(delete_query[1], DELETE_APP);
232         PREPARE_DELETE(delete_query[2], DELETE_IFACE);
233         PREPARE_DELETE(delete_query[3], DELETE_APP_IFACE);
234         PREPARE_DELETE(delete_query[4], DELETE_FIRST_BY_NUMBER);
235
236         initialized = 1;
237         __STC_LOG_FUNC_EXIT__;
238         return rc;
239 }
240
241 static void __finalize_delete(void)
242 {
243         __STC_LOG_FUNC_ENTER__;
244
245         unsigned int i;
246         for (i = 0; i < sizeof(delete_query) / sizeof(*delete_query); i++)
247                 FINALIZE(delete_query[i]);
248
249         __STC_LOG_FUNC_EXIT__;
250 }
251
252 static int __prepare_select(sqlite3 *db)
253 {
254         __STC_LOG_FUNC_ENTER__;
255         int rc;
256         static int initialized;
257
258         if (initialized) {
259                 __STC_LOG_FUNC_EXIT__;
260                 return SQLITE_OK;
261         }
262
263         PREPARE_SELECT(select_for_period, SELECT_FOR_PERIOD);
264         PREPARE_SELECT(select_for_period_iface, SELECT_FOR_PERIOD_IFACE);
265         PREPARE_SELECT(select_chunks, SELECT_CHUNKS);
266         PREPARE_SELECT(select_chunks_iface, SELECT_CHUNKS_IFACE);
267         PREPARE_SELECT(select_app_details, SELECT_APP_DETAILS);
268         PREPARE_SELECT(select_app_details_iface, SELECT_APP_DETAILS_IFACE);
269         PREPARE_SELECT(select_chunks_app, SELECT_CHUNKS_APP);
270         PREPARE_SELECT(select_chunks_app_iface, SELECT_CHUNKS_APP_IFACE);
271         PREPARE_SELECT(select_total, SELECT_TOTAL);
272         PREPARE_SELECT(select_total_iface, SELECT_TOTAL_IFACE);
273         PREPARE_SELECT(select_chunks_total, SELECT_CHUNKS_TOTAL);
274         PREPARE_SELECT(select_chunks_total_iface, SELECT_CHUNKS_TOTAL_IFACE);
275
276         initialized = 1;
277         __STC_LOG_FUNC_EXIT__;
278         return rc;
279 }
280
281 static void __finalize_select(void)
282 {
283         __STC_LOG_FUNC_ENTER__;
284
285         FINALIZE(select_for_period);
286         FINALIZE(select_for_period_iface);
287         FINALIZE(select_chunks);
288         FINALIZE(select_chunks_iface);
289         FINALIZE(select_app_details);
290         FINALIZE(select_app_details_iface);
291         FINALIZE(select_chunks_app);
292         FINALIZE(select_chunks_app_iface);
293         FINALIZE(select_total);
294         FINALIZE(select_total_iface);
295         FINALIZE(select_chunks_total);
296         FINALIZE(select_chunks_total_iface);
297
298         __STC_LOG_FUNC_EXIT__;
299 }
300
301 static int __prepare_insert(sqlite3 *db)
302 {
303         __STC_LOG_FUNC_ENTER__;
304         int rc;
305         static int initialized;
306
307         if (initialized) {
308                 __STC_LOG_FUNC_EXIT__;
309                 return SQLITE_OK;
310         }
311
312         PREPARE_INSERT(update_statistics_query, INSERT_VALUES);
313
314         initialized = 1;
315         __STC_LOG_FUNC_EXIT__;
316         return rc;
317 }
318
319 static void __finalize_insert(void)
320 {
321         __STC_LOG_FUNC_ENTER__;
322
323         FINALIZE(update_statistics_query);
324
325         __STC_LOG_FUNC_EXIT__;
326 }
327
328 static int __is_iftype_defined(const stc_iface_type_e iftype)
329 {
330         __STC_LOG_FUNC_ENTER__;
331         __STC_LOG_FUNC_EXIT__;
332         return iftype < STC_IFACE_LAST_ELEM &&
333                 iftype > STC_IFACE_UNKNOWN &&
334                 iftype != STC_IFACE_ALL;
335 }
336
337 /* the following array is strictly ordered
338  * to find required statement the following code will be used:
339  * (iface ? 1 : 0) | (total ? 2 : 0) | (chunks ? 4 : 0)
340  */
341 static sqlite3_stmt **details_stms[] = {
342         &select_app_details,
343         &select_app_details_iface,
344         &select_total,
345         &select_total_iface,
346         &select_chunks_app,
347         &select_chunks_app_iface,
348         &select_chunks_total,
349         &select_chunks_total_iface
350 };
351
352 static sqlite3_stmt *__select_statement(const char *app_id,
353                                         const table_statistics_select_rule *rule)
354 {
355         __STC_LOG_FUNC_ENTER__;
356         const int stm_index = __is_iftype_defined(rule->iftype) |
357                 ((strlen(app_id) > 0) ? 0 : 2) | (rule->granularity ? 4 : 0);
358         STC_LOGD("stm index %d", stm_index);
359         __STC_LOG_FUNC_EXIT__;
360         return *details_stms[stm_index];
361 }
362
363 stc_error_e table_statistics_reset_first_n_entries(int num)
364 {
365         __STC_LOG_FUNC_ENTER__;
366         stc_error_e error_code = STC_ERROR_NONE;
367
368         if (!num) {
369                 STC_LOGE("Invalid number of entries");
370                 return STC_ERROR_INVALID_PARAMETER;
371         }
372
373         DB_ACTION(sqlite3_bind_int(delete_query[4], 1, num));
374
375         if (sqlite3_step(delete_query[4]) != SQLITE_DONE) {
376                 STC_LOGE("Failed to drop collected statistics.");
377                 error_code = STC_ERROR_DB_FAILED;
378                 __STC_LOG_FUNC_EXIT__;
379         }
380 handle_error:
381         sqlite3_reset(delete_query[4]);
382         return error_code;
383 }
384
385 stc_error_e table_statistics_reset(const table_statistics_reset_rule *rule)
386 {
387         __STC_LOG_FUNC_ENTER__;
388         sqlite3_stmt *stmt;
389         stc_error_e error_code = STC_ERROR_NONE;
390         int pos = 1;/* running through positions where to
391                        bind parameters in the query */
392
393         if (!rule || !rule->interval) {
394                 __STC_LOG_FUNC_EXIT__;
395                 return STC_ERROR_INVALID_PARAMETER;
396         }
397
398         /* pick a statement depending on parameters.
399            See comment for delete_query */
400         stmt = delete_query[(rule->app_id ? 1 : 0) |
401                 (rule->iftype != STC_IFACE_UNKNOWN &&
402                 rule->iftype != STC_IFACE_LAST_ELEM ? 2 : 0)];
403
404         if (rule->app_id)
405                 DB_ACTION(sqlite3_bind_text(stmt, pos++, rule->app_id, -1,
406                                             SQLITE_TRANSIENT));
407
408         if (rule->iftype != STC_IFACE_LAST_ELEM &&
409                 rule->iftype != STC_IFACE_UNKNOWN)
410                 DB_ACTION(sqlite3_bind_int(stmt, pos++, rule->iftype));
411
412         DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->interval->from));
413         DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->interval->to));
414
415         if (sqlite3_step(stmt) != SQLITE_DONE) {
416                 STC_LOGE("Failed to drop collected statistics.");
417                 error_code = STC_ERROR_DB_FAILED;
418                 __STC_LOG_FUNC_EXIT__;
419                 goto handle_error;
420         }
421
422         STC_LOGD("Entry deleted successfully.");
423 handle_error:
424         sqlite3_reset(stmt);
425         return error_code;
426 }
427
428 stc_error_e table_statistics_foreach_app(const table_statistics_select_rule *rule,
429                                          table_statistics_info_cb info_cb,
430                                          void *user_data)
431 {
432         __STC_LOG_FUNC_ENTER__;
433         table_statistics_info data;
434         sqlite3_stmt *stmt;
435         stc_error_e error_code = STC_ERROR_NONE;
436         int rc;
437         int pos = 1;/* running through positions where to
438                        bind parameters in the query */
439         stc_db_tm_interval_s interval;
440
441         memset(&data, 0, sizeof(data));
442
443         if (!rule || !info_cb) {
444                 __STC_LOG_FUNC_EXIT__;
445                 return STC_ERROR_INVALID_PARAMETER;
446         }
447
448         /* pick a statement depending on parameters */
449         if (rule->granularity) {
450                 stmt = __is_iftype_defined(rule->iftype) ?
451                         select_chunks_iface : select_chunks;
452
453                 DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->granularity));
454                 data.interval = &interval;
455         } else {
456                 stmt = __is_iftype_defined(rule->iftype)
457                         ? select_for_period_iface : select_for_period;
458         }
459
460         DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->from));
461         DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->to));
462
463         if (__is_iftype_defined(rule->iftype)) {
464                 data.iftype = rule->iftype;
465                 DB_ACTION(sqlite3_bind_int(stmt, pos++, rule->iftype));
466         }
467
468         do {
469                 rc = sqlite3_step(stmt);
470                 switch (rc) {
471                 case SQLITE_ROW:
472                         data.app_id = (char *)sqlite3_column_text(stmt, 0);
473                         data.hw_net_protocol_type = sqlite3_column_int(stmt, 1);
474                         data.roaming = sqlite3_column_int(stmt, 2);
475                         data.cnt.in_bytes = sqlite3_column_int64(stmt, 3);
476                         data.cnt.out_bytes = sqlite3_column_int64(stmt, 4);
477                         data.imsi = (char *)sqlite3_column_text(stmt, 5);
478                         data.ground = sqlite3_column_int(stmt, 6);
479                         data.iftype = sqlite3_column_int(stmt, 7);
480                         data.ifname = (char *)sqlite3_column_text(stmt, 8);
481
482                         if (rule->granularity) {
483                                 interval.from = sqlite3_column_int64(stmt, 9);
484                                 interval.to = interval.from + rule->granularity;
485                         }
486
487                         if (info_cb(&data, user_data) == STC_CANCEL)
488                                 rc = SQLITE_DONE;/* emulate end of data */
489                         __STC_LOG_FUNC_EXIT__;
490                         break;
491                 case SQLITE_DONE:
492                         __STC_LOG_FUNC_EXIT__;
493                         break;
494                 case SQLITE_ERROR:
495                 default:
496                         error_code = STC_ERROR_DB_FAILED;
497                         __STC_LOG_FUNC_EXIT__;
498                         break;
499                 }
500         } while (rc == SQLITE_ROW);
501
502 handle_error:
503         sqlite3_reset(stmt);
504         __STC_LOG_FUNC_EXIT__;
505         return error_code;
506 }
507
508 stc_error_e table_statistics_per_app(const char *app_id,
509                                      table_statistics_select_rule *rule,
510                                      table_statistics_info_cb info_cb,
511                                      void *user_data)
512 {
513         __STC_LOG_FUNC_ENTER__;
514         table_statistics_info data;
515         sqlite3_stmt *stmt;
516         stc_error_e error_code = STC_ERROR_NONE;
517         int rc;
518         int pos = 1;/* running through positions
519                        where to bind parameters in the query */
520         stc_db_tm_interval_s interval;
521
522         memset(&data, 0, sizeof(data));
523
524         if (!rule || !info_cb) {
525                 __STC_LOG_FUNC_EXIT__;
526                 return STC_ERROR_INVALID_PARAMETER;
527         }
528
529         /* pick a statement depending on parameters.
530            See comment for details_stms */
531         stmt = __select_statement(app_id, rule);
532
533         if (rule->granularity) {
534                 DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->granularity));
535                 data.interval = &interval;
536         }
537
538         DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->from));
539         DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->to));
540
541         if (strlen(app_id) > 0) {
542                 DB_ACTION(sqlite3_bind_text(stmt, pos++, app_id, -1,
543                                             SQLITE_TRANSIENT));
544                 data.app_id = (char *)app_id;
545         }
546
547         if (__is_iftype_defined(rule->iftype))
548                 DB_ACTION(sqlite3_bind_int(stmt, pos++, rule->iftype));
549
550         do {
551                 rc = sqlite3_step(stmt);
552                 switch (rc) {
553                 case SQLITE_ROW:
554                         data.iftype = sqlite3_column_int(stmt, 0);
555                         data.hw_net_protocol_type = sqlite3_column_int(stmt, 1);
556                         data.roaming = sqlite3_column_int(stmt, 2);
557                         data.cnt.in_bytes = sqlite3_column_int64(stmt, 3);
558                         data.cnt.out_bytes = sqlite3_column_int64(stmt, 4);
559                         data.ifname = (char *)sqlite3_column_text(stmt, 5);
560                         data.imsi = (char *)sqlite3_column_text(stmt, 6);
561                         data.ground = sqlite3_column_int(stmt, 7);
562
563                         if (rule->granularity) {
564                                 interval.from = sqlite3_column_int64(stmt, 8);
565                                 interval.to = interval.from + rule->granularity;
566                         }
567
568                         if (info_cb(&data, user_data) == STC_CANCEL)
569                                 rc = SQLITE_DONE; /* emulate end of data */
570                         __STC_LOG_FUNC_EXIT__;
571                         break;
572                 case SQLITE_DONE:
573                         __STC_LOG_FUNC_EXIT__;
574                         break;
575                 case SQLITE_ERROR:
576                 default:
577                         error_code = STC_ERROR_DB_FAILED;
578                         __STC_LOG_FUNC_EXIT__;
579                         break;
580                 }
581         } while (rc == SQLITE_ROW);
582
583 handle_error:
584         sqlite3_reset(stmt);
585         __STC_LOG_FUNC_EXIT__;
586         return error_code;
587 }
588
589 stc_error_e table_statistics_insert(stc_db_classid_iftype_key *stat_key,
590                                     stc_db_app_stats *stat,
591                                     time_t last_touch_time)
592 {
593         stc_error_e error_code = STC_ERROR_NONE;
594         sqlite3_stmt *stmt = update_statistics_query;
595         stc_hw_net_protocol_type_e hw_net_protocol_type = STC_PROTOCOL_NONE;
596
597         if (!stat->rcv_count && !stat->snd_count) {
598                 error_code = STC_ERROR_INVALID_PARAMETER;
599                 goto handle_error;
600         }
601
602         DB_ACTION(sqlite3_bind_text(stmt, 1, stat->app_id, -1,
603                                     SQLITE_STATIC));
604         DB_ACTION(sqlite3_bind_int(stmt, 2, stat->rcv_count));
605         DB_ACTION(sqlite3_bind_int(stmt, 3, stat->snd_count));
606         DB_ACTION(sqlite3_bind_int64(stmt, 4,
607                                      (sqlite3_int64)(last_touch_time)));
608         DB_ACTION(sqlite3_bind_int(stmt, 5, (int)(stat_key->iftype)));
609         DB_ACTION(sqlite3_bind_int(stmt, 6, (int)(stat->is_roaming)));
610         DB_ACTION(sqlite3_bind_int(stmt, 7, (int)hw_net_protocol_type));
611         DB_ACTION(sqlite3_bind_text(stmt, 8, stat_key->ifname, -1,
612                                     SQLITE_STATIC));
613         DB_ACTION(sqlite3_bind_text(stmt, 9,
614                                     stat_key->imsi ? stat_key->imsi : "" , -1,
615                                     SQLITE_STATIC));
616         DB_ACTION(sqlite3_bind_int(stmt, 10, (int)stat->ground));
617
618         /*we want to reuse tree*/
619         stat->rcv_count = 0;
620         stat->snd_count = 0;
621         if (sqlite3_step(stmt) != SQLITE_DONE) {
622                 STC_LOGE("Failed to record appstat. %s",
623                          sqlite3_errmsg(stc_db_get_database()));
624                 error_code = STC_ERROR_DB_FAILED;
625                 __STC_LOG_FUNC_EXIT__;
626                 goto handle_error;
627         }
628
629         STC_LOGD("App stat recorded [%s]", stat->app_id);
630
631 handle_error:
632         sqlite3_reset(stmt);
633         return error_code;
634 }
635
636 /**
637  * This function will be somewhere consumer and will not be placed in this file.
638  */
639 #if 0
640 stc_error_e table_statistics_store_result(app_stat_tree *stats)
641 {
642         time_t current_time;
643
644         pthread_rwlock_rdlock(&stats->guard);
645         WALK_TREE(stats->tree, print_appstat);
646         pthread_rwlock_unlock(&stats->guard);
647
648         time(&current_time);
649         stats->last_touch_time = current_time;
650
651         /* it's reader only, we don't modify tree, don't reduce it,
652          *              due we want to reuse it in next iteration */
653         pthread_rwlock_rdlock(&stats->guard);
654         g_tree_foreach((GTree *) stats->tree, __store_application_stat,
655                        &stats->last_touch_time);
656
657         pthread_rwlock_unlock(&stats->guard);
658         flush_quota_table();
659         change_db_entries_num_num(g_tree_nnodes((GTree *)stats->tree));
660
661         return STC_ERROR_NONE;
662 }
663 #endif
664
665 stc_error_e table_statistics_prepare(sqlite3 *db)
666 {
667         __STC_LOG_FUNC_ENTER__;
668
669         stc_error_e error_code = STC_ERROR_NONE;
670
671         if (db == NULL) {
672                 __STC_LOG_FUNC_EXIT__;
673                 return STC_ERROR_DB_FAILED;
674         }
675
676         DB_ACTION(__prepare_delete(db));
677         DB_ACTION(__prepare_select(db));
678         DB_ACTION(__prepare_insert(db));
679
680 handle_error:
681
682         __STC_LOG_FUNC_EXIT__;
683         return error_code;
684 }
685
686 void table_statistics_finalize(void)
687 {
688         __STC_LOG_FUNC_ENTER__;
689         __finalize_delete();
690         __finalize_select();
691         __finalize_insert();
692         __STC_LOG_FUNC_EXIT__;
693 }