Writing last battery infos periodically rather than just at end 53/104153/6
authorKunal <kunal.chawla@samsung.com>
Mon, 12 Dec 2016 11:58:26 +0000 (20:58 +0900)
committerKunal <kunal.chawla@samsung.com>
Thu, 15 Dec 2016 08:51:53 +0000 (17:51 +0900)
To ensure no loss of info in case of sudden shutdown

Change-Id: Ifc2d3f18acab5d99c3ffe53578b17dcfbb91cb1b
Signed-off-by: Kunal <kunal.chawla@samsung.com>
src/battery-stats/BatteryMonitor.cpp
src/battery-stats/BatteryMonitor.h
src/battery-stats/BatteryStatisticsTypes.h

index 6e1687e..2a71946 100644 (file)
@@ -31,12 +31,16 @@ static int __bootingTime = 0;
 static int __lastResetTime = 0;
 static int __lastPercent = -1;
 static int __lastPercentTime = 0;
-static int __lastHeartTimestamp = 0;
+static int __lastBatteryTimeInfoDeleteTime = 0;
 static bool __isCharging = 0;
-static std::vector<Json> __batteryTimeInfoVec;
 static bool __timerRunning = false;
+static std::vector<Json> __batteryTimeInfoVec;
 
-#define DB_QUERY_DELAY 600000
+#define HEART_DB_QUERY_DELAY 600000
+#define BATTERY_TIME_INFO_DELETE_DELAY 10 * 3600
+
+#define UPDATE_BATTERY_LOGS_QUERY \
+       "UPDATE " BATTERY_TEMP_TIME_INFO " SET " BATTERY_TO_BE_PROCESSED " = %d WHERE " BATTERY_START_TIME " >= %d AND " BATTERY_END_TIME " <= %d"
 
 BatteryMonitor::BatteryMonitor()
 {
@@ -81,41 +85,24 @@ bool BatteryMonitor::__loadLastInfo()
                records[0].get(NULL, BATTERY_LAST_RESET_TIME, &__lastResetTime);
                records[0].get(NULL, BATTERY_LAST_PERCENT, &__lastPercent);
                records[0].get(NULL, BATTERY_LAST_PERCENT_TIME, &__lastPercentTime);
-               records[0].get(NULL, BATTERY_LAST_HEART_TIMESTAMP, &__lastHeartTimestamp);
+               records[0].get(NULL, BATTERY_TIME_INFO_LAST_DELETE_TIME, &__lastBatteryTimeInfoDeleteTime);
        }
        records.clear();
 
        __batteryTimeInfoVec.clear();
-       ret = __dbMgr.executeSync("SELECT * FROM " BATTERY_TEMP_TIME_INFO , &__batteryTimeInfoVec);
-
-       ret = __dbMgr.executeSync("DELETE FROM " BATTERY_TEMP_TIME_INFO, &records);
-
+       ret = __dbMgr.executeSync("SELECT * FROM " BATTERY_TEMP_TIME_INFO " WHERE " BATTERY_TO_BE_PROCESSED " = 1", &__batteryTimeInfoVec);
        return true;
 }
 
 bool BatteryMonitor::__updateLastInfo()
 {
-       if (__batteryTimeInfoVec.size() > 0) {
-               for (unsigned int i = 0; i < __batteryTimeInfoVec.size(); i++) {
-                       int64_t rowId;
-                       __dbMgr.insertSync(BATTERY_TEMP_TIME_INFO, __batteryTimeInfoVec.at(i), &rowId);
-               }
-       }
-
-       IF_FAIL_RETURN_TAG(__lastResetTime != 0 || __lastPercentTime != 0, false, _W, "Last info doesn't exist");
-
-       char *query = sqlite3_mprintf(
-                       "INSERT OR REPLACE INTO " BATTERY_LAST_INFO_TABLE " (" \
-                       BATTERY_ROW_ID ", " BATTERY_LAST_RESET_TIME ", " \
-                       BATTERY_LAST_PERCENT ", " BATTERY_LAST_PERCENT_TIME ", " BATTERY_LAST_HEART_TIMESTAMP ") VALUES (%s, %d, %d, %d, %d)",
-                       DEFAULT_ROW_ID_STR, __lastResetTime, __lastPercent, __lastPercentTime, __lastHeartTimestamp);
-
-       std::vector<Json> records;
-       bool ret = __dbMgr.executeSync(query, &records);
+       char *query = sqlite3_mprintf("INSERT OR REPLACE INTO " BATTERY_LAST_INFO_TABLE " (" \
+                       BATTERY_ROW_ID ", " BATTERY_LAST_PERCENT ", " BATTERY_LAST_PERCENT_TIME \
+                       ", " BATTERY_TIME_INFO_LAST_DELETE_TIME ", " BATTERY_LAST_RESET_TIME ") VALUES (%s, %d, %d, %d, %d)",
+                       DEFAULT_ROW_ID_STR, __lastPercent, __lastPercentTime, __lastBatteryTimeInfoDeleteTime, __lastResetTime);
+       bool ret = __dbMgr.executeSync(query, NULL);
        sqlite3_free(query);
-       IF_FAIL_RETURN_TAG(ret, false, _E, "Failed to update battery stat last info");
-
-       return true;
+       return ret;
 }
 
 void BatteryMonitor::__batteryChangeCb(device_callback_e type, void* value, void* userData)
