Refactor weekly storage into a generic time period storage; added

weekly/monthly storage classes which extend the time period storage
This commit is contained in:
Darnell Andries
2022-06-07 11:02:19 -07:00
parent 417777540a
commit 640d28d0fc
39 changed files with 459 additions and 305 deletions
+1 -1
View File
@@ -21,7 +21,7 @@
#include "base/memory/raw_ptr.h"
#include "base/timer/timer.h"
#include "brave/components/weekly_storage/weekly_storage.h"
#include "brave/components/time_period_storage/weekly_storage.h"
#include "chrome/browser/resource_coordinator/usage_clock.h"
#include "chrome/browser/ui/browser_list_observer.h"
@@ -9,7 +9,7 @@
#include "base/memory/raw_ptr.h"
#include "base/memory/singleton.h"
#include "base/scoped_observation.h"
#include "brave/components/weekly_storage/weekly_event_storage.h"
#include "brave/components/time_period_storage/weekly_event_storage.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/prefs/pref_member.h"
+1 -1
View File
@@ -31,7 +31,7 @@ if (is_android) {
brave_browser_search_engines_deps = [
"//brave/browser/profiles:util",
"//brave/components/constants",
"//brave/components/weekly_storage",
"//brave/components/time_period_storage",
"//chrome/browser/profiles:profile",
"//components/keyed_service/content",
"//components/pref_registry",
+1 -1
View File
@@ -260,7 +260,7 @@ if (is_android) {
"//brave/browser/importer",
"//brave/browser/infobars",
"//brave/browser/ui/bookmark",
"//brave/components/weekly_storage",
"//brave/components/time_period_storage",
"//components/infobars/content",
]
}
+1 -1
View File
@@ -301,13 +301,13 @@ source_set("ui") {
"//brave/components/ntp_widget_utils/browser",
"//brave/components/p3a:buildflags",
"//brave/components/sidebar/buildflags",
"//brave/components/time_period_storage",
"//brave/components/tor",
"//brave/components/tor:pref_names",
"//brave/components/tor/buildflags",
"//brave/components/vector_icons",
"//brave/components/webcompat_reporter/browser",
"//brave/components/webcompat_reporter/ui:generated_resources",
"//brave/components/weekly_storage",
"//brave/vendor/bat-native-ledger:headers",
"//chrome/app:command_ids",
"//chrome/app/vector_icons:vector_icons",
@@ -11,7 +11,7 @@
#include "base/values.h"
#include "brave/browser/autocomplete/brave_autocomplete_scheme_classifier.h"
#include "brave/components/constants/pref_names.h"
#include "brave/components/weekly_storage/weekly_storage.h"
#include "brave/components/time_period_storage/weekly_storage.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/omnibox/chrome_omnibox_client.h"
#include "chrome/browser/ui/omnibox/chrome_omnibox_edit_controller.h"
@@ -30,7 +30,7 @@
#include "brave/components/ntp_background_images/common/pref_names.h"
#include "brave/components/p3a/brave_p3a_utils.h"
#include "brave/components/services/bat_ads/public/interfaces/bat_ads.mojom.h"
#include "brave/components/weekly_storage/weekly_storage.h"
#include "brave/components/time_period_storage/weekly_storage.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/first_run/first_run.h"
#include "chrome/browser/profiles/profile.h"
+1 -1
View File
@@ -30,7 +30,7 @@ source_set("browser") {
"//brave/components/brave_component_updater/browser",
"//brave/components/l10n/browser",
"//brave/components/l10n/common",
"//brave/components/weekly_storage",
"//brave/components/time_period_storage",
"//components/keyed_service/core",
"//components/pref_registry",
"//components/prefs",
+1 -1
View File
@@ -7,7 +7,7 @@
#include "base/metrics/histogram_functions.h"
#include "brave/components/brave_ads/common/pref_names.h"
#include "brave/components/weekly_storage/weekly_storage.h"
#include "brave/components/time_period_storage/weekly_storage.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
@@ -22,7 +22,7 @@ static_library("browser") {
"//brave/components/brave_perf_predictor/common",
"//brave/components/resources",
"//brave/components/resources:static_resources_grit",
"//brave/components/weekly_storage",
"//brave/components/time_period_storage",
"//components/keyed_service/content:content",
"//components/page_load_metrics/browser",
"//components/page_load_metrics/common",
@@ -12,7 +12,7 @@
#include "base/time/clock.h"
#include "base/time/default_clock.h"
#include "brave/components/brave_perf_predictor/common/pref_names.h"
#include "brave/components/weekly_storage/weekly_storage.h"
#include "brave/components/time_period_storage/weekly_storage.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
+1 -1
View File
@@ -15,7 +15,7 @@ static_library("browser") {
"//base",
"//brave/components/brave_search/common",
"//brave/components/brave_search/common:mojom",
"//brave/components/weekly_storage",
"//brave/components/time_period_storage",
"//components/prefs",
"//components/search_engines",
"//net",
@@ -12,7 +12,7 @@
#include "brave/components/brave_search/browser/brave_search_fallback_host.h"
#include "brave/components/brave_search/browser/prefs.h"
#include "brave/components/brave_search/common/features.h"
#include "brave/components/weekly_storage/daily_storage.h"
#include "brave/components/time_period_storage/daily_storage.h"
#include "build/build_config.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
+1 -1
View File
@@ -36,7 +36,7 @@ static_library("browser") {
"//brave/components/brave_today/common:mojom",
"//brave/components/l10n/browser",
"//brave/components/l10n/common",
"//brave/components/weekly_storage",
"//brave/components/time_period_storage",
"//components/history/core/browser",
"//components/keyed_service/core",
"//components/prefs",
@@ -11,7 +11,7 @@
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "brave/components/brave_today/common/pref_names.h"
#include "brave/components/weekly_storage/weekly_storage.h"
#include "brave/components/time_period_storage/weekly_storage.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
+1 -1
View File
@@ -138,7 +138,7 @@ static_library("browser") {
"//brave/components/json/rs:cxx",
"//brave/components/json/rs:cxx_gen",
"//brave/components/resources:strings_grit",
"//brave/components/weekly_storage:weekly_storage",
"//brave/components/time_period_storage",
"//components/component_updater",
"//components/content_settings/core/browser",
"//components/keyed_service/core",
@@ -27,7 +27,7 @@
#include "brave/components/brave_wallet/common/solana_utils.h"
#include "brave/components/brave_wallet/common/value_conversion_utils.h"
#include "brave/components/brave_wallet/common/web3_provider_constants.h"
#include "brave/components/weekly_storage/weekly_storage.h"
#include "brave/components/time_period_storage/weekly_storage.h"
#include "components/grit/brave_components_strings.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
@@ -44,7 +44,7 @@ static_library("browser") {
"//brave/components/l10n/common",
"//brave/components/ntp_background_images/buildflags",
"//brave/components/ntp_background_images/common",
"//brave/components/weekly_storage",
"//brave/components/time_period_storage",
"//brave/vendor/bat-native-ads",
"//components/component_updater",
"//components/keyed_service/core",
@@ -24,7 +24,7 @@
#include "brave/components/ntp_background_images/browser/ntp_sponsored_images_data.h"
#include "brave/components/ntp_background_images/browser/url_constants.h"
#include "brave/components/ntp_background_images/common/pref_names.h"
#include "brave/components/weekly_storage/weekly_storage.h"
#include "brave/components/time_period_storage/weekly_storage.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/web_ui_data_source.h"
+2 -3
View File
@@ -58,13 +58,12 @@ constexpr inline auto kCollectedHistograms =
"Brave.SpeedReader.Enabled",
"Brave.SpeedReader.ToggleCount",
"Brave.Today.DirectFeedsTotal",
"Brave.Today.HasEverInteracted",
"Brave.Today.WeeklyTotalCardViews",
"Brave.Today.WeeklyAddedDirectFeedsCount",
"Brave.Today.WeeklyDisplayAdsViewedCount",
"Brave.Today.WeeklyMaxCardViewsCount",
"Brave.Today.WeeklyMaxCardVisitsCount",
"Brave.Today.WeeklySessionCount",
"Brave.Today.WeeklyAddedDirectFeedsCount",
"Brave.Today.WeeklyTotalCardViews",
"Brave.Sync.Status.2",
"Brave.Sync.ProgressTokenEverReset",
"Brave.Uptime.BrowserOpenMinutes",
+1 -1
View File
@@ -46,7 +46,7 @@ static_library("speedreader") {
"//brave/components/brave_component_updater/browser",
"//brave/components/resources",
"//brave/components/resources:static_resources_grit",
"//brave/components/weekly_storage",
"//brave/components/time_period_storage",
"//components/content_settings/core/browser",
"//components/content_settings/core/common",
"//components/keyed_service/core:core",
@@ -9,7 +9,7 @@
#include "base/metrics/histogram_macros.h"
#include "brave/components/speedreader/features.h"
#include "brave/components/speedreader/speedreader_pref_names.h"
#include "brave/components/weekly_storage/weekly_storage.h"
#include "brave/components/time_period_storage/weekly_storage.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
@@ -1,7 +1,11 @@
static_library("weekly_storage") {
static_library("time_period_storage") {
sources = [
"daily_storage.cc",
"daily_storage.h",
"monthly_storage.cc",
"monthly_storage.h",
"time_period_storage.cc",
"time_period_storage.h",
"weekly_event_storage.cc",
"weekly_event_storage.h",
"weekly_storage.cc",
@@ -3,7 +3,7 @@
// 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/.
#include "brave/components/weekly_storage/daily_storage.h"
#include "brave/components/time_period_storage/daily_storage.h"
#include <numeric>
#include <utility>
@@ -3,8 +3,8 @@
// 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 BRAVE_COMPONENTS_WEEKLY_STORAGE_DAILY_STORAGE_H_
#define BRAVE_COMPONENTS_WEEKLY_STORAGE_DAILY_STORAGE_H_
#ifndef BRAVE_COMPONENTS_TIME_PERIOD_STORAGE_DAILY_STORAGE_H_
#define BRAVE_COMPONENTS_TIME_PERIOD_STORAGE_DAILY_STORAGE_H_
#include <list>
#include <memory>
@@ -20,6 +20,7 @@ class PrefService;
// Allows to track a sum of some
// values added from time to time via |AddDelta| over the last 24 hours.
// Requires |pref_name| to be already registered.
// TODO(djandries): Refactor to extend TimePeriodStorage
class DailyStorage {
public:
DailyStorage(PrefService* prefs, const char* pref_name);
@@ -52,4 +53,4 @@ class DailyStorage {
std::list<DailyValue> daily_values_;
};
#endif // BRAVE_COMPONENTS_WEEKLY_STORAGE_DAILY_STORAGE_H_
#endif // BRAVE_COMPONENTS_TIME_PERIOD_STORAGE_DAILY_STORAGE_H_
@@ -3,7 +3,7 @@
// 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/.
#include "brave/components/weekly_storage/daily_storage.h"
#include "brave/components/time_period_storage/daily_storage.h"
#include <memory>
#include <utility>
@@ -0,0 +1,21 @@
/* Copyright 2022 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/. */
#include "brave/components/time_period_storage/monthly_storage.h"
namespace {
constexpr size_t kAverageDaysInMonth = 30;
}
MonthlyStorage::MonthlyStorage(PrefService* prefs, const char* pref_name)
: TimePeriodStorage(prefs, pref_name, kAverageDaysInMonth) {}
uint64_t MonthlyStorage::GetMonthlySum() const {
return GetPeriodSum();
}
uint64_t MonthlyStorage::GetHighestValueInMonth() const {
return GetHighestValueInPeriod();
}
@@ -0,0 +1,24 @@
/* Copyright 2022 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 BRAVE_COMPONENTS_TIME_PERIOD_STORAGE_MONTHLY_STORAGE_H_
#define BRAVE_COMPONENTS_TIME_PERIOD_STORAGE_MONTHLY_STORAGE_H_
#include "brave/components/time_period_storage/time_period_storage.h"
class PrefService;
class MonthlyStorage : public TimePeriodStorage {
public:
MonthlyStorage(PrefService* prefs, const char* pref_name);
MonthlyStorage(const MonthlyStorage&) = delete;
MonthlyStorage& operator=(const MonthlyStorage&) = delete;
uint64_t GetMonthlySum() const;
uint64_t GetHighestValueInMonth() const;
};
#endif // BRAVE_COMPONENTS_TIME_PERIOD_STORAGE_MONTHLY_STORAGE_H_
@@ -1,9 +1,9 @@
/* Copyright 2020 The Brave Authors. All rights reserved.
/* Copyright 2022 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/. */
#include "brave/components/weekly_storage/weekly_storage.h"
#include "brave/components/time_period_storage/time_period_storage.h"
#include <algorithm>
#include <numeric>
@@ -15,13 +15,12 @@
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
namespace {
constexpr size_t kDaysInWeek = 7;
}
WeeklyStorage::WeeklyStorage(PrefService* prefs, const char* pref_name)
TimePeriodStorage::TimePeriodStorage(PrefService* prefs,
const char* pref_name,
size_t period_days)
: prefs_(prefs),
pref_name_(pref_name),
period_days_(period_days),
clock_(std::make_unique<base::DefaultClock>()) {
DCHECK(pref_name);
if (prefs) {
@@ -29,25 +28,29 @@ WeeklyStorage::WeeklyStorage(PrefService* prefs, const char* pref_name)
}
}
WeeklyStorage::WeeklyStorage(PrefService* prefs,
const char* pref_name,
std::unique_ptr<base::Clock> clock)
: prefs_(prefs), pref_name_(pref_name), clock_(std::move(clock)) {
TimePeriodStorage::TimePeriodStorage(PrefService* prefs,
const char* pref_name,
size_t period_days,
std::unique_ptr<base::Clock> clock)
: prefs_(prefs),
pref_name_(pref_name),
period_days_(period_days),
clock_(std::move(clock)) {
DCHECK(prefs);
DCHECK(pref_name);
Load();
}
WeeklyStorage::~WeeklyStorage() = default;
TimePeriodStorage::~TimePeriodStorage() = default;
void WeeklyStorage::AddDelta(uint64_t delta) {
FilterToWeek();
void TimePeriodStorage::AddDelta(uint64_t delta) {
FilterToPeriod();
daily_values_.front().value += delta;
Save();
}
void WeeklyStorage::SubDelta(uint64_t delta) {
FilterToWeek();
void TimePeriodStorage::SubDelta(uint64_t delta) {
FilterToPeriod();
for (DailyValue& daily_value : daily_values_) {
if (delta == 0) {
break;
@@ -59,8 +62,8 @@ void WeeklyStorage::SubDelta(uint64_t delta) {
Save();
}
void WeeklyStorage::ReplaceTodaysValueIfGreater(uint64_t value) {
FilterToWeek();
void TimePeriodStorage::ReplaceTodaysValueIfGreater(uint64_t value) {
FilterToPeriod();
DailyValue& today = daily_values_.front();
if (today.value < value) {
today.value = value;
@@ -68,9 +71,9 @@ void WeeklyStorage::ReplaceTodaysValueIfGreater(uint64_t value) {
Save();
}
uint64_t WeeklyStorage::GetWeeklySum() const {
uint64_t TimePeriodStorage::GetPeriodSum() const {
// We record only value for last N days.
const base::Time n_days_ago = clock_->Now() - base::Days(kDaysInWeek);
const base::Time n_days_ago = clock_->Now() - base::Days(period_days_);
return std::accumulate(daily_values_.begin(), daily_values_.end(), 0ull,
[n_days_ago](const uint64_t acc, const auto& u2) {
uint64_t add = 0;
@@ -82,35 +85,35 @@ uint64_t WeeklyStorage::GetWeeklySum() const {
});
}
uint64_t WeeklyStorage::GetHighestValueInWeek() const {
uint64_t TimePeriodStorage::GetHighestValueInPeriod() const {
// We record only value for last N days.
const base::Time n_days_ago = clock_->Now() - base::Days(kDaysInWeek);
std::list<DailyValue> last_weeks_daily_values(daily_values_.size());
const base::Time n_days_ago = clock_->Now() - base::Days(period_days_);
std::list<DailyValue> in_period_daily_values(daily_values_.size());
auto copied_it =
std::copy_if(daily_values_.begin(), daily_values_.end(),
last_weeks_daily_values.begin(),
in_period_daily_values.begin(),
[n_days_ago](auto i) { return i.day > n_days_ago; });
last_weeks_daily_values.resize(
std::distance(last_weeks_daily_values.begin(), copied_it));
in_period_daily_values.resize(
std::distance(in_period_daily_values.begin(), copied_it));
auto highest_it = std::max_element(last_weeks_daily_values.begin(),
last_weeks_daily_values.end(),
auto highest_it = std::max_element(in_period_daily_values.begin(),
in_period_daily_values.end(),
[](const auto& left, const auto& right) {
return left.value < right.value;
});
if (highest_it == last_weeks_daily_values.end()) {
if (highest_it == in_period_daily_values.end()) {
return 0;
}
return highest_it->value;
}
bool WeeklyStorage::IsOneWeekPassed() const {
bool TimePeriodStorage::IsOnePeriodPassed() const {
// TODO(iefremov): This is not true 100% (if the browser was launched once
// per week just after installation, for example).
return daily_values_.size() == kDaysInWeek;
// per the time period just after installation, for example).
return daily_values_.size() == period_days_;
}
void WeeklyStorage::FilterToWeek() {
void TimePeriodStorage::FilterToPeriod() {
base::Time now_midnight = clock_->Now().LocalMidnight();
base::Time last_saved_midnight;
@@ -122,13 +125,13 @@ void WeeklyStorage::FilterToWeek() {
// Day changed. Since we consider only small incoming intervals, lets just
// save it with a new timestamp.
daily_values_.push_front({now_midnight, 0});
if (daily_values_.size() > kDaysInWeek) {
if (daily_values_.size() > period_days_) {
daily_values_.pop_back();
}
}
}
void WeeklyStorage::Load() {
void TimePeriodStorage::Load() {
DCHECK(daily_values_.empty());
const base::Value* list = prefs_->GetList(pref_name_);
if (!list) {
@@ -140,7 +143,7 @@ void WeeklyStorage::Load() {
if (!day || !value || !day->is_double() || !value->is_double()) {
continue;
}
if (daily_values_.size() == kDaysInWeek) {
if (daily_values_.size() == period_days_) {
break;
}
daily_values_.push_back({base::Time::FromDoubleT(day->GetDouble()),
@@ -148,9 +151,9 @@ void WeeklyStorage::Load() {
}
}
void WeeklyStorage::Save() {
void TimePeriodStorage::Save() {
DCHECK(!daily_values_.empty());
DCHECK_LE(daily_values_.size(), kDaysInWeek);
DCHECK_LE(daily_values_.size(), period_days_);
ListPrefUpdate update(prefs_, pref_name_);
base::Value* list = update.Get();
@@ -0,0 +1,63 @@
/* Copyright 2022 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 BRAVE_COMPONENTS_TIME_PERIOD_STORAGE_TIME_PERIOD_STORAGE_H_
#define BRAVE_COMPONENTS_TIME_PERIOD_STORAGE_TIME_PERIOD_STORAGE_H_
#include <list>
#include <memory>
#include "base/time/time.h"
namespace base {
class Clock;
}
class PrefService;
// Mostly used by various P3A recorders - allows to track a sum of some
// values added from time to time via |AddDelta| over the last predefined time
// period. Requires |pref_name| to be already registered.
class TimePeriodStorage {
public:
TimePeriodStorage(PrefService* prefs,
const char* pref_name,
size_t period_days);
// For tests.
TimePeriodStorage(PrefService* prefs,
const char* pref_name,
size_t period_days,
std::unique_ptr<base::Clock> clock);
~TimePeriodStorage();
TimePeriodStorage(const TimePeriodStorage&) = delete;
TimePeriodStorage& operator=(const TimePeriodStorage&) = delete;
void AddDelta(uint64_t delta);
void SubDelta(uint64_t delta);
void ReplaceTodaysValueIfGreater(uint64_t value);
uint64_t GetPeriodSum() const;
uint64_t GetHighestValueInPeriod() const;
bool IsOnePeriodPassed() const;
private:
struct DailyValue {
base::Time day;
uint64_t value = 0ull;
};
void FilterToPeriod();
void Load();
void Save();
PrefService* prefs_ = nullptr;
const char* pref_name_ = nullptr;
size_t period_days_;
std::unique_ptr<base::Clock> clock_;
std::list<DailyValue> daily_values_;
};
#endif // BRAVE_COMPONENTS_TIME_PERIOD_STORAGE_TIME_PERIOD_STORAGE_H_
@@ -0,0 +1,216 @@
/* Copyright (c) 2022 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/. */
#include "brave/components/time_period_storage/time_period_storage.h"
#include <memory>
#include <utility>
#include "base/memory/raw_ptr.h"
#include "base/test/simple_test_clock.h"
#include "base/time/time.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "testing/gtest/include/gtest/gtest.h"
constexpr char kPrefName[] = "brave.weekly_test";
class TimePeriodStorageTest : public ::testing::Test {
public:
TimePeriodStorageTest() : clock_(new base::SimpleTestClock) {
pref_service_.registry()->RegisterListPref(kPrefName);
clock_->SetNow(base::Time::Now());
}
void InitStorage(size_t days) {
state_ = std::make_unique<TimePeriodStorage>(
&pref_service_, kPrefName, days, std::unique_ptr<base::Clock>(clock_));
}
protected:
raw_ptr<base::SimpleTestClock> clock_ = nullptr;
TestingPrefServiceSimple pref_service_;
std::unique_ptr<TimePeriodStorage> state_;
};
TEST_F(TimePeriodStorageTest, StartsZero) {
InitStorage(7);
EXPECT_EQ(state_->GetPeriodSum(), 0ULL);
}
TEST_F(TimePeriodStorageTest, AddsSavings) {
InitStorage(7);
uint64_t saving = 10000;
state_->AddDelta(saving);
EXPECT_EQ(state_->GetPeriodSum(), saving);
// Accumulate
state_->AddDelta(saving);
state_->AddDelta(saving);
EXPECT_EQ(state_->GetPeriodSum(), saving * 3);
}
TEST_F(TimePeriodStorageTest, SubDelta) {
InitStorage(7);
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_->GetPeriodSum(), 8500U);
state_->SubDelta(4000);
EXPECT_EQ(state_->GetPeriodSum(), 4500U);
clock_->Advance(base::Days(4));
// First day value should expire
EXPECT_EQ(state_->GetPeriodSum(), 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_->GetPeriodSum(), 0U);
state_->SubDelta(100000);
EXPECT_EQ(state_->GetPeriodSum(), 0U);
}
TEST_F(TimePeriodStorageTest, ForgetsOldSavingsWeekly) {
InitStorage(7);
uint64_t saving = 10000;
state_->AddDelta(saving);
EXPECT_EQ(state_->GetPeriodSum(), saving);
clock_->Advance(base::Days(8));
// More savings
state_->AddDelta(saving);
state_->AddDelta(saving);
// Should have forgotten about older days
EXPECT_EQ(state_->GetPeriodSum(), saving * 2);
}
TEST_F(TimePeriodStorageTest, ForgetsOldSavingsMonthly) {
InitStorage(30);
uint64_t saving = 10000;
state_->AddDelta(saving);
EXPECT_EQ(state_->GetPeriodSum(), saving);
clock_->Advance(base::Days(31));
// More savings
state_->AddDelta(saving);
state_->AddDelta(saving);
// Should have forgotten about older days
EXPECT_EQ(state_->GetPeriodSum(), saving * 2);
}
TEST_F(TimePeriodStorageTest, RetrievesDailySavings) {
InitStorage(7);
uint64_t saving = 10000;
for (int day = 0; day <= 7; day++) {
clock_->Advance(base::Days(1));
state_->AddDelta(saving);
}
EXPECT_EQ(state_->GetPeriodSum(), 7 * saving);
}
TEST_F(TimePeriodStorageTest, HandlesSkippedDay) {
InitStorage(7);
uint64_t saving = 10000;
for (int day = 0; day < 7; day++) {
clock_->Advance(base::Days(1));
if (day == 3)
continue;
state_->AddDelta(saving);
}
EXPECT_EQ(state_->GetPeriodSum(), 6 * saving);
}
TEST_F(TimePeriodStorageTest, IntermittentUsageWeekly) {
InitStorage(7);
uint64_t saving = 10000;
for (int day = 0; day < 10; day++) {
clock_->Advance(base::Days(2));
state_->AddDelta(saving);
}
EXPECT_EQ(state_->GetPeriodSum(), 4 * saving);
}
TEST_F(TimePeriodStorageTest, IntermittentUsageMonthly) {
InitStorage(30);
uint64_t saving = 10000;
for (int day = 0; day < 40; day++) {
clock_->Advance(base::Days(10));
state_->AddDelta(saving);
}
EXPECT_EQ(state_->GetPeriodSum(), 3 * saving);
}
TEST_F(TimePeriodStorageTest, InfrequentUsageWeekly) {
InitStorage(7);
uint64_t saving = 10000;
state_->AddDelta(saving);
clock_->Advance(base::Days(6));
state_->AddDelta(saving);
EXPECT_EQ(state_->GetPeriodSum(), 2 * saving);
}
TEST_F(TimePeriodStorageTest, InfrequentUsageMonthly) {
InitStorage(30);
uint64_t saving = 10000;
state_->AddDelta(saving);
clock_->Advance(base::Days(29));
state_->AddDelta(saving);
EXPECT_EQ(state_->GetPeriodSum(), 2 * saving);
}
TEST_F(TimePeriodStorageTest, GetHighestValueInPeriod) {
InitStorage(7);
uint64_t lowest_value = 20;
uint64_t low_value = 50;
uint64_t high_value = 75;
state_->AddDelta(low_value);
clock_->Advance(base::Days(1));
state_->AddDelta(high_value);
clock_->Advance(base::Days(1));
state_->AddDelta(lowest_value);
EXPECT_EQ(state_->GetHighestValueInPeriod(), high_value);
clock_->Advance(base::Days(1));
EXPECT_EQ(state_->GetHighestValueInPeriod(), high_value);
}
TEST_F(TimePeriodStorageTest, RecordsHigherValueForToday) {
InitStorage(30);
uint64_t low_value = 50;
uint64_t high_value = 75;
state_->ReplaceTodaysValueIfGreater(low_value);
EXPECT_EQ(state_->GetHighestValueInPeriod(), low_value);
// Replace with higher value
state_->ReplaceTodaysValueIfGreater(high_value);
EXPECT_EQ(state_->GetHighestValueInPeriod(), high_value);
// Sanity check value was replaced and not added.
EXPECT_EQ(state_->GetPeriodSum(), high_value);
// Should not replace with lower value
state_->ReplaceTodaysValueIfGreater(low_value);
EXPECT_EQ(state_->GetHighestValueInPeriod(), high_value);
}
TEST_F(TimePeriodStorageTest, GetsHighestValueInWeekFromReplacement) {
InitStorage(30);
// Add a low value a couple days after a high value,
// should return highest day value.
uint64_t low_value = 50;
uint64_t high_value = 75;
state_->ReplaceTodaysValueIfGreater(high_value);
clock_->Advance(base::Days(2));
state_->ReplaceTodaysValueIfGreater(low_value);
EXPECT_EQ(state_->GetHighestValueInPeriod(), high_value);
// Sanity check disparate days were not replaced
EXPECT_EQ(state_->GetPeriodSum(), high_value + low_value);
}
@@ -3,7 +3,7 @@
* 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/. */
#include "brave/components/weekly_storage/weekly_event_storage.h"
#include "brave/components/time_period_storage/weekly_event_storage.h"
#include <list>
#include <memory>
@@ -3,8 +3,8 @@
* 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 BRAVE_COMPONENTS_WEEKLY_STORAGE_WEEKLY_EVENT_STORAGE_H_
#define BRAVE_COMPONENTS_WEEKLY_STORAGE_WEEKLY_EVENT_STORAGE_H_
#ifndef BRAVE_COMPONENTS_TIME_PERIOD_STORAGE_WEEKLY_EVENT_STORAGE_H_
#define BRAVE_COMPONENTS_TIME_PERIOD_STORAGE_WEEKLY_EVENT_STORAGE_H_
#include <list>
#include <memory>
@@ -24,6 +24,7 @@
// after approximately a week.
//
// Requires |pref_name| to be already registered.
// TODO(djandries): Refactor to extend TimePeriodStorage?
class WeeklyEventStorage {
public:
WeeklyEventStorage(PrefService* prefs, const char* pref_name);
@@ -64,4 +65,4 @@ class WeeklyEventStorage {
std::list<Event> events_;
};
#endif // BRAVE_COMPONENTS_WEEKLY_STORAGE_WEEKLY_EVENT_STORAGE_H_
#endif // BRAVE_COMPONENTS_TIME_PERIOD_STORAGE_WEEKLY_EVENT_STORAGE_H_
@@ -3,7 +3,7 @@
* 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/. */
#include "brave/components/weekly_storage/weekly_event_storage.h"
#include "brave/components/time_period_storage/weekly_event_storage.h"
#include <memory>
#include <utility>
@@ -0,0 +1,25 @@
/* Copyright 2022 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/. */
#include "brave/components/time_period_storage/weekly_storage.h"
namespace {
constexpr size_t kDaysInWeek = 7;
}
WeeklyStorage::WeeklyStorage(PrefService* prefs, const char* pref_name)
: TimePeriodStorage(prefs, pref_name, kDaysInWeek) {}
uint64_t WeeklyStorage::GetWeeklySum() const {
return GetPeriodSum();
}
uint64_t WeeklyStorage::GetHighestValueInWeek() const {
return GetHighestValueInPeriod();
}
bool WeeklyStorage::IsOneWeekPassed() const {
return IsOnePeriodPassed();
}
@@ -0,0 +1,25 @@
/* Copyright 2022 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 BRAVE_COMPONENTS_TIME_PERIOD_STORAGE_WEEKLY_STORAGE_H_
#define BRAVE_COMPONENTS_TIME_PERIOD_STORAGE_WEEKLY_STORAGE_H_
#include "brave/components/time_period_storage/time_period_storage.h"
class PrefService;
class WeeklyStorage : public TimePeriodStorage {
public:
WeeklyStorage(PrefService* prefs, const char* pref_name);
WeeklyStorage(const WeeklyStorage&) = delete;
WeeklyStorage& operator=(const WeeklyStorage&) = delete;
uint64_t GetWeeklySum() const;
uint64_t GetHighestValueInWeek() const;
bool IsOneWeekPassed() const;
};
#endif // BRAVE_COMPONENTS_TIME_PERIOD_STORAGE_WEEKLY_STORAGE_H_
@@ -1,61 +0,0 @@
/* Copyright 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 BRAVE_COMPONENTS_WEEKLY_STORAGE_WEEKLY_STORAGE_H_
#define BRAVE_COMPONENTS_WEEKLY_STORAGE_WEEKLY_STORAGE_H_
#include <list>
#include <memory>
#include "base/time/time.h"
namespace base {
class Clock;
}
class PrefService;
// Mostly used by various P3A recorders - allows to track a sum of some
// values added from time to time via |AddDelta| over a last week.
// Requires |pref_name| to be already registered.
// Feel free to improve and refactor it - templatize a stored value type,
// change weekly interval or make a keyed service from it.
class WeeklyStorage {
public:
WeeklyStorage(PrefService* prefs, const char* pref_name);
// For tests.
WeeklyStorage(PrefService* user_prefs,
const char* pref_name,
std::unique_ptr<base::Clock> clock);
~WeeklyStorage();
WeeklyStorage(const WeeklyStorage&) = delete;
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;
bool IsOneWeekPassed() const;
private:
struct DailyValue {
base::Time day;
uint64_t value = 0ull;
};
void FilterToWeek();
void Load();
void Save();
PrefService* prefs_ = nullptr;
const char* pref_name_ = nullptr;
std::unique_ptr<base::Clock> clock_;
std::list<DailyValue> daily_values_;
};
#endif // BRAVE_COMPONENTS_WEEKLY_STORAGE_WEEKLY_STORAGE_H_
@@ -1,167 +0,0 @@
/* 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/. */
#include "brave/components/weekly_storage/weekly_storage.h"
#include <memory>
#include <utility>
#include "base/memory/raw_ptr.h"
#include "base/test/simple_test_clock.h"
#include "base/time/time.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "testing/gtest/include/gtest/gtest.h"
class WeeklyStorageTest : public ::testing::Test {
public:
WeeklyStorageTest() : clock_(new base::SimpleTestClock) {
constexpr char kPrefName[] = "brave.weekly_test";
pref_service_.registry()->RegisterListPref(kPrefName);
state_ = std::make_unique<WeeklyStorage>(
&pref_service_, kPrefName, std::unique_ptr<base::Clock>(clock_));
clock_->SetNow(base::Time::Now());
}
protected:
raw_ptr<base::SimpleTestClock> clock_ = nullptr;
TestingPrefServiceSimple pref_service_;
std::unique_ptr<WeeklyStorage> state_;
};
TEST_F(WeeklyStorageTest, StartsZero) {
EXPECT_EQ(state_->GetWeeklySum(), 0ULL);
}
TEST_F(WeeklyStorageTest, AddsSavings) {
uint64_t saving = 10000;
state_->AddDelta(saving);
EXPECT_EQ(state_->GetWeeklySum(), saving);
// Accumulate
state_->AddDelta(saving);
state_->AddDelta(saving);
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);
EXPECT_EQ(state_->GetWeeklySum(), saving);
clock_->Advance(base::Days(8));
// More savings
state_->AddDelta(saving);
state_->AddDelta(saving);
// Should have forgotten about older days
EXPECT_EQ(state_->GetWeeklySum(), saving * 2);
}
TEST_F(WeeklyStorageTest, RetrievesDailySavings) {
uint64_t saving = 10000;
for (int day = 0; day <= 7; day++) {
clock_->Advance(base::Days(1));
state_->AddDelta(saving);
}
EXPECT_EQ(state_->GetWeeklySum(), 7 * saving);
}
TEST_F(WeeklyStorageTest, HandlesSkippedDay) {
uint64_t saving = 10000;
for (int day = 0; day < 7; day++) {
clock_->Advance(base::Days(1));
if (day == 3)
continue;
state_->AddDelta(saving);
}
EXPECT_EQ(state_->GetWeeklySum(), 6 * saving);
}
TEST_F(WeeklyStorageTest, IntermittentUsage) {
uint64_t saving = 10000;
for (int day = 0; day < 10; day++) {
clock_->Advance(base::Days(2));
state_->AddDelta(saving);
}
EXPECT_EQ(state_->GetWeeklySum(), 4 * saving);
}
TEST_F(WeeklyStorageTest, InfrequentUsage) {
uint64_t saving = 10000;
state_->AddDelta(saving);
clock_->Advance(base::Days(6));
state_->AddDelta(saving);
EXPECT_EQ(state_->GetWeeklySum(), 2 * saving);
}
TEST_F(WeeklyStorageTest, GetHighestValueInWeek) {
uint64_t lowest_value = 20;
uint64_t low_value = 50;
uint64_t high_value = 75;
state_->AddDelta(low_value);
clock_->Advance(base::Days(1));
state_->AddDelta(high_value);
clock_->Advance(base::Days(1));
state_->AddDelta(lowest_value);
EXPECT_EQ(state_->GetHighestValueInWeek(), high_value);
clock_->Advance(base::Days(1));
EXPECT_EQ(state_->GetHighestValueInWeek(), high_value);
}
TEST_F(WeeklyStorageTest, RecordsHigherValueForToday) {
uint64_t low_value = 50;
uint64_t high_value = 75;
state_->ReplaceTodaysValueIfGreater(low_value);
EXPECT_EQ(state_->GetHighestValueInWeek(), low_value);
// Replace with higher value
state_->ReplaceTodaysValueIfGreater(high_value);
EXPECT_EQ(state_->GetHighestValueInWeek(), high_value);
// Sanity check value was replaced and not added.
EXPECT_EQ(state_->GetWeeklySum(), high_value);
// Should not replace with lower value
state_->ReplaceTodaysValueIfGreater(low_value);
EXPECT_EQ(state_->GetHighestValueInWeek(), high_value);
}
TEST_F(WeeklyStorageTest, GetsHighestValueInWeekFromReplacement) {
// Add a low value a couple days after a high value,
// should return highest day value.
uint64_t low_value = 50;
uint64_t high_value = 75;
state_->ReplaceTodaysValueIfGreater(high_value);
clock_->Advance(base::Days(2));
state_->ReplaceTodaysValueIfGreater(low_value);
EXPECT_EQ(state_->GetHighestValueInWeek(), high_value);
// Sanity check disparate days were not replaced
EXPECT_EQ(state_->GetWeeklySum(), high_value + low_value);
}
+4 -4
View File
@@ -137,9 +137,9 @@ test("brave_unit_tests") {
"//brave/components/ntp_widget_utils/browser/ntp_widget_utils_region_unittest.cc",
"//brave/components/p3a/brave_p2a_protocols_unittest.cc",
"//brave/components/p3a/metric_names_unittest.cc",
"//brave/components/weekly_storage/daily_storage_unittest.cc",
"//brave/components/weekly_storage/weekly_event_storage_unittest.cc",
"//brave/components/weekly_storage/weekly_storage_unittest.cc",
"//brave/components/time_period_storage/daily_storage_unittest.cc",
"//brave/components/time_period_storage/time_period_storage_unittest.cc",
"//brave/components/time_period_storage/weekly_event_storage_unittest.cc",
"//brave/third_party/blink/renderer/brave_font_whitelist_unittest.cc",
"//brave/third_party/libaddressinput/chromium/chrome_metadata_source_unittest.cc",
"//brave/vendor/brave_base/random_unittest.cc",
@@ -239,10 +239,10 @@ test("brave_unit_tests") {
"//brave/components/signin/public/identity_manager:unit_tests",
"//brave/components/sync/driver:unit_tests",
"//brave/components/sync/engine:unit_tests",
"//brave/components/time_period_storage",
"//brave/components/tor:tor_unit_tests",
"//brave/components/tor/buildflags",
"//brave/components/translate/core/common:buildflags",
"//brave/components/weekly_storage",
"//brave/extensions:common",
"//brave/mojo/brave_ast_patcher:unit_tests",
"//brave/net:unit_tests",