Skip to content

Commit

Permalink
Add News custom RSS feed P3A questions
Browse files Browse the repository at this point in the history
  • Loading branch information
DJAndries committed Mar 11, 2022
1 parent 7c25b55 commit 286f792
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 0 deletions.
6 changes: 6 additions & 0 deletions components/brave_today/browser/brave_news_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ void BraveNewsController::SubscribeToNewDirectFeed(
},
std::move(callback)),
true);

p3a::RecordDirectFeedsTotal(controller->prefs_);
p3a::RecordWeeklyAddedDirectFeedsCount(controller->prefs_, 1);
},
feed_url, std::move(callback), base::Unretained(this)));
}
Expand All @@ -196,6 +199,9 @@ void BraveNewsController::RemoveDirectFeed(const std::string& publisher_id) {

// Mark feed as requiring update
publishers_controller_.EnsurePublishersIsUpdating();

p3a::RecordDirectFeedsTotal(prefs_);
p3a::RecordWeeklyAddedDirectFeedsCount(prefs_, -1);
}

void BraveNewsController::GetImageData(const GURL& padded_image_url,
Expand Down
23 changes: 23 additions & 0 deletions components/brave_today/browser/brave_news_p3a.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ uint64_t AddToWeeklyStorageAndGetSum(PrefService* prefs,
WeeklyStorage storage(prefs, pref_name);
if (change > 0)
storage.AddDelta(1);
else if (change < 0)
storage.SubDelta(1);
return storage.GetWeeklySum();
}

Expand Down Expand Up @@ -95,7 +97,27 @@ void RecordWeeklyDisplayAdsViewedCount(PrefService* prefs, bool is_add) {
RecordToHistogramBucket(kWeeklyDisplayAdsViewedHistogramName, buckets, total);
}

void RecordDirectFeedsTotal(PrefService* prefs) {
constexpr int buckets[] = {0, 1, 2, 3, 4, 5, 10};
const base::Value* direct_feeds_dict =
prefs->GetDictionary(prefs::kBraveTodayDirectFeeds);
DCHECK(direct_feeds_dict && direct_feeds_dict->is_dict());
std::size_t feed_count = direct_feeds_dict->DictSize();
RecordToHistogramBucket(kDirectFeedsTotalHistogramName, buckets, feed_count);
}

void RecordWeeklyAddedDirectFeedsCount(PrefService* prefs, int change) {
constexpr int buckets[] = {0, 1, 2, 3, 4, 5, 10};
uint64_t weekly_total = AddToWeeklyStorageAndGetSum(
prefs, prefs::kBraveTodayWeeklyAddedDirectFeedsCount, change);

RecordToHistogramBucket(kWeeklyAddedDirectFeedsHistogramName, buckets,
weekly_total);
}

void RecordAtStart(PrefService* prefs) {
RecordDirectFeedsTotal(prefs);
RecordWeeklyAddedDirectFeedsCount(prefs, 0);
RecordWeeklySessionCount(prefs, false);
RecordWeeklyMaxCardVisitsCount(prefs, 0);
RecordWeeklyMaxCardViewsCount(prefs, 0);
Expand All @@ -107,6 +129,7 @@ void RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterListPref(prefs::kBraveTodayWeeklyCardViewsCount);
registry->RegisterListPref(prefs::kBraveTodayWeeklyCardVisitsCount);
registry->RegisterListPref(prefs::kBraveTodayWeeklyDisplayAdViewedCount);
registry->RegisterListPref(prefs::kBraveTodayWeeklyAddedDirectFeedsCount);
}

} // namespace p3a
Expand Down
6 changes: 6 additions & 0 deletions components/brave_today/browser/brave_news_p3a.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ constexpr char kWeeklyMaxCardViewsHistogramName[] =
"Brave.Today.WeeklyMaxCardViewsCount";
constexpr char kWeeklyDisplayAdsViewedHistogramName[] =
"Brave.Today.WeeklyDisplayAdsViewedCount";
constexpr char kDirectFeedsTotalHistogramName[] =
"Brave.Today.DirectFeedsTotal";
constexpr char kWeeklyAddedDirectFeedsHistogramName[] =
"Brave.Today.WeeklyAddedDirectFeedsCount";

