Review fixes.

This commit is contained in:
Vladimir Byko-Ianko 2019-11-15 08:34:51 +03:00 committed by mesozoic-drones
parent f467c02a8b
commit ff0a85d33a
5 changed files with 55 additions and 59 deletions

View file

@ -23,6 +23,7 @@
#include "base/timer.hpp"
#include <algorithm>
#include <exception>
#include <memory>
#include <string>
#include <thread>
@ -93,7 +94,7 @@ void MatchTracks(MwmToTracks const & mwmToTracks, storage::Storage const & stora
namespace track_analyzing
{
void CmdMatch(string const & logFile, string const & trackFile,
shared_ptr<NumMwmIds> const & numMwmIds, Storage const & storage, Stat & stat)
shared_ptr<NumMwmIds> const & numMwmIds, Storage const & storage, Stats & stat)
{
MwmToTracks mwmToTracks;
ParseTracks(logFile, numMwmIds, mwmToTracks);
@ -115,13 +116,13 @@ void CmdMatch(string const & logFile, string const & trackFile)
storage.RegisterAllLocalMaps(false /* enableDiffs */);
shared_ptr<NumMwmIds> numMwmIds = CreateNumMwmIds(storage);
Stat stat;
Stats stat;
CmdMatch(logFile, trackFile, numMwmIds, storage, stat);
LOG(LINFO, ("CmdMatch. DataPoint distribution by mwms and countries."));
LOG(LINFO, ("DataPoint distribution by mwms and countries."));
LOG(LINFO, (stat));
}
void UnzipAndMatch(Iter begin, Iter end, string const & trackExt, Stat & stat)
void UnzipAndMatch(Iter begin, Iter end, string const & trackExt, Stats & stats)
{
Storage storage;
storage.RegisterAllLocalMaps(false /* enableDiffs */);
@ -151,13 +152,13 @@ void UnzipAndMatch(Iter begin, Iter end, string const & trackExt, Stat & stat)
FileWriter w(file);
w.Write(track.data(), track.size());
}
catch (FileWriter::WriteException const & e)
catch (std::exception const & e)
{
LOG(LWARNING, (e.what()));
continue;
}
CmdMatch(file, file + trackExt, numMwmIds, storage, stat);
CmdMatch(file, file + trackExt, numMwmIds, storage, stats);
FileWriter::DeleteFileX(file);
}
}
@ -200,7 +201,7 @@ void CmdMatchDir(string const & logDir, string const & trackExt)
auto const threadsCount = min(size, hardwareConcurrency);
auto const blockSize = size / threadsCount;
vector<thread> threads(threadsCount - 1);
vector<Stat> stats(threadsCount);
vector<Stats> stats(threadsCount);
auto begin = filesList.begin();
for (size_t i = 0; i < threadsCount - 1; ++i)
{
@ -213,11 +214,11 @@ void CmdMatchDir(string const & logDir, string const & trackExt)
for (auto & t : threads)
t.join();
Stat statSum;
Stats statSum;
for (auto const & s : stats)
statSum.Add(s);
LOG(LINFO, ("CmdMatchDir. DataPoint distribution by mwms and countries."));
LOG(LINFO, ("DataPoint distribution by mwms and countries."));
LOG(LINFO, (statSum));
}
} // namespace track_analyzing

View file

@ -283,7 +283,6 @@ private:
uint32_t m_dataPointsNumber = 0;
};
// @TODO Make a ctor and pass string const & mwm, string const & country to it.
class MoveTypeAggregator final
{
public:
@ -309,7 +308,8 @@ public:
m_moveInfos[moveType].Add(length, time, crossroads, static_cast<uint32_t>(distance(begin, end)));
}
string GetSummary(string const & user, string const & mwmName, string const & countryName, Stat & stat) const
string GetSummary(string const & user, string const & mwmName, string const & countryName,
Stats & stats) const
{
ostringstream out;
for (auto const & it : m_moveInfos)
@ -317,9 +317,11 @@ public:
if (!it.first.IsValid())
continue;
out << user << "," << countryName << "," << it.first.GetSummary() << "," << it.second.GetSummary() << '\n';
stat.m_mwmToTotalDataPoints[mwmName] += it.second.GetDataPointsNumber();
stat.m_countryToTotalDataPoints[countryName] += it.second.GetDataPointsNumber();
out << user << "," << countryName << "," << it.first.GetSummary() << ","
<< it.second.GetSummary() << '\n';
stats.m_mwmToTotalDataPoints[mwmName] += it.second.GetDataPointsNumber();
stats.m_countryToTotalDataPoints[countryName] += it.second.GetDataPointsNumber();
}
return out.str();
@ -396,19 +398,12 @@ void CmdTagsTable(string const & filepath, string const & trackExtension, String
FrozenDataSource dataSource;
auto numMwmIds = CreateNumMwmIds(storage);
Stat stat;
Stats stats;
auto processMwm = [&](string const & mwmName, UserToMatchedTracks const & userToMatchedTracks) {
if (mwmFilter(mwmName))
return;
auto const countryName = storage.GetTopmostParentFor(mwmName);
// @TODO It's better to implement value as a class.
if (stat.m_mwmToTotalDataPoints.count(mwmName) == 0)
stat.m_mwmToTotalDataPoints[mwmName] = 0;
if (stat.m_countryToTotalDataPoints.count(countryName) == 0)
stat.m_countryToTotalDataPoints[countryName] = 0;
auto const carModelFactory = make_shared<CarModelFactory>(VehicleModelFactory::CountryParentNameGetterFn{});
shared_ptr<VehicleModelInterface> vehicleModel =
carModelFactory->GetVehicleModelForCountry(mwmName);
@ -471,7 +466,7 @@ void CmdTagsTable(string const & filepath, string const & trackExtension, String
info = move(crossroad);
}
auto const summary = aggregator.GetSummary(user, mwmName, countryName, stat);
auto const summary = aggregator.GetSummary(user, mwmName, countryName, stats);
if (!summary.empty())
cout << summary;
}
@ -486,7 +481,7 @@ void CmdTagsTable(string const & filepath, string const & trackExtension, String
ForEachTrackFile(filepath, trackExtension, numMwmIds, processTrack);
LOG(LINFO,
("CmdTagsTable. DataPoint distribution by mwms and countries and match and table commands."));
LOG(LINFO, (stat));
("DataPoint distribution by mwms and countries and match and table commands."));
LOG(LINFO, (stats));
}
} // namespace track_analyzing