@@ -133,18 +120,25 @@ void BatteryMonitor::__batteryChangeCb(device_callback_e type, void* value, void
                battTimeInfo.set(NULL, BATTERY_AMOUNT, percent);
                battTimeInfo.set(NULL, BATTERY_START_TIME, __lastPercentTime);
                battTimeInfo.set(NULL, BATTERY_END_TIME, currentTime);
+               battTimeInfo.set(NULL, BATTERY_TO_BE_PROCESSED, 1);
                __batteryTimeInfoVec.push_back(battTimeInfo);
 
+               int64_t rowId;
+               (instance->__dbMgr).insertSync(BATTERY_TEMP_TIME_INFO, battTimeInfo, &rowId);
+
                bool dataRemaining = instance->processBatteryUsage();
                if (dataRemaining && !__timerRunning) {
                                __timerRunning = true;
                                _D("Start timer to request HEART data");
-                               g_timeout_add(DB_QUERY_DELAY, __timeoutCb, instance);
+                               g_timeout_add(HEART_DB_QUERY_DELAY, __timeoutCb, instance);
                }
        }
 
        __lastPercentTime = currentTime;
        __lastPercent = percent;
+
+       bool ret = instance->__updateLastInfo();
+       IF_FAIL_VOID_TAG(ret, _E, "Failed to update last percent and last percent times");
 }
 
 void BatteryMonitor::__chargerChangeCb(device_callback_e type, void* value, void* userData)
@@ -153,6 +147,7 @@ void BatteryMonitor::__chargerChangeCb(device_callback_e type, void* value, void
 
        __isCharging = intptr_t(value);
        IF_FAIL_VOID(!__isCharging);
+       BatteryMonitor* instance = static_cast<BatteryMonitor*>(userData);
 
        int percent;
        if (__lastPercent < 0) {
@@ -167,6 +162,9 @@ void BatteryMonitor::__chargerChangeCb(device_callback_e type, void* value, void
                __lastResetTime = CURRENT_TIME;
                __lastPercentTime = __lastResetTime;
                _D("Charger is disconnected after fully charged. ResetTime: %d", __lastResetTime);
+
+               bool ret = instance->__updateLastInfo();
+               IF_FAIL_VOID_TAG(ret, _E, "Failed to update last reset time and last percent time");
        }
 }
 
@@ -193,15 +191,13 @@ int BatteryMonitor::start()
        IF_FAIL_RETURN_TAG(error == DEVICE_ERROR_NONE, ERR_OPERATION_FAILED, _E, "Failed to set battery charging change cb");
 
        __timerRunning = true;
-       _D("Start timer to request HEAERT data");
-       g_timeout_add(DB_QUERY_DELAY, __timeoutCb, this);
+       _D("Start timer to request HEART data");
+       g_timeout_add(HEART_DB_QUERY_DELAY, __timeoutCb, this);
        return ERR_NONE;
 }
 
 int BatteryMonitor::stop()
 {
-       __updateLastInfo();
-
        int error = device_remove_callback(DEVICE_CALLBACK_BATTERY_CAPACITY, __batteryChangeCb);
        IF_FAIL_RETURN_TAG(error == DEVICE_ERROR_NONE, ERR_OPERATION_FAILED, _E, "Failed to remove callback for battery capacity");
 
@@ -267,7 +263,8 @@ bool BatteryMonitor::processBatteryUsage()
        _D("Read %d rows from heart cpu table from %d to %d", cpuUsageLog.size(), totalStartTime, totalEndTime);
 
        // Get the last timestamp of HEART cpu data and maximum app times tables in cache
-       cpuUsageLog.back().get(NULL, BATTERY_TIMESTAMP, &__lastHeartTimestamp);
+       int lastHeartTimestamp;
+       cpuUsageLog.back().get(NULL, BATTERY_TIMESTAMP, &lastHeartTimestamp);
        CpuUsageMap lastCpuUsage;
        ret = __getLastCpuUsageTable(&lastCpuUsage);
 
@@ -280,7 +277,7 @@ bool BatteryMonitor::processBatteryUsage()
                row.get(NULL, BATTERY_START_TIME, &startTime);
                row.get(NULL, BATTERY_END_TIME, &endTime);
 
-               if (endTime > __lastHeartTimestamp) {
+               if (endTime > lastHeartTimestamp) {
                        _W("[%d] Heart cpu data is not prepared yet (%d ~ %d)", i, startTime, endTime);
                        break;
                }
@@ -301,17 +298,25 @@ bool BatteryMonitor::processBatteryUsage()
                }
        }
 
