[battery-usage] add defence code for time changes & Version 0.9.13 62/107662/4
authorMu-Woong Lee <muwoong.lee@samsung.com>
Thu, 29 Dec 2016 05:52:42 +0000 (14:52 +0900)
committerSomin Kim <somin926.kim@samsung.com>
Thu, 29 Dec 2016 10:17:36 +0000 (02:17 -0800)
Change-Id: I97470e5f0cb4d3b011e5d44060b8d50ce3527c33
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
packaging/context-provider.spec
src/battery-stats/BatteryMonitor.cpp
src/battery-stats/HeartDbReader.cpp

index 7a22304..f66d6a2 100644 (file)
@@ -1,6 +1,6 @@
 Name:       context-provider
 Summary:    Context Provider
-Version:    0.9.12
+Version:    0.9.13
 Release:    1
 Group:      Service/Context
 License:    Apache-2.0
index 3e1715f..0bb5097 100644 (file)
@@ -27,6 +27,8 @@
 #define DEFAULT_ROW_ID_STR "1"
 #define HEART_DB_QUERY_INTERVAL 10 * 60 * 1000
 #define ONE_DAY_IN_SEC 86400
+#define DEFAULT_TIME_DIFF      600
+#define TIME_DIFF_THRESHOLD    300
 
 using namespace ctx;
 
@@ -204,6 +206,13 @@ bool BatteryMonitor::__getLastCpuUsageTable(CpuUsageMap* lastHeartCpuLog)
 
 bool BatteryMonitor::__processBatteryUsage()
 {
+       if (__lastHeartAccessTime > CURRENT_TIME) {
+               __lastHeartAccessTime = CURRENT_TIME - DEFAULT_TIME_DIFF;
+               __lastFullTime = CURRENT_TIME - DEFAULT_TIME_DIFF;
+               __updateLastInfo();
+               __dbMgr.executeSync("DELETE FROM " BATTERY_LAST_CPU_USAGE_TABLE, NULL);
+       }
+
        // Read from heart cpu table
        bool ret = __heartReader->dbOpen();
        IF_FAIL_RETURN_TAG(ret, true, _E, "Failed to open heart db");
@@ -282,19 +291,21 @@ void BatteryMonitor::__removeExpiredLog()
        IF_FAIL_VOID(currentTime - lastCleanupTime >= ONE_DAY_IN_SEC);
        lastCleanupTime = currentTime;
 
-       std::stringstream query;
-       query << "DELETE FROM " BATTERY_USAGE_TABLE " WHERE " \
-               "EndTime < strftime('%s', 'now') - " << LOG_RETENTION_PERIOD;
-       __dbMgr.execute(0, query.str().c_str(), NULL);
+       char *sql = sqlite3_mprintf(
+                       "DELETE FROM " BATTERY_USAGE_TABLE " WHERE EndTime < %d OR EndTime > %d",
+                       CURRENT_TIME - LOG_RETENTION_PERIOD, CURRENT_TIME);
+       __dbMgr.execute(0, sql, NULL);
+       sqlite3_free(sql);
        _D("Remove expired log");
 }
 
 bool BatteryMonitor::__updateLastCpuUsageLog(CpuUsageMap& usage)
 {
-       IF_FAIL_RETURN_TAG(usage.size(), true, _W, "No data");
        _D("Delete all rows from app last times table");
        __dbMgr.executeSync("DELETE FROM " BATTERY_LAST_CPU_USAGE_TABLE, NULL);
 
+       IF_FAIL_RETURN_TAG(usage.size(), true, _W, "No data");
+
        std::string query("INSERT INTO " BATTERY_LAST_CPU_USAGE_TABLE "(" BATTERY_APP_ID ", " BATTERY_UTIME ", " BATTERY_STIME ", " BATTERY_TIMESTAMP ", " BATTERY_PID ") VALUES");
 
        std::string appId;
@@ -331,7 +342,7 @@ int BatteryMonitor::getLastFullTime()
 void BatteryMonitor::prepareData()
 {
        int timeDiff = CURRENT_TIME - __lastHeartAccessTime;
-       IF_FAIL_VOID_TAG(__needSync || timeDiff >= 5 * 60,
+       IF_FAIL_VOID_TAG(__needSync || timeDiff >= TIME_DIFF_THRESHOLD || timeDiff < 0,
                _D, "Battery usage was updated %d minutes ago", timeDiff / 60);
 
        _D("Request to sync heart cpu data");
index d8db28f..ee79f85 100644 (file)
@@ -85,7 +85,7 @@ bool HeartDbReader::readCpuLog(int lastHeartAccessTime, std::vector<Json>* cpuUs
 
        char* sql = sqlite3_mprintf(
                                        "SELECT " HEART_APP_ID ", " HEART_TIME ", " HEART_DATA ", " HEART_INDEX " FROM " HEART_CPU_TABLE \
-                                       " WHERE " HEART_TIME " > %d", lastHeartAccessTime);
+                                       " WHERE " HEART_TIME " > %d AND " HEART_TIME " <= %d", lastHeartAccessTime, CURRENT_TIME);
 
        char* err = NULL;
        int ret = sqlite3_exec(__heartDb, sql, __cpuUsageLogCb, cpuUsageLog, &err);