View file

@ -14,6 +14,7 @@
#include <algorithm>
#include <set>
#include <sstream>
#include <utility>
using namespace routing;
using namespace std;
@ -22,15 +23,15 @@ using namespace track_analyzing;
namespace
{
set<string> GetKeys(Stat::NameToCountMapping const & mapping)
set<string> GetKeys(Stats::NameToCountMapping const & mapping)
{
set<string> keys;
transform(mapping.begin(), mapping.end(), inserter(keys, keys.end()),
[](auto const & kv) { return kv.first; });
[](auto const & kv) { return kv.first; });
return keys;
}
inline void Add(Stat::NameToCountMapping const & addition, Stat::NameToCountMapping & base)
void Add(Stats::NameToCountMapping const & addition, Stats::NameToCountMapping & base)
{
set<storage::CountryId> userKeys = GetKeys(addition);
set<storage::CountryId> const userKeys2 = GetKeys(base);
@ -52,14 +53,14 @@ inline void Add(Stat::NameToCountMapping const & addition, Stat::NameToCountMapp
}
void AddStat(uint32_t dataPointNum, routing::NumMwmId numMwmId,
NumMwmIds const & numMwmIds, Storage const & storage, Stat & stat)
NumMwmIds const & numMwmIds, Storage const & storage, Stats & stats)
{
auto const mwmName = numMwmIds.GetFile(numMwmId).GetName();
auto const countryName = storage.GetTopmostParentFor(mwmName);
// Note. In case of disputed mwms |countryName| will be empty.
stat.m_mwmToTotalDataPoints[mwmName] += dataPointNum;
stat.m_countryToTotalDataPoints[countryName] += dataPointNum;
stats.m_mwmToTotalDataPoints[mwmName] += dataPointNum;
stats.m_countryToTotalDataPoints[countryName] += dataPointNum;
}
void PrintMap(string const & keyType, string const & descr,
@ -91,7 +92,7 @@ void PrintMap(string const & keyType, string const & descr,
for (auto const & kv : keyValues)
allValues += kv.m_value;
ss << keyType << ",number,percent";
ss << keyType << ",number,percent\n";
for (auto const & kv : keyValues)
{
if (kv.m_value == 0)
@ -106,17 +107,17 @@ void PrintMap(string const & keyType, string const & descr,
namespace track_analyzing
{
// Stat ============================================================================================
void Stat::Add(Stat const & stat)
// Stats ===========================================================================================
void Stats::Add(Stats const & stats)
{
::Add(stat.m_mwmToTotalDataPoints, m_mwmToTotalDataPoints);
::Add(stat.m_countryToTotalDataPoints, m_countryToTotalDataPoints);
::Add(stats.m_mwmToTotalDataPoints, m_mwmToTotalDataPoints);
::Add(stats.m_countryToTotalDataPoints, m_countryToTotalDataPoints);
}
bool Stat::operator==(Stat const & stat) const
bool Stats::operator==(Stats const & stats) const
{
return m_mwmToTotalDataPoints == stat.m_mwmToTotalDataPoints &&
m_countryToTotalDataPoints == stat.m_countryToTotalDataPoints;
return m_mwmToTotalDataPoints == stats.m_mwmToTotalDataPoints &&
m_countryToTotalDataPoints == stats.m_countryToTotalDataPoints;
}
void ParseTracks(string const & logFile, shared_ptr<NumMwmIds> const & numMwmIds,
@ -134,7 +135,7 @@ void ParseTracks(string const & logFile, shared_ptr<NumMwmIds> const & numMwmIds
}
void AddStat(MwmToTracks const & mwmToTracks, NumMwmIds const & numMwmIds, Storage const & storage,
Stat & stat)
Stats & stats)
{
for (auto const & kv : mwmToTracks)
{
@ -143,14 +144,14 @@ void AddStat(MwmToTracks const & mwmToTracks, NumMwmIds const & numMwmIds, Stora
for (auto const & userTrack : userToTrack)
dataPointNum += userTrack.second.size();
::AddStat(dataPointNum, kv.first, numMwmIds, storage, stat);
::AddStat(dataPointNum, kv.first, numMwmIds, storage, stats);
}
}
string DebugPrint(Stat const & s)
string DebugPrint(Stats const & s)
{
ostringstream ss;
ss << "Stat [\n";
ss << "Stats [\n";
PrintMap("mwm", "Mwm to total data points number:", s.m_mwmToTotalDataPoints, ss);
PrintMap("country", "Country name to data points number:", s.m_countryToTotalDataPoints, ss);
ss << "]\n" << endl;

View file

@ -14,13 +14,12 @@
namespace track_analyzing
{
// @TODO Rename to Stats
struct Stat
struct Stats
{
using NameToCountMapping = std::map<std::string, uint32_t>;
void Add(Stat const & stat);
bool operator==(Stat const & stat) const;
void Add(Stats const & stats);
bool operator==(Stats const & stats) const;
/// \note These fields may present mapping from territory name to either DataPoints
/// or MatchedTrackPoint count.
@ -28,13 +27,13 @@ struct Stat
NameToCountMapping m_countryToTotalDataPoints;
};
/// \brief Parses tracks from |logFile| and fills |numMwmIds|, |storage| and |mwmToTracks|.
/// \brief Parses tracks from |logFile| and fills |mwmToTracks|.
void ParseTracks(std::string const & logFile, std::shared_ptr<routing::NumMwmIds> const & numMwmIds,
MwmToTracks & mwmToTracks);
/// \brief Fills |stat| according to |mwmToTracks|.
void AddStat(MwmToTracks const & mwmToTracks, routing::NumMwmIds const & numMwmIds,
storage::Storage const & storage, Stat & stat);
storage::Storage const & storage, Stats & stats);
std::string DebugPrint(Stat const & s);
std::string DebugPrint(Stats const & s);
} // namespace track_analyzing

View file

@ -25,20 +25,20 @@ using namespace traffic;
UNIT_TEST(StatTest)
{
Stat mapping1 = {
Stats stats1 = {
{{"Belarus_Minsk Region", 1}, {"Uzbekistan", 7}, {"Russia_Moscow", 5} /* Mwm to number */},
{{"Russian Federation", 10}, {"Poland", 5} /* Country to number */}};
Stat const mapping2 = {{{"Belarus_Minsk Region", 2} /* Mwm to number */},
{{"Russian Federation", 1}, {"Belarus", 8} /* Country to number */}};
Stats const stats2 = {{{"Belarus_Minsk Region", 2} /* Mwm to number */},
{{"Russian Federation", 1}, {"Belarus", 8} /* Country to number */}};
mapping1.Add(mapping2);
stats1.Add(stats2);
Stat const expected = {
Stats const expected = {
{{"Belarus_Minsk Region", 3}, {"Uzbekistan", 7}, {"Russia_Moscow", 5} /* Mwm to number */},
{{"Russian Federation", 11}, {"Poland", 5}, {"Belarus", 8} /* Country to number */}};
TEST_EQUAL(mapping1, expected, ());
TEST_EQUAL(stats1, expected, ());
}
UNIT_TEST(AddStatTest)
@ -62,13 +62,13 @@ UNIT_TEST(AddStatTest)
auto numMwmIds = CreateNumMwmIds(storage);
MwmToTracks const mwmToTracks = {{numMwmIds->GetId(CountryFile(kMwmName)), userToTrack}};
Stat stat;
Stats stat;
AddStat(mwmToTracks, *numMwmIds, storage, stat);
Stat::NameToCountMapping const expectedMwmToTotalDataMapping = {{kMwmName, kDataPointNumber}};
Stats::NameToCountMapping const expectedMwmToTotalDataMapping = {{kMwmName, kDataPointNumber}};
TEST_EQUAL(stat.m_mwmToTotalDataPoints, expectedMwmToTotalDataMapping, ());
Stat::NameToCountMapping expectedCountryToTotalDataMapping = {{"Italy", kDataPointNumber}};
Stats::NameToCountMapping expectedCountryToTotalDataMapping = {{"Italy", kDataPointNumber}};
TEST_EQUAL(stat.m_countryToTotalDataPoints, expectedCountryToTotalDataMapping, ());
}
} // namespace