#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;
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");
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;
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");
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);