forked from organicmaps/organicmaps
Merge pull request #3318 from mgsergio/get-user-stats-from-server
[editor] Add user rating getter.
This commit is contained in:
commit
6fe84ed308
7 changed files with 148 additions and 3 deletions
|
@ -17,6 +17,7 @@ SOURCES += \
|
|||
osm_feature_matcher.cpp \
|
||||
server_api.cpp \
|
||||
ui2oh.cpp \
|
||||
user_stats.cpp \
|
||||
xml_feature.cpp \
|
||||
|
||||
HEADERS += \
|
||||
|
@ -28,5 +29,6 @@ HEADERS += \
|
|||
osm_feature_matcher.hpp \
|
||||
server_api.hpp \
|
||||
ui2oh.hpp \
|
||||
user_stats.hpp \
|
||||
xml_feature.hpp \
|
||||
yes_no_unknown.hpp \
|
||||
|
|
|
@ -21,4 +21,5 @@ SOURCES += \
|
|||
osm_feature_matcher_test.cpp \
|
||||
server_api_test.cpp \
|
||||
ui2oh_test.cpp \
|
||||
user_stats_test.cpp \
|
||||
xml_feature_test.cpp \
|
||||
|
|
20
editor/editor_tests/user_stats_test.cpp
Normal file
20
editor/editor_tests/user_stats_test.cpp
Normal file
|
@ -0,0 +1,20 @@
|
|||
#include "testing/testing.hpp"
|
||||
|
||||
#include "editor/user_stats.hpp"
|
||||
|
||||
namespace editor
|
||||
{
|
||||
namespace
|
||||
{
|
||||
UNIT_TEST(UserStats_Smoke)
|
||||
{
|
||||
// This user made only two changes and the possibility of further changes is very low.
|
||||
UserStats userStats("Vladimir BI");
|
||||
TEST(userStats.GetUpdateStatus(), ());
|
||||
TEST(userStats.IsChangesCountInitialized(), ());
|
||||
TEST(userStats.IsRankInitialized(), ());
|
||||
TEST_EQUAL(userStats.GetChangesCount(), 2, ());
|
||||
TEST_GREATER_OR_EQUAL(userStats.GetRank(), 5800, ());
|
||||
}
|
||||
} // namespace
|
||||
} // namespace editor
|
68
editor/user_stats.cpp
Normal file
68
editor/user_stats.cpp
Normal file
|
@ -0,0 +1,68 @@
|
|||
#include "editor/user_stats.hpp"
|
||||
|
||||
#include "coding/url_encode.hpp"
|
||||
|
||||
#include "base/logging.hpp"
|
||||
|
||||
#include "3party/Alohalytics/src/http_client.h"
|
||||
#include "3party/pugixml/src/pugixml.hpp"
|
||||
|
||||
using TRequest = alohalytics::HTTPClientPlatformWrapper;
|
||||
|
||||
|
||||
namespace
|
||||
{
|
||||
string const kUserStatsUrl = "http://py.osmz.ru/mmwatch/user?format=xml";
|
||||
auto constexpr kUninitialized = -1;
|
||||
} // namespace
|
||||
|
||||
namespace editor
|
||||
{
|
||||
UserStats::UserStats(string const & userName)
|
||||
: m_userName(userName), m_changesCount(kUninitialized), m_rank(kUninitialized)
|
||||
{
|
||||
m_updateStatus = Update();
|
||||
}
|
||||
|
||||
bool UserStats::IsChangesCountInitialized() const
|
||||
{
|
||||
return m_changesCount != kUninitialized;
|
||||
}
|
||||
|
||||
bool UserStats::IsRankInitialized() const
|
||||
{
|
||||
return m_rank != kUninitialized;
|
||||
}
|
||||
|
||||
bool UserStats::Update()
|
||||
{
|
||||
auto const url = kUserStatsUrl + "&name=" + UrlEncode(m_userName);
|
||||
TRequest request(url);
|
||||
|
||||
if (!request.RunHTTPRequest())
|
||||
{
|
||||
LOG(LWARNING, ("Network error while connecting to", url));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (request.error_code() != 200)
|
||||
{
|
||||
LOG(LWARNING, ("Server returned", request.error_code(), "for url", url));
|
||||
return false;
|
||||
}
|
||||
|
||||
auto const response = request.server_response();
|
||||
|
||||
pugi::xml_document document;
|
||||
if (!document.load_buffer(response.data(), response.size()))
|
||||
{
|
||||
LOG(LWARNING, ("Cannot parse server response:", response));
|
||||
return false;
|
||||
}
|
||||
|
||||
m_changesCount = document.select_node("mmwatch/edits/@value").attribute().as_int(kUninitialized);
|
||||
m_rank = document.select_node("mmwatch/rank/@value").attribute().as_int(kUninitialized);
|
||||
|
||||
return true;
|
||||
}
|
||||
} // namespace editor
|
30
editor/user_stats.hpp
Normal file
30
editor/user_stats.hpp
Normal file
|
@ -0,0 +1,30 @@
|
|||
#pragma once
|
||||
|
||||
#include "std/cstdint.hpp"
|
||||
#include "std/string.hpp"
|
||||
|
||||
namespace editor
|
||||
{
|
||||
class UserStats
|
||||
{
|
||||
public:
|
||||
explicit UserStats(string const & userName);
|
||||
|
||||
bool IsChangesCountInitialized() const;
|
||||
bool IsRankInitialized() const;
|
||||
|
||||
int32_t GetChangesCount() const { return m_changesCount; }
|
||||
int32_t GetRank() const { return m_rank; }
|
||||
|
||||
bool GetUpdateStatus() const { return m_updateStatus; }
|
||||
bool Update();
|
||||
|
||||
private:
|
||||
string m_userName;
|
||||
int32_t m_changesCount;
|
||||
int32_t m_rank;
|
||||
|
||||
/// True if last update was successful.
|
||||
bool m_updateStatus;
|
||||
};
|
||||
} // namespace editor
|
|
@ -1432,7 +1432,7 @@ void Framework::FillSearchResultsMarks(search::Results const & results)
|
|||
if (r.GetResultType() == search::Result::RESULT_FEATURE)
|
||||
mark->SetFoundFeature(r.GetFeatureID());
|
||||
mark->SetMatchedName(r.GetString());
|
||||
|
||||
|
||||
if (r.m_metadata.m_isSponsoredHotel)
|
||||
mark->SetCustomSymbol("search-booking");
|
||||
}
|
||||
|
@ -1909,9 +1909,9 @@ void Framework::ActivateMapSelection(bool needAnimation, df::SelectionShape::ESe
|
|||
m_selectedFeature = info.GetID();
|
||||
CallDrapeFunction(bind(&df::DrapeEngine::SelectObject, _1, selectionType, info.GetMercator(),
|
||||
needAnimation));
|
||||
|
||||
|
||||
SetDisplacementMode(info.m_isSponsoredHotel ? dp::displacement::kHotelMode : dp::displacement::kDefaultMode);
|
||||
|
||||
|
||||
if (m_activateMapSelectionFn)
|
||||
m_activateMapSelectionFn(info);
|
||||
else
|
||||
|
@ -2882,3 +2882,12 @@ bool Framework::RollBackChanges(FeatureID const & fid)
|
|||
}
|
||||
return rolledBack;
|
||||
}
|
||||
|
||||
bool Framework::UpdateUserStats(string const & userName)
|
||||
{
|
||||
auto userStats = make_unique<editor::UserStats>(userName);
|
||||
if (!userStats->GetUpdateStatus())
|
||||
return false;
|
||||
m_userStats = move(userStats);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -19,6 +19,8 @@
|
|||
#include "indexer/map_style.hpp"
|
||||
#include "indexer/new_feature_categories.hpp"
|
||||
|
||||
#include "editor/user_stats.hpp"
|
||||
|
||||
#include "search/query_saver.hpp"
|
||||
#include "search/search_engine.hpp"
|
||||
|
||||
|
@ -671,5 +673,18 @@ private:
|
|||
routing::RouterType m_currentRouterType;
|
||||
//@}
|
||||
|
||||
public:
|
||||
//@{
|
||||
//User statistics.
|
||||
editor::UserStats const * GetUserStats() const { return m_userStats.get(); }
|
||||
/// Sends a synchronous request to the server and updates user's stats.
|
||||
/// @returns true on success.
|
||||
bool UpdateUserStats(string const & userName);
|
||||
void DropUserStats() { m_userStats = nullptr; }
|
||||
|
||||
private:
|
||||
unique_ptr<editor::UserStats> m_userStats;
|
||||
//@}
|
||||
|
||||
DECLARE_THREAD_CHECKER(m_threadChecker);
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue