Modify database query appropriately
[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, subscriber_id, ground, iftype, ifname from statistics " \
46         "where time_stamp between ? and ? " \
47         "group by binpath, is_roaming, subscriber_id 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, subscriber_id, ground, iftype, ifname from statistics " \
52         "where time_stamp between ? and ? " \
53         "and iftype=? group by binpath, is_roaming, subscriber_id 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, subscriber_id, ground, iftype, ifname, " \
58         "time_stamp - time_stamp % ? as timestamp " \
59         "from statistics where time_stamp between ? and ? " \
60         "group by binpath, timestamp, subscriber_id 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, subscriber_id, 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, subscriber_id 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, subscriber_id, ground from statistics " \
72         "where time_stamp between ? and ? and binpath=? " \
73         "group by binpath, iftype, ifname, subscriber_id, hw_net_protocol_type, " \
74         "is_roaming " \
75         "order by time_stamp, binpath, iftype, ifname, subscriber_id, " \
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, subscriber_id, ground from statistics " \
81         "where time_stamp between ? and ? and binpath=? and iftype=?" \
82         "group by hw_net_protocol_type, is_roaming, iftype, ifname, subscriber_id " \
83         "order by time_stamp, hw_net_protocol_type, is_roaming, iftype, " \
84         "ifname, subscriber_id"
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, subscriber_id, ground, time_stamp - time_stamp % ? as timestamp " \
89         "from statistics " \
90         "group by iftype, ifname, timestamp, hw_net_protocol_type, is_roaming  " \
91         "order by timestamp, 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, subscriber_id, ground, time_stamp - time_stamp % ? as timestamp " \
96         "from statistics where time_stamp between ? and ? and binpath = ? " \
97         "and iftype = ? " \
98         "group by timestamp, hw_net_protocol_type, is_roaming, " \
99         "iftype, ifname, subscriber_id " \
100         "order by timestamp, iftype, ifname, subscriber_id, 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, subscriber_id, ground from statistics " \
106         "where (time_stamp between ? and ?) " \
107         "and binpath NOT LIKE 'TOTAL_%' " \
108         "group by iftype, ifname, subscriber_id, hw_net_protocol_type, is_roaming " \
109         "order by time_stamp, iftype, ifname, subscriber_id, 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, subscriber_id, 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, subscriber_id " \
119         "order by time_stamp, iftype, ifname, subscriber_id, 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, subscriber_id, ground, time_stamp - time_stamp % ? as timestamp " \
125         "from statistics where time_stamp between ? and ? " \
126         "and binpath NOT LIKE 'TOTAL_%' " \
127         "group by timestamp, iftype, ifname, subscriber_id, hw_net_protocol_type, " \
128         "is_roaming " \
129         "order by timestamp, iftype, ifname, subscriber_id, 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, subscriber_id, ground, time_stamp - time_stamp % ? as timestamp " \
135         "from statistics where time_stamp between ? and ? " \
136         "and iftype = ? " \
137         "and binpath NOT LIKE 'TOTAL_%' " \
138         "group by timestamp, hw_net_protocol_type, is_roaming, iftype, ifname, subscriber_id " \
139         "order by timestamp, hw_net_protocol_type, is_roaming, iftype, " \
140         "ifname, subscriber_id"
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, subscriber_id, 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         int rc;
222         static int initialized;
223
224         if (initialized)
225                 return SQLITE_OK;
226
227         PREPARE_DELETE(delete_query[0], DELETE_ALL);
228         PREPARE_DELETE(delete_query[1], DELETE_APP);
229         PREPARE_DELETE(delete_query[2], DELETE_IFACE);
230         PREPARE_DELETE(delete_query[3], DELETE_APP_IFACE);
231         PREPARE_DELETE(delete_query[4], DELETE_FIRST_BY_NUMBER);
232
233         initialized = 1;
234         return rc;
235 }
236
237 static void __finalize_delete(void)
238 {
239         unsigned int i;
240         for (i = 0; i < sizeof(delete_query) / sizeof(*delete_query); i++)
241                 FINALIZE(delete_query[i]);
242 }
243
244 static int __prepare_select(sqlite3 *db)
245 {
246         int rc;
247         static int initialized;
248
249         if (initialized)
250                 return SQLITE_OK;
251
252         PREPARE_SELECT(select_for_period, SELECT_FOR_PERIOD);
253         PREPARE_SELECT(select_for_period_iface, SELECT_FOR_PERIOD_IFACE);
254         PREPARE_SELECT(select_chunks, SELECT_CHUNKS);
255         PREPARE_SELECT(select_chunks_iface, SELECT_CHUNKS_IFACE);
256         PREPARE_SELECT(select_app_details, SELECT_APP_DETAILS);
257         PREPARE_SELECT(select_app_details_iface, SELECT_APP_DETAILS_IFACE);
258         PREPARE_SELECT(select_chunks_app, SELECT_CHUNKS_APP);
259         PREPARE_SELECT(select_chunks_app_iface, SELECT_CHUNKS_APP_IFACE);
260         PREPARE_SELECT(select_total, SELECT_TOTAL);
261         PREPARE_SELECT(select_total_iface, SELECT_TOTAL_IFACE);
262         PREPARE_SELECT(select_chunks_total, SELECT_CHUNKS_TOTAL);
263         PREPARE_SELECT(select_chunks_total_iface, SELECT_CHUNKS_TOTAL_IFACE);
264
265         initialized = 1;
266         return rc;
267 }
268
269 static void __finalize_select(void)
270 {
271         FINALIZE(select_for_period);
272         FINALIZE(select_for_period_iface);
273         FINALIZE(select_chunks);
274         FINALIZE(select_chunks_iface);
275         FINALIZE(select_app_details);
276         FINALIZE(select_app_details_iface);
277         FINALIZE(select_chunks_app);
278         FINALIZE(select_chunks_app_iface);
279         FINALIZE(select_total);
280         FINALIZE(select_total_iface);
281         FINALIZE(select_chunks_total);
282         FINALIZE(select_chunks_total_iface);
283 }
284
285 static int __prepare_insert(sqlite3 *db)
286 {
287         int rc;
288         static int initialized;
289
290         if (initialized)
291                 return SQLITE_OK;
292
293         PREPARE_INSERT(update_statistics_query, INSERT_VALUES);
294
295         initialized = 1;
296         return rc;
297 }
298
299 static void __finalize_insert(void)
300 {
301         FINALIZE(update_statistics_query);
302 }
303
304 static int __is_iftype_defined(const stc_iface_type_e iftype)
305 {
306         return iftype < STC_IFACE_LAST_ELEM &&
307                 iftype > STC_IFACE_UNKNOWN &&
308                 iftype != STC_IFACE_ALL;
309 }
310
311 /* the following array is strictly ordered
312  * to find required statement the following code will be used:
313  * (iface ? 1 : 0) | (total ? 2 : 0) | (chunks ? 4 : 0)
314  */
315 static sqlite3_stmt **details_stms[] = {
316         &select_app_details,
317         &select_app_details_iface,
318         &select_total,
319         &select_total_iface,
320         &select_chunks_app,
321         &select_chunks_app_iface,
322         &select_chunks_total,
323         &select_chunks_total_iface
324 };
325
326 static sqlite3_stmt *__select_statement(const char *app_id,
327                                         const table_statistics_select_rule *rule)
328 {
329         const int stm_index = __is_iftype_defined(rule->iftype) |
330                 ((strlen(app_id) > 0) ? 0 : 2) | (rule->granularity ? 4 : 0);
331         STC_LOGD("stm index %d", stm_index);
332         return *details_stms[stm_index];
333 }
334
335 //LCOV_EXCL_START
336 stc_error_e table_statistics_reset_first_n_entries(int num)
337 {
338         __STC_LOG_FUNC_ENTER__;
339         stc_error_e error_code = STC_ERROR_NONE;
340
341         if (!num) {
342                 STC_LOGE("Invalid number of entries");
343                 return STC_ERROR_INVALID_PARAMETER;
344         }
345
346         DB_ACTION(sqlite3_bind_int(delete_query[4], 1, num));
347
348         if (sqlite3_step(delete_query[4]) != SQLITE_DONE) {
349                 STC_LOGE("Failed to drop collected statistics.");
350                 error_code = STC_ERROR_DB_FAILED;
351                 __STC_LOG_FUNC_EXIT__;
352         }
353 handle_error:
354         if (sqlite3_reset(delete_query[4]) != SQLITE_OK)
355                 error_code = STC_ERROR_DB_FAILED;
356
357         return error_code;
358 }
359 //LCOV_EXCL_STOP
360
361 stc_error_e table_statistics_reset(const table_statistics_reset_rule *rule)
362 {
363         __STC_LOG_FUNC_ENTER__;
364         sqlite3_stmt *stmt;
365         stc_error_e error_code = STC_ERROR_NONE;
366         int pos = 1; /* running through positions where to
367                        bind parameters in the query */
368
369         if (!rule || !rule->interval) {
370                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
371                 return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
372         }
373
374         /* pick a statement depending on parameters.
375            See comment for delete_query */
376         stmt = delete_query[(rule->app_id ? 1 : 0) |
377                 (rule->iftype != STC_IFACE_UNKNOWN &&
378                 rule->iftype != STC_IFACE_LAST_ELEM ? 2 : 0)];
379
380         if (rule->app_id)
381                 DB_ACTION(sqlite3_bind_text(stmt, pos++, rule->app_id, -1,
382                                             SQLITE_TRANSIENT));
383
384         if (rule->iftype != STC_IFACE_LAST_ELEM &&
385                 rule->iftype != STC_IFACE_UNKNOWN)
386                 DB_ACTION(sqlite3_bind_int(stmt, pos++, rule->iftype));
387
388         DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->interval->from));
389         DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->interval->to));
390
391         if (sqlite3_step(stmt) != SQLITE_DONE) {
392                 STC_LOGE("Failed to drop collected statistics."); //LCOV_EXCL_LINE
393                 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
394                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
395                 goto handle_error; //LCOV_EXCL_LINE
396         }
397
398         STC_LOGD("Entry deleted successfully.");
399 handle_error:
400         if (sqlite3_reset(stmt) != SQLITE_OK)
401                 error_code = STC_ERROR_DB_FAILED;
402
403         return error_code;
404 }
405
406 stc_error_e table_statistics_foreach_app(const table_statistics_select_rule *rule,
407                                          table_statistics_info_cb info_cb,
408                                          void *user_data)
409 {
410         __STC_LOG_FUNC_ENTER__;
411         table_statistics_info data;
412         sqlite3_stmt *stmt;
413         stc_error_e error_code = STC_ERROR_NONE;
414         int rc;
415         int pos = 1;/* running through positions where to
416                        bind parameters in the query */
417         stc_db_tm_interval_s interval;
418
419         if (!rule || !info_cb) {
420                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
421                 return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
422         }
423
424         memset(&data, 0, sizeof(data));
425
426         if (rule->app_id) {
427                 int ret = table_statistics_per_app(rule->app_id, rule, info_cb, user_data);
428                 __STC_LOG_FUNC_EXIT__;
429                 return ret;
430         }
431
432         /* pick a statement depending on parameters */
433         if (rule->granularity) {
434                 stmt = __is_iftype_defined(rule->iftype) ?
435                         select_chunks_iface : select_chunks;
436
437                 DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->granularity));
438                 data.interval = &interval;
439         } else {
440                 stmt = __is_iftype_defined(rule->iftype)
441                         ? select_for_period_iface : select_for_period;
442         }
443
444         DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->from));
445         DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->to));
446
447         if (__is_iftype_defined(rule->iftype)) {
448                 data.iftype = rule->iftype;
449                 DB_ACTION(sqlite3_bind_int(stmt, pos++, rule->iftype));
450         }
451
452         do {
453                 rc = sqlite3_step(stmt);
454                 switch (rc) {
455                 case SQLITE_ROW:
456                         data.app_id = (char *)sqlite3_column_text(stmt, 0);
457                         data.hw_net_protocol_type = sqlite3_column_int(stmt, 1);
458                         data.roaming = sqlite3_column_int(stmt, 2);
459                         data.cnt.in_bytes = sqlite3_column_int64(stmt, 3);
460                         data.cnt.out_bytes = sqlite3_column_int64(stmt, 4);
461                         data.subscriber_id = (char *)sqlite3_column_text(stmt, 5);
462                         data.ground = sqlite3_column_int(stmt, 6);
463                         data.iftype = sqlite3_column_int(stmt, 7);
464                         data.ifname = (char *)sqlite3_column_text(stmt, 8);
465
466                         if (rule->granularity) {
467                                 interval.from = sqlite3_column_int64(stmt, 9);
468                                 interval.to = interval.from + rule->granularity;
469                         }
470
471                         if (info_cb(&data, user_data) == STC_CANCEL)
472                                 rc = SQLITE_DONE; //LCOV_EXCL_LINE
473                         break;
474                 case SQLITE_DONE:
475                         break;
476                 case SQLITE_ERROR:
477                 default:
478                         error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
479                         break; //LCOV_EXCL_LINE
480                 }
481         } while (rc == SQLITE_ROW);
482
483 handle_error:
484         rc = sqlite3_reset(stmt);
485         if (rc != SQLITE_OK)
486                 error_code = STC_ERROR_DB_FAILED;
487
488         __STC_LOG_FUNC_EXIT__;
489         return error_code;
490 }
491
492 API stc_error_e table_statistics_per_app(const char *app_id,
493                                      const table_statistics_select_rule *rule,
494                                      table_statistics_info_cb info_cb,
495                                      void *user_data)
496 {
497         __STC_LOG_FUNC_ENTER__;
498         table_statistics_info data;
499         sqlite3_stmt *stmt;
500         stc_error_e error_code = STC_ERROR_NONE;
501         int rc;
502         int pos = 1; /* running through positions
503                        where to bind parameters in the query */
504         stc_db_tm_interval_s interval;
505
506         memset(&data, 0, sizeof(data));
507
508         if (!rule || !info_cb) {
509                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
510                 return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
511         }
512
513         /* pick a statement depending on parameters.
514            See comment for details_stms */
515         stmt = __select_statement(app_id, rule);
516
517         if (rule->granularity) {
518                 DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->granularity));
519                 data.interval = &interval;
520         }
521
522         DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->from));
523         DB_ACTION(sqlite3_bind_int64(stmt, pos++, rule->to));
524
525         if (strlen(app_id) > 0) {
526                 DB_ACTION(sqlite3_bind_text(stmt, pos++, app_id, -1,
527                                             SQLITE_TRANSIENT));
528                 data.app_id = (char *)app_id;
529         }
530
531         if (__is_iftype_defined(rule->iftype))
532                 DB_ACTION(sqlite3_bind_int(stmt, pos++, rule->iftype));
533
534         do {
535                 rc = sqlite3_step(stmt);
536                 switch (rc) {
537                 case SQLITE_ROW:
538                         data.iftype = sqlite3_column_int(stmt, 0);
539                         data.hw_net_protocol_type = sqlite3_column_int(stmt, 1);
540                         data.roaming = sqlite3_column_int(stmt, 2);
541                         data.cnt.in_bytes = sqlite3_column_int64(stmt, 3);
542                         data.cnt.out_bytes = sqlite3_column_int64(stmt, 4);
543                         data.ifname = (char *)sqlite3_column_text(stmt, 5);
544                         data.subscriber_id = (char *)sqlite3_column_text(stmt, 6);
545                         data.ground = sqlite3_column_int(stmt, 7);
546
547                         if (rule->granularity) {
548                                 interval.from = sqlite3_column_int64(stmt, 8);
549                                 interval.to = interval.from + rule->granularity;
550                         }
551
552                         if (info_cb(&data, user_data) == STC_CANCEL)
553                                 rc = SQLITE_DONE; //LCOV_EXCL_LINE
554                         break;
555                 case SQLITE_DONE:
556                         break;
557                 case SQLITE_ERROR:
558                 default:
559                         error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
560                         break; //LCOV_EXCL_LINE
561                 }
562         } while (rc == SQLITE_ROW);
563
564 handle_error:
565         rc = sqlite3_reset(stmt);
566         if (rc != SQLITE_OK)
567                 error_code = STC_ERROR_DB_FAILED;
568
569         __STC_LOG_FUNC_EXIT__;
570         return error_code;
571 }
572
573 API stc_error_e table_statistics_insert(stc_db_classid_iftype_key *stat_key,
574                                     stc_db_app_stats *stat,
575                                     time_t last_touch_time)
576 {
577         stc_error_e error_code = STC_ERROR_NONE;
578         sqlite3_stmt *stmt = update_statistics_query;
579         stc_hw_net_protocol_type_e hw_net_protocol_type = STC_PROTOCOL_UNKNOWN;
580         long long int rcv;
581         long long int snd;
582
583         if (!stat->rcv_count && !stat->snd_count) {
584                 error_code = STC_ERROR_INVALID_PARAMETER;
585                 goto handle_error;
586         }
587
588         DB_ACTION(sqlite3_bind_text(stmt, 1, stat->app_id, -1,
589                                     SQLITE_STATIC));
590         DB_ACTION(sqlite3_bind_int(stmt, 2, stat->rcv_count));
591         DB_ACTION(sqlite3_bind_int(stmt, 3, stat->snd_count));
592         DB_ACTION(sqlite3_bind_int64(stmt, 4, (sqlite3_int64)last_touch_time));
593         DB_ACTION(sqlite3_bind_int(stmt, 5, (int)(stat_key->iftype)));
594         DB_ACTION(sqlite3_bind_int(stmt, 6, (int)(stat->is_roaming)));
595         DB_ACTION(sqlite3_bind_int(stmt, 7, (int)hw_net_protocol_type));
596         DB_ACTION(sqlite3_bind_text(stmt, 8, stat_key->ifname ? stat_key->ifname : "",
597                                         -1, SQLITE_STATIC));
598         DB_ACTION(sqlite3_bind_text(stmt, 9,
599                                     stat_key->subscriber_id ? stat_key->subscriber_id : "" ,
600                                     -1, SQLITE_STATIC));
601         DB_ACTION(sqlite3_bind_int(stmt, 10, (int)stat->ground));
602
603         /*we want to reuse tree*/
604         rcv = stat->rcv_count;
605         snd = stat->snd_count;
606         stat->rcv_count = 0;
607         stat->snd_count = 0;
608         if (sqlite3_step(stmt) != SQLITE_DONE) {
609                 STC_LOGE("Failed to record appstat. %s", //LCOV_EXCL_LINE
610                          sqlite3_errmsg(stc_db_get_database()));
611                 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
612                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
613                 goto handle_error; //LCOV_EXCL_LINE
614         }
615
616         if (STC_STAT_LOG)
617                 STC_LOGD("App stat recorded [\033[0;34m%s\033[0;m] "
618                         "rcv[%lld] snd[%lld]", stat->app_id, rcv, snd);
619
620 handle_error:
621         if (sqlite3_reset(stmt) != SQLITE_OK)
622                 error_code = STC_ERROR_DB_FAILED;
623
624         return error_code;
625 }
626
627 /**
628  * This function will be somewhere consumer and will not be placed in this file.
629  */
630 #if 0
631 stc_error_e table_statistics_store_result(app_stat_tree *stats)
632 {
633         time_t current_time;
634
635         pthread_rwlock_rdlock(&stats->guard);
636         WALK_TREE(stats->tree, print_appstat);
637         pthread_rwlock_unlock(&stats->guard);
638
639         time(&current_time);
640         stats->last_touch_time = current_time;
641
642         /* it's reader only, we don't modify tree, don't reduce it,
643          *              due we want to reuse it in next iteration */
644         pthread_rwlock_rdlock(&stats->guard);
645         g_tree_foreach((GTree *) stats->tree, __store_application_stat,
646                        &stats->last_touch_time);
647
648         pthread_rwlock_unlock(&stats->guard);
649         flush_quota_table();
650         change_db_entries_num_num(g_tree_nnodes((GTree *)stats->tree));
651
652         return STC_ERROR_NONE;
653 }
654 #endif
655
656 stc_error_e table_statistics_prepare(sqlite3 *db)
657 {
658         __STC_LOG_FUNC_ENTER__;
659
660         stc_error_e error_code = STC_ERROR_NONE;
661
662         if (db == NULL) {
663                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
664                 return STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
665         }
666
667         DB_ACTION(__prepare_delete(db));
668         DB_ACTION(__prepare_select(db));
669         DB_ACTION(__prepare_insert(db));
670
671 handle_error:
672
673         __STC_LOG_FUNC_EXIT__;
674         return error_code;
675 }
676
677 void table_statistics_finalize(void)
678 {
679         __STC_LOG_FUNC_ENTER__;
680         __finalize_delete();
681         __finalize_select();
682         __finalize_insert();
683         __STC_LOG_FUNC_EXIT__;
684 }