Inactive Application detector engine add 96/61696/5
authorAndrey Glushkov <a.glushkov@samsung.com>
Thu, 10 Mar 2016 01:58:15 +0000 (10:58 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Thu, 14 Apr 2016 06:01:39 +0000 (23:01 -0700)
Change-Id: I409ec771a2287c66f406f7af3c4959a029e37079
Signed-off-by: Andrey Glushkov <a.glushkov@samsung.com>
15 files changed:
src/statistics/app/app_inactive_detector/app_inactive_detector.cpp [new file with mode: 0644]
src/statistics/app/app_inactive_detector/app_inactive_detector.h [new file with mode: 0644]
src/statistics/app/app_inactive_detector/app_inactive_detector_types.h [new file with mode: 0644]
src/statistics/app/app_inactive_detector/inactive_detector.cpp [new file with mode: 0644]
src/statistics/app/app_inactive_detector/inactive_detector.h [new file with mode: 0644]
src/statistics/app/app_inactive_detector/inactive_detector_classificator.cpp [new file with mode: 0644]
src/statistics/app/app_inactive_detector/inactive_detector_classificator.h [new file with mode: 0644]
src/statistics/app/app_inactive_detector/inactive_detector_classificator_kmeans.cpp [new file with mode: 0644]
src/statistics/app/app_inactive_detector/inactive_detector_classificator_kmeans.h [new file with mode: 0644]
src/statistics/app/app_inactive_detector/inactive_detector_classificator_kmeans_types.h [new file with mode: 0644]
src/statistics/app/app_inactive_detector/inactive_detector_storage.cpp [new file with mode: 0644]
src/statistics/app/app_inactive_detector/inactive_detector_storage.h [new file with mode: 0644]
src/statistics/app/app_inactive_detector/inactive_detector_storage_queries.h [new file with mode: 0644]
src/statistics/app/app_inactive_detector/inactive_detector_weight.cpp [new file with mode: 0644]
src/statistics/app/app_inactive_detector/inactive_detector_weight.h [new file with mode: 0644]

diff --git a/src/statistics/app/app_inactive_detector/app_inactive_detector.cpp b/src/statistics/app/app_inactive_detector/app_inactive_detector.cpp
new file mode 100644 (file)
index 0000000..3e36f7d
--- /dev/null
@@ -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 <types_internal.h>
+#include <ContextManager.h>
+#include <Json.h>
+#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
new file mode 100644 (file)
index 0000000..94e5b83
--- /dev/null
@@ -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 <ContextProviderBase.h>
+#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
new file mode 100644 (file)
index 0000000..b058b54
--- /dev/null
@@ -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 <string>
+
+// 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
new file mode 100644 (file)
index 0000000..f6e82f9
--- /dev/null
@@ -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 <ctime>
+#include <types_internal.h>
+#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<int> 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<int>::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<app_t> *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_t> *app_infos)
+{
+       ctx::Json data;
+
+       for(std::vector<app_t>::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
new file mode 100644 (file)
index 0000000..62701d8
--- /dev/null
@@ -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 <string>
+#include <Json.h>
+#include <vector>
+#include "app_inactive_detector_types.h"
+#include <TimerManager.h>
+
+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<app_t> *apps_classified,
+                               int error);
+                       ctx::Json to_json(std::vector<app_t> *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
new file mode 100644 (file)
index 0000000..9f85f94
--- /dev/null
@@ -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 <types_internal.h>
+#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<app_t> *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
new file mode 100644 (file)
index 0000000..e5655d9
--- /dev/null
@@ -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 <vector>
+#include "app_inactive_detector_types.h"
+#include <Json.h>
+
+namespace ctx {
+
+       class inactive_detector_classificator
+       {
+               public:
+                       inactive_detector_classificator();
+                       ~inactive_detector_classificator();
+
+                       int classify(std::vector<app_t> *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
new file mode 100644 (file)
index 0000000..f020875
--- /dev/null
@@ -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 <types_internal.h>
+#include "app_inactive_detector_types.h"
+#include "inactive_detector_classificator_kmeans.h"
+#include "inactive_detector_classificator_kmeans_types.h"
+#include <math.h>
+#include <stdlib.h>
+
+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<app_t> *apps_with_weights)
+{
+       int distr_size = apps_with_weights->size();
+       point_s *p = new point_s[distr_size];
+
+       int i=0;
+       for(std::vector<app_t>::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<app_t> *apps_with_weights, point_s *c)
+{
+       int i=0, max_group=-1;
+
+       for(std::vector<app_t>::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<app_t> *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
new file mode 100644 (file)
index 0000000..dd99125
--- /dev/null
@@ -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 <vector>
+#include "inactive_detector_classificator_kmeans_types.h"
+#include <Json.h>
+
+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<app_t> *apps_with_weights);
+                       bool annotate_data(std::vector<app_t> *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<app_t> *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
new file mode 100644 (file)
index 0000000..8aa7270
--- /dev/null
@@ -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/inactive_detector_storage.cpp b/src/statistics/app/app_inactive_detector/inactive_detector_storage.cpp
new file mode 100644 (file)
index 0000000..c2207a6
--- /dev/null
@@ -0,0 +1,250 @@
+/*
+ * 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 <sstream>
+#include <ctime>
+#include <string>
+#include <memory>
+#include <types_internal.h>
+#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"
+#include "db_mgr.h"
+#include <db_mgr.h>
+
+/*int ctx::inactive_detector_storage::create_table()
+{
+       bool ret = db_manager::create_table(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 = db_manager::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<app_t> *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<app_t> *apps_with_weights)
+{
+       std::stringstream select_elements;
+
+       for(std::vector<app_t>::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<Json>& records,
+       std::vector<app_t> *apps_with_weights, bool result_mode)
+{
+       for(std::vector<Json>::iterator row = records.begin();
+               row != records.end(); row++)
+       {
+               app_t app_with_weight;
+               if (result_mode)
+               {
+                       row->get(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_PACKAGE_NAME,
+                               &app_with_weight.package_name);
+                       row->get(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_NODISPLAY,
+                               &app_with_weight.is_nodisplay);
+                       row->get(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_ENABLED,
+                               &app_with_weight.is_enabled);
+                       row->get(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_ATBOOT,
+                               &app_with_weight.is_atboot);
+                       row->get(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_IS_PRELOADED,
+                               &app_with_weight.is_preloaded);
+                       row->get(NULL, APP_INACTIVE_DETECTOR_VIRTUAL_COLUMN_WEIGHT,
+                               &app_with_weight.weight);
+               }
+               else
+               {
+                       row->get(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_ID,
+                               &app_with_weight.id);
+                       row->get(NULL, APP_INACTIVE_DETECTOR_APPINFO_COLUMN_TIMESTAMP,
+                               &app_with_weight.timestamp);
+                       row->get(NULL, APP_INACTIVE_DETECTOR_VIRTUAL_COLUMN_WEIGHT,
+                               &app_with_weight.weight);
+                       row->get(NULL, APP_INACTIVE_DETECTOR_ACTIVITYCLASSIFIED_COLUMN_IS_ACTIVE,
+                               &app_with_weight.is_active);
+               }
+
+               apps_with_weights->push_back(app_with_weight);
+       }
+}
+
+void ctx::inactive_detector_storage::on_query_result_received(unsigned int query_id,
+       int error,
+       std::vector<Json>& records)
+{
+       if (error != ERR_NONE) {
+               _E("on_query_result_received query_id:%d, error:%d", query_id, error);
+               return;
+       }
+
+       std::vector<app_t> *apps_with_weights = NULL;
+       if (query_id == APP_INACTIVE_QUERY_ID_GET_APPS_INACTIVE ||
+               query_id == APP_INACTIVE_QUERY_ID_GET_APPS_ACTIVE)
+       {
+               json_to_object(records, apps_with_weights, TRUE);
+       }
+       else if (query_id == APP_INACTIVE_QUERY_ID_GET_APPS_WEIGHT) {
+
+               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 = db_manager::execute(APP_INACTIVE_QUERY_ID_UPDATE_CLUSTERS,
+                                       query.c_str(),
+                                       this);
+                               _D("load visits execute query result: %s", ret ? "SUCCESS" : "FAIL");
+                       }
+                       else
+                       {
+                               _E("on_query_result_received/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("on_query_result_received 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 = db_manager::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
new file mode 100644 (file)
index 0000000..7b03c85
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * 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 <vector>
+#include "app_inactive_detector_types.h"
+#include <Json.h>
+#include <db_listener_iface.h>
+
+namespace ctx {
+
+       class inactive_detector_storage : public db_listener_iface
+       {
+               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<Json>& records,
+                               std::vector<app_t> *apps_with_weights, bool result_mode);
+
+                       std::string query_get_apps(const char *subject,
+                               ctx::Json filter);
+
+                       std::string query_update_apps(std::vector<app_t> *apps_with_weights);
+
+                       std::string subquery_form_values(std::vector<app_t> *apps_with_weights);
+
+                       void on_creation_result_received(unsigned int query_id, int error) {}
+                       void on_insertion_result_received(unsigned int query_id, int error, int64_t row_id) {}
+                       void on_query_result_received(unsigned int query_id, int error, std::vector<Json>& records);// {}
+               
+               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
new file mode 100644 (file)
index 0000000..469bd17
--- /dev/null
@@ -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 <vector>
+#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
new file mode 100644 (file)
index 0000000..5b21c1e
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * 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 <types_internal.h>
+#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
new file mode 100644 (file)
index 0000000..8e87473
--- /dev/null
@@ -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 <string>
+// #include <sstream>
+
+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