From: Ryszard Matuszyk Date: Thu, 15 Jan 2015 12:14:36 +0000 (+0100) Subject: [DataSync] getLastSyncStatistics X-Git-Tag: submit/tizen_mobile/20150603.064609~1^2~619 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9ad62532854be4e4f8d0701236c64ee2c9bba349;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [DataSync] getLastSyncStatistics Verification: not available. Platform function sync_agent_ds_init not work Change-Id: I8c073b4b3a0ce6906553802280ba8e765cf6e4b4 Signed-off-by: Ryszard Matuszyk --- diff --git a/src/datasync/datasync_instance.cc b/src/datasync/datasync_instance.cc index fa356fd..9d856de 100644 --- a/src/datasync/datasync_instance.cc +++ b/src/datasync/datasync_instance.cc @@ -64,11 +64,12 @@ void DatasyncInstance::GetAll(const picojson::value &args, picojson::object &out } void DatasyncInstance::GetLastSyncStatistics(const picojson::value &args, picojson::object &out) { -// TODO: implementation + picojson::value result = picojson::value(picojson::array()); - picojson::value val{picojson::object{}}; + DataSyncManager::Instance().GetLastSyncStatistics(args.get("profileId").get(), + result.get()); - ReportSuccess(val, out); + ReportSuccess(result, out); } void DatasyncInstance::Add(const picojson::value &args, picojson::object &out) { diff --git a/src/datasync/datasync_manager.cc b/src/datasync/datasync_manager.cc index bf72d9d..65d60fd 100644 --- a/src/datasync/datasync_manager.cc +++ b/src/datasync/datasync_manager.cc @@ -87,6 +87,21 @@ int DataSyncManager::StrToPlatformEnum(const std::string& field, const std::stri throw InvalidValuesException(message); } +std::string DataSyncManager::PlatformEnumToStr(const std::string& field, const int value) { + if (platform_enum_map_.find(field) == platform_enum_map_.end()) { + throw UnknownException(std::string("Undefined platform enum type ") + field); + } + + auto def = platform_enum_map_.at(field); + + for (auto& item : def) { + if (item.second == value) return item.first; + } + + std::string message = "Platform enum value " + std::to_string(value) + " not found for " + field; + throw InvalidValuesException(message); +} + datasync::SyncInfo::SyncMode ConvertToSyncMode( sync_agent_ds_sync_mode_e sync_mode) { switch (sync_mode) { @@ -695,93 +710,76 @@ DataSyncManager& DataSyncManager::Instance() { return manager; } -ResultOrError DataSyncManager::GetLastSyncStatistics( - const std::string& profile_str_id) const { +void DataSyncManager::GetLastSyncStatistics(const std::string& id, picojson::array& out) { ds_profile_h profile_h = nullptr; GList* statistics_list = nullptr; - auto exit = common::MakeScopeExit([&statistics_list, &profile_h]() { - if (statistics_list) { - g_list_free(statistics_list); - } - - if (profile_h) { - sync_agent_ds_free_profile_info(profile_h); - } - }); - - SyncStatisticsListPtr statistics_list_ptr(new SyncStatisticsList()); - - sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL; - - int profile_id = std::stoi(profile_str_id); + int profile_id = std::stoi(id); LoggerD("profileId: %d", profile_id); - ret = sync_agent_ds_get_profile(profile_id, &profile_h); + + sync_agent_ds_error_e ret = sync_agent_ds_get_profile(profile_id, &profile_h); if (SYNC_AGENT_DS_SUCCESS != ret) { - return Error("NotFoundException", - "Platform error while getting a profile"); + throw NotFoundException("Platform error while getting a profile"); } ret = sync_agent_ds_get_sync_statistics(profile_h, &statistics_list); if (SYNC_AGENT_DS_SUCCESS != ret) { - return Error("Exception", - "Platform error while gettting sync statistics"); + throw UnknownException("Platform error while gettting sync statistics"); } int statistics_count = g_list_length(statistics_list); LoggerD("statistics_count: %d", statistics_count); sync_agent_ds_statistics_info* statistics = nullptr; + for (int i = 0; i < statistics_count; i++) { - statistics = static_cast( - g_list_nth_data(statistics_list, i)); + statistics = static_cast(g_list_nth_data(statistics_list, i)); - SyncStatisticsPtr statistics_ptr(new SyncStatistics()); + picojson::value item = picojson::value(picojson::object()); + picojson::object& item_obj = item.get(); if (0 == i) { LoggerD("Statistics for contact."); - statistics_ptr->set_service_type( - ConvertToSyncServiceType(SYNC_AGENT_CONTACT)); + item_obj["serviceType"] = + picojson::value(PlatformEnumToStr(kSyncServiceType, SYNC_AGENT_CONTACT)); } else if (1 == i) { LoggerD("Statistics for event."); - statistics_ptr->set_service_type( - ConvertToSyncServiceType(SYNC_AGENT_CALENDAR)); + item_obj["serviceType"] = + picojson::value(PlatformEnumToStr(kSyncServiceType, SYNC_AGENT_CALENDAR)); } else { LoggerW("Unsupported category for statistics: %d", i); continue; } + out.push_back(item); + LoggerD("dbsynced: %d", statistics->dbsynced); - if (statistics->dbsynced) { - statistics_ptr->set_sync_status( - ConvertToSyncStatus(statistics->dbsynced)); - statistics_ptr->set_client_to_server_total( - statistics->client2server_total); - statistics_ptr->set_client_to_server_added( - statistics->client2server_nrofadd); - statistics_ptr->set_client_to_server_updated( - statistics->client2server_nrofreplace); - statistics_ptr->set_client_to_server_removed( - statistics->client2server_nrofdelete); - statistics_ptr->set_server_to_client_total( - statistics->server2client_total); - statistics_ptr->set_server_to_client_added( - statistics->server2client_nrofadd); - statistics_ptr->set_server_to_client_updated( - statistics->server2client_nrofreplace); - statistics_ptr->set_server_to_client_removed( - statistics->server2client_nrofdelete); - - statistics_ptr->set_last_sync_time( - static_cast(statistics->last_session_time)); - } - - LoggerD("ClientToServerTotal: %d, ServerToClientTotal: %d", - statistics_ptr->client_to_server_total(), statistics_ptr->server_to_client_total()); - - statistics_list_ptr->push_back(statistics_ptr); - } - - return statistics_list_ptr; + if (!statistics->dbsynced) continue; + + std::string db_synced = statistics->dbsynced; + std::transform(db_synced.begin(), db_synced.end(), db_synced.begin(), ::toupper); + + item_obj["syncStatus"] = picojson::value(db_synced); + item_obj["clientToServerTotal"] = + picojson::value(static_cast(statistics->client2server_total)); + item_obj["clientToServerAdded"] = + picojson::value(static_cast(statistics->client2server_nrofadd)); + item_obj["clientToServerUpdated"] = + picojson::value(static_cast(statistics->client2server_nrofreplace)); + item_obj["clientToServerRemoved"] = + picojson::value(static_cast(statistics->client2server_nrofdelete)); + item_obj["serverToClientTotal"] = + picojson::value(static_cast(statistics->server2client_total)); + item_obj["serverToClientAdded"] = + picojson::value(static_cast(statistics->server2client_nrofadd)); + item_obj["serverToClientUpdated"] = + picojson::value(static_cast(statistics->server2client_nrofreplace)); + item_obj["serverToClientRemoved"] = + picojson::value(static_cast(statistics->server2client_nrofdelete)); + item_obj["lastSyncTime"] = picojson::value(static_cast(statistics->last_session_time)); + + LoggerD("ClientToServerTotal: %d, ServerToClientTotal: %d", statistics->client2server_total, + statistics->server2client_total); + } } int DataSyncManager::StateChangedCallback(sync_agent_event_data_s* request) { diff --git a/src/datasync/datasync_manager.h b/src/datasync/datasync_manager.h index 9660e66..d7a29a3 100644 --- a/src/datasync/datasync_manager.h +++ b/src/datasync/datasync_manager.h @@ -44,8 +44,7 @@ class DataSyncManager { ResultOrError Get( const std::string& profile_id) const; ResultOrError GetAll() const; - ResultOrError GetLastSyncStatistics( - const std::string& profile_str_id) const; + void GetLastSyncStatistics(const std::string& id, picojson::array &out); ResultOrError StartSync(const std::string& profile_id_str, int callback_id, DatasyncInstance* instance); @@ -56,6 +55,7 @@ class DataSyncManager { static DataSyncManager& Instance(); static int StrToPlatformEnum(const std::string& field, const std::string& value); + static std::string PlatformEnumToStr(const std::string& field, const int value); private: DataSyncManager();