Skip to content

Commit

Permalink
Support day-parting for Brave Ads
Browse files Browse the repository at this point in the history
  • Loading branch information
yachtcaptain23 committed Oct 20, 2020
1 parent 8e3feb0 commit 4cc30f5
Show file tree
Hide file tree
Showing 24 changed files with 929 additions and 45 deletions.
1 change: 1 addition & 0 deletions components/brave_ads/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ source_set("brave_ads_unit_tests") {
"//brave/vendor/bat-native-ads/src/bat/ads/internal/frequency_capping/ad_exclusion_rules/new_tab_page_ad_wallpaper_frequency_cap_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/frequency_capping/exclusion_rules/conversion_frequency_cap_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/frequency_capping/exclusion_rules/daily_cap_frequency_cap_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/frequency_capping/exclusion_rules/daypart_frequency_cap_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/frequency_capping/exclusion_rules/dismissed_frequency_cap_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/frequency_capping/exclusion_rules/landed_frequency_cap_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/frequency_capping/exclusion_rules/marked_as_inappropriate_frequency_cap_unittest.cc",
Expand Down
6 changes: 5 additions & 1 deletion vendor/bat-native-ads/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ source_set("ads") {
"src/bat/ads/internal/catalog/catalog_creative_new_tab_page_ad_info.h",
"src/bat/ads/internal/catalog/catalog_creative_set_info.cc",
"src/bat/ads/internal/catalog/catalog_creative_set_info.h",
"src/bat/ads/internal/catalog/catalog_day_part_info.h",
"src/bat/ads/internal/catalog/catalog_daypart_info.h",
"src/bat/ads/internal/catalog/catalog_geo_target_info.h",
"src/bat/ads/internal/catalog/catalog_issuer_info.cc",
"src/bat/ads/internal/catalog/catalog_issuer_info.h",
Expand Down Expand Up @@ -234,6 +234,8 @@ source_set("ads") {
"src/bat/ads/internal/database/tables/creative_ads_database_table.h",
"src/bat/ads/internal/database/tables/creative_new_tab_page_ads_database_table.cc",
"src/bat/ads/internal/database/tables/creative_new_tab_page_ads_database_table.h",
"src/bat/ads/internal/database/tables/dayparts_database_table.cc",
"src/bat/ads/internal/database/tables/dayparts_database_table.h",
"src/bat/ads/internal/database/tables/geo_targets_database_table.cc",
"src/bat/ads/internal/database/tables/geo_targets_database_table.h",
"src/bat/ads/internal/eligible_ads/eligible_ads_filter.h",
Expand All @@ -259,6 +261,8 @@ source_set("ads") {
"src/bat/ads/internal/frequency_capping/exclusion_rules/conversion_frequency_cap.h",
"src/bat/ads/internal/frequency_capping/exclusion_rules/daily_cap_frequency_cap.cc",
"src/bat/ads/internal/frequency_capping/exclusion_rules/daily_cap_frequency_cap.h",
"src/bat/ads/internal/frequency_capping/exclusion_rules/daypart_frequency_cap.cc",
"src/bat/ads/internal/frequency_capping/exclusion_rules/daypart_frequency_cap.h",
"src/bat/ads/internal/frequency_capping/exclusion_rules/dismissed_frequency_cap.cc",
"src/bat/ads/internal/frequency_capping/exclusion_rules/dismissed_frequency_cap.h",
"src/bat/ads/internal/frequency_capping/exclusion_rules/exclusion_rule.h",
Expand Down
5 changes: 5 additions & 0 deletions vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "bat/ads/internal/filters/ads_history_filter_factory.h"
#include "bat/ads/internal/frequency_capping/exclusion_rules/conversion_frequency_cap.h"
#include "bat/ads/internal/frequency_capping/exclusion_rules/daily_cap_frequency_cap.h"
#include "bat/ads/internal/frequency_capping/exclusion_rules/daypart_frequency_cap.h"
#include "bat/ads/internal/frequency_capping/exclusion_rules/exclusion_rule.h"
#include "bat/ads/internal/frequency_capping/exclusion_rules/dismissed_frequency_cap.h"
#include "bat/ads/internal/frequency_capping/exclusion_rules/landed_frequency_cap.h"
Expand Down Expand Up @@ -947,6 +948,10 @@ AdsImpl::CreateAdNotificationExclusionRules() const {
std::make_unique<SubdivisionTargetingFrequencyCap>(this);
exclusion_rules.push_back(std::move(subdivision_targeting_frequency_cap));

std::unique_ptr<ExclusionRule> daypart_frequency_cap =
std::make_unique<DaypartFrequencyCap>(this);
exclusion_rules.push_back(std::move(daypart_frequency_cap));

std::unique_ptr<ExclusionRule> dismissed_frequency_cap =
std::make_unique<DismissedFrequencyCap>(this);
exclusion_rules.push_back(std::move(dismissed_frequency_cap));
Expand Down
28 changes: 28 additions & 0 deletions vendor/bat-native-ads/src/bat/ads/internal/bundle/bundle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ bool Bundle::UpdateFromCatalog(
DeleteCampaigns();
DeleteCategories();
DeleteCreativeAds();
DeleteDayparts();
DeleteGeoTargets();

SaveCreativeAdNotifications(bundle_state->creative_ad_notifications);
Expand Down Expand Up @@ -106,6 +107,11 @@ void Bundle::DeleteCreativeAds() {
database_table.Delete(std::bind(&Bundle::OnCreativeAdsDeleted, this, _1));
}

void Bundle::DeleteDayparts() {
database::table::Dayparts database_table(ads_);
database_table.Delete(std::bind(&Bundle::OnDaypartsDeleted, this, _1));
}

void Bundle::DeleteGeoTargets() {
database::table::GeoTargets database_table(ads_);
database_table.Delete(std::bind(&Bundle::OnGeoTargetsDeleted, this, _1));
Expand Down Expand Up @@ -183,6 +189,16 @@ std::unique_ptr<BundleState> Bundle::GenerateFromCatalog(
geo_targets.push_back(code);
}

std::vector<CreativeDaypartInfo> creative_dayparts;
for (const auto& daypart : campaign.dayparts) {
CreativeDaypartInfo creative_daypart_info;
creative_daypart_info.dow = daypart.dow;
creative_daypart_info.start_minute = daypart.start_minute;
creative_daypart_info.end_minute = daypart.end_minute;

creative_dayparts.push_back(creative_daypart_info);
}

// Creative Sets
for (const auto& creative_set : campaign.creative_sets) {
uint64_t entries = 0;
Expand Down Expand Up @@ -236,6 +252,7 @@ std::unique_ptr<BundleState> Bundle::GenerateFromCatalog(
creative_set.ad_conversions.size() != 0 ? true : false;
info.per_day = creative_set.per_day;
info.total_max = creative_set.total_max;
info.dayparts = creative_dayparts;
info.geo_targets = geo_targets;
info.title = creative.payload.title;
info.body = creative.payload.body;
Expand Down Expand Up @@ -318,6 +335,7 @@ std::unique_ptr<BundleState> Bundle::GenerateFromCatalog(
creative_set.ad_conversions.size() != 0 ? true : false;
info.per_day = creative_set.per_day;
info.total_max = creative_set.total_max;
info.dayparts = creative_dayparts;
info.geo_targets = geo_targets;
info.company_name = creative.payload.company_name;
info.alt = creative.payload.alt;
Expand Down Expand Up @@ -446,6 +464,16 @@ void Bundle::OnCreativeAdsDeleted(
BLOG(3, "Successfully deleted creative ads");
}

void Bundle::OnDaypartsDeleted(
const Result result) {
if (result != SUCCESS) {
BLOG(0, "Failed to delete dayparts");
return;
}

BLOG(3, "Successfully deleted dayparts");
}

void Bundle::OnGeoTargetsDeleted(
const Result result) {
if (result != SUCCESS) {
Expand Down
3 changes: 3 additions & 0 deletions vendor/bat-native-ads/src/bat/ads/internal/bundle/bundle.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Bundle {
void DeleteCampaigns();
void DeleteCategories();
void DeleteCreativeAds();
void DeleteDayparts();
void DeleteGeoTargets();

void SaveCreativeAdNotifications(
Expand Down Expand Up @@ -71,6 +72,8 @@ class Bundle {
const Result result);
void OnCreativeAdsDeleted(
const Result result);
void OnDaypartsDeleted(
const Result result);
void OnGeoTargetsDeleted(
const Result result);
void OnCreativeAdNotificationsSaved(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <string>
#include <vector>
#include "bat/ads/internal/bundle/creative_daypart_info.h"

namespace ads {

Expand All @@ -34,6 +35,7 @@ struct CreativeAdInfo {
std::string category;
std::vector<std::string> geo_targets;
std::string target_url;
std::vector<CreativeDaypartInfo> dayparts;
};

using CreativeAdList = std::vector<CreativeAdInfo>;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BAT_ADS_INTERNAL_BUNDLE_CREATIVE_DAYPART_INFO_H_
#define BAT_ADS_INTERNAL_BUNDLE_CREATIVE_DAYPART_INFO_H_

#include <string>
#include <vector>

#include "bat/ads/internal/time_util.h"

namespace ads {

struct CreativeDaypartInfo {
std::string dow = "0123456";
int start_minute = 0;
int end_minute =
(base::Time::kMinutesPerHour * base::Time::kHoursPerDay) - 1;
};

using CreativeDaypartList = std::vector<CreativeDaypartInfo>;

} // namespace ads

#endif // BAT_ADS_INTERNAL_CATALOG_CATALOG_DAYPART_INFO_H_

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <vector>

#include "bat/ads/internal/catalog/catalog_creative_set_info.h"
#include "bat/ads/internal/catalog/catalog_day_part_info.h"
#include "bat/ads/internal/catalog/catalog_daypart_info.h"
#include "bat/ads/internal/catalog/catalog_geo_target_info.h"

namespace ads {
Expand All @@ -30,7 +30,7 @@ struct CatalogCampaignInfo {
unsigned int daily_cap = 0;
std::string advertiser_id;
CatalogCreativeSetList creative_sets;
CatalogDayPartList day_parts;
CatalogDaypartList dayparts;
CatalogGeoTargetList geo_targets;
};

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BAT_ADS_INTERNAL_CATALOG_CATALOG_DAYPART_INFO_H_
#define BAT_ADS_INTERNAL_CATALOG_CATALOG_DAYPART_INFO_H_

#include <string>
#include <vector>

#include "bat/ads/internal/time_util.h"

namespace ads {

struct CatalogDaypartInfo {
std::string dow = "0123456";
int start_minute = 0;
int end_minute =
(base::Time::kMinutesPerHour * base::Time::kHoursPerDay) - 1;
};

using CatalogDaypartList = std::vector<CatalogDaypartInfo>;

} // namespace ads

#endif // BAT_ADS_INTERNAL_CATALOG_CATALOG_DAYPART_INFO_H_
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,19 @@ Result CatalogState::FromJson(
}

// Day parts
for (const auto& day_part : campaign["dayParts"].GetArray()) {
CatalogDayPartInfo day_part_info;
for (const auto& daypart : campaign["dayParts"].GetArray()) {
CatalogDaypartInfo daypart_info;

day_part_info.dow = day_part["dow"].GetString();
day_part_info.start_minute = day_part["startMinute"].GetUint();
day_part_info.end_minute = day_part["endMinute"].GetUint();
daypart_info.dow = daypart["dow"].GetString();
daypart_info.start_minute = daypart["startMinute"].GetInt();
daypart_info.end_minute = daypart["endMinute"].GetInt();

campaign_info.day_parts.push_back(day_part_info);
campaign_info.dayparts.push_back(daypart_info);
}

if (campaign_info.dayparts.empty()) {
CatalogDaypartInfo daypart_info;
campaign_info.dayparts.push_back(daypart_info);
}

// Creative sets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "bat/ads/internal/database/tables/creative_ad_notifications_database_table.h"
#include "bat/ads/internal/database/tables/creative_ads_database_table.h"
#include "bat/ads/internal/database/tables/creative_new_tab_page_ads_database_table.h"
#include "bat/ads/internal/database/tables/dayparts_database_table.h"
#include "bat/ads/internal/database/tables/geo_targets_database_table.h"
#include "bat/ads/internal/logging.h"

Expand Down Expand Up @@ -86,6 +87,9 @@ void Migration::ToVersion(

table::GeoTargets geo_targets_database_table(ads_);
geo_targets_database_table.Migrate(transaction, to_version);

table::Dayparts dayparts_database_table(ads_);
dayparts_database_table.Migrate(transaction, to_version);
}

} // namespace database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ namespace ads {
namespace database {

int32_t version() {
return 3;
return 4;
}

int32_t compatible_version() {
return 3;
return 4;
}

} // namespace database
Expand Down
Loading

0 comments on commit 4cc30f5

Please sign in to comment.