From: Somin Kim Date: Fri, 15 Apr 2016 08:24:43 +0000 (+0900) Subject: Rename app inactive detector in C++ style X-Git-Tag: submit/tizen/20160503.015801^2~25 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=00f6700316ce7e445782f77d52e899e16a9fb28a;p=platform%2Fcore%2Fcontext%2Fcontext-provider.git Rename app inactive detector in C++ style Change-Id: I6129687b83c38f6d9221d4bbeffff1f409384849 Signed-off-by: Somin Kim --- diff --git a/src/statistics/app/app_inactive_detector/AppInactiveDetector.cpp b/src/statistics/app/app_inactive_detector/AppInactiveDetector.cpp new file mode 100644 index 0000000..48dc313 --- /dev/null +++ b/src/statistics/app/app_inactive_detector/AppInactiveDetector.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include "AppInactiveDetector.h" +#include "AppInactiveDetectorTypes.h" +#include "InactiveDetector.h" + + +ctx::app_inactive_detector_provider *ctx::app_inactive_detector_provider::__instance = NULL; + +ctx::app_inactive_detector_provider::app_inactive_detector_provider() +{ +} + +ctx::app_inactive_detector_provider::~app_inactive_detector_provider() +{ +} + +ctx::ContextProviderBase *ctx::app_inactive_detector_provider::create(void *data) +{ + IF_FAIL_RETURN(!__instance, __instance); + __instance = new(std::nothrow) app_inactive_detector_provider(); + IF_FAIL_RETURN_TAG(__instance, NULL, _E, "Memory allocation failed"); + _I(BLUE("Created")); + return __instance; +} + +void ctx::app_inactive_detector_provider::destroy(void *data) +{ + IF_FAIL_VOID(__instance); + delete __instance; + __instance = NULL; + _I(BLUE("Destroyed")); +} + +int ctx::app_inactive_detector_provider::subscribe(const char *subject, ctx::Json option, ctx::Json* request_result) +{ + return ERR_NOT_SUPPORTED; +} + +int ctx::app_inactive_detector_provider::unsubscribe(const char *subject, ctx::Json option) +{ + return ERR_NOT_SUPPORTED; +} + +int ctx::app_inactive_detector_provider::read(const char *subject, ctx::Json option, ctx::Json* request_result) +{ + _I(BLUE("Read")); + _J("Option", option); + + int error = engine->read(subject, 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) +{ + return ERR_NOT_SUPPORTED; +} + +bool ctx::app_inactive_detector_provider::is_supported() +{ + return true; +} diff --git a/src/statistics/app/app_inactive_detector/AppInactiveDetector.h b/src/statistics/app/app_inactive_detector/AppInactiveDetector.h new file mode 100644 index 0000000..2dd427d --- /dev/null +++ b/src/statistics/app/app_inactive_detector/AppInactiveDetector.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __CONTEXT_APP_INACTIVE_DETECTOR_H__ +#define __CONTEXT_APP_INACTIVE_DETECTOR_H__ + +#include +#include "AppInactiveDetectorTypes.h" +#include "InactiveDetector.h" + +namespace ctx { + + class app_inactive_detector_provider : public ContextProviderBase { + + public: + static ContextProviderBase *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); + + private: + static app_inactive_detector_provider *__instance; + inactive_detector *engine; + + app_inactive_detector_provider(); + ~app_inactive_detector_provider(); + + }; /* class app_inactive_detector_provider */ + +} /* namespace ctx */ + +#endif /* __CONTEXT_APP_INACTIVE_DETECTOR_H__ */ \ No newline at end of file diff --git a/src/statistics/app/app_inactive_detector/AppInactiveDetectorTypes.h b/src/statistics/app/app_inactive_detector/AppInactiveDetectorTypes.h new file mode 100644 index 0000000..b058b54 --- /dev/null +++ b/src/statistics/app/app_inactive_detector/AppInactiveDetectorTypes.h @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __CONTEXT_APP_INACTIVE_DETECTOR_TYPES__ +#define __CONTEXT_APP_INACTIVE_DETECTOR_TYPES__ + +#include + +// Context Items +//#define APP_INACTIVE_SUBJ_RECALCULATE "app/recalculate" +#define APP_INACTIVE_SUBJ_GET_APPS_INACTIVE "app/inactive" +#define APP_INACTIVE_SUBJ_GET_APPS_ACTIVE "app/active" +#define APP_INACTIVE_SUBJ_GET_APPS_WEIGHT "app/weight" +#define APP_INACTIVE_SUBJ_UPDATE_CLUSTERS "app/update" + +enum { + APP_INACTIVE_QUERY_ID_GET_APPS_WEIGHT = 1, + APP_INACTIVE_QUERY_ID_GET_APPS_INACTIVE = 2, + APP_INACTIVE_QUERY_ID_GET_APPS_ACTIVE = 3, + APP_INACTIVE_QUERY_ID_UPDATE_CLUSTERS = 4 +}; + +// Database +#define APP_INACTIVE_DETECTOR_APPINFO_TABLE "context_app_info" +#define APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "app_info" +#define APP_INACTIVE_DETECTOR_APPINFO_COLUMN_ID "_id" +#define APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME "package_name" +#define APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_TYPE "package_type" +#define APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_NODISPLAY "is_nodisplay" +#define APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_ENABLED "is_enabled" +#define APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_ATBOOT "is_atboot" +#define APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_PRELOADED "is_preloaded" +#define APP_INACTIVE_DETECTOR_APPINFO_COLUMN_TIMESTAMP "timestamp" + +#define APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_TABLE "context_app_launch_log" +#define APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_TABLE_ALIAS "app_launch_log" +#define APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_COLUMN_ID "_id" +#define APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_COLUMN_CONTEXT_APP_INFO_ID "context_app_info_id" +#define APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_COLUMN_DURATION "duration" +#define APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_COLUMN_SYSTEM_VOLUME "system_volume" +#define APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_COLUMN_BSSID "bssid" +#define APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_COLUMN_TIMESTAMP "timestamp" +#define APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_COLUMN_TIMESTAMP_UTC "timestamp_utc" + +#define APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_TABLE "context_app_activity_classified" +#define APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_TABLE_ALIAS "app_activity_classified" +#define APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_ID "_id" +#define APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_CONTEXT_APP_INFO_ID "context_app_info_id" +#define APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_ACTIVITY_RATE "activity_rate" +#define APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_TIMEFRAME "timeframe" +#define APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_IS_ACTIVE "is_active" + + +#define APP_INACTIVE_DETECTOR_VIRTUAL_TABLE_ALIAS "virtual_app_history" +#define APP_INACTIVE_DETECTOR_VIRTUAL_COLUMN_WEIGHT "weight" + +// Output Data Key +#define APP_INACTIVE_DETECTOR_DATA_READ "AppsList" +#define APP_INACTIVE_DETECTOR_DATA_TIMESTAMP_FROM "timestamp_from" +#define APP_INACTIVE_DETECTOR_ACTIVITY_RATE "activity_rate" +#define APP_INACTIVE_DETECTOR_DATA_TIMEFRAME "timeframe" +#define APP_INACTIVE_DETECTOR_DATA_ISACTIVE "is_active" + +//Other +#define APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_TIMESTAMP "$timestamp" +#define APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_CLUSTER "$cluster" +#define APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_TIMEFRAME "$timeframe" +#define APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_VALUES "$values" + +// Objects +struct app_t { + int id; + std::string package_name; + int is_nodisplay; + int is_enabled; + int is_atboot; + int is_preloaded; + double timestamp; + int weight; + int is_active; + int timeframe; +}; + + +#define APP_TYPE_INACTIVE_INACTIVE 1 +#define APP_TYPE_INACTIVE_ACTIVE 2 + + +#define APP_INACTIVE_TASK_START_HOUR 3 +#define APP_INACTIVE_TASK_START_MINUTE 0 + + +#define CONTEXT_HISTORY_FILTER_TIME_INTERVAL_ONEDAY 60*60*24 +#define CONTEXT_HISTORY_FILTER_TIME_INTERVAL_THREEDAYS 60*60*24*3 +#define CONTEXT_HISTORY_FILTER_TIME_INTERVAL_ONEWEEK 60*60*24*7 +#define CONTEXT_HISTORY_FILTER_TIME_INTERVAL_TWOWEEKS 60*60*24*14 +#define CONTEXT_HISTORY_FILTER_TIME_INTERVAL_ONEMONTH 60*60*24*31 + + + +#endif /* __CONTEXT_APP_INACTIVE_DETECTOR_TYPES__ */ \ No newline at end of file diff --git a/src/statistics/app/app_inactive_detector/InactiveDetector.cpp b/src/statistics/app/app_inactive_detector/InactiveDetector.cpp new file mode 100644 index 0000000..6cc1820 --- /dev/null +++ b/src/statistics/app/app_inactive_detector/InactiveDetector.cpp @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include "InactiveDetector.h" +#include "InactiveDetectorWeight.h" +#include "InactiveDetectorStorage.h" +#include "InactiveDetectorClassificator.h" +#include "TimerManager.h" + +ctx::inactive_detector::inactive_detector() +{ + 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"); + return; + } else { + update_clusters(); + _D("inactive_detector timer set SUCCESS"); + } + +} + +ctx::inactive_detector::~inactive_detector() +{ + if (timer_id >= 0) { + timer_mgr.remove(timer_id); + _D("inactive_detejctor timer removed"); + } + +} + +bool ctx::inactive_detector::onTimerExpired(int timerId) +{ + update_clusters(); + return true; +} + +int ctx::inactive_detector::update_clusters() +{ + // define timeframes stack + std::vector timeframes; + timeframes.push_back(CONTEXT_HISTORY_FILTER_TIME_INTERVAL_ONEDAY); + timeframes.push_back(CONTEXT_HISTORY_FILTER_TIME_INTERVAL_THREEDAYS); + timeframes.push_back(CONTEXT_HISTORY_FILTER_TIME_INTERVAL_ONEWEEK); + timeframes.push_back(CONTEXT_HISTORY_FILTER_TIME_INTERVAL_TWOWEEKS); + timeframes.push_back(CONTEXT_HISTORY_FILTER_TIME_INTERVAL_ONEMONTH); + + // 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); + } + + return ERR_NONE; +} + +void ctx::inactive_detector::on_cluster_update_finished(std::vector *apps_with_weight, int error) +{ + if (error == ERR_NONE) { + _I("on_cluster_update_finished success"); + } + // clean memory + delete apps_with_weight; +} + +int ctx::inactive_detector::read( + const char *subject, + ctx::Json option) +{ + ctx::inactive_detector_storage *handle = new(std::nothrow) ctx::inactive_detector_storage(); + IF_FAIL_RETURN_TAG(handle, ERR_OPERATION_FAILED, _E, "Memory allocation failed"); + + int err = handle->read(subject, option); + if (err != ERR_NONE) { + delete handle; + return err; + } + + return ERR_NONE; +} + +ctx::Json ctx::inactive_detector::to_json(std::vector *app_infos) +{ + 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); + } + return data; +} diff --git a/src/statistics/app/app_inactive_detector/InactiveDetector.h b/src/statistics/app/app_inactive_detector/InactiveDetector.h new file mode 100644 index 0000000..8c2d0a0 --- /dev/null +++ b/src/statistics/app/app_inactive_detector/InactiveDetector.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __CONTEXT_INACTIVE_DETECTOR_H__ +#define __CONTEXT_INACTIVE_DETECTOR_H__ + +#include +#include +#include +#include "AppInactiveDetectorTypes.h" +#include + +namespace ctx { + + class inactive_detector : public ITimerListener { + private: + int timer_id; + TimerManager timer_mgr; + public: + inactive_detector(); + ~inactive_detector(); + + int read(const char *subject, + ctx::Json option); + + int update_clusters(); + void on_cluster_update_finished( + std::vector *apps_classified, + int error); + ctx::Json to_json(std::vector *apps); + + bool onTimerExpired(int timerId); + }; /* class inactive_detector */ + +} /* namespace ctx */ + + +#endif /* __CONTEXT_INACTIVE_DETECTOR_H__ */ diff --git a/src/statistics/app/app_inactive_detector/InactiveDetectorClassificator.cpp b/src/statistics/app/app_inactive_detector/InactiveDetectorClassificator.cpp new file mode 100644 index 0000000..5e5859b --- /dev/null +++ b/src/statistics/app/app_inactive_detector/InactiveDetectorClassificator.cpp @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include "InactiveDetector.h" +#include "AppInactiveDetectorTypes.h" +#include "InactiveDetectorClassificator.h" +#include "InactiveDetectorClassificatorKmeans.h" + +int ctx::inactive_detector_classificator::classify(std::vector *apps_with_weights) +{ + inactive_detector_classificator_kmeans kmeans; + int error = kmeans.classify(apps_with_weights); + + return error; +} diff --git a/src/statistics/app/app_inactive_detector/InactiveDetectorClassificator.h b/src/statistics/app/app_inactive_detector/InactiveDetectorClassificator.h new file mode 100644 index 0000000..fc9abd0 --- /dev/null +++ b/src/statistics/app/app_inactive_detector/InactiveDetectorClassificator.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_H__ +#define __CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_H__ + +#include +#include "AppInactiveDetectorTypes.h" +#include + +namespace ctx { + + class inactive_detector_classificator + { + public: + inactive_detector_classificator(); + ~inactive_detector_classificator(); + + int classify(std::vector *apps_with_weights); + }; /* class inactive_detector_classificator */ + +} /* namespace ctx */ + +#endif /* __CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_H__ */ diff --git a/src/statistics/app/app_inactive_detector/InactiveDetectorClassificatorKmeans.cpp b/src/statistics/app/app_inactive_detector/InactiveDetectorClassificatorKmeans.cpp new file mode 100644 index 0000000..c0cb93e --- /dev/null +++ b/src/statistics/app/app_inactive_detector/InactiveDetectorClassificatorKmeans.cpp @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include "AppInactiveDetectorTypes.h" +#include "InactiveDetectorClassificatorKmeans.h" +#include "InactiveDetectorClassificatorKmeansTypes.h" +#include +#include + +double ctx::inactive_detector_classificator_kmeans::randomf(double x) +{ + return x * rand() / (RAND_MAX - 1.); +} + +double ctx::inactive_detector_classificator_kmeans::glide_function(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) +{ + int distr_size = apps_with_weights->size(); + point_s *p = new point_s[distr_size]; + + 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; + + 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; + + p[i] = the_point; + i++; + } + + return p; +} + +bool ctx::inactive_detector_classificator_kmeans::annotate_data(std::vector *apps_with_weights, point_s *c) +{ + int i=0, max_group=-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; + i++; + max_group = c[i].group > max_group ? c[i].group : max_group; + } + + return max_group == APP_INACTIVE_DETECTOR_KMEANS_CLUSTER_COUNT; +} + +double ctx::inactive_detector_classificator_kmeans::distance_to(point_s *p_from, + point_s *p_to) +{ + double x = p_from->x - p_to->x, y = p_from->y - p_to->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 i; + int min_i = pt->group; + point_s *c; + + double d; + double min_d = HUGE_VAL; + + for_n { + if (min_d >(d = distance_to(c, pt))) { + min_d = d; min_i = i; + } + } + + if (distance) *distance = min_d; + return min_i; +} + +void ctx::inactive_detector_classificator_kmeans::kpp(point_s *points, int length, point_s *centers, int cluster_number) +{ + int j; + int n_cluster; + double sum; + double *d = new double[length]; + + point_s *p; + centers[0] = points[rand() % length]; + for (n_cluster = 1; n_cluster < cluster_number; n_cluster++) { + sum = 0; + for_len{ + nearest(p, centers, n_cluster, d + j); + sum += d[j]; + } + sum = randomf(sum); + for_len{ + if ((sum -= d[j]) > 0) continue; + centers[n_cluster] = points[j]; + break; + } + } + for_len p->group = nearest(p, centers, n_cluster, 0); + + delete d; +} + +point_s *ctx::inactive_detector_classificator_kmeans::lloyd(point_s *points, int length, int cluster_number) +{ + int i, j, min_i; + int changed; + + point_s *centers = new point_s; + point_s *p, *c; + + /* k++ init */ + kpp(points, length, centers, cluster_number); + + 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; } + + changed = 0; + /* find closest centroid of each point */ + for_len{ + min_i = nearest(p, centers, cluster_number, 0); + if (min_i != p->group) { + changed++; + p->group = min_i; + } + } + } while (changed > (length >> 10)); /* stop when 99.9% of points are good */ + + for_n{ c->group = i; } + + return centers; +} + +int ctx::inactive_detector_classificator_kmeans::classify(std::vector *apps_with_weights) +{ + //array of generated points + point_s *v = reproject_to_2d(apps_with_weights); + //mark cluster for each point, output centers + point_s *c = lloyd(v, apps_with_weights->size(), APP_INACTIVE_DETECTOR_KMEANS_CLUSTER_COUNT); + //append the result to input data + bool classification_is_success = annotate_data(apps_with_weights, c); + + return classification_is_success ? ERR_NONE : ERR_OPERATION_FAILED; +} diff --git a/src/statistics/app/app_inactive_detector/InactiveDetectorClassificatorKmeans.h b/src/statistics/app/app_inactive_detector/InactiveDetectorClassificatorKmeans.h new file mode 100644 index 0000000..adc1fe8 --- /dev/null +++ b/src/statistics/app/app_inactive_detector/InactiveDetectorClassificatorKmeans.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_KMEANS_H__ +#define __CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_KMEANS_H__ + +#include +#include "InactiveDetectorClassificatorKmeansTypes.h" +#include + +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++) + + class inactive_detector_classificator_kmeans + { + 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 */ +} /* namespace ctx */ + +#endif /* __CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_KMEANS_H__ */ diff --git a/src/statistics/app/app_inactive_detector/InactiveDetectorClassificatorKmeansTypes.h b/src/statistics/app/app_inactive_detector/InactiveDetectorClassificatorKmeansTypes.h new file mode 100644 index 0000000..8aa7270 --- /dev/null +++ b/src/statistics/app/app_inactive_detector/InactiveDetectorClassificatorKmeansTypes.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __CONTEXT_APP_INACTIVE_DETECTOR_CLASSIFICATOR_KMEANS_TYPES__ +#define __CONTEXT_APP_INACTIVE_DETECTOR_CLASSIFICATOR_KMEANS_TYPES__ + +#define APP_INACTIVE_DETECTOR_KMEANS_CLUSTER_COUNT 2 + +typedef struct { + std::string origin_id; + double x; + double y; + int group; +} point_s; + +#endif \ No newline at end of file diff --git a/src/statistics/app/app_inactive_detector/InactiveDetectorStorage.cpp b/src/statistics/app/app_inactive_detector/InactiveDetectorStorage.cpp new file mode 100644 index 0000000..052c3fe --- /dev/null +++ b/src/statistics/app/app_inactive_detector/InactiveDetectorStorage.cpp @@ -0,0 +1,248 @@ +/* + * Copyright (c) 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include "InactiveDetector.h" +#include "InactiveDetectorStorage.h" +#include "InactiveDetectorStorageQueries.h" +#include "InactiveDetectorClassificator.h" +#include "AppInactiveDetectorTypes.h" + +/*int ctx::inactive_detector_storage::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) +{ + std::string query; + query = query_get_apps(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); + + 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) +{ + double timeframe; + int is_active; + + 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); + + filter.get(NULL, APP_INACTIVE_DETECTOR_DATA_TIMEFRAME, &timeframe); + filter.get(NULL, APP_INACTIVE_DETECTOR_DATA_ISACTIVE, &is_active); + + std::stringstream timeframe_stream; + timeframe_stream << timeframe; + + std::stringstream is_active_stream; + is_active_stream << is_active; + + inject_params(query, placeholder_timeframe, timeframe_stream.str()); + inject_params(query, placeholder_is_active, is_active_stream.str()); + + return query; +} + +std::string ctx::inactive_detector_storage::query_update_apps(std::vector *apps_with_weights) +{ + 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::stringstream timeframe_stream; + timeframe_stream << apps_with_weights->front().timeframe; + + inject_params(delete_query, placeholder_timeframe, timeframe_stream.str()); + inject_params(insert_query, placeholder_values, subquery_form_values(apps_with_weights)); + + std::stringstream result; + result << delete_query << insert_query; + 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::stringstream select_elements; + + for(std::vector::iterator row = apps_with_weights->begin(); + row != apps_with_weights->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; + } + + return select_elements.str(); +} + +void ctx::inactive_detector_storage::json_to_object(std::vector& records, + std::vector *apps_with_weights, bool result_mode) +{ + 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); + } + + apps_with_weights->push_back(app_with_weight); + } +} + +void ctx::inactive_detector_storage::onExecuted(unsigned int query_id, + int error, + std::vector& records) +{ + if (error != ERR_NONE) { + _E("query_id:%d, error:%d", query_id, 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) { + + json_to_object(records, apps_with_weights, FALSE); + + 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) + { + std::string query; + query = query_update_apps(apps_with_weights); + 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 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); + } +} + +void ctx::inactive_detector_storage::inject_params(std::string& str, const std::string& from, const std::string& to) +{ + 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(); + } +} + +// normalzie weights +int ctx::inactive_detector_storage::update_ranks() +{ + return ERR_NONE; +} + +int ctx::inactive_detector_storage::get_apps_info_w_weights( + double timestamp_from) +{ + std::stringstream timestamp_stream; + timestamp_stream << timestamp_from; + std::string timestamp_str = timestamp_stream.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); + + 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/statistics/app/app_inactive_detector/InactiveDetectorStorage.h b/src/statistics/app/app_inactive_detector/InactiveDetectorStorage.h new file mode 100644 index 0000000..0eab066 --- /dev/null +++ b/src/statistics/app/app_inactive_detector/InactiveDetectorStorage.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __CONTEXT_INACTIVE_DETECTOR_STORAGE_H__ +#define __CONTEXT_INACTIVE_DETECTOR_STORAGE_H__ + +#include +#include "AppInactiveDetectorTypes.h" +#include +#include + +namespace ctx { + + class inactive_detector_storage : public IDatabaseListener + { + private: + //int type; //TODO: enum + void inject_params(std::string& str, + const std::string& from, + const std::string& to); + + void json_to_object(std::vector& records, + std::vector *apps_with_weights, bool result_mode); + + std::string query_get_apps(const char *subject, + ctx::Json filter); + + std::string query_update_apps(std::vector *apps_with_weights); + + std::string subquery_form_values(std::vector *apps_with_weights); + + 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 */ + +} /* namespace ctx */ + +#endif /* __CONTEXT_INACTIVE_DETECTOR_STORAGE_H__ */ diff --git a/src/statistics/app/app_inactive_detector/InactiveDetectorStorageQueries.h b/src/statistics/app/app_inactive_detector/InactiveDetectorStorageQueries.h new file mode 100644 index 0000000..4ef20d3 --- /dev/null +++ b/src/statistics/app/app_inactive_detector/InactiveDetectorStorageQueries.h @@ -0,0 +1,192 @@ +/* + * Copyright (c) 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __CONTEXT_INACTIVE_DETECTOR_STORAGE_QUERIES_H__ +#define __CONTEXT_INACTIVE_DETECTOR_STORAGE_QUERIES_H__ + +#include +#include "AppInactiveDetectorTypes.h" + +/* + +--1 calculate weights +SELECT + app_info._id, + app_info.package_name, + app_info.is_nodisplay, + app_info.is_enabled, + app_info.is_atboot, + app_info.is_preloaded, + app_info.timestamp, + + app_hist_g.weight as weight +FROM + context_app_info app_info + +INNER JOIN + + (SELECT + distinct package_name , + 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 + where package_name == app_hist.package_name AND timestamp> 8640412900) , 0) + as weight + + FROM context_app_launch_log app_hist +)app_hist_g + +ON app_hist_g.package_name += +app_info.package_name + +ORDER BY app_hist_g.weight + +*/ + +#define GET_APP_INFO_W_WEIGHT_QUERY "SELECT "\ + " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_ID ", "\ + " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME ", "\ + " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_TYPE ", "\ + " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_NODISPLAY ", "\ + " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_ENABLED ", "\ + " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_ATBOOT ", "\ + " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_PRELOADED ", "\ + " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_TIMESTAMP ", "\ + ""\ + " " APP_INACTIVE_DETECTOR_VIRTUAL_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_VIRTUAL_COLUMN_WEIGHT \ + ""\ + " FROM " APP_INACTIVE_DETECTOR_APPINFO_TABLE " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS \ + ""\ + " INNER JOIN " \ + " (SELECT "\ + " DISTINCT " APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME "," \ + " IFNULL((SELECT COUNT(" APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME ")" \ + " FROM " APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_TABLE \ + " WHERE " APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME "==" APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME \ + " AND " \ + " " APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_COLUMN_TIMESTAMP " >= " APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_TIMESTAMP \ + "), 0) " \ + " * " \ + " IFNULL((SELECT SUM(" APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_COLUMN_DURATION ")" \ + " FROM " APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_TABLE \ + " WHERE " APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME "==" APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME \ + " AND " \ + " " APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_COLUMN_TIMESTAMP " >= " APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_TIMESTAMP \ + ""\ + "), 0) " \ + " as " APP_INACTIVE_DETECTOR_VIRTUAL_COLUMN_WEIGHT \ + " FROM " APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_TABLE " " APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_TABLE_ALIAS \ + ") " APP_INACTIVE_DETECTOR_VIRTUAL_TABLE_ALIAS \ + " ON " APP_INACTIVE_DETECTOR_VIRTUAL_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME \ + " = " \ + " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME \ + " ORDER BY " APP_INACTIVE_DETECTOR_VIRTUAL_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_VIRTUAL_COLUMN_WEIGHT + +/* +--2 select inner join context_app_info +SELECT + app_info.package_name, + app_info.is_nodisplay, + app_info.is_enabled, + app_info.is_atboot, + app_info.is_preloaded, + + activity_classified.activity_rate + +FROM + context_activity_classified activity_classified +INNER JOIN + context_app_info app_info +ON activity_classified.context_app_info_id = app_info._id + +WHERE + activity_classified.is_active = 1 + AND + activity_classified.timeframe = 1 +*/ + +#define GET_APP_INFO_INACTIVE_QUERY "SELECT "\ + " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_ID ", "\ + " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME ", "\ + " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_NODISPLAY ", "\ + " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_ENABLED ", "\ + " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_ATBOOT ", "\ + " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_PRELOADED ", "\ + " "\ + " " APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_ACTIVITY_RATE " "\ + " FROM "\ + " " APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_TABLE " " APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_TABLE_ALIAS " "\ + " INNER JOIN "\ + " " APP_INACTIVE_DETECTOR_APPINFO_TABLE " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS " "\ + " ON " APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_CONTEXT_APP_INFO_ID ""\ + " = " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_ID " "\ + " WHERE "\ + APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_IS_ACTIVE ""\ + " = " APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_CLUSTER ""\ + " AND "\ + " " APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_TIMEFRAME ""\ + " = " APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_TIMEFRAME "" + +/* +--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 + timeframe = 1 +*/ + +#define DELETE_APP_ACTIVITY_CLASSIFIED_BY_TIMEFRAME "DELETE FROM "\ + APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_TABLE \ + " WHERE "\ + " timeframe = " APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_TIMEFRAME + + +/*INSERT INTO + context_activity_classified + (is_active, + timeframe, + context_app_info_id) +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 + ) q +*/ + +#define INSERT_APP_ACTIVITY_CLASSIFIED " INSERT INTO "\ + APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_TABLE \ + "( " APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_IS_ACTIVE ", "\ + APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_TIMEFRAME ", "\ + APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_CONTEXT_APP_INFO_ID ")" \ +"SELECT " \ + APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_IS_ACTIVE "," \ + APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_TIMEFRAME ", "\ + APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_CONTEXT_APP_INFO_ID \ +"FROM"\ + "(" \ + APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_VALUES \ + ") q" + + + +#endif /* __CONTEXT_INACTIVE_DETECTOR_STORAGE_QUERIES_H__ */ diff --git a/src/statistics/app/app_inactive_detector/InactiveDetectorWeight.cpp b/src/statistics/app/app_inactive_detector/InactiveDetectorWeight.cpp new file mode 100644 index 0000000..202301b --- /dev/null +++ b/src/statistics/app/app_inactive_detector/InactiveDetectorWeight.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "InactiveDetectorWeight.h" +#include "InactiveDetectorStorage.h" + +int ctx::inactive_detector_weight::request_weights( + double timestamp_from) +{ + inactive_detector_storage ids; + // query the database for the result + int error = ids.get_apps_info_w_weights(timestamp_from); + + return error; +} diff --git a/src/statistics/app/app_inactive_detector/InactiveDetectorWeight.h b/src/statistics/app/app_inactive_detector/InactiveDetectorWeight.h new file mode 100644 index 0000000..8e87473 --- /dev/null +++ b/src/statistics/app/app_inactive_detector/InactiveDetectorWeight.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#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 */ + +} /* namespace ctx */ + +#endif /* __CONTEXT_INACTIVE_DETECTOR_WEIGHT_H__ */ \ No newline at end of file diff --git a/src/statistics/app/app_inactive_detector/app_inactive_detector.cpp b/src/statistics/app/app_inactive_detector/app_inactive_detector.cpp deleted file mode 100644 index ef5c23a..0000000 --- a/src/statistics/app/app_inactive_detector/app_inactive_detector.cpp +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (c) 2015 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include "app_inactive_detector.h" -#include "app_inactive_detector_types.h" -#include "inactive_detector.h" - - -ctx::app_inactive_detector_provider *ctx::app_inactive_detector_provider::__instance = NULL; - -ctx::app_inactive_detector_provider::app_inactive_detector_provider() -{ -} - -ctx::app_inactive_detector_provider::~app_inactive_detector_provider() -{ -} - -ctx::ContextProviderBase *ctx::app_inactive_detector_provider::create(void *data) -{ - IF_FAIL_RETURN(!__instance, __instance); - __instance = new(std::nothrow) app_inactive_detector_provider(); - IF_FAIL_RETURN_TAG(__instance, NULL, _E, "Memory allocation failed"); - _I(BLUE("Created")); - return __instance; -} - -void ctx::app_inactive_detector_provider::destroy(void *data) -{ - IF_FAIL_VOID(__instance); - delete __instance; - __instance = NULL; - _I(BLUE("Destroyed")); -} - -int ctx::app_inactive_detector_provider::subscribe(const char *subject, ctx::Json option, ctx::Json* request_result) -{ - return ERR_NOT_SUPPORTED; -} - -int ctx::app_inactive_detector_provider::unsubscribe(const char *subject, ctx::Json option) -{ - return ERR_NOT_SUPPORTED; -} - -int ctx::app_inactive_detector_provider::read(const char *subject, ctx::Json option, ctx::Json* request_result) -{ - _I(BLUE("Read")); - _J("Option", option); - - int error = engine->read(subject, 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) -{ - return ERR_NOT_SUPPORTED; -} - -bool ctx::app_inactive_detector_provider::is_supported() -{ - return true; -} diff --git a/src/statistics/app/app_inactive_detector/app_inactive_detector.h b/src/statistics/app/app_inactive_detector/app_inactive_detector.h deleted file mode 100644 index 94e5b83..0000000 --- a/src/statistics/app/app_inactive_detector/app_inactive_detector.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2015 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __CONTEXT_APP_INACTIVE_DETECTOR_H__ -#define __CONTEXT_APP_INACTIVE_DETECTOR_H__ - -#include -#include "app_inactive_detector_types.h" -#include "inactive_detector.h" - -namespace ctx { - - class app_inactive_detector_provider : public ContextProviderBase { - - public: - static ContextProviderBase *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); - - private: - static app_inactive_detector_provider *__instance; - inactive_detector *engine; - - app_inactive_detector_provider(); - ~app_inactive_detector_provider(); - - }; /* class app_inactive_detector_provider */ - -} /* namespace ctx */ - -#endif /* __CONTEXT_APP_INACTIVE_DETECTOR_H__ */ \ No newline at end of file diff --git a/src/statistics/app/app_inactive_detector/app_inactive_detector_types.h b/src/statistics/app/app_inactive_detector/app_inactive_detector_types.h deleted file mode 100644 index b058b54..0000000 --- a/src/statistics/app/app_inactive_detector/app_inactive_detector_types.h +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (c) 2015 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __CONTEXT_APP_INACTIVE_DETECTOR_TYPES__ -#define __CONTEXT_APP_INACTIVE_DETECTOR_TYPES__ - -#include - -// Context Items -//#define APP_INACTIVE_SUBJ_RECALCULATE "app/recalculate" -#define APP_INACTIVE_SUBJ_GET_APPS_INACTIVE "app/inactive" -#define APP_INACTIVE_SUBJ_GET_APPS_ACTIVE "app/active" -#define APP_INACTIVE_SUBJ_GET_APPS_WEIGHT "app/weight" -#define APP_INACTIVE_SUBJ_UPDATE_CLUSTERS "app/update" - -enum { - APP_INACTIVE_QUERY_ID_GET_APPS_WEIGHT = 1, - APP_INACTIVE_QUERY_ID_GET_APPS_INACTIVE = 2, - APP_INACTIVE_QUERY_ID_GET_APPS_ACTIVE = 3, - APP_INACTIVE_QUERY_ID_UPDATE_CLUSTERS = 4 -}; - -// Database -#define APP_INACTIVE_DETECTOR_APPINFO_TABLE "context_app_info" -#define APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "app_info" -#define APP_INACTIVE_DETECTOR_APPINFO_COLUMN_ID "_id" -#define APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME "package_name" -#define APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_TYPE "package_type" -#define APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_NODISPLAY "is_nodisplay" -#define APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_ENABLED "is_enabled" -#define APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_ATBOOT "is_atboot" -#define APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_PRELOADED "is_preloaded" -#define APP_INACTIVE_DETECTOR_APPINFO_COLUMN_TIMESTAMP "timestamp" - -#define APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_TABLE "context_app_launch_log" -#define APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_TABLE_ALIAS "app_launch_log" -#define APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_COLUMN_ID "_id" -#define APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_COLUMN_CONTEXT_APP_INFO_ID "context_app_info_id" -#define APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_COLUMN_DURATION "duration" -#define APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_COLUMN_SYSTEM_VOLUME "system_volume" -#define APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_COLUMN_BSSID "bssid" -#define APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_COLUMN_TIMESTAMP "timestamp" -#define APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_COLUMN_TIMESTAMP_UTC "timestamp_utc" - -#define APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_TABLE "context_app_activity_classified" -#define APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_TABLE_ALIAS "app_activity_classified" -#define APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_ID "_id" -#define APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_CONTEXT_APP_INFO_ID "context_app_info_id" -#define APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_ACTIVITY_RATE "activity_rate" -#define APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_TIMEFRAME "timeframe" -#define APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_IS_ACTIVE "is_active" - - -#define APP_INACTIVE_DETECTOR_VIRTUAL_TABLE_ALIAS "virtual_app_history" -#define APP_INACTIVE_DETECTOR_VIRTUAL_COLUMN_WEIGHT "weight" - -// Output Data Key -#define APP_INACTIVE_DETECTOR_DATA_READ "AppsList" -#define APP_INACTIVE_DETECTOR_DATA_TIMESTAMP_FROM "timestamp_from" -#define APP_INACTIVE_DETECTOR_ACTIVITY_RATE "activity_rate" -#define APP_INACTIVE_DETECTOR_DATA_TIMEFRAME "timeframe" -#define APP_INACTIVE_DETECTOR_DATA_ISACTIVE "is_active" - -//Other -#define APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_TIMESTAMP "$timestamp" -#define APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_CLUSTER "$cluster" -#define APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_TIMEFRAME "$timeframe" -#define APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_VALUES "$values" - -// Objects -struct app_t { - int id; - std::string package_name; - int is_nodisplay; - int is_enabled; - int is_atboot; - int is_preloaded; - double timestamp; - int weight; - int is_active; - int timeframe; -}; - - -#define APP_TYPE_INACTIVE_INACTIVE 1 -#define APP_TYPE_INACTIVE_ACTIVE 2 - - -#define APP_INACTIVE_TASK_START_HOUR 3 -#define APP_INACTIVE_TASK_START_MINUTE 0 - - -#define CONTEXT_HISTORY_FILTER_TIME_INTERVAL_ONEDAY 60*60*24 -#define CONTEXT_HISTORY_FILTER_TIME_INTERVAL_THREEDAYS 60*60*24*3 -#define CONTEXT_HISTORY_FILTER_TIME_INTERVAL_ONEWEEK 60*60*24*7 -#define CONTEXT_HISTORY_FILTER_TIME_INTERVAL_TWOWEEKS 60*60*24*14 -#define CONTEXT_HISTORY_FILTER_TIME_INTERVAL_ONEMONTH 60*60*24*31 - - - -#endif /* __CONTEXT_APP_INACTIVE_DETECTOR_TYPES__ */ \ No newline at end of file diff --git a/src/statistics/app/app_inactive_detector/inactive_detector.cpp b/src/statistics/app/app_inactive_detector/inactive_detector.cpp deleted file mode 100644 index 5da86fe..0000000 --- a/src/statistics/app/app_inactive_detector/inactive_detector.cpp +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright (c) 2015 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include "inactive_detector.h" -#include "inactive_detector_weight.h" -#include "inactive_detector_storage.h" -#include "inactive_detector_classificator.h" -#include "TimerManager.h" - -ctx::inactive_detector::inactive_detector() -{ - 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"); - return; - } else { - update_clusters(); - _D("inactive_detector timer set SUCCESS"); - } - -} - -ctx::inactive_detector::~inactive_detector() -{ - if (timer_id >= 0) { - timer_mgr.remove(timer_id); - _D("inactive_detejctor timer removed"); - } - -} - -bool ctx::inactive_detector::onTimerExpired(int timerId) -{ - update_clusters(); - return true; -} - -int ctx::inactive_detector::update_clusters() -{ - // define timeframes stack - std::vector timeframes; - timeframes.push_back(CONTEXT_HISTORY_FILTER_TIME_INTERVAL_ONEDAY); - timeframes.push_back(CONTEXT_HISTORY_FILTER_TIME_INTERVAL_THREEDAYS); - timeframes.push_back(CONTEXT_HISTORY_FILTER_TIME_INTERVAL_ONEWEEK); - timeframes.push_back(CONTEXT_HISTORY_FILTER_TIME_INTERVAL_TWOWEEKS); - timeframes.push_back(CONTEXT_HISTORY_FILTER_TIME_INTERVAL_ONEMONTH); - - // 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); - } - - return ERR_NONE; -} - -void ctx::inactive_detector::on_cluster_update_finished(std::vector *apps_with_weight, int error) -{ - if (error == ERR_NONE) { - _I("on_cluster_update_finished success"); - } - // clean memory - delete apps_with_weight; -} - -int ctx::inactive_detector::read( - const char *subject, - ctx::Json option) -{ - ctx::inactive_detector_storage *handle = new(std::nothrow) ctx::inactive_detector_storage(); - IF_FAIL_RETURN_TAG(handle, ERR_OPERATION_FAILED, _E, "Memory allocation failed"); - - int err = handle->read(subject, option); - if (err != ERR_NONE) { - delete handle; - return err; - } - - return ERR_NONE; -} - -ctx::Json ctx::inactive_detector::to_json(std::vector *app_infos) -{ - 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); - } - return data; -} diff --git a/src/statistics/app/app_inactive_detector/inactive_detector.h b/src/statistics/app/app_inactive_detector/inactive_detector.h deleted file mode 100644 index 62701d8..0000000 --- a/src/statistics/app/app_inactive_detector/inactive_detector.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2015 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __CONTEXT_INACTIVE_DETECTOR_H__ -#define __CONTEXT_INACTIVE_DETECTOR_H__ - -#include -#include -#include -#include "app_inactive_detector_types.h" -#include - -namespace ctx { - - class inactive_detector : public ITimerListener { - private: - int timer_id; - TimerManager timer_mgr; - public: - inactive_detector(); - ~inactive_detector(); - - int read(const char *subject, - ctx::Json option); - - int update_clusters(); - void on_cluster_update_finished( - std::vector *apps_classified, - int error); - ctx::Json to_json(std::vector *apps); - - bool onTimerExpired(int timerId); - }; /* class inactive_detector */ - -} /* namespace ctx */ - - -#endif /* __CONTEXT_INACTIVE_DETECTOR_H__ */ diff --git a/src/statistics/app/app_inactive_detector/inactive_detector_classificator.cpp b/src/statistics/app/app_inactive_detector/inactive_detector_classificator.cpp deleted file mode 100644 index adcf699..0000000 --- a/src/statistics/app/app_inactive_detector/inactive_detector_classificator.cpp +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2015 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include -#include "inactive_detector.h" -#include "app_inactive_detector_types.h" -#include "inactive_detector_classificator.h" -#include "inactive_detector_classificator_kmeans.h" - -int ctx::inactive_detector_classificator::classify(std::vector *apps_with_weights) -{ - inactive_detector_classificator_kmeans kmeans; - int error = kmeans.classify(apps_with_weights); - - return error; -} \ No newline at end of file diff --git a/src/statistics/app/app_inactive_detector/inactive_detector_classificator.h b/src/statistics/app/app_inactive_detector/inactive_detector_classificator.h deleted file mode 100644 index e5655d9..0000000 --- a/src/statistics/app/app_inactive_detector/inactive_detector_classificator.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2015 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_H__ -#define __CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_H__ - -#include -#include "app_inactive_detector_types.h" -#include - -namespace ctx { - - class inactive_detector_classificator - { - public: - inactive_detector_classificator(); - ~inactive_detector_classificator(); - - int classify(std::vector *apps_with_weights); - }; /* class inactive_detector_classificator */ - -} /* namespace ctx */ - -#endif /* __CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_H__ */ diff --git a/src/statistics/app/app_inactive_detector/inactive_detector_classificator_kmeans.cpp b/src/statistics/app/app_inactive_detector/inactive_detector_classificator_kmeans.cpp deleted file mode 100644 index 298f60d..0000000 --- a/src/statistics/app/app_inactive_detector/inactive_detector_classificator_kmeans.cpp +++ /dev/null @@ -1,174 +0,0 @@ -/* - * Copyright (c) 2015 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include -#include "app_inactive_detector_types.h" -#include "inactive_detector_classificator_kmeans.h" -#include "inactive_detector_classificator_kmeans_types.h" -#include -#include - -double ctx::inactive_detector_classificator_kmeans::randomf(double x) -{ - return x * rand() / (RAND_MAX - 1.); -} - -double ctx::inactive_detector_classificator_kmeans::glide_function(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) -{ - int distr_size = apps_with_weights->size(); - point_s *p = new point_s[distr_size]; - - 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; - - 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; - - p[i] = the_point; - i++; - } - - return p; -} - -bool ctx::inactive_detector_classificator_kmeans::annotate_data(std::vector *apps_with_weights, point_s *c) -{ - int i=0, max_group=-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; - i++; - max_group = c[i].group > max_group ? c[i].group : max_group; - } - - return max_group == APP_INACTIVE_DETECTOR_KMEANS_CLUSTER_COUNT; -} - -double ctx::inactive_detector_classificator_kmeans::distance_to(point_s *p_from, - point_s *p_to) -{ - double x = p_from->x - p_to->x, y = p_from->y - p_to->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 i; - int min_i = pt->group; - point_s *c; - - double d; - double min_d = HUGE_VAL; - - for_n { - if (min_d >(d = distance_to(c, pt))) { - min_d = d; min_i = i; - } - } - - if (distance) *distance = min_d; - return min_i; -} - -void ctx::inactive_detector_classificator_kmeans::kpp(point_s *points, int length, point_s *centers, int cluster_number) -{ - int j; - int n_cluster; - double sum; - double *d = new double[length]; - - point_s *p; - centers[0] = points[rand() % length]; - for (n_cluster = 1; n_cluster < cluster_number; n_cluster++) { - sum = 0; - for_len{ - nearest(p, centers, n_cluster, d + j); - sum += d[j]; - } - sum = randomf(sum); - for_len{ - if ((sum -= d[j]) > 0) continue; - centers[n_cluster] = points[j]; - break; - } - } - for_len p->group = nearest(p, centers, n_cluster, 0); - - delete d; -} - -point_s *ctx::inactive_detector_classificator_kmeans::lloyd(point_s *points, int length, int cluster_number) -{ - int i, j, min_i; - int changed; - - point_s *centers = new point_s; - point_s *p, *c; - - /* k++ init */ - kpp(points, length, centers, cluster_number); - - 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; } - - changed = 0; - /* find closest centroid of each point */ - for_len{ - min_i = nearest(p, centers, cluster_number, 0); - if (min_i != p->group) { - changed++; - p->group = min_i; - } - } - } while (changed > (length >> 10)); /* stop when 99.9% of points are good */ - - for_n{ c->group = i; } - - return centers; -} - -int ctx::inactive_detector_classificator_kmeans::classify(std::vector *apps_with_weights) -{ - //array of generated points - point_s *v = reproject_to_2d(apps_with_weights); - //mark cluster for each point, output centers - point_s *c = lloyd(v, apps_with_weights->size(), APP_INACTIVE_DETECTOR_KMEANS_CLUSTER_COUNT); - //append the result to input data - bool classification_is_success = annotate_data(apps_with_weights, c); - - return classification_is_success ? ERR_NONE : ERR_OPERATION_FAILED; -} diff --git a/src/statistics/app/app_inactive_detector/inactive_detector_classificator_kmeans.h b/src/statistics/app/app_inactive_detector/inactive_detector_classificator_kmeans.h deleted file mode 100644 index dd99125..0000000 --- a/src/statistics/app/app_inactive_detector/inactive_detector_classificator_kmeans.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2015 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_KMEANS_H__ -#define __CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_KMEANS_H__ - -#include -#include "inactive_detector_classificator_kmeans_types.h" -#include - -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++) - - class inactive_detector_classificator_kmeans - { - 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 */ -} /* namespace ctx */ - -#endif /* __CONTEXT_INACTIVE_DETECTOR_CLASSIFICATOR_KMEANS_H__ */ diff --git a/src/statistics/app/app_inactive_detector/inactive_detector_classificator_kmeans_types.h b/src/statistics/app/app_inactive_detector/inactive_detector_classificator_kmeans_types.h deleted file mode 100644 index 8aa7270..0000000 --- a/src/statistics/app/app_inactive_detector/inactive_detector_classificator_kmeans_types.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2015 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __CONTEXT_APP_INACTIVE_DETECTOR_CLASSIFICATOR_KMEANS_TYPES__ -#define __CONTEXT_APP_INACTIVE_DETECTOR_CLASSIFICATOR_KMEANS_TYPES__ - -#define APP_INACTIVE_DETECTOR_KMEANS_CLUSTER_COUNT 2 - -typedef struct { - std::string origin_id; - double x; - double y; - int group; -} point_s; - -#endif \ No newline at end of file diff --git a/src/statistics/app/app_inactive_detector/inactive_detector_storage.cpp b/src/statistics/app/app_inactive_detector/inactive_detector_storage.cpp deleted file mode 100644 index 3cfcb2d..0000000 --- a/src/statistics/app/app_inactive_detector/inactive_detector_storage.cpp +++ /dev/null @@ -1,248 +0,0 @@ -/* - * Copyright (c) 2015 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include -#include "inactive_detector.h" -#include "inactive_detector_storage.h" -#include "inactive_detector_storage_queries.h" -#include "inactive_detector_classificator.h" -#include "app_inactive_detector_types.h" - -/*int ctx::inactive_detector_storage::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) -{ - std::string query; - query = query_get_apps(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); - - 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) -{ - double timeframe; - int is_active; - - 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); - - filter.get(NULL, APP_INACTIVE_DETECTOR_DATA_TIMEFRAME, &timeframe); - filter.get(NULL, APP_INACTIVE_DETECTOR_DATA_ISACTIVE, &is_active); - - std::stringstream timeframe_stream; - timeframe_stream << timeframe; - - std::stringstream is_active_stream; - is_active_stream << is_active; - - inject_params(query, placeholder_timeframe, timeframe_stream.str()); - inject_params(query, placeholder_is_active, is_active_stream.str()); - - return query; -} - -std::string ctx::inactive_detector_storage::query_update_apps(std::vector *apps_with_weights) -{ - 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::stringstream timeframe_stream; - timeframe_stream << apps_with_weights->front().timeframe; - - inject_params(delete_query, placeholder_timeframe, timeframe_stream.str()); - inject_params(insert_query, placeholder_values, subquery_form_values(apps_with_weights)); - - std::stringstream result; - result << delete_query << insert_query; - 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::stringstream select_elements; - - for(std::vector::iterator row = apps_with_weights->begin(); - row != apps_with_weights->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; - } - - return select_elements.str(); -} - -void ctx::inactive_detector_storage::json_to_object(std::vector& records, - std::vector *apps_with_weights, bool result_mode) -{ - 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); - } - - apps_with_weights->push_back(app_with_weight); - } -} - -void ctx::inactive_detector_storage::onExecuted(unsigned int query_id, - int error, - std::vector& records) -{ - if (error != ERR_NONE) { - _E("query_id:%d, error:%d", query_id, 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) { - - json_to_object(records, apps_with_weights, FALSE); - - 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) - { - std::string query; - query = query_update_apps(apps_with_weights); - 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 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); - } -} - -void ctx::inactive_detector_storage::inject_params(std::string& str, const std::string& from, const std::string& to) -{ - 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(); - } -} - -// normalzie weights -int ctx::inactive_detector_storage::update_ranks() -{ - return ERR_NONE; -} - -int ctx::inactive_detector_storage::get_apps_info_w_weights( - double timestamp_from) -{ - std::stringstream timestamp_stream; - timestamp_stream << timestamp_from; - std::string timestamp_str = timestamp_stream.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); - - 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/statistics/app/app_inactive_detector/inactive_detector_storage.h b/src/statistics/app/app_inactive_detector/inactive_detector_storage.h deleted file mode 100644 index 6292768..0000000 --- a/src/statistics/app/app_inactive_detector/inactive_detector_storage.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (c) 2015 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __CONTEXT_INACTIVE_DETECTOR_STORAGE_H__ -#define __CONTEXT_INACTIVE_DETECTOR_STORAGE_H__ - -#include -#include "app_inactive_detector_types.h" -#include -#include - -namespace ctx { - - class inactive_detector_storage : public IDatabaseListener - { - private: - //int type; //TODO: enum - void inject_params(std::string& str, - const std::string& from, - const std::string& to); - - void json_to_object(std::vector& records, - std::vector *apps_with_weights, bool result_mode); - - std::string query_get_apps(const char *subject, - ctx::Json filter); - - std::string query_update_apps(std::vector *apps_with_weights); - - std::string subquery_form_values(std::vector *apps_with_weights); - - 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 */ - -} /* namespace ctx */ - -#endif /* __CONTEXT_INACTIVE_DETECTOR_STORAGE_H__ */ diff --git a/src/statistics/app/app_inactive_detector/inactive_detector_storage_queries.h b/src/statistics/app/app_inactive_detector/inactive_detector_storage_queries.h deleted file mode 100644 index 469bd17..0000000 --- a/src/statistics/app/app_inactive_detector/inactive_detector_storage_queries.h +++ /dev/null @@ -1,192 +0,0 @@ -/* - * Copyright (c) 2015 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __CONTEXT_INACTIVE_DETECTOR_STORAGE_QUERIES_H__ -#define __CONTEXT_INACTIVE_DETECTOR_STORAGE_QUERIES_H__ - -#include -#include "app_inactive_detector_types.h" - -/* - ---1 calculate weights -SELECT - app_info._id, - app_info.package_name, - app_info.is_nodisplay, - app_info.is_enabled, - app_info.is_atboot, - app_info.is_preloaded, - app_info.timestamp, - - app_hist_g.weight as weight -FROM - context_app_info app_info - -INNER JOIN - - (SELECT - distinct package_name , - 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 - where package_name == app_hist.package_name AND timestamp> 8640412900) , 0) - as weight - - FROM context_app_launch_log app_hist -)app_hist_g - -ON app_hist_g.package_name -= -app_info.package_name - -ORDER BY app_hist_g.weight - -*/ - -#define GET_APP_INFO_W_WEIGHT_QUERY "SELECT "\ - " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_ID ", "\ - " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME ", "\ - " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_TYPE ", "\ - " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_NODISPLAY ", "\ - " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_ENABLED ", "\ - " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_ATBOOT ", "\ - " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_PRELOADED ", "\ - " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_TIMESTAMP ", "\ - ""\ - " " APP_INACTIVE_DETECTOR_VIRTUAL_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_VIRTUAL_COLUMN_WEIGHT \ - ""\ - " FROM " APP_INACTIVE_DETECTOR_APPINFO_TABLE " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS \ - ""\ - " INNER JOIN " \ - " (SELECT "\ - " DISTINCT " APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME "," \ - " IFNULL((SELECT COUNT(" APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME ")" \ - " FROM " APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_TABLE \ - " WHERE " APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME "==" APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME \ - " AND " \ - " " APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_COLUMN_TIMESTAMP " >= " APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_TIMESTAMP \ - "), 0) " \ - " * " \ - " IFNULL((SELECT SUM(" APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_COLUMN_DURATION ")" \ - " FROM " APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_TABLE \ - " WHERE " APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME "==" APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME \ - " AND " \ - " " APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_COLUMN_TIMESTAMP " >= " APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_TIMESTAMP \ - ""\ - "), 0) " \ - " as " APP_INACTIVE_DETECTOR_VIRTUAL_COLUMN_WEIGHT \ - " FROM " APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_TABLE " " APP_INACTIVE_DETECTOR_APPLAUNCH_LOG_TABLE_ALIAS \ - ") " APP_INACTIVE_DETECTOR_VIRTUAL_TABLE_ALIAS \ - " ON " APP_INACTIVE_DETECTOR_VIRTUAL_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME \ - " = " \ - " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME \ - " ORDER BY " APP_INACTIVE_DETECTOR_VIRTUAL_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_VIRTUAL_COLUMN_WEIGHT - -/* ---2 select inner join context_app_info -SELECT - app_info.package_name, - app_info.is_nodisplay, - app_info.is_enabled, - app_info.is_atboot, - app_info.is_preloaded, - - activity_classified.activity_rate - -FROM - context_activity_classified activity_classified -INNER JOIN - context_app_info app_info -ON activity_classified.context_app_info_id = app_info._id - -WHERE - activity_classified.is_active = 1 - AND - activity_classified.timeframe = 1 -*/ - -#define GET_APP_INFO_INACTIVE_QUERY "SELECT "\ - " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_ID ", "\ - " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME ", "\ - " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_NODISPLAY ", "\ - " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_ENABLED ", "\ - " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_ATBOOT ", "\ - " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_PRELOADED ", "\ - " "\ - " " APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_ACTIVITY_RATE " "\ - " FROM "\ - " " APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_TABLE " " APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_TABLE_ALIAS " "\ - " INNER JOIN "\ - " " APP_INACTIVE_DETECTOR_APPINFO_TABLE " " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS " "\ - " ON " APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_CONTEXT_APP_INFO_ID ""\ - " = " APP_INACTIVE_DETECTOR_APPINFO_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_APPINFO_COLUMN_ID " "\ - " WHERE "\ - APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_IS_ACTIVE ""\ - " = " APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_CLUSTER ""\ - " AND "\ - " " APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_TABLE_ALIAS "." APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_TIMEFRAME ""\ - " = " APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_TIMEFRAME "" - -/* ---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 - timeframe = 1 -*/ - -#define DELETE_APP_ACTIVITY_CLASSIFIED_BY_TIMEFRAME "DELETE FROM "\ - APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_TABLE \ - " WHERE "\ - " timeframe = " APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_TIMEFRAME - - -/*INSERT INTO - context_activity_classified - (is_active, - timeframe, - context_app_info_id) -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 - ) q -*/ - -#define INSERT_APP_ACTIVITY_CLASSIFIED " INSERT INTO "\ - APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_TABLE \ - "( " APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_IS_ACTIVE ", "\ - APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_TIMEFRAME ", "\ - APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_CONTEXT_APP_INFO_ID ")" \ -"SELECT " \ - APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_IS_ACTIVE "," \ - APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_TIMEFRAME ", "\ - APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_CONTEXT_APP_INFO_ID \ -"FROM"\ - "(" \ - APP_INACTIVE_DETECTOR_VALUE_PLACEHOLDER_VALUES \ - ") q" - - - -#endif /* __CONTEXT_INACTIVE_DETECTOR_STORAGE_QUERIES_H__ */ diff --git a/src/statistics/app/app_inactive_detector/inactive_detector_weight.cpp b/src/statistics/app/app_inactive_detector/inactive_detector_weight.cpp deleted file mode 100644 index 52ed158..0000000 --- a/src/statistics/app/app_inactive_detector/inactive_detector_weight.cpp +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2015 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include "inactive_detector_weight.h" -#include "inactive_detector_storage.h" - - -int ctx::inactive_detector_weight::request_weights( - double timestamp_from) -{ - inactive_detector_storage ids; - // query the database for the result - int error = ids.get_apps_info_w_weights(timestamp_from); - - return error; -} \ No newline at end of file diff --git a/src/statistics/app/app_inactive_detector/inactive_detector_weight.h b/src/statistics/app/app_inactive_detector/inactive_detector_weight.h deleted file mode 100644 index 8e87473..0000000 --- a/src/statistics/app/app_inactive_detector/inactive_detector_weight.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2015 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#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 */ - -} /* namespace ctx */ - -#endif /* __CONTEXT_INACTIVE_DETECTOR_WEIGHT_H__ */ \ No newline at end of file