Battery Stats Algorithm modified to include CPU Usage of both background and foregrou... 78/89078/14
authorKunal <kunal.chawla@samsung.com>
Thu, 29 Sep 2016 06:53:56 +0000 (15:53 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 17 Oct 2016 09:46:05 +0000 (02:46 -0700)
Applied code review comments and coding rules, and bug fixes

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

index 6c9ba5d..4352019 100644 (file)
@@ -20,7 +20,6 @@
 #include <glib.h>
 #include <Types.h>
 #include <Json.h>
-#include "BatteryStatisticsTypes.h"
 #include "HeartDbReader.h"
 #include "BatteryMonitor.h"
 
@@ -31,6 +30,7 @@ using namespace ctx;
 static int __lastResetTime = 0;
 static int __lastPercent = -1;
 static int __lastPercentTime = 0;
+static int __lastHeartTimestamp = 0;
 static bool __isCharging = 0;
 static std::vector<Json> __batteryTimeInfoVec;
 
@@ -55,6 +55,10 @@ void BatteryMonitor::__initialize()
                "CREATE TABLE IF NOT EXISTS " BATTERY_LAST_INFO_TABLE \
                " (" BATTERY_LAST_INFO_TABLE_COLUMNS ")", &records);
 
+       __dbMgr.executeSync(
+               "CREATE TABLE IF NOT EXISTS " BATTERY_LAST_CPU_USAGE_TABLE \
+               " (" BATTERY_LAST_CPU_USAGE_TABLE_COLUMNS ")", &records);
+
        __dbMgr.createTableSync(BATTERY_TEMP_TIME_INFO, BATTERY_TEMP_TIME_INFO_COLUMNS, NULL);
 }
 
@@ -72,11 +76,12 @@ 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.clear();
 
        __batteryTimeInfoVec.clear();
-       ret = __dbMgr.executeSync("SELECT *  FROM " BATTERY_TEMP_TIME_INFO , &__batteryTimeInfoVec);
+       ret = __dbMgr.executeSync("SELECT * FROM " BATTERY_TEMP_TIME_INFO , &__batteryTimeInfoVec);
 
        ret = __dbMgr.executeSync("DELETE FROM " BATTERY_TEMP_TIME_INFO, &records);
 
@@ -97,8 +102,8 @@ bool BatteryMonitor::__updateLastInfo()
        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 ") VALUES (%s, %d, %d, %d)",
-                       DEFAULT_ROW_ID_STR, __lastResetTime, __lastPercent, __lastPercentTime);
+                       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);
@@ -181,6 +186,36 @@ int BatteryMonitor::stop()
        return ERR_NONE;
 }
 