void RecordAtStart(PrefService* prefs);
void RecordEverInteracted();
Expand All @@ -32,6 +36,8 @@ void RecordWeeklyMaxCardVisitsCount(PrefService* prefs,
void RecordWeeklyMaxCardViewsCount(PrefService* prefs,
uint64_t cards_viewed_session_total_count);
void RecordWeeklyDisplayAdsViewedCount(PrefService* prefs, bool is_add);
void RecordWeeklyAddedDirectFeedsCount(PrefService* prefs, int change);
void RecordDirectFeedsTotal(PrefService* prefs);
void RegisterProfilePrefs(PrefRegistrySimple* registry);

} // namespace p3a
Expand Down
49 changes: 49 additions & 0 deletions components/brave_today/browser/brave_news_p3a_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,54 @@ TEST_F(BraveNewsP3ATest, TestWeeklyDisplayAdsViewedCount) {
2);
}

TEST_F(BraveNewsP3ATest, TestWeeklyAddedDirectFeedsCount) {
PrefService* prefs = GetPrefs();
RecordAtStart(prefs);
histogram_tester_.ExpectTotalCount(kWeeklyAddedDirectFeedsHistogramName, 1);
histogram_tester_.ExpectBucketCount(kWeeklyAddedDirectFeedsHistogramName, 0,
1);

RecordWeeklyAddedDirectFeedsCount(prefs, 1);
RecordWeeklyAddedDirectFeedsCount(prefs, 1);

task_environment_.AdvanceClock(base::Days(2));
RecordWeeklyAddedDirectFeedsCount(prefs, 0);
histogram_tester_.ExpectTotalCount(kWeeklyAddedDirectFeedsHistogramName, 4);
histogram_tester_.ExpectBucketCount(kWeeklyAddedDirectFeedsHistogramName, 2,
2);

RecordWeeklyAddedDirectFeedsCount(prefs, 1);
RecordWeeklyAddedDirectFeedsCount(prefs, 1);
histogram_tester_.ExpectTotalCount(kWeeklyAddedDirectFeedsHistogramName, 6);
histogram_tester_.ExpectBucketCount(kWeeklyAddedDirectFeedsHistogramName, 4,
1);
RecordWeeklyAddedDirectFeedsCount(prefs, -1);
histogram_tester_.ExpectTotalCount(kWeeklyAddedDirectFeedsHistogramName, 7);
histogram_tester_.ExpectBucketCount(kWeeklyAddedDirectFeedsHistogramName, 3,
2);

task_environment_.AdvanceClock(base::Days(6));
RecordWeeklyAddedDirectFeedsCount(prefs, 0);
histogram_tester_.ExpectTotalCount(kWeeklyAddedDirectFeedsHistogramName, 8);
histogram_tester_.ExpectBucketCount(kWeeklyAddedDirectFeedsHistogramName, 1,
2);
}

TEST_F(BraveNewsP3ATest, TestDirectFeedsTotal) {
PrefService* prefs = GetPrefs();
RecordAtStart(prefs);
histogram_tester_.ExpectTotalCount(kDirectFeedsTotalHistogramName, 1);
histogram_tester_.ExpectBucketCount(kDirectFeedsTotalHistogramName, 0, 1);

DictionaryPrefUpdate update1(prefs, prefs::kBraveTodayDirectFeeds);
update1->SetPath("id1", base::Value(base::Value::Type::DICTIONARY));
DictionaryPrefUpdate update2(prefs, prefs::kBraveTodayDirectFeeds);
update2->SetPath("id2", base::Value(base::Value::Type::DICTIONARY));

RecordDirectFeedsTotal(prefs);
histogram_tester_.ExpectTotalCount(kDirectFeedsTotalHistogramName, 2);
histogram_tester_.ExpectBucketCount(kDirectFeedsTotalHistogramName, 2, 1);
}

} // namespace p3a
} // namespace brave_news
2 changes: 2 additions & 0 deletions components/brave_today/common/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ constexpr char kBraveTodayWeeklyCardVisitsCount[] =
"brave.today.p3a_weekly_card_visits_count";
constexpr char kBraveTodayWeeklyDisplayAdViewedCount[] =
"brave.today.p3a_weekly_display_ad_viewed_count";
constexpr char kBraveTodayWeeklyAddedDirectFeedsCount[] =
"brave.today.p3a_weekly_added_direct_feeds_count";

