Restrictions are added for RB banners

This commit is contained in:
Arsentiy Milchakov 2017-04-03 13:28:59 +03:00 committed by Vladimir Byko-Ianko
parent f4e886def4
commit 92f67ec158
9 changed files with 126 additions and 55 deletions

View file

@ -181,7 +181,7 @@ bool Info::HasBanner() const
if (IsMyPosition())
return false;
return m_adsEngine->HasBanner(m_types);
return m_adsEngine->HasBanner(m_types, m_countryId);
}
vector<ads::Banner> Info::GetBanners() const
@ -189,7 +189,7 @@ vector<ads::Banner> Info::GetBanners() const
if (!m_adsEngine)
return {};
return m_adsEngine->GetBanners(m_types);
return m_adsEngine->GetBanners(m_types, m_countryId);
}
bool Info::IsReachableByTaxi() const

View file

@ -61,14 +61,27 @@ void Container::AppendExcludedTypes(std::vector<std::vector<std::string>> const
}
}
bool Container::HasBanner(feature::TypesHolder const & types) const
void Container::AppendSupportedCountries(std::vector<storage::TCountryId> const & countries)
{
m_supportedCountries.insert(countries.begin(), countries.end());
}
bool Container::HasBanner(feature::TypesHolder const & types,
storage::TCountryId const & countryId) const
{
if (!m_supportedCountries.empty() &&
m_supportedCountries.find(countryId) == m_supportedCountries.end())
{
return false;
}
return FindType(types, m_excludedTypes) == m_excludedTypes.cend();
}
std::string Container::GetBannerId(feature::TypesHolder const & types) const
std::string Container::GetBannerId(feature::TypesHolder const & types,
storage::TCountryId const & countryId) const
{
if (!HasBanner(types))
if (!HasBanner(types, countryId))
return {};
auto const it = FindType(types, m_typesToBanners);

View file

@ -1,6 +1,6 @@
#pragma once
#include "indexer/feature_data.hpp"
#include "storage/index.hpp"
#include "base/macros.hpp"
@ -8,6 +8,12 @@
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
namespace feature
{
class TypesHolder;
}
namespace ads
{
@ -15,8 +21,10 @@ class ContainerBase
{
public:
virtual ~ContainerBase() = default;
virtual bool HasBanner(feature::TypesHolder const & types) const = 0;
virtual std::string GetBannerId(feature::TypesHolder const & types) const = 0;
virtual bool HasBanner(feature::TypesHolder const & types,
storage::TCountryId const & countryId) const = 0;
virtual std::string GetBannerId(feature::TypesHolder const & types,
storage::TCountryId const & countryId) const = 0;
virtual std::string GetBannerIdForOtherTypes() const = 0;
};
@ -27,17 +35,22 @@ public:
Container();
// ContainerBase overrides:
bool HasBanner(feature::TypesHolder const & types) const override;
std::string GetBannerId(feature::TypesHolder const & types) const override;
bool HasBanner(feature::TypesHolder const & types,
storage::TCountryId const & countryId) const override;
std::string GetBannerId(feature::TypesHolder const & types,
storage::TCountryId const & countryId) const override;
std::string GetBannerIdForOtherTypes() const override;
protected:
void AppendEntry(std::vector<std::vector<std::string>> const & types, std::string const & id);
void AppendExcludedTypes(std::vector<std::vector<std::string>> const & types);
void AppendSupportedCountries(std::vector<storage::TCountryId> const & countries);
private:
std::unordered_map<uint32_t, std::string> m_typesToBanners;
std::unordered_set<uint32_t> m_excludedTypes;
// All countries are supported when empty.
std::unordered_set<storage::TCountryId> m_supportedCountries;
DISALLOW_COPY(Container);
};

View file

@ -2,6 +2,8 @@
#include "partners_api/facebook_ads.hpp"
#include "partners_api/rb_ads.hpp"
#include "indexer/feature_data.hpp"
#include "base/stl_add.hpp"
#include <algorithm>
@ -15,20 +17,22 @@ Engine::Engine()
m_containers.emplace_back(Banner::Type::Facebook, my::make_unique<Facebook>());
}
bool Engine::HasBanner(feature::TypesHolder const & types) const
bool Engine::HasBanner(feature::TypesHolder const & types,
storage::TCountryId const & countryId) const
{
return std::any_of(m_containers.cbegin(), m_containers.cend(), [&types](ContainerItem const & item)
{
return item.m_container->HasBanner(types);
});
return std::any_of(m_containers.cbegin(), m_containers.cend(),
[&types, &countryId](ContainerItem const & item) {
return item.m_container->HasBanner(types, countryId);
});
}
std::vector<Banner> Engine::GetBanners(feature::TypesHolder const & types) const
std::vector<Banner> Engine::GetBanners(feature::TypesHolder const & types,
storage::TCountryId const & countryId) const
{
std::vector<Banner> banners;
for (auto const & item : m_containers)
{
auto const bannerId = item.m_container->GetBannerId(types);
auto const bannerId = item.m_container->GetBannerId(types, countryId);
if (!bannerId.empty())
banners.emplace_back(item.m_type, bannerId);
}

View file

@ -7,6 +7,11 @@
#include <string>
#include <vector>
namespace feature
{
class TypesHolder;
}
namespace ads
{
class Engine
@ -14,8 +19,9 @@ class Engine
public:
Engine();
bool HasBanner(feature::TypesHolder const & types) const;
std::vector<Banner> GetBanners(feature::TypesHolder const & types) const;
bool HasBanner(feature::TypesHolder const & types, storage::TCountryId const & countryId) const;
std::vector<Banner> GetBanners(feature::TypesHolder const & types,
storage::TCountryId const & countryId) const;
private:
using ContainerPtr = std::unique_ptr<ContainerBase>;

View file

@ -34,38 +34,38 @@ UNIT_TEST(AdsEngine_Smoke)
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"amenity", "dentist"}));
TEST(engine.HasBanner(holder), ());
auto result = engine.GetBanners(holder);
TEST(engine.HasBanner(holder, "Ukraine"), ());
auto result = engine.GetBanners(holder, "Ukraine");
CheckCountAndTypes(result);
CheckIds(result, {"7", "185237551520383_1384652351578891"});
holder.Add(c.GetTypeByPath({"amenity", "pub"}));
TEST(engine.HasBanner(holder), ());
result = engine.GetBanners(holder);
TEST(engine.HasBanner(holder, "Ukraine"), ());
result = engine.GetBanners(holder, "Ukraine");
CheckCountAndTypes(result);
CheckIds(result, {"7", "185237551520383_1384652351578891"});
}
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"tourism", "information", "map"}));
TEST(engine.HasBanner(holder), ());
auto result = engine.GetBanners(holder);
TEST(engine.HasBanner(holder, "Moldova"), ());
auto result = engine.GetBanners(holder, "Moldova");
CheckCountAndTypes(result);
CheckIds(result, {"5", "185237551520383_1384651734912286"});
}
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"shop", "ticket"}));
TEST(engine.HasBanner(holder), ());
auto result = engine.GetBanners(holder);
TEST(engine.HasBanner(holder, "Russia"), ());
auto result = engine.GetBanners(holder, "Russia");
CheckCountAndTypes(result);
CheckIds(result, {"2", "185237551520383_1384650804912379"});
}
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"amenity", "bank"}));
TEST(engine.HasBanner(holder), ());
auto result = engine.GetBanners(holder);
TEST(engine.HasBanner(holder, "Belarus"), ());
auto result = engine.GetBanners(holder, "Belarus");
CheckCountAndTypes(result);
CheckIds(result, {"8", "185237551520383_1384652658245527"});
}
@ -74,24 +74,32 @@ UNIT_TEST(AdsEngine_Smoke)
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"amenity", "toilets"}));
TEST(engine.HasBanner(holder), ());
auto result = engine.GetBanners(holder);
TEST(engine.HasBanner(holder, "Armenia"), ());
auto result = engine.GetBanners(holder, "Armenia");
CheckCountAndTypes(result);
CheckIds(result, {rb.GetBannerIdForOtherTypes(), facebook.GetBannerIdForOtherTypes()});
}
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"sponsored", "opentable"}));
TEST(engine.HasBanner(holder), ());
auto result = engine.GetBanners(holder);
CheckCountAndTypes(result);
CheckIds(result, {rb.GetBannerIdForOtherTypes(), facebook.GetBannerIdForOtherTypes()});
TEST(engine.HasBanner(holder, "Brazil"), ());
auto result = engine.GetBanners(holder, "Brazil");
CheckIds(result, {facebook.GetBannerIdForOtherTypes()});
TEST_EQUAL(result[0].m_type, ads::Banner::Type::Facebook, ());
}
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"amenity", "pub"}));
TEST(engine.HasBanner(holder, "Spain"), ());
auto result = engine.GetBanners(holder, "Spain");
CheckIds(result, {"185237551520383_1384650164912443"});
TEST_EQUAL(result[0].m_type, ads::Banner::Type::Facebook, ());
}
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"sponsored", "booking"}));
TEST(!engine.HasBanner(holder), ());
auto result = engine.GetBanners(holder);
TEST(!engine.HasBanner(holder, "Russia"), ());
auto result = engine.GetBanners(holder, "Russia");
TEST(result.empty(), ());
}
}