+bool BatteryMonitor::__getLastCpuUsageTable(CpuUsageMap* lastCpuUsage)
+{
+       std::vector<Json> lastCpuUsageLog;
+       bool ret = __dbMgr.executeSync(
+                       "SELECT * FROM " BATTERY_LAST_CPU_USAGE_TABLE,
+                       &lastCpuUsageLog);
+       IF_FAIL_RETURN_TAG(ret, false, _E, "Failed to load last Cpu Times of Apps");
+
+       unsigned int k = 0;
+       std::string appId;
+       int timestamp;
+       int stime;
+       int utime;
+
+       while (k < lastCpuUsageLog.size()) {
+               lastCpuUsageLog[k].get(NULL, BATTERY_APP_ID, &appId);
+               lastCpuUsageLog[k].get(NULL, BATTERY_TIMESTAMP, &timestamp);
+               lastCpuUsageLog[k].get(NULL, BATTERY_UTIME, &utime);
+               lastCpuUsageLog[k].get(NULL, BATTERY_STIME, &stime);
+               k++;
+
+               LastAppCpuUsageInfo lastAppCpuUsage;
+               lastAppCpuUsage.timestamp = timestamp;
+               lastAppCpuUsage.utime = utime;
+               lastAppCpuUsage.stime = stime;
+               (*lastCpuUsage)[appId] = lastAppCpuUsage;
+       }
+       return true;
+}
+
 bool BatteryMonitor::processBatteryUsage()
 {
        IF_FAIL_RETURN_TAG(__batteryTimeInfoVec.size() > 0, true, _D, "All per-app battery usages are already calculated");
@@ -195,16 +230,17 @@ bool BatteryMonitor::processBatteryUsage()
        bool ret = heartReader.open();
        IF_FAIL_RETURN_TAG(ret, false, _E, "Failed to open heart db");
 
-       std::vector<Json> appLog;
-       ret = heartReader.readAppLaunchLog(totalStartTime, totalEndTime, &appLog);
+       std::vector<Json> cpuUsageLog;
+       ret = heartReader.readCpuUsageLog(totalStartTime, totalEndTime, &cpuUsageLog);
        heartReader.close();
        IF_FAIL_RETURN_TAG(ret, false, _E, "Cannot read from heart cpu table");
-       IF_FAIL_RETURN_TAG(appLog.size() > 0, true, _W, "Heart cpu data is not prepared yet (%d ~ %d)", totalStartTime, totalEndTime);
-       _D("Read %d rows from heart cpu table from %d to %d", appLog.size(), totalStartTime, totalEndTime);
+       IF_FAIL_RETURN_TAG(cpuUsageLog.size() > 0, true, _W, "Heart cpu data is not prepared yet (%d ~ %d)", totalStartTime, totalEndTime);
+       _D("Read %d rows from heart cpu table from %d to %d", cpuUsageLog.size(), totalStartTime, totalEndTime);
 
-       // Get the last timestamp of HEART cpu data
-       int lastHeartTimestamp;
-       appLog.back().get(NULL, BATTERY_TIMESTAMP, &lastHeartTimestamp);
+       // Get the last timestamp of HEART cpu data and maximum app times tables in cache
+       cpuUsageLog.back().get(NULL, BATTERY_TIMESTAMP, &__lastHeartTimestamp);
+       CpuUsageMap lastCpuUsage;
+       ret = __getLastCpuUsageTable(&lastCpuUsage);
 
        unsigned int i;
        for (i = 0; i < __batteryTimeInfoVec.size(); i++) {
@@ -215,14 +251,14 @@ 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;
                }
 
                // Calculate per app battery usage
                std::vector<Json> usage;
-               ret = __analyzer.calculateBatteryUsage(startTime, endTime, appLog, &usage);
+               ret = __analyzer.calculateBatteryUsage(startTime, endTime, cpuUsageLog, lastCpuUsage, &usage);
                if (!ret) {
                        _E("[%d] Failed to calculate battery usage (%d ~ %d)", i, startTime, endTime);
                        continue;
@@ -230,12 +266,20 @@ bool BatteryMonitor::processBatteryUsage()
                _D("[%d] Battery usage per app calculated (%d ~ %d)", i, startTime, endTime);
 
                // Insert battery usage
-               ret = __insertUsageLog(usage);
+               ret = __insertBatteryUsageLog(usage);
                if (!ret) {
                        _E("Failed to insert per app battery usage");
                }
        }
 
+       // Insert log of last times of apps
+       if (i != 0) {
+               ret = __insertLastCpuUsageLog(lastCpuUsage);
+               if (!ret) {
+                       _E("Failed to insert last Cpu Usage of apps");
+               }
+       }
+
        // Remove completed time info
        _D("Total %d time intervals, %d intervals are calculated", __batteryTimeInfoVec.size(), i);
        __batteryTimeInfoVec.erase(__batteryTimeInfoVec.begin(), __batteryTimeInfoVec.begin() + i);
@@ -243,14 +287,17 @@ bool BatteryMonitor::processBatteryUsage()
        return true;
 }
 
-bool BatteryMonitor::__insertUsageLog(std::vector<Json>& usage)
+bool BatteryMonitor::__insertBatteryUsageLog(std::vector<Json>& usage)
 {
        IF_FAIL_RETURN_TAG(usage.size(), true, _W, "No data");
-       std::string query = std::string("INSERT INTO " BATTERY_USAGE_TABLE \
-                               "(" BATTERY_APP_ID ", " BATTERY_START_TIME ", " BATTERY_END_TIME "," BATTERY_AMOUNT ") VALUES");
+       std::string query("INSERT INTO " BATTERY_USAGE_TABLE \
+                               "(" BATTERY_APP_ID ", " BATTERY_START_TIME ", " \
+                               BATTERY_END_TIME ", " BATTERY_UTIME ", " \
+                               BATTERY_STIME ", " BATTERY_AMOUNT ") VALUES");
 
        std::string appId;
        int startTime;
+       int stime, utime;
        int endTime;
        double amount;
 
@@ -258,19 +305,54 @@ bool BatteryMonitor::__insertUsageLog(std::vector<Json>& usage)
                usage[i].get(NULL, BATTERY_APP_ID, &appId);
                usage[i].get(NULL, BATTERY_START_TIME, &startTime);
                usage[i].get(NULL, BATTERY_END_TIME, &endTime);
+               usage[i].get(NULL, BATTERY_UTIME, &utime);
+               usage[i].get(NULL, BATTERY_STIME, &stime);
                usage[i].get(NULL, BATTERY_AMOUNT, &amount);
 
-               query += std::string(" ('" + appId + "', " + std::to_string(startTime) + ", " +
-                               std::to_string(endTime) + ", " + std::to_string((int) (amount * 10000)) + ")");
+               query += " ('" + appId + "', "
+                               + std::to_string(startTime) + ", "
+                               + std::to_string(endTime) + ", "
+                               + std::to_string(utime) + ", "
+                               + std::to_string(stime) + ", "
+                               + std::to_string((int) (amount * 10000)) + ")";
 
-               if (i != usage.size() - 1 ) {
-                       query += ", ";
-               }
+               query += ", ";
        }
