From 640d28d0fc01aa2f51d0cb0d88d50118fc049e8a Mon Sep 17 00:00:00 2001 From: Darnell Andries Date: Wed, 18 May 2022 14:10:33 -0700 Subject: [PATCH] Refactor weekly storage into a generic time period storage; added weekly/monthly storage classes which extend the time period storage --- browser/p3a/p3a_core_metrics.h | 2 +- .../search_engines/search_engine_tracker.h | 2 +- browser/search_engines/sources.gni | 2 +- browser/sources.gni | 2 +- browser/ui/BUILD.gn | 2 +- .../ui/omnibox/brave_omnibox_client_impl.cc | 2 +- .../brave_new_tab_message_handler.cc | 2 +- components/brave_ads/browser/BUILD.gn | 2 +- components/brave_ads/browser/ads_p2a.cc | 2 +- .../brave_perf_predictor/browser/BUILD.gn | 2 +- .../browser/p3a_bandwidth_savings_tracker.cc | 2 +- components/brave_search/browser/BUILD.gn | 2 +- .../browser/brave_search_default_host.cc | 2 +- components/brave_today/browser/BUILD.gn | 2 +- .../brave_today/browser/brave_news_p3a.cc | 2 +- components/brave_wallet/browser/BUILD.gn | 2 +- .../browser/brave_wallet_service.cc | 2 +- .../ntp_background_images/browser/BUILD.gn | 2 +- .../browser/view_counter_service.cc | 2 +- components/p3a/metric_names.h | 5 +- components/speedreader/BUILD.gn | 2 +- components/speedreader/speedreader_service.cc | 2 +- .../BUILD.gn | 6 +- .../daily_storage.cc | 2 +- .../daily_storage.h | 7 +- .../daily_storage_unittest.cc | 2 +- .../time_period_storage/monthly_storage.cc | 21 ++ .../time_period_storage/monthly_storage.h | 24 ++ .../time_period_storage.cc} | 79 ++++--- .../time_period_storage/time_period_storage.h | 63 +++++ .../time_period_storage_unittest.cc | 216 ++++++++++++++++++ .../weekly_event_storage.cc | 2 +- .../weekly_event_storage.h | 7 +- .../weekly_event_storage_unittest.cc | 2 +- .../time_period_storage/weekly_storage.cc | 25 ++ .../time_period_storage/weekly_storage.h | 25 ++ components/weekly_storage/weekly_storage.h | 61 ----- .../weekly_storage/weekly_storage_unittest.cc | 167 -------------- test/BUILD.gn | 8 +- 39 files changed, 459 insertions(+), 305 deletions(-) rename components/{weekly_storage => time_period_storage}/BUILD.gn (61%) rename components/{weekly_storage => time_period_storage}/daily_storage.cc (97%) rename components/{weekly_storage => time_period_storage}/daily_storage.h (83%) rename components/{weekly_storage => time_period_storage}/daily_storage_unittest.cc (97%) create mode 100644 components/time_period_storage/monthly_storage.cc create mode 100644 components/time_period_storage/monthly_storage.h rename components/{weekly_storage/weekly_storage.cc => time_period_storage/time_period_storage.cc} (62%) create mode 100644 components/time_period_storage/time_period_storage.h create mode 100644 components/time_period_storage/time_period_storage_unittest.cc rename components/{weekly_storage => time_period_storage}/weekly_event_storage.cc (97%) rename components/{weekly_storage => time_period_storage}/weekly_event_storage.h (87%) rename components/{weekly_storage => time_period_storage}/weekly_event_storage_unittest.cc (98%) create mode 100644 components/time_period_storage/weekly_storage.cc create mode 100644 components/time_period_storage/weekly_storage.h delete mode 100644 components/weekly_storage/weekly_storage.h delete mode 100644 components/weekly_storage/weekly_storage_unittest.cc diff --git a/browser/p3a/p3a_core_metrics.h b/browser/p3a/p3a_core_metrics.h index 681ebefbd25..be675c338be 100644 --- a/browser/p3a/p3a_core_metrics.h +++ b/browser/p3a/p3a_core_metrics.h @@ -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" diff --git a/browser/search_engines/search_engine_tracker.h b/browser/search_engines/search_engine_tracker.h index 8d64163949b..50cd8b58f9d 100644 --- a/browser/search_engines/search_engine_tracker.h +++ b/browser/search_engines/search_engine_tracker.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" diff --git a/browser/search_engines/sources.gni b/browser/search_engines/sources.gni index c528cd2f36d..ba45386a607 100644 --- a/browser/search_engines/sources.gni +++ b/browser/search_engines/sources.gni @@ -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", diff --git a/browser/sources.gni b/browser/sources.gni index 29aaa569f85..8cd7e67718a 100644 --- a/browser/sources.gni +++ b/browser/sources.gni @@ -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", ] } diff --git a/browser/ui/BUILD.gn b/browser/ui/BUILD.gn index dc13452a6a0..2392e804a06 100644 --- a/browser/ui/BUILD.gn +++ b/browser/ui/BUILD.gn @@ -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", diff --git a/browser/ui/omnibox/brave_omnibox_client_impl.cc b/browser/ui/omnibox/brave_omnibox_client_impl.cc index b106bdb1b73..f474d40da42 100644 --- a/browser/ui/omnibox/brave_omnibox_client_impl.cc +++ b/browser/ui/omnibox/brave_omnibox_client_impl.cc @@ -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" diff --git a/browser/ui/webui/new_tab_page/brave_new_tab_message_handler.cc b/browser/ui/webui/new_tab_page/brave_new_tab_message_handler.cc index dff0af8be0e..a3ea712df78 100644 --- a/browser/ui/webui/new_tab_page/brave_new_tab_message_handler.cc +++ b/browser/ui/webui/new_tab_page/brave_new_tab_message_handler.cc @@ -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" diff --git a/components/brave_ads/browser/BUILD.gn b/components/brave_ads/browser/BUILD.gn index 57c9cda57cb..ac3bed06c15 100644 --- a/components/brave_ads/browser/BUILD.gn +++ b/components/brave_ads/browser/BUILD.gn @@ -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", diff --git a/components/brave_ads/browser/ads_p2a.cc b/components/brave_ads/browser/ads_p2a.cc index 48ccb0b539a..5ff11ced377 100644 --- a/components/brave_ads/browser/ads_p2a.cc +++ b/components/brave_ads/browser/ads_p2a.cc @@ -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" diff --git a/components/brave_perf_predictor/browser/BUILD.gn b/components/brave_perf_predictor/browser/BUILD.gn index 2007de61fbe..5c63d83df63 100644 --- a/components/brave_perf_predictor/browser/BUILD.gn +++ b/components/brave_perf_predictor/browser/BUILD.gn @@ -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", diff --git a/components/brave_perf_predictor/browser/p3a_bandwidth_savings_tracker.cc b/components/brave_perf_predictor/browser/p3a_bandwidth_savings_tracker.cc index f0678340854..1ffa91ebb76 100644 --- a/components/brave_perf_predictor/browser/p3a_bandwidth_savings_tracker.cc +++ b/components/brave_perf_predictor/browser/p3a_bandwidth_savings_tracker.cc @@ -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" diff --git a/components/brave_search/browser/BUILD.gn b/components/brave_search/browser/BUILD.gn index 4090da7273e..4b6a199c6d6 100644 --- a/components/brave_search/browser/BUILD.gn +++ b/components/brave_search/browser/BUILD.gn @@ -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", diff --git a/components/brave_search/browser/brave_search_default_host.cc b/components/brave_search/browser/brave_search_default_host.cc index ed78ebd49c9..f7d5a3c0c5d 100644 --- a/components/brave_search/browser/brave_search_default_host.cc +++ b/components/brave_search/browser/brave_search_default_host.cc @@ -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" diff --git a/components/brave_today/browser/BUILD.gn b/components/brave_today/browser/BUILD.gn index c4ab4730b66..fe1bc5d2d8f 100644 --- a/components/brave_today/browser/BUILD.gn +++ b/components/brave_today/browser/BUILD.gn @@ -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", diff --git a/components/brave_today/browser/brave_news_p3a.cc b/components/brave_today/browser/brave_news_p3a.cc index aa5c2669fd5..b9a665946f6 100644 --- a/components/brave_today/browser/brave_news_p3a.cc +++ b/components/brave_today/browser/brave_news_p3a.cc @@ -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" diff --git a/components/brave_wallet/browser/BUILD.gn b/components/brave_wallet/browser/BUILD.gn index 058ff736852..3a09e1d8230 100644 --- a/components/brave_wallet/browser/BUILD.gn +++ b/components/brave_wallet/browser/BUILD.gn @@ -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", diff --git a/components/brave_wallet/browser/brave_wallet_service.cc b/components/brave_wallet/browser/brave_wallet_service.cc index 43be774f077..7c12a519b2f 100644 --- a/components/brave_wallet/browser/brave_wallet_service.cc +++ b/components/brave_wallet/browser/brave_wallet_service.cc @@ -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" diff --git a/components/ntp_background_images/browser/BUILD.gn b/components/ntp_background_images/browser/BUILD.gn index 2345942f4fc..219850466c7 100644 --- a/components/ntp_background_images/browser/BUILD.gn +++ b/components/ntp_background_images/browser/BUILD.gn @@ -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", diff --git a/components/ntp_background_images/browser/view_counter_service.cc b/components/ntp_background_images/browser/view_counter_service.cc index e2476eae95a..77a60767865 100644 --- a/components/ntp_background_images/browser/view_counter_service.cc +++ b/components/ntp_background_images/browser/view_counter_service.cc @@ -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" diff --git a/components/p3a/metric_names.h b/components/p3a/metric_names.h index 9af92f73d6c..19c90a83dca 100644 --- a/components/p3a/metric_names.h +++ b/components/p3a/metric_names.h @@ -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", diff --git a/components/speedreader/BUILD.gn b/components/speedreader/BUILD.gn index 8c24af65e54..6ae543e99ec 100644 --- a/components/speedreader/BUILD.gn +++ b/components/speedreader/BUILD.gn @@ -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", diff --git a/components/speedreader/speedreader_service.cc b/components/speedreader/speedreader_service.cc index 95b0dcfa302..b427a9da5dd 100644 --- a/components/speedreader/speedreader_service.cc +++ b/components/speedreader/speedreader_service.cc @@ -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" diff --git a/components/weekly_storage/BUILD.gn b/components/time_period_storage/BUILD.gn similarity index 61% rename from components/weekly_storage/BUILD.gn rename to components/time_period_storage/BUILD.gn index 9200af530ee..e2f604fee0f 100644 --- a/components/weekly_storage/BUILD.gn +++ b/components/time_period_storage/BUILD.gn @@ -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", diff --git a/components/weekly_storage/daily_storage.cc b/components/time_period_storage/daily_storage.cc similarity index 97% rename from components/weekly_storage/daily_storage.cc rename to components/time_period_storage/daily_storage.cc index 907048af7d2..a0129e4af7b 100644 --- a/components/weekly_storage/daily_storage.cc +++ b/components/time_period_storage/daily_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 #include diff --git a/components/weekly_storage/daily_storage.h b/components/time_period_storage/daily_storage.h similarity index 83% rename from components/weekly_storage/daily_storage.h rename to components/time_period_storage/daily_storage.h index c6732334464..792b7247954 100644 --- a/components/weekly_storage/daily_storage.h +++ b/components/time_period_storage/daily_storage.h @@ -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 #include @@ -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 daily_values_; }; -#endif // BRAVE_COMPONENTS_WEEKLY_STORAGE_DAILY_STORAGE_H_ +#endif // BRAVE_COMPONENTS_TIME_PERIOD_STORAGE_DAILY_STORAGE_H_ diff --git a/components/weekly_storage/daily_storage_unittest.cc b/components/time_period_storage/daily_storage_unittest.cc similarity index 97% rename from components/weekly_storage/daily_storage_unittest.cc rename to components/time_period_storage/daily_storage_unittest.cc index ec2d4752b90..01378079e4c 100644 --- a/components/weekly_storage/daily_storage_unittest.cc +++ b/components/time_period_storage/daily_storage_unittest.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 #include diff --git a/components/time_period_storage/monthly_storage.cc b/components/time_period_storage/monthly_storage.cc new file mode 100644 index 00000000000..aacd132d623 --- /dev/null +++ b/components/time_period_storage/monthly_storage.cc @@ -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(); +} diff --git a/components/time_period_storage/monthly_storage.h b/components/time_period_storage/monthly_storage.h new file mode 100644 index 00000000000..0a14409ba95 --- /dev/null +++ b/components/time_period_storage/monthly_storage.h @@ -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_ diff --git a/components/weekly_storage/weekly_storage.cc b/components/time_period_storage/time_period_storage.cc similarity index 62% rename from components/weekly_storage/weekly_storage.cc rename to components/time_period_storage/time_period_storage.cc index 08541e9feec..84382b1a505 100644 --- a/components/weekly_storage/weekly_storage.cc +++ b/components/time_period_storage/time_period_storage.cc @@ -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 #include @@ -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()) { 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 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 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 last_weeks_daily_values(daily_values_.size()); + const base::Time n_days_ago = clock_->Now() - base::Days(period_days_); + std::list 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(); diff --git a/components/time_period_storage/time_period_storage.h b/components/time_period_storage/time_period_storage.h new file mode 100644 index 00000000000..96e3bb0c253 --- /dev/null +++ b/components/time_period_storage/time_period_storage.h @@ -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 +#include + +#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 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 clock_; + + std::list daily_values_; +}; + +#endif // BRAVE_COMPONENTS_TIME_PERIOD_STORAGE_TIME_PERIOD_STORAGE_H_ diff --git a/components/time_period_storage/time_period_storage_unittest.cc b/components/time_period_storage/time_period_storage_unittest.cc new file mode 100644 index 00000000000..2d446656b47 --- /dev/null +++ b/components/time_period_storage/time_period_storage_unittest.cc @@ -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 +#include + +#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( + &pref_service_, kPrefName, days, std::unique_ptr(clock_)); + } + + protected: + raw_ptr clock_ = nullptr; + TestingPrefServiceSimple pref_service_; + std::unique_ptr 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); +} diff --git a/components/weekly_storage/weekly_event_storage.cc b/components/time_period_storage/weekly_event_storage.cc similarity index 97% rename from components/weekly_storage/weekly_event_storage.cc rename to components/time_period_storage/weekly_event_storage.cc index a2ba87ca318..b5697b3d939 100644 --- a/components/weekly_storage/weekly_event_storage.cc +++ b/components/time_period_storage/weekly_event_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/weekly_event_storage.h" +#include "brave/components/time_period_storage/weekly_event_storage.h" #include #include diff --git a/components/weekly_storage/weekly_event_storage.h b/components/time_period_storage/weekly_event_storage.h similarity index 87% rename from components/weekly_storage/weekly_event_storage.h rename to components/time_period_storage/weekly_event_storage.h index 1adf98fdc5e..6b6afd816d3 100644 --- a/components/weekly_storage/weekly_event_storage.h +++ b/components/time_period_storage/weekly_event_storage.h @@ -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 #include @@ -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 events_; }; -#endif // BRAVE_COMPONENTS_WEEKLY_STORAGE_WEEKLY_EVENT_STORAGE_H_ +#endif // BRAVE_COMPONENTS_TIME_PERIOD_STORAGE_WEEKLY_EVENT_STORAGE_H_ diff --git a/components/weekly_storage/weekly_event_storage_unittest.cc b/components/time_period_storage/weekly_event_storage_unittest.cc similarity index 98% rename from components/weekly_storage/weekly_event_storage_unittest.cc rename to components/time_period_storage/weekly_event_storage_unittest.cc index 63a964b3f39..981a3358f98 100644 --- a/components/weekly_storage/weekly_event_storage_unittest.cc +++ b/components/time_period_storage/weekly_event_storage_unittest.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/weekly_event_storage.h" +#include "brave/components/time_period_storage/weekly_event_storage.h" #include #include diff --git a/components/time_period_storage/weekly_storage.cc b/components/time_period_storage/weekly_storage.cc new file mode 100644 index 00000000000..42852a35e3d --- /dev/null +++ b/components/time_period_storage/weekly_storage.cc @@ -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(); +} diff --git a/components/time_period_storage/weekly_storage.h b/components/time_period_storage/weekly_storage.h new file mode 100644 index 00000000000..228a5ebec87 --- /dev/null +++ b/components/time_period_storage/weekly_storage.h @@ -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_ diff --git a/components/weekly_storage/weekly_storage.h b/components/weekly_storage/weekly_storage.h deleted file mode 100644 index 0b128d7efde..00000000000 --- a/components/weekly_storage/weekly_storage.h +++ /dev/null @@ -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 -#include - -#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 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 clock_; - - std::list daily_values_; -}; - -#endif // BRAVE_COMPONENTS_WEEKLY_STORAGE_WEEKLY_STORAGE_H_ diff --git a/components/weekly_storage/weekly_storage_unittest.cc b/components/weekly_storage/weekly_storage_unittest.cc deleted file mode 100644 index 3de432cee53..00000000000 --- a/components/weekly_storage/weekly_storage_unittest.cc +++ /dev/null @@ -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 -#include - -#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( - &pref_service_, kPrefName, std::unique_ptr(clock_)); - clock_->SetNow(base::Time::Now()); - } - - protected: - raw_ptr clock_ = nullptr; - TestingPrefServiceSimple pref_service_; - std::unique_ptr 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); -} diff --git a/test/BUILD.gn b/test/BUILD.gn index 7f9a7170a8a..e476e28c24d 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -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",