View file

@ -16,41 +16,41 @@ UNIT_TEST(Facebook_GetBanner)
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"amenity", "dentist"}));
TEST_EQUAL(facebook.GetBannerId(holder), "185237551520383_1384652351578891", ());
TEST_EQUAL(facebook.GetBannerId(holder, "Brazil"), "185237551520383_1384652351578891", ());
holder.Add(c.GetTypeByPath({"amenity", "pub"}));
TEST_EQUAL(facebook.GetBannerId(holder), "185237551520383_1384652351578891", ());
TEST_EQUAL(facebook.GetBannerId(holder, "Cuba"), "185237551520383_1384652351578891", ());
}
{
feature::TypesHolder holder;
holder.Add(c.GetTypeByPath({"amenity", "restaurant"}));
TEST_EQUAL(facebook.GetBannerId(holder), "185237551520383_1384650164912443", ());
TEST_EQUAL(facebook.GetBannerId(holder, "Any country"), "185237551520383_1384650164912443", ());
}
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"tourism", "information", "map"}));
TEST_EQUAL(facebook.GetBannerId(holder), "185237551520383_1384651734912286", ());
TEST_EQUAL(facebook.GetBannerId(holder, "Russia"), "185237551520383_1384651734912286", ());
}
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"shop", "ticket"}));
TEST_EQUAL(facebook.GetBannerId(holder), "185237551520383_1384650804912379", ());
TEST_EQUAL(facebook.GetBannerId(holder, "USA"), "185237551520383_1384650804912379", ());
}
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"amenity", "toilets"}));
auto const bannerId = facebook.GetBannerIdForOtherTypes();
TEST_EQUAL(facebook.GetBannerId(holder), bannerId, ());
TEST_EQUAL(facebook.GetBannerId(holder, "Spain"), bannerId, ());
}
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"sponsored", "opentable"}));
auto const bannerId = facebook.GetBannerIdForOtherTypes();
TEST_EQUAL(facebook.GetBannerId(holder), bannerId, ());
TEST_EQUAL(facebook.GetBannerId(holder, "Denmark"), bannerId, ());
}
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"sponsored", "booking"}));
TEST_EQUAL(facebook.GetBannerId(holder), "", ());
TEST_EQUAL(facebook.GetBannerId(holder, "India"), "", ());
}
}
} // namespace

