Applying C++ coding style to app inactive detector
[platform/core/context/context-provider.git] / src / app-stats / app_inactive_detector / InactiveDetectorStorage.cpp
index 052c3fe..7fd11a7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include "InactiveDetectorClassificator.h"
 #include "AppInactiveDetectorTypes.h"
 
-/*int ctx::inactive_detector_storage::create_table()
+/*int ctx::InactiveDetectorStorage::create_table()
 {
        bool ret = __dbManager.createTable(0, WIFI_TABLE_NAME, WIFI_CREATE_TABLE_COLUMNS, NULL, NULL);
        _D("Table Creation Request: %s", ret ? "SUCCESS" : "FAIL");
        return ret;
 }*/
 
-
-
-// expected Json format example: {timeframe: 1; is_active: 0}
-int ctx::inactive_detector_storage::read(
-                       const char *subject,
-                       ctx::Json filter)
+// expected Json format example: {timeframe: 1; isActive: 0}
+int ctx::InactiveDetectorStorage::read(const char *subject, ctx::Json filter)
 {
        std::string query;
-       query = query_get_apps(subject, filter);
+       query = __getQueryGetApps(subject, filter);
 
        IF_FAIL_RETURN(!query.empty(), ERR_OPERATION_FAILED);
 
-       bool ret = __dbManager.execute(
-               STR_EQ(subject, APP_INACTIVE_SUBJ_GET_APPS_INACTIVE) ?
-                       APP_INACTIVE_QUERY_ID_GET_APPS_INACTIVE :
-                       APP_INACTIVE_QUERY_ID_GET_APPS_ACTIVE,
-               query.c_str(),
-               this);
+       bool ret = __dbManager.execute(STR_EQ(subject, APP_INACTIVE_SUBJ_GET_APPS_INACTIVE) ?
+                       APP_INACTIVE_QUERY_ID_GET_APPS_INACTIVE : APP_INACTIVE_QUERY_ID_GET_APPS_ACTIVE,
+                       query.c_str(), this);
 
        IF_FAIL_RETURN(ret, ERR_OPERATION_FAILED);
 
        return ERR_NONE;
 }
 
-std::string ctx::inactive_detector_storage::query_get_apps(const char *subject, ctx::Json filter)
+std::string ctx::InactiveDetectorStorage::__getQueryGetApps(const char *subject, ctx::Json filter)
 {
        double timeframe;
-       int is_active;
+       int isActive;
 
        std::string query(GET_APP_INFO_INACTIVE_QUERY);
-       std::string placeholder_timeframe(APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_TIMEFRAME);
-       std::string placeholder_is_active(APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_CLUSTER);
+       std::string placeholderTimeframe(APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_TIMEFRAME);
+       std::string placeholderIsActive(APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_CLUSTER);
 
        filter.get(NULL, APP_INACTIVE_DETECTOR_DATA_TIMEFRAME, &timeframe);
-       filter.get(NULL, APP_INACTIVE_DETECTOR_DATA_ISACTIVE, &is_active);
+       filter.get(NULL, APP_INACTIVE_DETECTOR_DATA_ISACTIVE, &isActive);
 
-       std::stringstream timeframe_stream;
-       timeframe_stream << timeframe;
+       std::stringstream timeframeStream;
+       timeframeStream << timeframe;
 
-       std::stringstream is_active_stream;
-       is_active_stream << is_active;
+       std::stringstream isActiveStream;
+       isActiveStream << isActive;
 
-       inject_params(query, placeholder_timeframe, timeframe_stream.str());
-       inject_params(query, placeholder_is_active, is_active_stream.str());
+       __injectParams(query, placeholderTimeframe, timeframeStream.str());
+       __injectParams(query, placeholderIsActive, isActiveStream.str());
 
        return query;
 }
 
