Merge Version 0.5.5 94/45294/1 accepted/tizen/mobile/20150810.043506 accepted/tizen/tv/20150810.043543 submit/tizen_mobile/20150810.020750 submit/tizen_tv/20150810.020506
authorMu-Woong <muwoong.lee@samsung.com>
Tue, 4 Aug 2015 12:08:35 +0000 (21:08 +0900)
committerMu-Woong <muwoong.lee@samsung.com>
Tue, 4 Aug 2015 12:08:35 +0000 (21:08 +0900)
- Add expired log cleanup routine to the media db monitor
- Add expired log cleanup routine to the contact log puller
- Add expired log cleanup routine to the active app monitor

Change-Id: Id120c31cbfbbbf14547c7bb567881bdb1deda586
Signed-off-by: Mu-Woong <muwoong.lee@samsung.com>
packaging/statistics-context-provider.spec
src/app/app_use_monitor/active_window_monitor.cpp
src/app/app_use_monitor/active_window_monitor.h
src/media/media_content_monitor.cpp
src/media/media_content_monitor.h
src/media/media_stats_types.h
src/shared/common_types.h
src/social/log_aggregator.cpp
src/social/log_aggregator.h

index 4497991..b8d57ab 100644 (file)
@@ -1,6 +1,6 @@
 Name:       statistics-context-provider
 Summary:    Statistics Context Provider
-Version:    0.5.3
+Version:    0.5.5
 Release:    1
 Group:      System/Libraries
 License:    Apache-2.0
index e73398f..1aca2c7 100644 (file)
@@ -16,7 +16,6 @@
 
 #include <sys/types.h>
 #include <time.h>
-#include <sstream>
 #include <Ecore_X.h>
 #include <app_manager.h>
 
  * We thus consider the apps being foregrounded at least 3 secs */
 #define MIN_VALID_USE_TIME 2
 
+#define ONE_DAY_IN_SEC 86400
+
 static Ecore_Event_Handler *window_property_event_handler = NULL;
 
 ctx::app_use_monitor::app_use_monitor()
-       : last_timestamp(0)
+       : last_cleanup_time(0)
+       , last_timestamp(0)
        , last_pid(-1)
 {
        start_logging();
@@ -174,8 +176,19 @@ void ctx::app_use_monitor::insert_log(const char *app_id, int duration)
        vals << duration;
 
        std::stringstream query;
+       append_cleanup_query(query);
        query << "INSERT INTO " << APP_TABLE_USAGE_LOG << " ("
                << cols.str() << ") VALUES (" << vals.str() << ")";
 
        db_manager::execute(0, query.str().c_str(), NULL);
 }
+
+void ctx::app_use_monitor::append_cleanup_query(std::stringstream &query)
+{
+       IF_FAIL_VOID(last_timestamp - last_cleanup_time >= ONE_DAY_IN_SEC);
+
+       last_cleanup_time = last_timestamp;
+
+       query << "DELETE FROM " APP_TABLE_USAGE_LOG " WHERE " \
+               STATS_UNIV_TIME " < strftime('%s', 'now') - " << LOG_RETENTION_PERIOD << ";";
+}
index e853c7c..443d2bc 100644 (file)
 #define __CONTEXT_APP_USE_MONITOR_H__
 
 #include <string>