-       // Insert log of last times of apps
+       // Insert log of last times of apps, and update battery infos ToBeProcessed field
        if (i != 0) {
                ret = __insertLastCpuUsageLog(lastCpuUsage);
                if (!ret) {
                        _E("Failed to insert last Cpu Usage of apps");
                }
+
+               int processedStartTime = totalStartTime;
+               int processedEndTime;
+               __batteryTimeInfoVec[i-1].get(NULL, BATTERY_END_TIME, &processedEndTime);
+               char *sql = sqlite3_mprintf(UPDATE_BATTERY_LOGS_QUERY, 0, processedStartTime, processedEndTime);
+               __dbMgr.executeSync(sql, NULL);
+               sqlite3_free(sql);
        }
 
        // Remove completed time info
        _D("Total %d time intervals, %d intervals are calculated", __batteryTimeInfoVec.size(), i);
        __batteryTimeInfoVec.erase(__batteryTimeInfoVec.begin(), __batteryTimeInfoVec.begin() + i);
+       __deleteBatteryTimeInfo();
 
        if (__batteryTimeInfoVec.size() == 0) {
                return false;
@@ -401,3 +406,22 @@ int BatteryMonitor::getLastResetTime()
 
        return __lastResetTime;
 }
+
+void BatteryMonitor::__deleteBatteryTimeInfo()
+{
+       int batteryTimeInfoDeleteTime = CURRENT_TIME - BATTERY_TIME_INFO_DELETE_DELAY;
+       if (__lastBatteryTimeInfoDeleteTime > batteryTimeInfoDeleteTime) {
+               _D("Old battery time info already deleted");
+               return;
+       }
+
+       std::string query("DELETE FROM " BATTERY_TEMP_TIME_INFO " WHERE StartTime <= ");
+       query += std::to_string(batteryTimeInfoDeleteTime);
+
+       __dbMgr.executeSync(query.c_str(), NULL);
+       __lastBatteryTimeInfoDeleteTime = CURRENT_TIME;
+
+       bool ret = __updateLastInfo();
+       IF_FAIL_VOID_TAG(ret, _E, "Failed to update last battery time info deletion time");
+       _D("Old battery time info deleted");
+}
index 2ac3db8..c97a2bc 100644 (file)
@@ -40,7 +40,6 @@ namespace ctx {
                bool __loadLastInfo();
                bool __updateLastInfo();
 
-               static int __timeoutCb(void* data);
                static void __batteryChangeCb(device_callback_e type, void* value, void* userData);
                static void __chargerChangeCb(device_callback_e type, void* value, void* userData);
 
@@ -48,6 +47,9 @@ namespace ctx {
                bool __insertLastCpuUsageLog(CpuUsageMap& usage);
                bool __getLastCpuUsageTable(CpuUsageMap* lastCpuUsage);
 
+               void __deleteBatteryTimeInfo();
+               static int __timeoutCb(void* data);
+
                DatabaseManager __dbMgr;
                BatteryUsageAnalyzer __analyzer;
 
index 31622bc..d9bead6 100644 (file)
        "LastResetTime UNSIGNED INT NOT NULL DEFAULT 0, " \
        "LastPercent UNSIGNED INT NOT NULL DEFAULT 0, " \
        "LastPercentTime UNSIGNED INT NOT NULL DEFAULT 0, " \
-       "LastHeartTimestamp UNSIGNED INT NOT NULL DEFAULT 0"
+       "LastBatteryTimeInfoDeleteTime UNSIGNED INT NOT NULL DEFAULT 0"
 
 #define BATTERY_TEMP_TIME_INFO "Temp_BatteryTimeInfo"
 #define BATTERY_TEMP_TIME_INFO_COLUMNS \
        "Amount INTEGER NOT NULL DEFAULT 0, StartTime INTEGER NOT NULL DEFAULT 0, " \
-       "EndTime INTEGER NOT NULL DEFAULT 0"
+       "EndTime INTEGER NOT NULL DEFAULT 0, ToBeProcessed BOOLEAN DEFAULT 1"
 
 #define BATTERY_LAST_CPU_USAGE_TABLE "Temp_LastCpuUsagePerApp"
 #define BATTERY_LAST_CPU_USAGE_TABLE_COLUMNS \
@@ -59,8 +59,9 @@
 #define BATTERY_LAST_RESET_TIME "LastResetTime"
 #define BATTERY_LAST_PERCENT "LastPercent"
 #define BATTERY_LAST_PERCENT_TIME "LastPercentTime"
-#define BATTERY_LAST_HEART_TIMESTAMP "LastHeartTimestamp"
+#define BATTERY_TIME_INFO_LAST_DELETE_TIME "LastBatteryTimeInfoDeleteTime"
 #define BATTERY_ROW_ID "RowId"
+#define BATTERY_TO_BE_PROCESSED "ToBeProcessed"
 #define BATTERY_PID "Pid"
 
 #define CURRENT_TIME (int)(time(0))