+       query = query.substr(0, query.size() - 2);
 
        _D("Insert %d rows of per app battery usage", usage.size());
-       std::vector<Json> records;
-       return __dbMgr.executeSync(query.c_str(), &records);
+       return __dbMgr.executeSync(query.c_str(), NULL);
+}
+
+bool BatteryMonitor::__insertLastCpuUsageLog(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);
+
+       std::string query("INSERT INTO " BATTERY_LAST_CPU_USAGE_TABLE "(" BATTERY_APP_ID ", " BATTERY_UTIME ", " BATTERY_STIME "," BATTERY_TIMESTAMP ") VALUES");
+
+       std::string appId;
+       int stime, utime;
+       int timestamp;
+
+       for (auto it = usage.begin(); it != usage.end(); it++) {
+               appId = it->first;
+               timestamp = (it->second).timestamp;
+               utime = (it->second).utime;
+               stime = (it->second).stime;
+
+               query += " ('" + appId
+                               + "', " + std::to_string(utime)
+                               + ", " + std::to_string(stime)
+                               + ", " + std::to_string(timestamp) + ")";
+
+               query += ", ";
+       }
+
+       _D("Insert %d rows in app last times table", usage.size());
+       query = query.substr(0, query.size() - 2);
+       return __dbMgr.executeSync(query.c_str(), NULL);
 }
 
 int BatteryMonitor::getLastResetTime()
