Merge pull request #1794 from mpimenov/search-stat-events

[search] More statistics.
This commit is contained in:
ygorshenin 2016-02-04 14:03:47 +03:00
commit 690003a836
5 changed files with 102 additions and 35 deletions

View file

@ -1082,6 +1082,10 @@ void Framework::ShowSearchResult(search::Result const & res)
using namespace search;
using namespace feature;
alohalytics::TStringMap const stats = {{"pos", strings::to_string(res.GetPositionInResults())},
{"result", res.ToStringForStats()}};
alohalytics::LogEvent("searchShowResult", stats);
switch (res.GetResultType())
{
case Result::RESULT_FEATURE:

View file

@ -13,20 +13,42 @@ Result::Result(FeatureID const & id, m2::PointD const & pt, string const & str,
, m_region(region)
, m_type(type)
, m_featureType(featureType)
, m_positionInResults(-1)
, m_metadata(meta)
{
Init(true /* metadataInitialized */);
}
Result::Result(FeatureID const & id, m2::PointD const & pt, string const & str, string const & type)
: m_id(id)
, m_center(pt)
, m_str(str)
, m_type(type)
: m_id(id), m_center(pt), m_str(str), m_type(type), m_positionInResults(-1)
{
Init(false /* metadataInitialized */);
}
Result::Result(m2::PointD const & pt, string const & str, string const & region,
string const & type)
: m_center(pt), m_str(str), m_region(region), m_type(type), m_positionInResults(-1)
{
}
Result::Result(string const & str, string const & suggest)
: m_str(str), m_suggestionStr(suggest), m_positionInResults(-1)
{
}
Result::Result(Result const & res, string const & suggest)
: m_id(res.m_id)
, m_center(res.m_center)
, m_str(res.m_str)
, m_region(res.m_region)
, m_type(res.m_type)
, m_featureType(res.m_featureType)
, m_suggestionStr(suggest)
, m_hightlightRanges(res.m_hightlightRanges)
, m_positionInResults(-1)
{
}
void Result::Init(bool metadataInitialized)
{
// Features with empty names can be found after suggestion.
@ -36,24 +58,6 @@ void Result::Init(bool metadataInitialized)
m_metadata.m_isInitialized = metadataInitialized;
}
Result::Result(m2::PointD const & pt, string const & str,
string const & region, string const & type)
: m_center(pt), m_str(str), m_region(region), m_type(type)
{
}
Result::Result(string const & str, string const & suggest)
: m_str(str), m_suggestionStr(suggest)
{
}
Result::Result(Result const & res, string const & suggest)
: m_id(res.m_id), m_center(res.m_center), m_str(res.m_str),
m_region(res.m_region), m_type(res.m_type), m_featureType(res.m_featureType),
m_suggestionStr(suggest), m_hightlightRanges(res.m_hightlightRanges)
{
}
Result::ResultType Result::GetResultType() const
{
bool const idValid = m_id.IsValid();
@ -148,6 +152,17 @@ void Result::AppendCity(string const & name)
m_region += (", " + name);
}
string Result::ToStringForStats() const
{
string s;
s.append(GetString());
s.append("|");
s.append(GetFeatureType());
s.append("|");
s.append(IsSuggest() ? "1" : "0");
return s;
}
bool Results::AddResult(Result && res)
{
// Find first feature result.
@ -172,6 +187,13 @@ bool Results::AddResult(Result && res)
if (res.IsEqualSuggest(*i))
return false;
for (auto i = it; i != m_vec.end(); ++i)
{
auto & r = *i;
auto const oldPos = r.GetPositionInResults();
r.SetPositionInResults(oldPos + 1);
}
res.SetPositionInResults(distance(m_vec.begin(), it));
m_vec.insert(it, move(res));
}
else
@ -180,6 +202,7 @@ bool Results::AddResult(Result && res)
if (res.IsEqualFeature(*it))
return false;
res.SetPositionInResults(m_vec.size());
m_vec.push_back(move(res));
}

View file

@ -91,6 +91,14 @@ public:
void AppendCity(string const & name);
int32_t GetPositionInResults() const { return m_positionInResults; }
void SetPositionInResults(int32_t pos) { m_positionInResults = pos; }
// Returns a representation of this result that is
// sent to the statistics servers and later used to measure
// the quality of our search engine.
string ToStringForStats() const;
private:
void Init(bool metadataInitialized);
@ -101,6 +109,10 @@ private:
string m_suggestionStr;
buffer_vector<pair<uint16_t, uint16_t>, 4> m_hightlightRanges;
// The position that this result occupied in the vector returned
// by a search query. -1 if undefined.
int32_t m_positionInResults;
public:
Metadata m_metadata;
};
@ -137,6 +149,7 @@ public:
/// Used in viewport search only.
void AddResultNoChecks(Result && res)
{
res.SetPositionInResults(m_vec.size());
m_vec.push_back(move(res));
}
@ -187,4 +200,4 @@ struct AddressInfo
void Clear();
};
}
} // namespace search