-std::string ctx::inactive_detector_storage::query_update_apps(std::vector<app_t> *apps_with_weights)
+std::string ctx::InactiveDetectorStorage::__getQueryUpdateApps(std::vector<AppInfo> *appsWithWeights)
 {
-       std::string delete_query(DELETE_APP_ACTIVITY_CLASSIFIED_BY_TIMEFRAME);
-       std::string insert_query(INSERT_APP_ACTIVITY_CLASSIFIED);
-       std::string placeholder_timeframe(APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_TIMEFRAME);
-       std::string placeholder_values(APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_VALUES);
-       std::string placeholder_is_active(APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_CLUSTER);
+       std::string deleteQuery(DELETE_APP_ACTIVITY_CLASSIFIED_BY_TIMEFRAME);
+       std::string insertQuery(INSERT_APP_ACTIVITY_CLASSIFIED);
+       std::string placeholderTimeframe(APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_TIMEFRAME);
+       std::string placeholderValues(APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_VALUES);
+       std::string placeholderIsActive(APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_CLUSTER);
 
-       std::stringstream timeframe_stream;
-       timeframe_stream << apps_with_weights->front().timeframe;
+       std::stringstream timeframeStream;
+       timeframeStream << appsWithWeights->front().timeframe;
 
-       inject_params(delete_query, placeholder_timeframe, timeframe_stream.str());
-       inject_params(insert_query, placeholder_values, subquery_form_values(apps_with_weights));
+       __injectParams(deleteQuery, placeholderTimeframe, timeframeStream.str());
+       __injectParams(insertQuery, placeholderValues, __getSubqueryFormValues(appsWithWeights));
 
        std::stringstream result;
-       result << delete_query << insert_query;
+       result << deleteQuery << insertQuery;
        return result.str();
 }
 
 // foreach app_info id+cluster -> select for insert
-std::string ctx::inactive_detector_storage::subquery_form_values(std::vector<app_t> *apps_with_weights)
+std::string ctx::InactiveDetectorStorage::__getSubqueryFormValues(std::vector<AppInfo> *appsWithWeights)
 {
-       std::stringstream select_elements;
+       std::stringstream selectElements;
 
-       for(std::vector<app_t>::iterator row = apps_with_weights->begin();
-                       row != apps_with_weights->end(); row++)
+       for (auto row = appsWithWeights->begin(); row != appsWithWeights->end(); row++)
        {
-               //SELECT 1 as is_active, 1 as timeframe,  3964 as context_app_info_id
-               std::stringstream select_element;
-               select_element << " SELECT " << row->is_active << " as ";
-               select_element << APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_IS_ACTIVE;
-               select_element << ", " << row->timeframe << " as ";
-               select_element << APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_TIMEFRAME;
-               select_element << ", " << row->id << " as ";
-               select_element << APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_CONTEXT_APP_INFO_ID;
-
-               if ((row != apps_with_weights->end()) && (row == --apps_with_weights->end()))
-                       select_element << " UNION ";
-
-               select_elements << select_element;
+               //SELECT 1 as isActive, 1 as timeframe,  3964 as context_app_info_id
+               std::stringstream selectElement;
+               selectElement << " SELECT " << row->isActive << " as ";
+               selectElement << APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_IS_ACTIVE;
+               selectElement << ", " << row->timeframe << " as ";
+               selectElement << APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_TIMEFRAME;
+               selectElement << ", " << row->id << " as ";
+               selectElement << APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_CONTEXT_APP_INFO_ID;
+
+               if ((row != appsWithWeights->end()) && (row == --appsWithWeights->end()))
+                       selectElement << " UNION ";
+
+               selectElements << selectElement;
        }
 
-       return select_elements.str();
+       return selectElements.str();
 }
 
