Fix 'frequency' statistics for the trigger to emit proper results
[platform/core/context/statistics-context-provider.git] / src / media / db_handle.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 <sstream>
18 #include <types_internal.h>
19 #include <context_mgr.h>
20 #include <db_mgr.h>
21 #include <system_info.h>
22 #include "media_stats_types.h"
23 #include "db_handle.h"
24
25 ctx::media_db_handle::media_db_handle(const char* zone)
26         : stats_db_handle_base(zone)
27 {
28 }
29
30 ctx::media_db_handle::~media_db_handle()
31 {
32 }
33
34 int ctx::media_db_handle::read(const char* subject, ctx::json filter)
35 {
36         //TODO: filter validation (in the API side?)
37         std::string query;
38
39         if (STR_EQ(subject, MEDIA_SUBJ_PEAK_TIME_FOR_MUSIC)) {
40                 query = create_sql_peak_time(MEDIA_TYPE_MUSIC, filter);
41
42         } else if (STR_EQ(subject, MEDIA_SUBJ_PEAK_TIME_FOR_VIDEO)) {
43                 query = create_sql_peak_time(MEDIA_TYPE_VIDEO, filter);
44
45         } else if (STR_EQ(subject, MEDIA_SUBJ_COMMON_SETTING_FOR_MUSIC)) {
46                 query = create_sql_common_setting(MEDIA_TYPE_MUSIC, filter);
47
48         } else if (STR_EQ(subject, MEDIA_SUBJ_COMMON_SETTING_FOR_VIDEO)) {
49                 query = create_sql_common_setting(MEDIA_TYPE_VIDEO, filter);
50
51         } else if (STR_EQ(subject, MEDIA_SUBJ_MUSIC_FREQUENCY)) {
52                 is_trigger_item = true;
53                 query = create_sql_frequency(MEDIA_TYPE_MUSIC, filter);
54
55         } else if (STR_EQ(subject, MEDIA_SUBJ_VIDEO_FREQUENCY)) {
56                 is_trigger_item = true;
57                 query = create_sql_frequency(MEDIA_TYPE_VIDEO, filter);
58         }
59
60         IF_FAIL_RETURN(!query.empty(), ERR_OPERATION_FAILED);
61
62         bool ret = execute_query(subject, filter, query.c_str());
63         IF_FAIL_RETURN(ret, ERR_OPERATION_FAILED);
64
65         return ERR_NONE;
66 }
67
68 std::string ctx::media_db_handle::create_where_clause(int media_type, ctx::json filter)
69 {
70         std::stringstream where_clause;
71
72         where_clause << CX_MEDIA_TYPE " = " << media_type << " AND ";
73         where_clause << stats_db_handle_base::create_where_clause(filter);
74
75         return where_clause.str();
76 }
77
78 std::string ctx::media_db_handle::create_sql_peak_time(int media_type, ctx::json filter)
79 {
80         std::string where = create_where_clause(media_type, filter);
81         return stats_db_handle_base::create_sql_peak_time(filter, MEDIA_TABLE_NAME, where);
82 }
83
84 std::string ctx::media_db_handle::create_sql_common_setting(int media_type, ctx::json filter)
85 {
86         std::string where = create_where_clause(media_type, filter);
87         return stats_db_handle_base::create_sql_common_setting(filter, MEDIA_TABLE_NAME, where);
88 }
89
90 std::string ctx::media_db_handle::create_sql_frequency(int media_type, ctx::json filter)
91 {
92         ctx::json filter_cleaned;
93         std::string week_str;
94         std::string time_of_day;
95
96         if (filter.get(NULL, STATS_DAY_OF_WEEK, &week_str))
97                 filter_cleaned.set(NULL, STATS_DAY_OF_WEEK, week_str);
98
99         if (filter.get(NULL, STATS_TIME_OF_DAY, &time_of_day))
100                 filter_cleaned.set(NULL, STATS_TIME_OF_DAY, time_of_day);
101
102         std::string where_clause = create_where_clause(media_type, filter_cleaned);
103
104         std::stringstream query;
105         query <<
106                 "SELECT IFNULL(COUNT(*),0) AS " STATS_TOTAL_COUNT \
107                 " FROM " MEDIA_TABLE_NAME \
108                 " WHERE " << where_clause;
109
110         return query.str();
111 }