From 0daf5aca0ca4530fab42eff27172550b39dde861 Mon Sep 17 00:00:00 2001 From: Somin Kim Date: Wed, 20 Apr 2016 14:31:41 +0900 Subject: [PATCH] Applying C++ coding style to app inactive detector Change-Id: Ibe7fd394872d14c113f332843b589834e374d140 Signed-off-by: Somin Kim --- .../AppInactiveDetector.cpp | 29 ++- .../AppInactiveDetector.h | 33 +-- .../AppInactiveDetectorTypes.h | 23 +- .../InactiveDetector.cpp | 76 +++--- .../app_inactive_detector/InactiveDetector.h | 38 ++- .../InactiveDetectorClassificator.cpp | 8 +- .../InactiveDetectorClassificator.h | 20 +- .../InactiveDetectorClassificatorKmeans.cpp | 132 +++++----- .../InactiveDetectorClassificatorKmeans.h | 47 ++-- ...InactiveDetectorClassificatorKmeansTypes.h | 12 +- .../InactiveDetectorStorage.cpp | 232 ++++++++---------- .../InactiveDetectorStorage.h | 55 ++--- .../InactiveDetectorStorageQueries.h | 34 +-- .../InactiveDetectorWeight.cpp | 9 +- .../InactiveDetectorWeight.h | 20 +- 15 files changed, 351 insertions(+), 417 deletions(-) diff --git a/src/app-stats/app_inactive_detector/AppInactiveDetector.cpp b/src/app-stats/app_inactive_detector/AppInactiveDetector.cpp index 148d655..ad13698 100644 --- a/src/app-stats/app_inactive_detector/AppInactiveDetector.cpp +++ b/src/app-stats/app_inactive_detector/AppInactiveDetector.cpp @@ -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. @@ -15,33 +15,32 @@ */ #include -#include #include #include "AppInactiveDetector.h" #include "AppInactiveDetectorTypes.h" #include "InactiveDetector.h" +ctx::AppInactiveDetectorProvider *ctx::AppInactiveDetectorProvider::__instance = NULL; -ctx::app_inactive_detector_provider *ctx::app_inactive_detector_provider::__instance = NULL; - -ctx::app_inactive_detector_provider::app_inactive_detector_provider() +ctx::AppInactiveDetectorProvider::AppInactiveDetectorProvider() + : ContextProvider(APP_INACTIVE_SUBJ_GET_APPS_INACTIVE) { } -ctx::app_inactive_detector_provider::~app_inactive_detector_provider() +ctx::AppInactiveDetectorProvider::~AppInactiveDetectorProvider() { } -ctx::ContextProvider *ctx::app_inactive_detector_provider::create(void *data) +ctx::ContextProvider *ctx::AppInactiveDetectorProvider::create(void *data) { IF_FAIL_RETURN(!__instance, __instance); - __instance = new(std::nothrow) app_inactive_detector_provider(); + __instance = new(std::nothrow) AppInactiveDetectorProvider(); IF_FAIL_RETURN_TAG(__instance, NULL, _E, "Memory allocation failed"); _I(BLUE("Created")); return __instance; } -void ctx::app_inactive_detector_provider::destroy(void *data) +void ctx::AppInactiveDetectorProvider::destroy(void *data) { IF_FAIL_VOID(__instance); delete __instance; @@ -49,31 +48,31 @@ void ctx::app_inactive_detector_provider::destroy(void *data) _I(BLUE("Destroyed")); } -int ctx::app_inactive_detector_provider::subscribe(const char *subject, ctx::Json option, ctx::Json* request_result) +int ctx::AppInactiveDetectorProvider::subscribe(ctx::Json option, ctx::Json* requestResult) { return ERR_NOT_SUPPORTED; } -int ctx::app_inactive_detector_provider::unsubscribe(const char *subject, ctx::Json option) +int ctx::AppInactiveDetectorProvider::unsubscribe(ctx::Json option) { return ERR_NOT_SUPPORTED; } -int ctx::app_inactive_detector_provider::read(const char *subject, ctx::Json option, ctx::Json* request_result) +int ctx::AppInactiveDetectorProvider::read(ctx::Json option, ctx::Json* requestResult) { _I(BLUE("Read")); _J("Option", option); - int error = engine->read(subject, option); + int error = __engine->read(APP_INACTIVE_SUBJ_GET_APPS_INACTIVE, option); return error == ERR_NONE ? ERR_NONE : error; } -int ctx::app_inactive_detector_provider::write(const char *subject, ctx::Json data, ctx::Json* request_result) +int ctx::AppInactiveDetectorProvider::write(ctx::Json data, ctx::Json* requestResult) { return ERR_NOT_SUPPORTED; } -bool ctx::app_inactive_detector_provider::is_supported() +bool ctx::AppInactiveDetectorProvider::isSupported() { return true; } diff --git a/src/app-stats/app_inactive_detector/AppInactiveDetector.h b/src/app-stats/app_inactive_detector/AppInactiveDetector.h index 588130a..693c082 100644 --- a/src/app-stats/app_inactive_detector/AppInactiveDetector.h +++ b/src/app-stats/app_inactive_detector/AppInactiveDetector.h @@ -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. @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef __CONTEXT_APP_INACTIVE_DETECTOR_H__ -#define __CONTEXT_APP_INACTIVE_DETECTOR_H__ +#ifndef _CONTEXT_APP_INACTIVE_DETECTOR_H_ +#define _CONTEXT_APP_INACTIVE_DETECTOR_H_ #include #include "AppInactiveDetectorTypes.h" @@ -23,27 +23,28 @@ namespace ctx { - class app_inactive_detector_provider : public ContextProvider { + class AppInactiveDetectorProvider : public ContextProvider { public: + AppInactiveDetectorProvider(); + ~AppInactiveDetectorProvider(); + static ContextProvider *create(void *data); static void destroy(void *data); - static bool is_supported(); - int subscribe(const char *subject, ctx::Json option, ctx::Json *request_result); - int unsubscribe(const char *subject, ctx::Json option); - int read(const char *subject, ctx::Json option, ctx::Json *request_result); - int write(const char *subject, ctx::Json data, ctx::Json *request_result); + int subscribe(ctx::Json option, ctx::Json *requestResult); + int unsubscribe(ctx::Json option); + int read(ctx::Json option, ctx::Json *requestResult); + int write(ctx::Json data, ctx::Json *requestResult); - private: - static app_inactive_detector_provider *__instance; - inactive_detector *engine; + bool isSupported(); - app_inactive_detector_provider(); - ~app_inactive_detector_provider(); + private: + static AppInactiveDetectorProvider *__instance; + InactiveDetector *__engine; - }; /* class app_inactive_detector_provider */ + }; /* class AppInactiveDetectorProvider */ } /* namespace ctx */ -#endif /* __CONTEXT_APP_INACTIVE_DETECTOR_H__ */ \ No newline at end of file +#endif /* _CONTEXT_APP_INACTIVE_DETECTOR_H_ */ diff --git a/src/app-stats/app_inactive_detector/AppInactiveDetectorTypes.h b/src/app-stats/app_inactive_detector/AppInactiveDetectorTypes.h index b058b54..1221100 100644 --- a/src/app-stats/app_inactive_detector/AppInactiveDetectorTypes.h +++ b/src/app-stats/app_inactive_detector/AppInactiveDetectorTypes.h @@ -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. @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef __CONTEXT_APP_INACTIVE_DETECTOR_TYPES__ -#define __CONTEXT_APP_INACTIVE_DETECTOR_TYPES__ +#ifndef _CONTEXT_APP_INACTIVE_DETECTOR_TYPES_H_ +#define _CONTEXT_APP_INACTIVE_DETECTOR_TYPES_H_ #include @@ -81,16 +81,16 @@ enum { #define APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_VALUES "$values" // Objects -struct app_t { +struct AppInfo { int id; - std::string package_name; - int is_nodisplay; - int is_enabled; - int is_atboot; - int is_preloaded; + std::string packageName; + int isNodisplay; + int isEnabled; + int isAtBoot; + int isPreloaded; double timestamp; int weight; - int is_active; + int isActive; int timeframe; }; @@ -110,5 +110,4 @@ struct app_t { #define CONTEXT_HISTORY_FILTER_TIME_INTERVAL_ONEMONTH 60*60*24*31 - -#endif /* __CONTEXT_APP_INACTIVE_DETECTOR_TYPES__ */ \ No newline at end of file +#endif /* _CONTEXT_APP_INACTIVE_DETECTOR_TYPES_H_ */ diff --git a/src/app-stats/app_inactive_detector/InactiveDetector.cpp b/src/app-stats/app_inactive_detector/InactiveDetector.cpp index 6cc1820..5f32585 100644 --- a/src/app-stats/app_inactive_detector/InactiveDetector.cpp +++ b/src/app-stats/app_inactive_detector/InactiveDetector.cpp @@ -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. @@ -22,39 +22,37 @@ #include "InactiveDetectorClassificator.h" #include "TimerManager.h" -ctx::inactive_detector::inactive_detector() +ctx::InactiveDetector::InactiveDetector() { - timer_id = timer_mgr.setAt( // execute once every night - APP_INACTIVE_TASK_START_HOUR, - APP_INACTIVE_TASK_START_MINUTE, - DayOfWeek::EVERYDAY, - this); - if (timer_id < 0) { - _E("inactive_detector timer set FAIL"); + // execute once every night + __timerId = __timerMgr.setAt(APP_INACTIVE_TASK_START_HOUR, APP_INACTIVE_TASK_START_MINUTE, + DayOfWeek::EVERYDAY, this); + + if (__timerId < 0) { + _E("InactiveDetector timer set FAIL"); return; } else { - update_clusters(); - _D("inactive_detector timer set SUCCESS"); + updateClusters(); + _D("InactiveDetector timer set SUCCESS"); } } -ctx::inactive_detector::~inactive_detector() +ctx::InactiveDetector::~InactiveDetector() { - if (timer_id >= 0) { - timer_mgr.remove(timer_id); + if (__timerId >= 0) { + __timerMgr.remove(__timerId); _D("inactive_detejctor timer removed"); } - } -bool ctx::inactive_detector::onTimerExpired(int timerId) +bool ctx::InactiveDetector::onTimerExpired(int timerId) { - update_clusters(); + updateClusters(); return true; } -int ctx::inactive_detector::update_clusters() +int ctx::InactiveDetector::updateClusters() { // define timeframes stack std::vector timeframes; @@ -66,31 +64,27 @@ int ctx::inactive_detector::update_clusters() // recalculate weights // foreach timeframe - for(std::vector::iterator timeframe = timeframes.begin(); - timeframe != timeframes.end(); timeframe++) - { - inactive_detector_weight i_detector_weight; - int error = i_detector_weight.request_weights(*timeframe); - _E("update_clusters request_weights:%d, error:%d", *timeframe, error); + for(auto timeframe = timeframes.begin(); timeframe != timeframes.end(); timeframe++) { + InactiveDetectorWeight inactDetWeight; + int error = inactDetWeight.requestWeights(*timeframe); + _E("updateClusters requestWeights:%d, error:%d", *timeframe, error); } return ERR_NONE; } -void ctx::inactive_detector::on_cluster_update_finished(std::vector *apps_with_weight, int error) +void ctx::InactiveDetector::onClusterUpdateFinished(std::vector *appsWithWeight, int error) { if (error == ERR_NONE) { - _I("on_cluster_update_finished success"); + _I("onClusterUpdateFinished success"); } // clean memory - delete apps_with_weight; + delete appsWithWeight; } -int ctx::inactive_detector::read( - const char *subject, - ctx::Json option) +int ctx::InactiveDetector::read(const char *subject, ctx::Json option) { - ctx::inactive_detector_storage *handle = new(std::nothrow) ctx::inactive_detector_storage(); + ctx::InactiveDetectorStorage *handle = new(std::nothrow) ctx::InactiveDetectorStorage(); IF_FAIL_RETURN_TAG(handle, ERR_OPERATION_FAILED, _E, "Memory allocation failed"); int err = handle->read(subject, option); @@ -102,20 +96,18 @@ int ctx::inactive_detector::read( return ERR_NONE; } -ctx::Json ctx::inactive_detector::to_json(std::vector *app_infos) +ctx::Json ctx::InactiveDetector::toJson(std::vector *appInfos) { ctx::Json data; - for(std::vector::iterator app_info = app_infos->begin(); - app_info != app_infos->end(); app_info++) - { - ctx::Json app_j; - app_j.set(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME, app_info->package_name); - app_j.set(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_NODISPLAY, app_info->is_nodisplay); - app_j.set(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_ENABLED, app_info->is_enabled); - app_j.set(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_ATBOOT, app_info->is_atboot); - app_j.set(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_PRELOADED, app_info->is_preloaded); - app_j.set(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_TIMESTAMP, app_info->timestamp); + for(auto appInfo = appInfos->begin(); appInfo != appInfos->end(); appInfo++) { + ctx::Json appJ; + appJ.set(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME, appInfo->packageName); + appJ.set(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_NODISPLAY, appInfo->isNodisplay); + appJ.set(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_ENABLED, appInfo->isEnabled); + appJ.set(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_ATBOOT, appInfo->isAtBoot); + appJ.set(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_PRELOADED, appInfo->isPreloaded); + appJ.set(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_TIMESTAMP, appInfo->timestamp); } return data; } diff --git a/src/app-stats/app_inactive_detector/InactiveDetector.h b/src/app-stats/app_inactive_detector/InactiveDetector.h index 8c2d0a0..9a15ec8 100644 --- a/src/app-stats/app_inactive_detector/InactiveDetector.h +++ b/src/app-stats/app_inactive_detector/InactiveDetector.h @@ -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. @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef __CONTEXT_INACTIVE_DETECTOR_H__ -#define __CONTEXT_INACTIVE_DETECTOR_H__ +#ifndef _CONTEXT_INACTIVE_DETECTOR_H_ +#define _CONTEXT_INACTIVE_DETECTOR_H_ #include #include @@ -25,27 +25,25 @@ namespace ctx { - class inactive_detector : public ITimerListener { - private: - int timer_id; - TimerManager timer_mgr; - public: - inactive_detector(); - ~inactive_detector(); + class InactiveDetector : public ITimerListener { + private: + int __timerId; + TimerManager __timerMgr; - int read(const char *subject, - ctx::Json option); + public: + InactiveDetector(); + ~InactiveDetector(); - int update_clusters(); - void on_cluster_update_finished( - std::vector *apps_classified, - int error); - ctx::Json to_json(std::vector *apps); + int read(const char *subject, ctx::Json option); - bool onTimerExpired(int timerId); - }; /* class inactive_detector */ + int updateClusters(); + void onClusterUpdateFinished(std::vector *appsClassified, int error); + ctx::Json toJson(std::vector *apps); + + bool onTimerExpired(int timerId); + }; /* class InactiveDetector */ } /* namespace ctx */ -#endif /* __CONTEXT_INACTIVE_DETECTOR_H__ */ +#endif /* _CONTEXT_INACTIVE_DETECTOR_H_ */ diff --git a/src/app-stats/app_inactive_detector/InactiveDetectorClassificator.cpp b/src/app-stats/app_inactive_detector/InactiveDetectorClassificator.cpp index 5e5859b..1c3e645 100644 --- a/src/app-stats/app_inactive_detector/InactiveDetectorClassificator.cpp +++ b/src/app-stats/app_inactive_detector/InactiveDetectorClassificator.cpp @@ -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. @@ -19,10 +19,10 @@ #include "InactiveDetectorClassificator.h" #include "InactiveDetectorClassificatorKmeans.h" -int ctx::inactive_detector_classificator::classify(std::vector *apps_with_weights) +int ctx::InactiveDetectorClassificator::classify(std::vector *appsWithWeights) { - inactive_detector_classificator_kmeans kmeans; - int error = kmeans.classify(apps_with_weights); + InactiveDetectorClassificatorKmeans kmeans; + int error = kmeans.classify(appsWithWeights); return error; } diff --git a/src/app-stats/app_inactive_detector/InactiveDetectorClassificator.h b/src/app-stats/app_inactive_detector/InactiveDetectorClassificator.h index fc9abd0..59ceecd 100644 --- a/src/app-stats/app_inactive_detector/InactiveDetectorClassificator.h +++ b/src/app-stats/app_inactive_detector/InactiveDetectorClassificator.h @@ -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. @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef __CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_H__ -#define __CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_H__ +#ifndef _CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_H_ +#define _CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_H_ #include #include "AppInactiveDetectorTypes.h" @@ -23,15 +23,15 @@ namespace ctx { - class inactive_detector_classificator + class InactiveDetectorClassificator { - public: - inactive_detector_classificator(); - ~inactive_detector_classificator(); + public: + InactiveDetectorClassificator(); + ~InactiveDetectorClassificator(); - int classify(std::vector *apps_with_weights); - }; /* class inactive_detector_classificator */ + int classify(std::vector *appsWithWeights); + }; /* class InactiveDetectorClassificator */ } /* namespace ctx */ -#endif /* __CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_H__ */ +#endif /* _CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_H_ */ diff --git a/src/app-stats/app_inactive_detector/InactiveDetectorClassificatorKmeans.cpp b/src/app-stats/app_inactive_detector/InactiveDetectorClassificatorKmeans.cpp index c0cb93e..9a0680e 100644 --- a/src/app-stats/app_inactive_detector/InactiveDetectorClassificatorKmeans.cpp +++ b/src/app-stats/app_inactive_detector/InactiveDetectorClassificatorKmeans.cpp @@ -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. @@ -20,155 +20,149 @@ #include #include -double ctx::inactive_detector_classificator_kmeans::randomf(double x) +double ctx::InactiveDetectorClassificatorKmeans::randomf(double x) { return x * rand() / (RAND_MAX - 1.); } -double ctx::inactive_detector_classificator_kmeans::glide_function(double x) +double ctx::InactiveDetectorClassificatorKmeans::__glideFunction(double x) { double value = sqrt(log(x) + 1); return (value > 0 ? value : 0); } -point_s *ctx::inactive_detector_classificator_kmeans::reproject_to_2d(std::vector *apps_with_weights) +Point *ctx::InactiveDetectorClassificatorKmeans::__reprojectTo2d(std::vector *appsWithWeights) { - int distr_size = apps_with_weights->size(); - point_s *p = new point_s[distr_size]; + int distrSize = appsWithWeights->size(); + Point *p = new Point[distrSize]; - int i=0; - for(std::vector::iterator apps_with_weight = apps_with_weights->begin(); - apps_with_weight != apps_with_weights->end(); apps_with_weight++) - { - point_s the_point; + int i = 0; + for (auto appWithWeights = appsWithWeights->begin(); appWithWeights != appsWithWeights->end(); appWithWeights++) { + Point thePoint; - the_point.x = glide_function(apps_with_weight->weight); // normalize weight values - the_point.y = 0; //remove 3rd dimension of the data - the_point.origin_id = apps_with_weight->package_name; + thePoint.x = __glideFunction(appWithWeights->weight); // normalize weight values + thePoint.y = 0; //remove 3rd dimension of the data + thePoint.originId = appWithWeights->packageName; - p[i] = the_point; + p[i] = thePoint; i++; } return p; } -bool ctx::inactive_detector_classificator_kmeans::annotate_data(std::vector *apps_with_weights, point_s *c) +bool ctx::InactiveDetectorClassificatorKmeans::__annotateData(std::vector *appsWithWeights, Point *c) { - int i=0, max_group=-1; + int i = 0, maxGroup = -1; - for(std::vector::iterator apps_with_weight = apps_with_weights->begin(); - apps_with_weight != apps_with_weights->end(); apps_with_weight++) - { - apps_with_weight->is_active = c[i].group; + for (auto appWithWeights = appsWithWeights->begin(); appWithWeights != appsWithWeights->end(); appWithWeights++) { + appWithWeights->isActive = c[i].group; i++; - max_group = c[i].group > max_group ? c[i].group : max_group; + maxGroup = c[i].group > maxGroup ? c[i].group : maxGroup; } - return max_group == APP_INACTIVE_DETECTOR_KMEANS_CLUSTER_COUNT; + return maxGroup == APP_INACTIVE_DETECTOR_KMEANS_CLUSTER_COUNT; } -double ctx::inactive_detector_classificator_kmeans::distance_to(point_s *p_from, - point_s *p_to) +double ctx::InactiveDetectorClassificatorKmeans::__distanceTo(Point *pFrom, Point *pTo) { - double x = p_from->x - p_to->x, y = p_from->y - p_to->y; + double x = pFrom->x - pTo->x, y = pFrom->y - pTo->y; return x*x + y*y; } -int ctx::inactive_detector_classificator_kmeans::nearest(point_s *pt, - point_s *centers, - int cluster_number, - double *distance) +int ctx::InactiveDetectorClassificatorKmeans::__nearest(Point *pt, Point *centers, int clusterNumber, double *distance) { int i; - int min_i = pt->group; - point_s *c; + int minI = pt->group; + Point *c; double d; - double min_d = HUGE_VAL; + double minD = HUGE_VAL; - for_n { - if (min_d >(d = distance_to(c, pt))) { - min_d = d; min_i = i; + FOR_N { + if (minD >(d = __distanceTo(c, pt))) { + minD = d; minI = i; } } - if (distance) *distance = min_d; - return min_i; + if (distance) *distance = minD; + return minI; } -void ctx::inactive_detector_classificator_kmeans::kpp(point_s *points, int length, point_s *centers, int cluster_number) +void ctx::InactiveDetectorClassificatorKmeans::__kpp(Point *points, int length, Point *centers, int clusterNumber) { int j; - int n_cluster; + int nCluster; double sum; double *d = new double[length]; - point_s *p; + Point *p; centers[0] = points[rand() % length]; - for (n_cluster = 1; n_cluster < cluster_number; n_cluster++) { + for (nCluster = 1; nCluster < clusterNumber; nCluster++) { sum = 0; - for_len{ - nearest(p, centers, n_cluster, d + j); + FOR_LEN { + __nearest(p, centers, nCluster, d + j); sum += d[j]; } sum = randomf(sum); - for_len{ + FOR_LEN { if ((sum -= d[j]) > 0) continue; - centers[n_cluster] = points[j]; + centers[nCluster] = points[j]; break; } } - for_len p->group = nearest(p, centers, n_cluster, 0); + FOR_LEN { + p->group = __nearest(p, centers, nCluster, 0); + } delete d; } -point_s *ctx::inactive_detector_classificator_kmeans::lloyd(point_s *points, int length, int cluster_number) +Point *ctx::InactiveDetectorClassificatorKmeans::__lloyd(Point *points, int length, int clusterNumber) { - int i, j, min_i; + int i, j, minI; int changed; - point_s *centers = new point_s; - point_s *p, *c; + Point *centers = new Point; + Point *p, *c; /* k++ init */ - kpp(points, length, centers, cluster_number); + __kpp(points, length, centers, clusterNumber); do { /* group element for centroids are used as counters */ - for_n{ c->group = 0; c->x = c->y = 0; } - for_len{ - c = centers + p->group; - c->group++; - c->x += p->x; c->y += p->y; - } - for_n{ c->x /= c->group; c->y /= c->group; } + FOR_N { c->group = 0; c->x = c->y = 0; } + FOR_LEN { + c = centers + p->group; + c->group++; + c->x += p->x; c->y += p->y; + } + FOR_N { c->x /= c->group; c->y /= c->group; } changed = 0; /* find closest centroid of each point */ - for_len{ - min_i = nearest(p, centers, cluster_number, 0); - if (min_i != p->group) { + FOR_LEN { + minI = __nearest(p, centers, clusterNumber, 0); + if (minI != p->group) { changed++; - p->group = min_i; + p->group = minI; } } } while (changed > (length >> 10)); /* stop when 99.9% of points are good */ - for_n{ c->group = i; } + FOR_N { c->group = i; } return centers; } -int ctx::inactive_detector_classificator_kmeans::classify(std::vector *apps_with_weights) +int ctx::InactiveDetectorClassificatorKmeans::classify(std::vector *appsWithWeights) { //array of generated points - point_s *v = reproject_to_2d(apps_with_weights); + Point *v = __reprojectTo2d(appsWithWeights); //mark cluster for each point, output centers - point_s *c = lloyd(v, apps_with_weights->size(), APP_INACTIVE_DETECTOR_KMEANS_CLUSTER_COUNT); + Point *c = __lloyd(v, appsWithWeights->size(), APP_INACTIVE_DETECTOR_KMEANS_CLUSTER_COUNT); //append the result to input data - bool classification_is_success = annotate_data(apps_with_weights, c); + bool classificationIsSuccess = __annotateData(appsWithWeights, c); - return classification_is_success ? ERR_NONE : ERR_OPERATION_FAILED; + return classificationIsSuccess ? ERR_NONE : ERR_OPERATION_FAILED; } diff --git a/src/app-stats/app_inactive_detector/InactiveDetectorClassificatorKmeans.h b/src/app-stats/app_inactive_detector/InactiveDetectorClassificatorKmeans.h index adc1fe8..8b39943 100644 --- a/src/app-stats/app_inactive_detector/InactiveDetectorClassificatorKmeans.h +++ b/src/app-stats/app_inactive_detector/InactiveDetectorClassificatorKmeans.h @@ -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. @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef __CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_KMEANS_H__ -#define __CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_KMEANS_H__ +#ifndef _CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_KMEANS_H_ +#define _CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_KMEANS_H_ #include #include "InactiveDetectorClassificatorKmeansTypes.h" @@ -23,27 +23,28 @@ namespace ctx { - #define for_n for (c = centers, i = 0; i < cluster_number; i++, c++) - #define for_len for (j = 0, p = points; j < length; j++, p++) + #define FOR_N for (c = centers, i = 0; i < clusterNumber; i++, c++) + #define FOR_LEN for (j = 0, p = points; j < length; j++, p++) - class inactive_detector_classificator_kmeans + class InactiveDetectorClassificatorKmeans { - private: - double randomf(double x); - double glide_function(double x); - point_s *reproject_to_2d(std::vector *apps_with_weights); - bool annotate_data(std::vector *apps_with_weights, point_s *c); - - double distance_to(point_s *p_from, point_s *p_to); - int nearest(point_s *pt, point_s *centers, int cluster_number, double *distance); - void kpp(point_s *points, int length, point_s *centers, int centers_count); - point_s *lloyd(point_s *points, int length, int cluster_number); - public: - inactive_detector_classificator_kmeans(); - ~inactive_detector_classificator_kmeans(); - - int classify(std::vector *apps_with_weights); - }; /* class inactive_detector_classificator_kmeans */ + private: + double randomf(double x); + double __glideFunction(double x); + Point *__reprojectTo2d(std::vector *appsWithWeights); + bool __annotateData(std::vector *appsWithWeights, Point *c); + + double __distanceTo(Point *pFrom, Point *pTo); + int __nearest(Point *pt, Point *centers, int clusterNumber, double *distance); + void __kpp(Point *points, int length, Point *centers, int centersCount); + Point *__lloyd(Point *points, int length, int clusterNumber); + + public: + InactiveDetectorClassificatorKmeans(); + ~InactiveDetectorClassificatorKmeans(); + + int classify(std::vector *appsWithWeights); + }; /* class InactiveDetectorClassificatorKmeans */ } /* namespace ctx */ -#endif /* __CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_KMEANS_H__ */ +#endif /* _CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_KMEANS_H_ */ diff --git a/src/app-stats/app_inactive_detector/InactiveDetectorClassificatorKmeansTypes.h b/src/app-stats/app_inactive_detector/InactiveDetectorClassificatorKmeansTypes.h index 8aa7270..76e2a91 100644 --- a/src/app-stats/app_inactive_detector/InactiveDetectorClassificatorKmeansTypes.h +++ b/src/app-stats/app_inactive_detector/InactiveDetectorClassificatorKmeansTypes.h @@ -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. @@ -14,16 +14,16 @@ * limitations under the License. */ -#ifndef __CONTEXT_APP_INACTIVE_DETECTOR_CLASSIFICATOR_KMEANS_TYPES__ -#define __CONTEXT_APP_INACTIVE_DETECTOR_CLASSIFICATOR_KMEANS_TYPES__ +#ifndef _CONTEXT_APP_INACTIVE_DETECTOR_CLASSIFICATOR_KMEANS_TYPES_H_ +#define _CONTEXT_APP_INACTIVE_DETECTOR_CLASSIFICATOR_KMEANS_TYPES_H_ #define APP_INACTIVE_DETECTOR_KMEANS_CLUSTER_COUNT 2 typedef struct { - std::string origin_id; + std::string originId; double x; double y; int group; -} point_s; +} Point; -#endif \ No newline at end of file +#endif /* _CONTEXT_APP_INACTIVE_DETECTOR_CLASSIFICATOR_KMEANS_TYPES_H_ */ diff --git a/src/app-stats/app_inactive_detector/InactiveDetectorStorage.cpp b/src/app-stats/app_inactive_detector/InactiveDetectorStorage.cpp index 052c3fe..7fd11a7 100644 --- a/src/app-stats/app_inactive_detector/InactiveDetectorStorage.cpp +++ b/src/app-stats/app_inactive_detector/InactiveDetectorStorage.cpp @@ -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. @@ -25,223 +25,183 @@ #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 *apps_with_weights) +std::string ctx::InactiveDetectorStorage::__getQueryUpdateApps(std::vector *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 *apps_with_weights) +std::string ctx::InactiveDetectorStorage::__getSubqueryFormValues(std::vector *appsWithWeights) { - std::stringstream select_elements; + std::stringstream selectElements; - for(std::vector::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& records, - std::vector *apps_with_weights, bool result_mode) +void ctx::InactiveDetectorStorage::__jsonToObject(std::vector& records, std::vector *appsWithWeights, bool resultMode) { - for(std::vector::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& records) +void ctx::InactiveDetectorStorage::onExecuted(unsigned int queryId, int error, std::vector& records) { if (error != ERR_NONE) { - _E("query_id:%d, error:%d", query_id, error); + _E("queryId:%d, error:%d", queryId, error); return; } - std::vector *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 *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 ; diff --git a/src/app-stats/app_inactive_detector/InactiveDetectorStorage.h b/src/app-stats/app_inactive_detector/InactiveDetectorStorage.h index 0eab066..2974ba0 100644 --- a/src/app-stats/app_inactive_detector/InactiveDetectorStorage.h +++ b/src/app-stats/app_inactive_detector/InactiveDetectorStorage.h @@ -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. @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef __CONTEXT_INACTIVE_DETECTOR_STORAGE_H__ -#define __CONTEXT_INACTIVE_DETECTOR_STORAGE_H__ +#ifndef _CONTEXT_INACTIVE_DETECTOR_STORAGE_H_ +#define _CONTEXT_INACTIVE_DETECTOR_STORAGE_H_ #include #include "AppInactiveDetectorTypes.h" @@ -24,41 +24,32 @@ namespace ctx { - class inactive_detector_storage : public IDatabaseListener + class InactiveDetectorStorage : public IDatabaseListener { - private: - //int type; //TODO: enum - void inject_params(std::string& str, - const std::string& from, - const std::string& to); + private: + //int type; //TODO: enum + void __injectParams(std::string& str, const std::string& from, const std::string& to); + void __jsonToObject(std::vector& records, std::vector *appsWithWeights, bool resultMode); + std::string __getQueryGetApps(const char *subject, ctx::Json filter); + std::string __getQueryUpdateApps(std::vector *appsWithWeights); + std::string __getSubqueryFormValues(std::vector *appsWithWeights); - void json_to_object(std::vector& records, - std::vector *apps_with_weights, bool result_mode); + void onTableCreated(unsigned int queryId, int error) {} + void onInserted(unsigned int queryId, int error, int64_t rowId) {} + void onExecuted(unsigned int queryId, int error, std::vector& records); - std::string query_get_apps(const char *subject, - ctx::Json filter); + DatabaseManager __dbManager; - std::string query_update_apps(std::vector *apps_with_weights); + public: + InactiveDetectorStorage(); + ~InactiveDetectorStorage(); - std::string subquery_form_values(std::vector *apps_with_weights); + int read(const char *subject, ctx::Json filter); - void onTableCreated(unsigned int query_id, int error) {} - void onInserted(unsigned int query_id, int error, int64_t row_id) {} - void onExecuted(unsigned int query_id, int error, std::vector& records); - - DatabaseManager __dbManager; - - public: - inactive_detector_storage(); - ~inactive_detector_storage(); - - int read(const char *subject, - ctx::Json filter); - - int update_ranks(); - int get_apps_info_w_weights(double timestamp_from); - }; /* class inactive_detector_storage */ + int updateRanks(); + int getAppsInfoWithWeights(double timestampFrom); + }; /* class InactiveDetectorStorage */ } /* namespace ctx */ -#endif /* __CONTEXT_INACTIVE_DETECTOR_STORAGE_H__ */ +#endif /* _CONTEXT_INACTIVE_DETECTOR_STORAGE_H_ */ diff --git a/src/app-stats/app_inactive_detector/InactiveDetectorStorageQueries.h b/src/app-stats/app_inactive_detector/InactiveDetectorStorageQueries.h index 4ef20d3..a6268cc 100644 --- a/src/app-stats/app_inactive_detector/InactiveDetectorStorageQueries.h +++ b/src/app-stats/app_inactive_detector/InactiveDetectorStorageQueries.h @@ -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. @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef __CONTEXT_INACTIVE_DETECTOR_STORAGE_QUERIES_H__ -#define __CONTEXT_INACTIVE_DETECTOR_STORAGE_QUERIES_H__ +#ifndef _CONTEXT_INACTIVE_DETECTOR_STORAGE_QUERIES_H_ +#define _CONTEXT_INACTIVE_DETECTOR_STORAGE_QUERIES_H_ #include #include "AppInactiveDetectorTypes.h" @@ -31,7 +31,7 @@ SELECT app_info.is_atboot, app_info.is_preloaded, app_info.timestamp, - + app_hist_g.weight as weight FROM context_app_info app_info @@ -43,7 +43,7 @@ INNER JOIN IFNULL((select count(package_name) from context_app_launch_log where package_name == app_hist.package_name AND timestamp> 8640412900), 0 ) * - IFNULL((select sum(duration) from context_app_launch_log + IFNULL((select sum(duration) from context_app_launch_log where package_name == app_hist.package_name AND timestamp> 8640412900) , 0) as weight @@ -144,9 +144,9 @@ WHERE /* --3 update ranks and clusters for each timeframe: delete all for each time frame, add new calculations for apps -DELETE FROM - context_activity_classified -WHERE +DELETE FROM + context_activity_classified +WHERE timeframe = 1 */ @@ -156,20 +156,20 @@ WHERE " timeframe = " APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_TIMEFRAME -/*INSERT INTO - context_activity_classified - (is_active, - timeframe, +/*INSERT INTO + context_activity_classified + (is_active, + timeframe, context_app_info_id) -SELECT - is_active, +SELECT + is_active, timeframe, context_app_info_id FROM --make loop ( SELECT 1 as is_active, 1 as timeframe, 3964 as context_app_info_id - UNION - SELECT 0 as is_active, 1 as timeframe, 3964 as context_app_info_id + UNION + SELECT 0 as is_active, 1 as timeframe, 3964 as context_app_info_id ) q */ @@ -189,4 +189,4 @@ FROM --make loop -#endif /* __CONTEXT_INACTIVE_DETECTOR_STORAGE_QUERIES_H__ */ +#endif /* _CONTEXT_INACTIVE_DETECTOR_STORAGE_QUERIES_H_ */ diff --git a/src/app-stats/app_inactive_detector/InactiveDetectorWeight.cpp b/src/app-stats/app_inactive_detector/InactiveDetectorWeight.cpp index 202301b..445e0e4 100644 --- a/src/app-stats/app_inactive_detector/InactiveDetectorWeight.cpp +++ b/src/app-stats/app_inactive_detector/InactiveDetectorWeight.cpp @@ -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. @@ -18,12 +18,11 @@ #include "InactiveDetectorWeight.h" #include "InactiveDetectorStorage.h" -int ctx::inactive_detector_weight::request_weights( - double timestamp_from) +int ctx::InactiveDetectorWeight::requestWeights(double timestampFrom) { - inactive_detector_storage ids; + InactiveDetectorStorage ids; // query the database for the result - int error = ids.get_apps_info_w_weights(timestamp_from); + int error = ids.getAppsInfoWithWeights(timestampFrom); return error; } diff --git a/src/app-stats/app_inactive_detector/InactiveDetectorWeight.h b/src/app-stats/app_inactive_detector/InactiveDetectorWeight.h index 8e87473..45cb68e 100644 --- a/src/app-stats/app_inactive_detector/InactiveDetectorWeight.h +++ b/src/app-stats/app_inactive_detector/InactiveDetectorWeight.h @@ -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. @@ -14,21 +14,21 @@ * limitations under the License. */ -#ifndef __CONTEXT_INACTIVE_DETECTOR_WEIGHT_H__ -#define __CONTEXT_INACTIVE_DETECTOR_WEIGHT_H__ +#ifndef _CONTEXT_INACTIVE_DETECTOR_WEIGHT_H_ +#define _CONTEXT_INACTIVE_DETECTOR_WEIGHT_H_ // #include // #include namespace ctx { - class inactive_detector_weight { - public: - inactive_detector_weight() {}; - ~inactive_detector_weight() {}; - int request_weights(double timestamp_from);// {}; - }; /* class inactive_detector_weight */ + class InactiveDetectorWeight { + public: + InactiveDetectorWeight() {}; + ~InactiveDetectorWeight() {}; + int requestWeights(double timestampFrom); + }; /* class InactiveDetectorWeight */ } /* namespace ctx */ -#endif /* __CONTEXT_INACTIVE_DETECTOR_WEIGHT_H__ */ \ No newline at end of file +#endif /* _CONTEXT_INACTIVE_DETECTOR_WEIGHT_H_ */ -- 2.34.1