Skip to content

Commit

Permalink
Fix history should always respect wait time when showing Ads for mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
tmancey authored Jun 11, 2019
1 parent fc280df commit bc14d62
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
57 changes: 52 additions & 5 deletions vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -726,13 +726,19 @@ std::vector<AdInfo> AdsImpl::GetUnseenAds(
}

if (creative_set.size() >= ad.total_max) {
BLOG(WARNING) << "creativeSetId " << ad.creative_set_id
<< " has exceeded the totalMax";

continue;
}

auto day_window = base::Time::kSecondsPerHour * base::Time::kHoursPerDay;

if (!HistoryRespectsRollingTimeConstraint(
creative_set, day_window, ad.per_day)) {
BLOG(WARNING) << "creativeSetId " << ad.creative_set_id
<< " has exceeded the perDay";

continue;
}

Expand All @@ -745,6 +751,9 @@ std::vector<AdInfo> AdsImpl::GetUnseenAds(

if (!HistoryRespectsRollingTimeConstraint(
campaign, day_window, ad.daily_cap)) {
BLOG(WARNING) << "creativeSetId " << ad.creative_set_id
<< " has exceeded the dailyCap";

continue;
}

Expand Down Expand Up @@ -837,24 +846,62 @@ bool AdsImpl::HistoryRespectsRollingTimeConstraint(
}

bool AdsImpl::IsAllowedToShowAds() {
auto does_history_respect_ads_per_day_limit =
DoesHistoryRespectAdsPerDayLimit();

bool does_history_respect_minimum_wait_time;
if (!IsMobile()) {
does_history_respect_minimum_wait_time =
DoesHistoryRespectMinimumWaitTimeToShowAds();
} else {
does_history_respect_minimum_wait_time = true;
}

BLOG(INFO) << "IsAllowedToShowAds:";
BLOG(INFO) << " does_history_respect_minimum_wait_time: "
<< does_history_respect_minimum_wait_time;
BLOG(INFO) << " does_history_respect_ads_per_day_limit: "
<< does_history_respect_ads_per_day_limit;

return does_history_respect_minimum_wait_time &&
does_history_respect_ads_per_day_limit;
}

bool AdsImpl::DoesHistoryRespectMinimumWaitTimeToShowAds() {
auto ads_shown_history = client_->GetAdsShownHistory();

auto hour_window = base::Time::kSecondsPerHour;
auto hour_allowed = ads_client_->GetAdsPerHour();
auto respects_hour_limit = HistoryRespectsRollingTimeConstraint(
ads_shown_history, hour_window, hour_allowed);

auto minimum_wait_time = hour_window / hour_allowed;
auto respects_minimum_wait_time = HistoryRespectsRollingTimeConstraint(
ads_shown_history, minimum_wait_time, 0);

BLOG(INFO) << "DoesHistoryRespectMinimumWaitTimeToShowAds:";
BLOG(INFO) << " respects_hour_limit: "
<< respects_hour_limit;
BLOG(INFO) << " respects_minimum_wait_time: "
<< respects_minimum_wait_time;

return respects_hour_limit && respects_minimum_wait_time;
}

bool AdsImpl::DoesHistoryRespectAdsPerDayLimit() {
auto ads_shown_history = client_->GetAdsShownHistory();

auto day_window = base::Time::kSecondsPerHour * base::Time::kHoursPerDay;
auto day_allowed = ads_client_->GetAdsPerDay();

auto respects_day_limit = HistoryRespectsRollingTimeConstraint(
ads_shown_history, day_window, day_allowed);

auto minimum_wait_time = hour_window / hour_allowed;
bool respects_minimum_wait_time = HistoryRespectsRollingTimeConstraint(
ads_shown_history, minimum_wait_time, 0);
BLOG(INFO) << "DoesHistoryRespectAdsPerDayLimit:";
BLOG(INFO) << " respects_day_limit: "
<< respects_day_limit;

return respects_hour_limit && respects_day_limit &&
respects_minimum_wait_time;
return respects_day_limit;
}

void AdsImpl::StartCollectingActivity(const uint64_t start_timer_in) {
Expand Down
2 changes: 2 additions & 0 deletions vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ class AdsImpl : public Ads {
const uint64_t seconds_window,
const uint64_t allowable_ad_count) const;
bool IsAllowedToShowAds();
bool DoesHistoryRespectMinimumWaitTimeToShowAds();
bool DoesHistoryRespectAdsPerDayLimit();

uint32_t collect_activity_timer_id_;
void StartCollectingActivity(const uint64_t start_timer_in);
Expand Down

0 comments on commit bc14d62

Please sign in to comment.