View file

@ -7,19 +7,20 @@
#include "indexer/categories_holder.hpp"
#include "indexer/search_string_utils.hpp"
#include "geometry/mercator.hpp"
#include "indexer/scales.hpp"
#include "indexer/classificator.hpp"
#include "platform/platform.hpp"
#include "geometry/distance_on_sphere.hpp"
#include "geometry/mercator.hpp"
#include "base/stl_add.hpp"
#include "std/algorithm.hpp"
#include "std/bind.hpp"
#include "std/map.hpp"
#include "std/vector.hpp"
#include "std/bind.hpp"
#include "3party/Alohalytics/src/alohalytics.h"
@ -69,7 +70,35 @@ public:
}
};
void SendStatistics(SearchParams const & params, m2::RectD const & viewport, Results const & res)
{
size_t const kMaxNumResultsToSend = 10;
size_t const numResultsToSend = min(kMaxNumResultsToSend, res.GetCount());
string resultString = strings::to_string(numResultsToSend);
for (size_t i = 0; i < numResultsToSend; ++i)
resultString.append("\t" + res.GetResult(i).ToStringForStats());
string posX, posY;
if (params.IsValidPosition())
{
posX = strings::to_string(MercatorBounds::LonToX(params.m_lon));
posY = strings::to_string(MercatorBounds::LatToY(params.m_lat));
}
alohalytics::TStringMap const stats = {
{"posX", posX},
{"posY", posY},
{"viewportMinX", strings::to_string(viewport.minX())},
{"viewportMinY", strings::to_string(viewport.minY())},
{"viewportMaxX", strings::to_string(viewport.maxX())},
{"viewportMaxY", strings::to_string(viewport.maxY())},
{"query", params.m_query},
{"results", resultString},
};
alohalytics::LogEvent("searchEmitResultsAndCoords", stats);
}
} // namespace
Engine::Engine(Index & index, Reader * categoriesR, storage::CountryInfoGetter const & infoGetter,
string const & locale, unique_ptr<SearchQueryFactory> && factory)
@ -151,12 +180,8 @@ void Engine::SetViewportAsync(m2::RectD const & viewport)
m_query->SetViewport(r, true);
}
void Engine::EmitResults(SearchParams const & params, Results & res)
void Engine::EmitResults(SearchParams const & params, Results const & res)
{
// Basic test of our statistics engine.
alohalytics::LogEvent("searchEmitResults",
alohalytics::TStringMap({{params.m_query, strings::to_string(res.GetCount())}}));
params.m_callback(res);
}
@ -281,6 +306,9 @@ void Engine::SearchAsync()
EmitResults(params, res);
}
if (!viewportSearch && !m_query->IsCancelled())
SendStatistics(params, viewport, res);
// Emit finish marker to client.
params.m_callback(Results::GetEndMarker(m_query->IsCancelled()));
}

View file

@ -10,11 +10,10 @@
#include "base/mutex.hpp"
#include "std/unique_ptr.hpp"
#include "std/string.hpp"
#include "std/function.hpp"
#include "std/atomic.hpp"
#include "std/function.hpp"
#include "std/string.hpp"
#include "std/unique_ptr.hpp"
class Index;
@ -58,7 +57,7 @@ private:
void SetViewportAsync(m2::RectD const & viewport);
void SearchAsync();
void EmitResults(SearchParams const & params, Results & res);
void EmitResults(SearchParams const & params, Results const & res);
threads::Mutex m_searchMutex;
threads::Mutex m_updateMutex;