View file

@ -16,51 +16,61 @@ UNIT_TEST(Rb_GetBanner)
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"amenity", "dentist"}));
TEST_EQUAL(rb.GetBannerId(holder), "7", ());
TEST_EQUAL(rb.GetBannerId(holder, "Russia"), "7", ());
holder.Add(c.GetTypeByPath({"amenity", "pub"}));
TEST_EQUAL(rb.GetBannerId(holder), "7", ());
TEST_EQUAL(rb.GetBannerId(holder, "Russia"), "7", ());
}
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"amenity", "restaurant"}));
TEST_EQUAL(rb.GetBannerId(holder), "1", ());
TEST_EQUAL(rb.GetBannerId(holder, "Russia"), "1", ());
}
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"tourism", "information", "map"}));
TEST_EQUAL(rb.GetBannerId(holder), "5", ());
TEST_EQUAL(rb.GetBannerId(holder, "Russia"), "5", ());
}
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"shop", "ticket"}));
TEST_EQUAL(rb.GetBannerId(holder), "2", ());
TEST_EQUAL(rb.GetBannerId(holder, "Russia"), "2", ());
}
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"amenity", "bank"}));
TEST_EQUAL(rb.GetBannerId(holder), "8", ());
TEST_EQUAL(rb.GetBannerId(holder, "Russia"), "8", ());
}
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"amenity", "atm"}));
TEST_EQUAL(rb.GetBannerId(holder), "8", ());
TEST_EQUAL(rb.GetBannerId(holder, "Russia"), "8", ());
}
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"amenity", "atm"}));
TEST_EQUAL(rb.GetBannerId(holder, "Brazil"), "", ());
}
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"amenity", "toilets"}));
auto const bannerId = rb.GetBannerIdForOtherTypes();
TEST_EQUAL(rb.GetBannerId(holder), bannerId, ());
TEST_EQUAL(rb.GetBannerId(holder, "Russia"), bannerId, ());
}
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"sponsored", "opentable"}));
auto const bannerId = rb.GetBannerIdForOtherTypes();
TEST_EQUAL(rb.GetBannerId(holder), bannerId, ());
TEST_EQUAL(rb.GetBannerId(holder, "Russia"), bannerId, ());
}
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"sponsored", "opentable"}));
TEST_EQUAL(rb.GetBannerId(holder, "Brazil"), "", ());
}
{
feature::TypesHolder holder;
holder.Assign(c.GetTypeByPath({"sponsored", "booking"}));
TEST_EQUAL(rb.GetBannerId(holder), "", ());
TEST_EQUAL(rb.GetBannerId(holder, "Russia"), "", ());
}
}
} // namespace

View file

@ -14,6 +14,21 @@ auto const kFinancialPlacementId = "8";
auto const kEntertainmentPlacementId = "9";
auto const kBuildingPlacementId = "11";
auto const kBannerIdForOtherTypes = "14";
std::vector<storage::TCountryId> const kSupportedCountries =
{
"Azerbaijan",
"Armenia",
"Belarus",
"Kazakhstan",
"Kyrgyzstan",
"Moldova",
"Russia",
"Tajikistan",
"Turkmenistan",
"Uzbekistan",
"Ukraine"
};
} // namespace
namespace ads
@ -83,6 +98,8 @@ Rb::Rb()
kEntertainmentPlacementId);
AppendEntry({{"building"}}, kBuildingPlacementId);
AppendSupportedCountries(kSupportedCountries);
}
std::string Rb::GetBannerIdForOtherTypes() const