Replace json wrapper to jsoncpp 21/135721/8
authorSomin Kim <somin926.kim@samsung.com>
Mon, 26 Jun 2017 05:49:43 +0000 (14:49 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 3 Jul 2017 05:29:36 +0000 (14:29 +0900)
Change-Id: I1bbdc0cba71f6322ef210e18b6d7cda91c066d3f
Signed-off-by: Somin Kim <somin926.kim@samsung.com>
21 files changed:
packaging/context-app-history.spec
src/client/CMakeLists.txt
src/client/app_history.cpp
src/server/CMakeLists.txt
src/server/MethodCallHandler.cpp
src/server/StatsProvider.cpp
src/server/StatsProvider.h
src/server/battery-stats/BatteryMonitor.cpp
src/server/battery-stats/BatteryMonitor.h
src/server/battery-stats/BatteryStatsProvider.cpp
src/server/battery-stats/BatteryStatsProvider.h
src/server/battery-stats/BatteryUsage.cpp
src/server/battery-stats/BatteryUsage.h
src/server/battery-stats/BatteryUsageAnalyzer.cpp
src/server/battery-stats/BatteryUsageAnalyzer.h
src/server/battery-stats/HeartDbReader.cpp
src/server/battery-stats/HeartDbReader.h
src/server/battery-stats/RecentBatteryUsage.cpp
src/server/battery-stats/RecentBatteryUsage.h
src/server/usage-stats/UsageStatsProvider.cpp
src/server/usage-stats/UsageStatsProvider.h

index 1f5d32b..91f3094 100644 (file)
@@ -11,6 +11,7 @@ BuildRequires: pkgconfig(glib-2.0)
 BuildRequires: pkgconfig(gio-2.0)
 BuildRequires: pkgconfig(dlog)
 BuildRequires: pkgconfig(capi-base-common)
+BuildRequires: pkgconfig(jsoncpp)
 BuildRequires: pkgconfig(capi-system-device)
 BuildRequires: pkgconfig(capi-system-info)
 BuildRequires: pkgconfig(capi-appfw-package-manager)
index 8f551d7..46d4131 100644 (file)
@@ -1,6 +1,6 @@
 SET(target "${PROJECT_NAME}-client-genuine")
 
-SET(DEPS "${DEPS} context-common-client")
+SET(DEPS "${DEPS} jsoncpp context-common-client")
 
 FILE(GLOB_RECURSE SRCS *.cpp ../shared/*.cpp)
 MESSAGE("Sources: ${SRCS}")
index cc8314c..b312027 100644 (file)
@@ -16,7 +16,7 @@
 
 #include <string>
 #include <utility>
-#include <CtxJson.h>
+#include <json/json.h>
 #include <AppHistoryTypes.h>
 #include <AppHistoryTypesPrivate.h>
 #include <ServiceProxy.h>
@@ -33,12 +33,15 @@ static ServiceProxy* __getServiceProxy()
 
 EXPORT_API int ctx_history_query(const char* uri, int64_t start_time, int64_t end_time, unsigned int result_size, ctx_history_cursor_h* cursor)
 {
-       CtxJson filter;
-       filter.set(NULL, KEY_START_TIME, start_time);
-       filter.set(NULL, KEY_END_TIME, end_time);
-       filter.set(NULL, KEY_RESULT_SIZE, (int64_t) result_size);
+       Json::Value filter;
+       filter[KEY_START_TIME] = start_time;
+       filter[KEY_END_TIME] = end_time;
+       filter[KEY_RESULT_SIZE] = (int64_t) result_size;
 
-       return _ctx_history_query(uri, filter.str().c_str(), cursor);
+       Json::FastWriter fw;
+       fw.omitEndingLineFeed();
+       std::string filter_str = fw.write(filter);
+       return _ctx_history_query(uri, filter_str.c_str(), cursor);
 }
 
 EXPORT_API int _ctx_history_query(const char* uri, const char* filter, ctx_history_cursor_h* cursor)
index 12181c4..fd0f6b3 100644 (file)
@@ -1,6 +1,6 @@
 SET(target "${PROJECT_NAME}-server-genuine")
 
-SET(DEPS "${DEPS} context-common-server capi-system-info capi-system-device capi-appfw-package-manager capi-appfw-app-manager vconf")
+SET(DEPS "${DEPS} jsoncpp context-common-server capi-system-info capi-system-device capi-appfw-package-manager capi-appfw-app-manager vconf")
 
 FILE(GLOB_RECURSE SRCS *.cpp ../shared/*.cpp)
 MESSAGE("Sources: ${SRCS}")
index 663e6c3..bc19a95 100644 (file)
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include <CtxJson.h>
+#include <json/json.h>
 #include <ServerUtil.h>
 #include "StatsManager.h"
 #include "MethodCallHandler.h"
@@ -92,7 +92,9 @@ void MethodCallHandler::__query(IMethodCall* methodCall)
                throw static_cast<int>(E_ACCESS);
        }
 
-       CtxJson jfilter = filter;
+       Json::Value jfilter;
+       Json::Reader reader;
+       reader.parse(std::string(filter), jfilter);
        std::vector<std::shared_ptr<Tuple>> result;
 
        std::vector<std::string> columnNames;
index c49f69c..945be16 100644 (file)
@@ -46,12 +46,12 @@ bool StatsProvider::isAllowed(IClient& caller)
        return true;
 }
 
-int StatsProvider::read(CtxJson& filter, std::vector<std::string>& columnNames, std::vector<std::shared_ptr<Tuple>>& records)
+int StatsProvider::read(Json::Value& filter, std::vector<std::string>& columnNames, std::vector<std::shared_ptr<Tuple>>& records)
 {
        return E_NONE;
 }
 
-std::string StatsProvider::getQuery(CtxJson& filter)
+std::string StatsProvider::getQuery(Json::Value& filter)
 {
        return "";
 }
index b54ac13..cc7e30c 100644 (file)
@@ -19,7 +19,7 @@
 
 #include <sstream>
 #include <vector>
-#include <CtxJson.h>
+#include <json/json.h>
 #include <Tuple.h>
 #include <IClient.h>
 #include <AppHistoryTypes.h>
@@ -39,8 +39,8 @@ namespace ctx {
                virtual bool isSupported(void);
                virtual bool isAllowed(IClient& caller);
 
-               virtual std::string getQuery(CtxJson& filter);
-               virtual int read(CtxJson& filter, std::vector<std::string>& columnNames, std::vector<std::shared_ptr<Tuple>>& records);
+               virtual std::string getQuery(Json::Value& filter);
+               virtual int read(Json::Value& filter, std::vector<std::string>& columnNames, std::vector<std::shared_ptr<Tuple>>& records);
 
        protected:
                std::string __subject;
index 90d55b4..7d04505 100644 (file)
@@ -19,7 +19,6 @@
 #include <sqlite3.h>
 #include <glib.h>
 #include <device/battery.h>
-#include <CtxJson.h>
 #include <Tuple.h>
 #include <Timer.h>
 #include <AppHistoryTypesPrivate.h>
@@ -270,7 +269,7 @@ bool BatteryMonitor::__processBatteryUsage()
        bool ret = __heartReader->dbOpen();
        IF_FAIL_RETURN_TAG(ret, true, _E, "Failed to open heart db");
 
-       std::vector<CtxJson> heartCpuLog;
+       std::vector<Json::Value> heartCpuLog;
        ret = __heartReader->readCpuLog(__lastHeartAccessTime, &heartCpuLog);
        __heartReader->dbClose();
 
@@ -278,7 +277,7 @@ bool BatteryMonitor::__processBatteryUsage()
        IF_FAIL_RETURN_TAG(heartCpuLog.size() > 0, true, _W, "Heart cpu data is not prepared");
        _D("Read %d rows from heart cpu table from %d", heartCpuLog.size(), __lastHeartAccessTime);
 
-       heartCpuLog.back().get(NULL, BATTERY_TIMESTAMP, &__lastHeartAccessTime);
+       __lastHeartAccessTime = heartCpuLog.back()[BATTERY_TIMESTAMP].asInt64();
        __updateLastInfo();
 
        // Get last heart cpu log
@@ -286,7 +285,7 @@ bool BatteryMonitor::__processBatteryUsage()
        ret = __getLastCpuUsageTable(&lastHeartCpuLog);
 
        // Calculate per app battery usage
-       std::vector<CtxJson> usage;
+       std::vector<Json::Value> usage;
        BatteryUsageAnalyzer::calculateBatteryUsage(heartCpuLog, lastHeartCpuLog, &usage);
 
        // Insert battery usage
@@ -302,7 +301,7 @@ bool BatteryMonitor::__processBatteryUsage()
        return true;
 }
 
-bool BatteryMonitor::__insertBatteryUsageLog(std::vector<CtxJson>& usage)
+bool BatteryMonitor::__insertBatteryUsageLog(std::vector<Json::Value>& usage)
 {
        IF_FAIL_RETURN_TAG(usage.size(), true, _W, "No data");
        std::string query("INSERT INTO " BATTERY_USAGE_TABLE \
@@ -314,11 +313,11 @@ bool BatteryMonitor::__insertBatteryUsageLog(std::vector<CtxJson>& usage)
        int stime, utime;
 
        for (unsigned int i = 0; i < usage.size(); i++) {
-               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);
+               appId = usage[i][BATTERY_APP_ID].asString();
+               startTime = usage[i][BATTERY_START_TIME].asInt();
+               endTime = usage[i][BATTERY_END_TIME].asInt();
+               utime = usage[i][BATTERY_UTIME].asInt();
+               stime = usage[i][BATTERY_STIME].asInt();
 
                query += " ('" + appId + "', "
                                + std::to_string(startTime) + ", "
index 051efa2..ee53cb2 100644 (file)
@@ -46,7 +46,7 @@ namespace ctx {
                bool __updateLastInfo();
 
                bool __processBatteryUsage();
-               bool __insertBatteryUsageLog(std::vector<CtxJson>& usage);
+               bool __insertBatteryUsageLog(std::vector<Json::Value>& usage);
                bool __updateLastCpuUsageLog(CpuUsageMap& usage);
                bool __getLastCpuUsageTable(CpuUsageMap* lastCpuUsage);
                void __modifyLastCpuUsage(int timeDiff);
index 6624ddc..875d0d8 100644 (file)
@@ -40,7 +40,7 @@ bool BatteryStatsProvider::isSupported()
        return supported;
 }
 
-std::string BatteryStatsProvider::getQuery(CtxJson& filter)
+std::string BatteryStatsProvider::getQuery(Json::Value& filter)
 {
        std::string query;
 
@@ -53,32 +53,24 @@ std::string BatteryStatsProvider::getQuery(CtxJson& filter)
        return query;
 }
 
-std::string BatteryStatsProvider::createBatteryUsageQuery(CtxJson& filter)
+std::string BatteryStatsProvider::createBatteryUsageQuery(const Json::Value& filter)
 {
        std::stringstream query;
        int startTime;
        int endTime;
-       int timeSpan = DEFAULT_TIME_SPAN;
+       int timeSpan = filter.get(KEY_TIME_SPAN, DEFAULT_TIME_SPAN).asInt();
        int timeSpanPoint;
-       int resultSize = BATTERY_DEFAULT_LIMIT;
+       int resultSize = filter.get(KEY_RESULT_SIZE, BATTERY_DEFAULT_LIMIT).asInt();
 
-       filter.get(NULL, KEY_TIME_SPAN, &timeSpan);
        timeSpanPoint = CURRENT_TIME - timeSpan * SECONDS_IN_A_DAY;
 
-       if (!filter.get(NULL, KEY_START_TIME, &startTime)) {
-               startTime = timeSpanPoint;
-       }
-
-       if (!filter.get(NULL, KEY_END_TIME, &endTime)) {
-               endTime = CURRENT_TIME;
-       }
+       startTime = filter.get(KEY_START_TIME, timeSpanPoint).asInt();
+       endTime = filter.get(KEY_END_TIME, CURRENT_TIME).asInt();
 
        if (startTime < timeSpanPoint) {
                startTime = timeSpanPoint;
        }
 
-       filter.get(NULL, KEY_RESULT_SIZE, &resultSize);
-
        query <<
                "SELECT " BATTERY_APP_ID " AS " KEY_APP_ID ", " \
                        "ROUND(100.0 * SUM(" BATTERY_TOTAL_TIME ") / " \
@@ -92,14 +84,12 @@ std::string BatteryStatsProvider::createBatteryUsageQuery(CtxJson& filter)
        return query.str();
 }
 
-std::string BatteryStatsProvider::createRecentBatteryUsageQuery(CtxJson& filter)
+std::string BatteryStatsProvider::createRecentBatteryUsageQuery(const Json::Value& filter)
 {
        std::stringstream query;
        int startTime = BatteryMonitor::getInstance()->getLastFullTime();
        int endTime = CURRENT_TIME;
-       int resultSize = BATTERY_DEFAULT_LIMIT;
-
-       filter.get(NULL, KEY_RESULT_SIZE, &resultSize);
+       int resultSize = filter.get(KEY_RESULT_SIZE, BATTERY_DEFAULT_LIMIT).asInt();
 
        query <<
                "SELECT " BATTERY_APP_ID " AS " KEY_APP_ID ", " \
index 43f283d..b37e028 100644 (file)
@@ -29,12 +29,12 @@ namespace ctx {
                virtual ~BatteryStatsProvider();
 
                bool isSupported(void);
-               std::string getQuery(CtxJson& filter);
+               std::string getQuery(Json::Value& filter);
 
        protected:
 
-               std::string createBatteryUsageQuery(CtxJson& filter);
-               std::string createRecentBatteryUsageQuery(CtxJson& filter);
+               std::string createBatteryUsageQuery(const Json::Value& filter);
+               std::string createRecentBatteryUsageQuery(const Json::Value& filter);
 
        };
 
index 993cfd0..0479a8c 100644 (file)
@@ -29,7 +29,7 @@ BatteryUsageProvider::~BatteryUsageProvider()
 {
 }
 
-int BatteryUsageProvider::read(CtxJson& filter, std::vector<std::string>& columnNames, std::vector<std::shared_ptr<Tuple>>& records)
+int BatteryUsageProvider::read(Json::Value& filter, std::vector<std::string>& columnNames, std::vector<std::shared_ptr<Tuple>>& records)
 {
        BatteryMonitor::getInstance()->prepareData();
 
index 4a81f44..7a9bfb5 100644 (file)
@@ -26,7 +26,7 @@ namespace ctx {
                BatteryUsageProvider();
                ~BatteryUsageProvider();
 
-               int read(CtxJson& filter, std::vector<std::string>& columnNames, std::vector<std::shared_ptr<Tuple>>& records);
+               int read(Json::Value& filter, std::vector<std::string>& columnNames, std::vector<std::shared_ptr<Tuple>>& records);
        };
 }
 
index 3b55d06..06bc2a4 100644 (file)
@@ -27,7 +27,7 @@ BatteryUsageAnalyzer::~BatteryUsageAnalyzer()
 {
 }
 
-void BatteryUsageAnalyzer::calculateBatteryUsage(std::vector<CtxJson>& cpuLog, CpuUsageMap& recentCpuUsageMap, std::vector<CtxJson>* usage)
+void BatteryUsageAnalyzer::calculateBatteryUsage(std::vector<Json::Value>& cpuLog, CpuUsageMap& recentCpuUsageMap, std::vector<Json::Value>* usage)
 {
        std::string appId;
        int timestamp;
@@ -37,12 +37,12 @@ void BatteryUsageAnalyzer::calculateBatteryUsage(std::vector<CtxJson>& cpuLog, C
        int pid;
 
        for (unsigned int i = 0; i < cpuLog.size(); i++) {
-               cpuLog[i].get(NULL, BATTERY_APP_ID, &appId);
-               cpuLog[i].get(NULL, BATTERY_TIMESTAMP, &timestamp);
-               cpuLog[i].get(NULL, BATTERY_UTIME, &utime);
-               cpuLog[i].get(NULL, BATTERY_STIME, &stime);
-               cpuLog[i].get(NULL, BATTERY_INDEX, &idx);
-               cpuLog[i].get(NULL, BATTERY_PID, &pid);
+               appId = cpuLog[i][BATTERY_APP_ID].asString();
+               timestamp = cpuLog[i][BATTERY_TIMESTAMP].asInt();
+               utime = cpuLog[i][BATTERY_UTIME].asInt();
+               stime = cpuLog[i][BATTERY_STIME].asInt();
+               idx = cpuLog[i][BATTERY_INDEX].asInt();
+               pid = cpuLog[i][BATTERY_PID].asInt();
 
                //If CPU table is reset, clear last cpu usage
                if (idx == 0) {
@@ -76,12 +76,12 @@ void BatteryUsageAnalyzer::calculateBatteryUsage(std::vector<CtxJson>& cpuLog, C
                if (prevTimestamp > timestamp)
                        prevTimestamp = timestamp;
 
-               CtxJson row;
-               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_UTIME, utime - prevUtime);
-               row.set(NULL, BATTERY_STIME, stime - prevStime);
+               Json::Value row;
+               row[BATTERY_APP_ID] = appId;
+               row[BATTERY_START_TIME] = prevTimestamp;
+               row[BATTERY_END_TIME] = timestamp;
+               row[BATTERY_UTIME] = utime - prevUtime;
+               row[BATTERY_STIME] = stime - prevStime;
 
                usage->push_back(row);
 
index fac0766..aabe2aa 100644 (file)
@@ -18,7 +18,7 @@
 #define __CONTEXT_BATTERY_USAGE_ANALYZER_H__
 
 #include <vector>
-#include <CtxJson.h>
+#include <json/json.h>
 #include "BatteryStatisticsTypes.h"
 
 namespace ctx {
@@ -28,7 +28,7 @@ namespace ctx {
                BatteryUsageAnalyzer();
                ~BatteryUsageAnalyzer();
 
-               static void calculateBatteryUsage(std::vector<CtxJson>& cpuUsageLog, CpuUsageMap& lastCpuUsage, std::vector<CtxJson>* usage);
+               static void calculateBatteryUsage(std::vector<Json::Value>& cpuUsageLog, CpuUsageMap& lastCpuUsage, std::vector<Json::Value>* usage);
        };
 
 }      /* namespace ctx */
index d7fa483..e544ac2 100644 (file)
@@ -15,7 +15,7 @@
  */
 
 #include <sstream>
-#include <CtxJson.h>
+#include <json/json.h>
 #include <ServerUtil.h>
 #include "BatteryStatisticsTypes.h"
 #include "HeartDbReader.h"
@@ -80,7 +80,7 @@ void HeartDbReader::dbClose()
        __dbRefCount = 0;
 }
 
-bool HeartDbReader::readCpuLog(int lastHeartAccessTime, std::vector<CtxJson>* cpuUsageLog)
+bool HeartDbReader::readCpuLog(int lastHeartAccessTime, std::vector<Json::Value>* cpuUsageLog)
 {
        IF_FAIL_RETURN(__heartDb, false);
 
@@ -104,9 +104,9 @@ int HeartDbReader::__cpuUsageLogCb(void *userData, int dim, char **value, char *
 {
        IF_FAIL_RETURN(userData, 0);
 
-       std::vector<CtxJson> *records = static_cast<std::vector<CtxJson>*>(userData);
-       CtxJson row;
-       CtxJson newRow;
+       std::vector<Json::Value> *records = static_cast<std::vector<Json::Value>*>(userData);
+       std::map<std::string, std::string> row;
+
        bool columnNull = false;
 
        for (int i = 0; i < dim; ++i) {
@@ -116,43 +116,37 @@ int HeartDbReader::__cpuUsageLogCb(void *userData, int dim, char **value, char *
                        break;
                }
 
-               row.set(NULL, column[i], value[i]);
+               row[column[i]] = value[i];
        }
 
-       if (!columnNull) {
-               __convertCpuUsageLog(row, &newRow);
-               records->push_back(newRow);
-       }
+       if (columnNull)
+               return 0;
 
-       return 0;
-}
+       auto convToJson = [](std::map<std::string, std::string>& in)->Json::Value {
+               Json::Value jval;
+               int utime;
+               int stime;
+               int pid;
+               int type;
 
-void HeartDbReader::__convertCpuUsageLog(CtxJson& row, CtxJson* newRow)
-{
-       std::string appId;
-       std::string timestamp;
-       std::string data;
-       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);
-       row.get(NULL, HEART_INDEX, &index);
-
-       std::stringstream buf(data);
-       buf >> utime >> stime >> pid >> type;
-
-       newRow->set(NULL, BATTERY_APP_ID, appId);
-       newRow->set(NULL, BATTERY_TIMESTAMP, atoi(timestamp.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);
-       newRow->set(NULL, BATTERY_PID, pid);
+               std::stringstream buf(in[HEART_DATA]);
+               buf >> utime >> stime >> pid >> type;
+
+               jval[BATTERY_APP_ID] = in[HEART_APP_ID];
+               jval[BATTERY_TIMESTAMP] = atoi(in[HEART_TIME].c_str());
+               jval[BATTERY_INDEX] = atoi(in[HEART_INDEX].c_str());
+
+               jval[BATTERY_UTIME] = utime;
+               jval[BATTERY_STIME] = stime;
+               jval[BATTERY_TYPE] = type;
+               jval[BATTERY_PID] = pid;
+
+               return jval;
+       };
+
+       records->push_back(convToJson(row));
+
+       return 0;
 }
 
 bool HeartDbReader::__initializeDBusConnection()
index 255879c..46b5224 100644 (file)
@@ -31,12 +31,11 @@ namespace ctx {
 
                bool dbOpen();
                void dbClose();
-               bool readCpuLog(int __lastHeartAccessTime, std::vector<CtxJson>* cpuUsageLog);
+               bool readCpuLog(int __lastHeartAccessTime, std::vector<Json::Value>* cpuUsageLog);
                bool requestSync();
 
        private:
                static int __cpuUsageLogCb(void *userData, int dim, char **value, char **column);
-               static void __convertCpuUsageLog(CtxJson& row, CtxJson* newRow);
 
                bool __initializeDBusConnection();
                void __releaseDBusConnection();
index c042b4f..8007309 100644 (file)
@@ -30,7 +30,7 @@ RecentBatteryUsageProvider::~RecentBatteryUsageProvider()
 {
 }
 
-int RecentBatteryUsageProvider::read(CtxJson& filter, std::vector<std::string>& columnNames, std::vector<std::shared_ptr<Tuple>>& records)
+int RecentBatteryUsageProvider::read(Json::Value& filter, std::vector<std::string>& columnNames, std::vector<std::shared_ptr<Tuple>>& records)
 {
        BatteryMonitor::getInstance()->prepareData();
 
index 65a1e22..49b173e 100644 (file)
@@ -26,7 +26,7 @@ namespace ctx {
                RecentBatteryUsageProvider();
                ~RecentBatteryUsageProvider();
 
-               int read(CtxJson& filter, std::vector<std::string>& columnNames, std::vector<std::shared_ptr<Tuple>>& records);
+               int read(Json::Value& filter, std::vector<std::string>& columnNames, std::vector<std::shared_ptr<Tuple>>& records);
 
        };
 }
index c38918c..c307889 100644 (file)
@@ -28,7 +28,7 @@ UsageStatsProvider::~UsageStatsProvider()
 {
 }
 
-int UsageStatsProvider::read(CtxJson& filter, std::vector<std::string>& columnNames, std::vector<std::shared_ptr<Tuple>>& records)
+int UsageStatsProvider::read(Json::Value& filter, std::vector<std::string>& columnNames, std::vector<std::shared_ptr<Tuple>>& records)
 {
        std::string query = getQuery(filter);
        bool ret = execute(query, COL_STRING COL_INT64 COL_INT64 COL_INT64, columnNames, records);
@@ -37,7 +37,7 @@ int UsageStatsProvider::read(CtxJson& filter, std::vector<std::string>& columnNa
        return E_NONE;
 }
 
-std::string UsageStatsProvider::getQuery(CtxJson& filter)
+std::string UsageStatsProvider::getQuery(Json::Value& filter)
 {
        std::string query;
 
@@ -52,12 +52,10 @@ std::string UsageStatsProvider::getQuery(CtxJson& filter)
        return query;
 }
 
-std::string UsageStatsProvider::__createSqlRecentlyUsed(CtxJson filter)
+std::string UsageStatsProvider::__createSqlRecentlyUsed(const Json::Value& filter)
 {
        std::stringstream query;
-       int limit = DEFAULT_LIMIT;
-
-       filter.get(NULL, KEY_RESULT_SIZE, &limit);
+       int limit = filter.get(KEY_RESULT_SIZE, DEFAULT_LIMIT).asInt();
 
        query <<
                "SELECT " KEY_APP_ID ", " \
@@ -73,12 +71,10 @@ std::string UsageStatsProvider::__createSqlRecentlyUsed(CtxJson filter)
        return query.str();
 }
 
-std::string UsageStatsProvider::__createSqlFrequentlyUsed(CtxJson filter)
+std::string UsageStatsProvider::__createSqlFrequentlyUsed(const Json::Value& filter)
 {
        std::stringstream query;
-       int limit = DEFAULT_LIMIT;
-
-       filter.get(NULL, KEY_RESULT_SIZE, &limit);
+       int limit = filter.get(KEY_RESULT_SIZE, DEFAULT_LIMIT).asInt();
 
        query <<
                "SELECT " KEY_APP_ID ", " \
@@ -94,12 +90,10 @@ std::string UsageStatsProvider::__createSqlFrequentlyUsed(CtxJson filter)
        return query.str();
 }
 
-std::string UsageStatsProvider::__createSqlRarelyUsed(CtxJson filter)
+std::string UsageStatsProvider::__createSqlRarelyUsed(const Json::Value& filter)
 {
        std::stringstream query;
-       int limit = DEFAULT_LIMIT;
-
-       filter.get(NULL, KEY_RESULT_SIZE, &limit);
+       int limit = filter.get(KEY_RESULT_SIZE, DEFAULT_LIMIT).asInt();
 
        query <<
                "SELECT i." KEY_APP_ID ", " \
@@ -117,22 +111,18 @@ std::string UsageStatsProvider::__createSqlRarelyUsed(CtxJson filter)
        return query.str();
 }
 
-std::string UsageStatsProvider::__createWhereClause(CtxJson filter)
+std::string UsageStatsProvider::__createWhereClause(const Json::Value& filter)
 {
        std::stringstream whereClause;
-       int start = 0;
-       int end = 0;
-       int timeSpan = DEFAULT_TIME_SPAN;
 
-       if (filter.get(NULL, KEY_START_TIME, &start))
-               whereClause << KEY_UNIV_TIME " >= " << start << " AND ";
+       if (filter.isMember(KEY_START_TIME))
+               whereClause << KEY_UNIV_TIME " >= " << filter[KEY_START_TIME].asInt() << " AND ";
 
-       if (filter.get(NULL, KEY_END_TIME, &end))
-               whereClause << KEY_UNIV_TIME " <= " << end << " AND ";
+       if (filter.isMember(KEY_END_TIME))
+               whereClause << KEY_UNIV_TIME " <= " << filter[KEY_END_TIME].asInt() << " AND ";
 
        /* TODO */
-       filter.get(NULL, KEY_TIME_SPAN, &timeSpan);
-       whereClause << KEY_UNIV_TIME " > strftime('%s', 'now', '-" << timeSpan <<" day')";
+       whereClause << KEY_UNIV_TIME " > strftime('%s', 'now', '-" << filter.get(KEY_TIME_SPAN, DEFAULT_TIME_SPAN).asInt() <<" day')";
 
        return whereClause.str();
 }
index ec40491..61e71d3 100644 (file)
@@ -24,19 +24,19 @@ namespace ctx {
 
        class UsageStatsProvider : public StatsProvider {
        public:
-               int read(CtxJson& filter, std::vector<std::string>& columnNames, std::vector<std::shared_ptr<Tuple>>& records);
+               int read(Json::Value& filter, std::vector<std::string>& columnNames, std::vector<std::shared_ptr<Tuple>>& records);
 
        protected:
                UsageStatsProvider(const char *subject);
                virtual ~UsageStatsProvider();
 
-               std::string getQuery(CtxJson& filter);
+               std::string getQuery(Json::Value& filter);
 
        private:
-               std::string __createSqlRecentlyUsed(CtxJson filter);
-               std::string __createSqlFrequentlyUsed(CtxJson filter);
-               std::string __createSqlRarelyUsed(CtxJson filter);
-               std::string __createWhereClause(CtxJson filter);
+               std::string __createSqlRecentlyUsed(const Json::Value& filter);
+               std::string __createSqlFrequentlyUsed(const Json::Value& filter);
+               std::string __createSqlRarelyUsed(const Json::Value& filter);
+               std::string __createWhereClause(const Json::Value& filter);
 
        };