index 361a08f..7ec34f8 100644 (file)
@@ -43,7 +43,9 @@ namespace ctx {
                static void __batteryChangeCb(device_callback_e type, void* value, void* userData);
                static void __chargerChangeCb(device_callback_e type, void* value, void* userData);
 
-               bool __insertUsageLog(std::vector<Json>& usage);
+               bool __insertBatteryUsageLog(std::vector<Json>& usage);
+               bool __insertLastCpuUsageLog(CpuUsageMap& usage);
+               bool __getLastCpuUsageTable(CpuUsageMap* lastCpuUsage);
 
                DatabaseManager __dbMgr;
                BatteryUsageAnalyzer __analyzer;
index dc43678..d22faa7 100644 (file)
 #define _CONTEXT_STATS_BATTERY_TYPES_H_
 
 #include <ProviderTypes.h>
+#include <map>
 
 #define BATTERY_HISTORY_PRIV   PRIV_APP_HISTORY
 
 #define BATTERY_USAGE_TABLE "Log_BatteryUsagePerApp"
 #define BATTERY_USAGE_TABLE_COLUMNS \
        "AppId TEXT NOT NULL, StartTime INTEGER NOT NULL DEFAULT 0, " \
-       "EndTime INTEGER NOT NULL DEFAULT 0, Amount INTEGER NOT NULL DEFAULT 0"
+       "EndTime INTEGER NOT NULL DEFAULT 0, Amount INTEGER NOT NULL DEFAULT 0, " \
+       "Utime INTEGER NOT NULL DEFAULT 0, Stime INTEGER NOT NULL DEFAULT 0"
 
 #define BATTERY_LAST_INFO_TABLE "Battery_LastInfo"
 #define BATTERY_LAST_INFO_TABLE_COLUMNS \
        "RowId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " \
        "LastResetTime UNSIGNED INT NOT NULL DEFAULT 0, " \
-       "LastPercent UNSIGNED INT NOT NULL DEFAULT 0," \
-       "LastPercentTime 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"
 
 #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"
 
+#define BATTERY_LAST_CPU_USAGE_TABLE "Temp_LastCpuUsagePerApp"
+#define BATTERY_LAST_CPU_USAGE_TABLE_COLUMNS \
+       "AppId TEXT PRIMARY KEY, Utime INTEGER NOT NULL DEFAULT 0, " \
+       "Stime INTEGER NOT NULL DEFAULT 0, Timestamp INTEGER NOT NULL DEFAULT 0"
+
 #define BATTERY_APP_ID "AppId"
+#define BATTERY_INDEX "idx"
 #define BATTERY_TIMESTAMP "Timestamp"
 #define BATTERY_TYPE "Type"
+#define BATTERY_DATA "Data"
 #define BATTERY_START_TIME "StartTime"
 #define BATTERY_END_TIME "EndTime"
+#define BATTERY_UTIME "Utime"
+#define BATTERY_STIME "Stime"
 #define BATTERY_AMOUNT "Amount"
 #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_ROW_ID "RowId"
 
 #define CURRENT_TIME (int)(time(0))
 
+/* Typedefs */
+namespace ctx {
+       struct LastAppCpuUsageInfo {
+               int timestamp;
+               int utime;
+               int stime;
+       };
+       typedef std::map<std::string, LastAppCpuUsageInfo> CpuUsageMap;
+}
+
 enum AppLaunchType {
        TYPE_SERVICE = 0,
        TYPE_FOREGROUND,
index e02b7be..73f1b2c 100644 (file)
 
 #include <map>
 #include <Types.h>
-#include "BatteryStatisticsTypes.h"
 #include "BatteryUsageAnalyzer.h"
 
 using namespace ctx;
 
-bool BatteryUsageAnalyzer::calculateBatteryUsage(int lastTime, int currTime, std::vector<Json>& appLog, std::vector<Json>* usage)
+void BatteryUsageAnalyzer::__normalizeBatteryUsage(std::vector<Json>* usage, int totalUtime, int totalStime)
+{
+       if (totalStime + totalUtime == 0) {
+               return;
+       }
+
+       int utime;
+       int stime;
+       double batteryUsage;
+
+       for (unsigned int i= 0; i < usage->size(); i++) {
+               (*usage)[i].get(NULL, BATTERY_UTIME, &utime);
+               (*usage)[i].get(NULL, BATTERY_STIME, &stime);
+               batteryUsage = ((double)(utime + stime)) / ((double)(totalUtime + totalStime));
+               (*usage)[i].set(NULL, BATTERY_AMOUNT, batteryUsage);
+       }
+}
+
+bool BatteryUsageAnalyzer::calculateBatteryUsage(int lastTime, int currTime, std::vector<Json>& cpuUsageLog, CpuUsageMap& lastCpuUsage, std::vector<Json>* usage)
 {
        int timeInterval = currTime - lastTime;
        IF_FAIL_RETURN(timeInterval > 0, false);
 
-       std::map<std::string, int> lastAppTimeMap;
        std::string appId;
        int timestamp;
-       int type;
+       int stime;
+       int utime;
+       int idx;
+       int totalUtime = 0;
+       int totalStime = 0;
 
-       for (unsigned int i = 0; i < appLog.size(); i++) {
-               appLog[i].get(NULL, BATTERY_APP_ID, &appId);
-               appLog[i].get(NULL, BATTERY_TIMESTAMP, &timestamp);
-               appLog[i].get(NULL, BATTERY_TYPE, &type);
+       for (unsigned int i = 0; i < cpuUsageLog.size(); i++) {
+               cpuUsageLog[i].get(NULL, BATTERY_APP_ID, &appId);
+               cpuUsageLog[i].get(NULL, BATTERY_TIMESTAMP, &timestamp);
+               cpuUsageLog[i].get(NULL, BATTERY_UTIME, &utime);
+               cpuUsageLog[i].get(NULL, BATTERY_STIME, &stime);
+               cpuUsageLog[i].get(NULL, BATTERY_INDEX, &idx);
 
                if (timestamp < lastTime) {
                        continue;
                }
-
                if (timestamp > currTime) {
                        break;
                }
 
-               if (type == TYPE_FOREGROUND) {  // Mark timestamp of foreground
-                       lastAppTimeMap[appId] = timestamp;
-               } else if (type == TYPE_BACKGROUND) {   // Foreground duration calculation (Foreground->Background)
+               //If CPU table is reset, clear last app times and make lastTime as the timestamp
+               if (idx == 0) {
+                       lastCpuUsage.clear();
+                       lastTime = timestamp;
+               }
+
+               auto lastAppCpuUsage = lastCpuUsage.find(appId);
+               int prevUtime = 0;
+               int prevStime = 0;
+               int prevTimestamp = 0;
+
+               if (lastAppCpuUsage != lastCpuUsage.end()) {
+                       prevTimestamp = (lastAppCpuUsage->second).timestamp;
+                       prevUtime = (lastAppCpuUsage->second).utime;
+                       prevStime = (lastAppCpuUsage->second).stime;
+
                        Json row;
-                       if (lastAppTimeMap.find(appId) == lastAppTimeMap.end() || lastAppTimeMap[appId] == 0) {
-                               __createBatteryUsage(appId, lastTime, timestamp, timeInterval, &row);
-                       } else {
-                               __createBatteryUsage(appId, lastAppTimeMap[appId], timestamp, timeInterval, &row);
-                               lastAppTimeMap[appId] = 0;
-                       }
+                       row.set(NULL, BATTERY_APP_ID, appId);
+                       row.set(NULL, BATTERY_START_TIME, prevTimestamp);
+                       row.set(NULL, BATTERY_END_TIME, timestamp);
+                       row.set(NULL, BATTERY_AMOUNT, 0);
+                       row.set(NULL, BATTERY_UTIME, utime - prevUtime);
+                       row.set(NULL, BATTERY_STIME, stime - prevStime);
+
+                       totalUtime += utime - prevUtime;
+                       totalStime += stime - prevStime;
                        usage->push_back(row);
-               }
-       }
 
-       // Not closed app
-       for (auto it = lastAppTimeMap.begin(); it != lastAppTimeMap.end(); it++) {
-               if (it->second == 0) {
-                       continue;
+               } else {
+                       Json row;
+                       row.set(NULL, BATTERY_APP_ID, appId);
+                       row.set(NULL, BATTERY_START_TIME, lastTime);
+                       row.set(NULL, BATTERY_END_TIME, timestamp);
+                       row.set(NULL, BATTERY_AMOUNT, 0);
+                       row.set(NULL, BATTERY_UTIME, utime);
+                       row.set(NULL, BATTERY_STIME, stime);
+
+                       totalStime += stime;
+                       totalUtime += utime;
+                       usage->push_back(row);
                }
 
-               Json row;
-               __createBatteryUsage(it->first, it->second, currTime, timeInterval, &row);
-               usage->push_back(row);
+               LastAppCpuUsageInfo lastAppUsage;
+               lastAppUsage.timestamp = timestamp;
+               lastAppUsage.utime = utime;
+               lastAppUsage.stime = stime;
+               lastCpuUsage[appId] = lastAppUsage;
        }
-
+       __normalizeBatteryUsage(usage, totalUtime, totalStime);
        return true;
 }
-
-void BatteryUsageAnalyzer::__createBatteryUsage(std::string appId, int startTime, int endTime, int timeInterval, Json* record)
-{
-       record->set(NULL, BATTERY_APP_ID, appId);
-       record->set(NULL, BATTERY_START_TIME, startTime);
-       record->set(NULL, BATTERY_END_TIME, endTime);
-       record->set(NULL, BATTERY_AMOUNT, (double)(endTime - startTime) / (double)timeInterval);
-}
index 982ca59..f5c8333 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <vector>
 #include <Json.h>
+#include "BatteryStatisticsTypes.h"
 
 namespace ctx {
 
@@ -28,10 +29,10 @@ namespace ctx {
                BatteryUsageAnalyzer() {};
                ~BatteryUsageAnalyzer() {};
 
-               bool calculateBatteryUsage(int lastTime, int currTime, std::vector<Json>& appLog, std::vector<Json>* usage);
+               bool calculateBatteryUsage(int lastTime, int currTime, std::vector<Json>& cpuUsageLog, CpuUsageMap& lastCpuUsage, std::vector<Json>* usage);
 
        private:
-               void __createBatteryUsage(std::string appId, int startTime, int endTime, int timeInterval, Json* record);
+               void __normalizeBatteryUsage(std::vector<Json>* usage, int totalUtime, int totalStime);
 
        };
 
index 5b65b33..456d0d5 100644 (file)
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-//#include <sstream>
+#include <sstream>
 #include <tzplatform_config.h>
 #include <Json.h>
 #include "BatteryStatisticsTypes.h"
@@ -24,6 +24,7 @@
 #define HEART_APP_ID "appid"
 #define HEART_TIME "time"
 #define HEART_DATA "data"
+#define HEART_INDEX "idx"
 
 #define HEART_DB_PATH tzplatform_mkpath(TZ_USER_DB, ".resourced-heart-default.db")
 
@@ -59,16 +60,16 @@ void HeartDbReader::close()
        __heartDb = NULL;
 }
 
-bool HeartDbReader::readAppLaunchLog(int startTime, int endTime, std::vector<Json>* appLog)
+bool HeartDbReader::readCpuUsageLog(int startTime, int endTime, std::vector<Json>* cpuUsageLog)
 {
        IF_FAIL_RETURN(__heartDb, false);
 
        char* sql = sqlite3_mprintf(
-                                       "SELECT " HEART_APP_ID ", " HEART_TIME ", " HEART_DATA " FROM " HEART_CPU_TABLE \
+                                       "SELECT " HEART_APP_ID ", " HEART_TIME ", " HEART_DATA ", " HEART_INDEX " FROM " HEART_CPU_TABLE \
                                        " WHERE " HEART_TIME " >= %d AND " HEART_TIME " < %d", startTime, endTime);
 
        char* err = NULL;
-       int ret = sqlite3_exec(__heartDb, sql, __appLaunchLogCb, appLog, &err);
+       int ret = sqlite3_exec(__heartDb, sql, __cpuUsageLogCb, cpuUsageLog, &err);
        sqlite3_free(sql);
        if (ret != SQLITE_OK) {
                _E("Failed to read heart DB: Error(%s)", err);
@@ -79,7 +80,7 @@ bool HeartDbReader::readAppLaunchLog(int startTime, int endTime, std::vector<Jso
        return true;
 }
 
-int HeartDbReader::__appLaunchLogCb(void *userData, int dim, char **value, char **column)
+int HeartDbReader::__cpuUsageLogCb(void *userData, int dim, char **value, char **column)
 {
        IF_FAIL_RETURN(userData, 0);
 
@@ -99,33 +100,36 @@ int HeartDbReader::__appLaunchLogCb(void *userData, int dim, char **value, char
        }
 
        if (!columnNull) {
-               __convertAppLaunchLog(row, &newRow);
+               __convertCpuUsageLog(row, &newRow);
                records->push_back(newRow);
        }
 
        return 0;
 }
 
-void HeartDbReader::__convertAppLaunchLog(Json& row, Json* newRow)
+void HeartDbReader::__convertCpuUsageLog(Json& row, Json* newRow)
 {
        std::string appId;
        std::string timestamp;
        std::string data;
-/*     int utime;
+       int utime;
        int stime;
        int pid;
        int type;
-*/
+       int index;
+
        row.get(NULL, HEART_APP_ID, &appId);
        row.get(NULL, HEART_TIME, &timestamp);
        row.get(NULL, HEART_DATA, &data);
-/*
-       std::stringstream buf = data;           // TODO use utim & stime
+       row.get(NULL, HEART_INDEX, &index);
+
+       std::stringstream buf(data);
        buf >> utime >> stime >> pid >> type;
-*/
-       std::string type = data.substr(data.length() - 2, 1);
 
        newRow->set(NULL, BATTERY_APP_ID, appId);
        newRow->set(NULL, BATTERY_TIMESTAMP, atoi(timestamp.c_str()));
-       newRow->set(NULL, BATTERY_TYPE, atoi(type.c_str()));
+       newRow->set(NULL, BATTERY_UTIME, utime);
+       newRow->set(NULL, BATTERY_STIME, stime);
+       newRow->set(NULL, BATTERY_TYPE, type);
+       newRow->set(NULL, BATTERY_INDEX, index);
 }
index f355233..4c72902 100644 (file)
@@ -32,11 +32,11 @@ namespace ctx {
                bool open();
                void close();
 
-               bool readAppLaunchLog(int startTime, int endTime, std::vector<Json>* appLog);
+               bool readCpuUsageLog(int startTime, int endTime, std::vector<Json>* cpuUsageLog);
 
        private:
-               static int __appLaunchLogCb(void *userData, int dim, char **value, char **column);
-               static void __convertAppLaunchLog(Json& row, Json* newRow);
+               static int __cpuUsageLogCb(void *userData, int dim, char **value, char **column);
+               static void __convertCpuUsageLog(Json& row, Json* newRow);
 
                sqlite3 *__heartDb;
        };