-void ctx::inactive_detector_storage::json_to_object(std::vector<Json>& records,
-       std::vector<app_t> *apps_with_weights, bool result_mode)
+void ctx::InactiveDetectorStorage::__jsonToObject(std::vector<Json>& records, std::vector<AppInfo> *appsWithWeights, bool resultMode)
 {
-       for(std::vector<Json>::iterator row = records.begin();
-               row != records.end(); row++)
-       {
-               app_t app_with_weight;
-               if (result_mode)
-               {
-                       row->get(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME,
-                               &app_with_weight.package_name);
-                       row->get(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_NODISPLAY,
-                               &app_with_weight.is_nodisplay);
-                       row->get(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_ENABLED,
-                               &app_with_weight.is_enabled);
-                       row->get(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_ATBOOT,
-                               &app_with_weight.is_atboot);
-                       row->get(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_PRELOADED,
-                               &app_with_weight.is_preloaded);
-                       row->get(NULL, APP_INACTIVE_DETECTOR_VIRTUAL_COLUMN_WEIGHT,
-                               &app_with_weight.weight);
-               }
-               else
-               {
-                       row->get(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_ID,
-                               &app_with_weight.id);
-                       row->get(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_TIMESTAMP,
-                               &app_with_weight.timestamp);
-                       row->get(NULL, APP_INACTIVE_DETECTOR_VIRTUAL_COLUMN_WEIGHT,
-                               &app_with_weight.weight);
-                       row->get(NULL, APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_IS_ACTIVE,
-                               &app_with_weight.is_active);
+       for (auto row = records.begin(); row != records.end(); row++) {
+               AppInfo appWithWeight;
+               if (resultMode) {
+                       row->get(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME, &appWithWeight.packageName);
+                       row->get(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_NODISPLAY, &appWithWeight.isNodisplay);
+                       row->get(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_ENABLED, &appWithWeight.isEnabled);
+                       row->get(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_ATBOOT, &appWithWeight.isAtBoot);
+                       row->get(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_PRELOADED, &appWithWeight.isPreloaded);
+                       row->get(NULL, APP_INACTIVE_DETECTOR_VIRTUAL_COLUMN_WEIGHT, &appWithWeight.weight);
+               } else {
+                       row->get(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_ID, &appWithWeight.id);
+                       row->get(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_TIMESTAMP, &appWithWeight.timestamp);
+                       row->get(NULL, APP_INACTIVE_DETECTOR_VIRTUAL_COLUMN_WEIGHT, &appWithWeight.weight);
+                       row->get(NULL, APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_IS_ACTIVE, &appWithWeight.isActive);
                }
 
-               apps_with_weights->push_back(app_with_weight);
+               appsWithWeights->push_back(appWithWeight);
        }
 }
 
-void ctx::inactive_detector_storage::onExecuted(unsigned int query_id,
-       int error,
-       std::vector<Json>& records)
+void ctx::InactiveDetectorStorage::onExecuted(unsigned int queryId, int error, std::vector<Json>& records)
 {
        if (error != ERR_NONE) {
-               _E("query_id:%d, error:%d", query_id, error);
+               _E("queryId:%d, error:%d", queryId, error);
                return;
        }
 
-       std::vector<app_t> *apps_with_weights = NULL;
-       if (query_id == APP_INACTIVE_QUERY_ID_GET_APPS_INACTIVE ||
-               query_id == APP_INACTIVE_QUERY_ID_GET_APPS_ACTIVE)
-       {
-               json_to_object(records, apps_with_weights, TRUE);
-       }
-       else if (query_id == APP_INACTIVE_QUERY_ID_GET_APPS_WEIGHT) {
+       std::vector<AppInfo> *appsWithWeights = NULL;
+       if (queryId == APP_INACTIVE_QUERY_ID_GET_APPS_INACTIVE || queryId == APP_INACTIVE_QUERY_ID_GET_APPS_ACTIVE) {
+               __jsonToObject(records, appsWithWeights, TRUE);
+       } else if (queryId == APP_INACTIVE_QUERY_ID_GET_APPS_WEIGHT) {
+               __jsonToObject(records, appsWithWeights, FALSE);
 
-               json_to_object(records, apps_with_weights, FALSE);
+               if (appsWithWeights->size() > 0) {
+                       InactiveDetectorClassificator inactDetClassificator;
+                       int err = inactDetClassificator.classify(appsWithWeights);
 
-               if (apps_with_weights->size() > 0) {
-                       inactive_detector_classificator i_detector_classificator;
-                       int _error = i_detector_classificator.classify(apps_with_weights);
-
-                       if(_error == ERR_NONE)
-                       {
+                       if (err == ERR_NONE) {
                                std::string query;
-                               query = query_update_apps(apps_with_weights);
-                               bool ret = __dbManager.execute(APP_INACTIVE_QUERY_ID_UPDATE_CLUSTERS,
-                                       query.c_str(),
-                                       this);
+                               query = __getQueryUpdateApps(appsWithWeights);
+                               bool ret = __dbManager.execute(APP_INACTIVE_QUERY_ID_UPDATE_CLUSTERS, query.c_str(), this);
                                _D("load visits execute query result: %s", ret ? "SUCCESS" : "FAIL");
-                       }
-                       else
-                       {
-                               _E("classification query_id:%d, error:%d",
-                                       query_id, _error);
+                       } else {
+                               _E("classification queryId:%d, error:%d", queryId, err);
                        }
                }
-       }
-       else if (query_id == APP_INACTIVE_QUERY_ID_UPDATE_CLUSTERS) {
-               _D("UPDATE_CLUSTERS execute query id: %d", query_id);
-       }
-       else {
-               _E("unknown query_id:%d", query_id);
+       } else if (queryId == APP_INACTIVE_QUERY_ID_UPDATE_CLUSTERS) {
+               _D("UPDATE_CLUSTERS execute query id: %d", queryId);
+       } else {
+               _E("unknown queryId:%d", queryId);
        }
 }
 
-void ctx::inactive_detector_storage::inject_params(std::string& str, const std::string& from, const std::string& to)
+void ctx::InactiveDetectorStorage::__injectParams(std::string& str, const std::string& from, const std::string& to)
 {
-       if(from.empty())
+       if (from.empty())
                return;
-       size_t start_pos = 0;
-       while((start_pos = str.find(from, start_pos)) != std::string::npos) {
-               str.replace(start_pos, from.length(), to);
-               start_pos += to.length();
+
+       size_t startPos = 0;
+       while((startPos = str.find(from, startPos)) != std::string::npos) {
+               str.replace(startPos, from.length(), to);
+               startPos += to.length();
        }
 }
 
 // normalzie weights
-int ctx::inactive_detector_storage::update_ranks()
+int ctx::InactiveDetectorStorage::updateRanks()
 {
        return ERR_NONE;
 }
 
-int ctx::inactive_detector_storage::get_apps_info_w_weights(
-               double timestamp_from)
+int ctx::InactiveDetectorStorage::getAppsInfoWithWeights(double timestampFrom)
 {
-       std::stringstream timestamp_stream;
-       timestamp_stream << timestamp_from;
-       std::string timestamp_str = timestamp_stream.str();
+       std::stringstream timestampStream;
+       timestampStream << timestampFrom;
+       std::string timestampStr = timestampStream.str();
 
        std::string query(GET_APP_INFO_W_WEIGHT_QUERY);
        std::string placeholder(APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_TIMESTAMP);
 
-       inject_params(query, placeholder, timestamp_str);
+       __injectParams(query, placeholder, timestampStr);
 
-       bool ret = __dbManager.execute(APP_INACTIVE_QUERY_ID_GET_APPS_WEIGHT,
-               query.c_str(),
-               this);
+       bool ret = __dbManager.execute(APP_INACTIVE_QUERY_ID_GET_APPS_WEIGHT, query.c_str(), this);
        _D("load visits execute query result: %s", ret ? "SUCCESS" : "FAIL");
 
        return ERR_NONE ;