// Dictionary value keys
constexpr char kBraveTodayDirectFeedsKeyTitle[] = "title";
Expand Down
2 changes: 2 additions & 0 deletions components/p3a/brave_p3a_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ constexpr const char* kCollectedHistograms[] = {
"Brave.Today.WeeklyMaxCardViewsCount",
"Brave.Today.WeeklyMaxCardVisitsCount",
"Brave.Today.WeeklySessionCount",
"Brave.Today.WeeklyAddedDirectFeedsCount",
"Brave.Today.DirectFeedsTotal",
"Brave.Sync.Status.2",
"Brave.Sync.ProgressTokenEverReset",
"Brave.Uptime.BrowserOpenMinutes",
Expand Down
14 changes: 14 additions & 0 deletions components/weekly_storage/weekly_storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "brave/components/weekly_storage/weekly_storage.h"

#include <algorithm>
#include <numeric>
#include <utility>

Expand Down Expand Up @@ -45,6 +46,19 @@ void WeeklyStorage::AddDelta(uint64_t delta) {
Save();
}

void WeeklyStorage::SubDelta(uint64_t delta) {
FilterToWeek();
for (DailyValue& daily_value : daily_values_) {
if (delta == 0) {
break;
}
uint64_t day_delta = std::min(daily_value.value, delta);
daily_value.value -= day_delta;
delta -= day_delta;
}
Save();
}

void WeeklyStorage::ReplaceTodaysValueIfGreater(uint64_t value) {
FilterToWeek();
DailyValue& today = daily_values_.front();
Expand Down
1 change: 1 addition & 0 deletions components/weekly_storage/weekly_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class WeeklyStorage {
WeeklyStorage& operator=(const WeeklyStorage&) = delete;

void AddDelta(uint64_t delta);
void SubDelta(uint64_t delta);
void ReplaceTodaysValueIfGreater(uint64_t value);
uint64_t GetWeeklySum() const;
uint64_t GetHighestValueInWeek() const;
Expand Down
26 changes: 26 additions & 0 deletions components/weekly_storage/weekly_storage_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,32 @@ TEST_F(WeeklyStorageTest, AddsSavings) {
EXPECT_EQ(state_->GetWeeklySum(), saving * 3);
}

TEST_F(WeeklyStorageTest, SubDelta) {
state_->AddDelta(5000);
clock_->Advance(base::Days(1));
state_->AddDelta(3000);
clock_->Advance(base::Days(1));
state_->AddDelta(1000);
clock_->Advance(base::Days(1));

state_->SubDelta(500);
EXPECT_EQ(state_->GetWeeklySum(), 8500U);
state_->SubDelta(4000);
EXPECT_EQ(state_->GetWeeklySum(), 4500U);

clock_->Advance(base::Days(4));
// First day value should expire
EXPECT_EQ(state_->GetWeeklySum(), 0U);

// If subtracting by an amount greater than the current sum,
// the sum should not become negative or underflow.
state_->AddDelta(3000);
state_->SubDelta(5000);
EXPECT_EQ(state_->GetWeeklySum(), 0U);
state_->SubDelta(100000);
EXPECT_EQ(state_->GetWeeklySum(), 0U);
}

TEST_F(WeeklyStorageTest, ForgetsOldSavings) {
uint64_t saving = 10000;
state_->AddDelta(saving);
Expand Down

0 comments on commit 286f792

Please sign in to comment.