+#include <sstream>
 #include <Ecore.h>
 
 namespace ctx {
 
        class app_use_monitor {
        private:
+               int last_cleanup_time;
                int last_timestamp;
                int last_pid;
                std::string last_app_id;
@@ -33,6 +35,7 @@ namespace ctx {
 
                void verify_used_app(const char *app_id, int duration);
                void insert_log(const char *app_id, int duration);
+               void append_cleanup_query(std::stringstream &query);
 
                void on_active_window_changed(int pid);
                static Eina_Bool on_window_property_changed(void* data, int type, void* event);
index e92c290..4fbeed7 100644 (file)
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include <sstream>
+#include <time.h>
 #include <types_internal.h>
 #include <db_mgr.h>
 #include <system_info.h>
 #include "db_handle.h"
 #include "media_content_monitor.h"
 
+#define PLAYCOUNT_RETENTION_PERIOD 259200      /* 1 month in secs */
+#define ONE_DAY_IN_SEC 86400
+
 ctx::media_content_monitor::media_content_monitor()
        : started(false)
+       , last_cleanup_time(0)
 {
        db_manager::create_table(0, MEDIA_TABLE_NAME, MEDIA_TABLE_COLUMNS, NULL, NULL);
        db_manager::execute(0, MEDIA_PLAYCOUNT_TABLE_SCHEMA, NULL);
@@ -85,13 +89,32 @@ void ctx::media_content_monitor::on_media_content_db_updated(
                        cnt);
 }
 
+void ctx::media_content_monitor::append_cleanup_query(std::stringstream &query)
+{
+       int timestamp = static_cast<int>(time(NULL));
+       IF_FAIL_VOID(timestamp - last_cleanup_time >= ONE_DAY_IN_SEC);
+
+       last_cleanup_time = timestamp;
+
+       query <<
+               "DELETE FROM Log_MediaPlayCount WHERE UTC < strftime('%s', 'now') - " << PLAYCOUNT_RETENTION_PERIOD << ";" \
+               "DELETE FROM " MEDIA_TABLE_NAME " WHERE UTC < strftime('%s', 'now') - " << LOG_RETENTION_PERIOD << ";";
+}
+
 void ctx::media_content_monitor::update_play_count(const char *uuid, int type, int count)
 {
        std::stringstream query;
-       query << "INSERT OR IGNORE INTO Log_MediaPlayCount" \
+       query <<
+               /* Inserting the media record to the play count table, if not exist */
+               "INSERT OR IGNORE INTO Log_MediaPlayCount" \
                " (UUID, MediaType) VALUES ('" << uuid << "'," << type <<");" \
+               /* Updating the play count and getting the diff from the previous count */
                "UPDATE Log_MediaPlayCount SET Diff = " << count << " - Count," \
-               " Count = " << count << " WHERE UUID = '" << uuid << "';" \
+               " Count = " << count << ", UTC = strftime('%s', 'now')" \
+               " WHERE UUID = '" << uuid << "';";
+       append_cleanup_query(query);
+       query <<
+               /* Checking whether the play count changes */
                "SELECT MediaType FROM Log_MediaPlayCount" \
                " WHERE UUID = '" << uuid << "' AND Diff > 0;";
 
index 0c7dbfd..d51d54c 100644 (file)
@@ -17,6 +17,7 @@
 #ifndef __CONTEXT_MEDIA_CONTENT_MONITOR_H__
 #define __CONTEXT_MEDIA_CONTENT_MONITOR_H__
 
+#include <sstream>
 #include <media_content.h>
 #include <db_listener_iface.h>
 
@@ -25,9 +26,12 @@ namespace ctx {
        class media_content_monitor : public db_listener_iface {
        private:
                bool started;
+               int last_cleanup_time;
+
                bool start_monitoring();
                void stop_monitoring();
 
+               void append_cleanup_query(std::stringstream &query);
                void update_play_count(const char *uuid, int type, int count);
                void insert_log(int media_type);
 
index ede37f7..278b5e5 100644 (file)
@@ -37,7 +37,8 @@
 #define MEDIA_PLAYCOUNT_TABLE_SCHEMA \
        "CREATE TABLE IF NOT EXISTS Log_MediaPlayCount" \
        " (UUID TEXT NOT NULL PRIMARY KEY, MediaType INTEGER NOT NULL," \
-       " Count INTEGER DEFAULT 0, Diff INTEGER DEFAULT 0)"
+       " Count INTEGER DEFAULT 0, Diff INTEGER DEFAULT 0, " \
+       " UTC TIMESTAMP DEFAULT (strftime('%s', 'now')))"
 
 #define CX_MEDIA_TYPE          "MediaType"
 
index 952ea26..e8a3424 100644 (file)
@@ -17,6 +17,8 @@
 #ifndef __CONTEXT_STATS_COMMON_TYPES_H__
 #define __CONTEXT_STATS_COMMON_TYPES_H__
 
+#define LOG_RETENTION_PERIOD   7776000         /* 90 days in secs */
+
 #define DEFAULT_TIMESPAN               30
 #define DEFAULT_LIMIT                  10
 
index ce6dcdf..b94400a 100644 (file)
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <sstream>
 #include <json.h>
 #include <db_mgr.h>
 #include <timer_mgr.h>
@@ -73,6 +74,7 @@ void ctx::contact_log_aggregator::on_query_result_received(unsigned int query_id
        get_updated_contact_log_list(last_time, &list);
        IF_FAIL_VOID(list);
 
+       remove_expired_log();
        insert_contact_log_list(list);
        destroy_contact_log_list(list);
 }
@@ -150,3 +152,11 @@ void ctx::contact_log_aggregator::insert_contact_log_list(contacts_list_h list)
 
        } while(contacts_list_next(list) == CONTACTS_ERROR_NONE);
 }
+
+void ctx::contact_log_aggregator::remove_expired_log()
+{
+       std::stringstream query;
+       query << "DELETE FROM " SOCIAL_TABLE_CONTACT_LOG " WHERE " \
+               STATS_UNIV_TIME " < strftime('%s', 'now') - " << LOG_RETENTION_PERIOD;
+       db_manager::execute(0, query.str().c_str(), NULL);
+}
index e7b0c62..57c154f 100644 (file)
@@ -31,6 +31,7 @@ namespace ctx {
                        void get_updated_contact_log_list(int last_time, contacts_list_h *list);
                        void insert_contact_log_list(contacts_list_h list);
                        void destroy_contact_log_list(contacts_list_h list);
+                       void remove_expired_log();
 
                public:
                        contact_log_aggregator();