Add profile count P3A metric

This commit is contained in:
Darnell Andries
2023-04-25 16:34:29 -07:00
parent d5c1ea6d06
commit ccbf5ec405
9 changed files with 152 additions and 15 deletions
+2 -2
View File
@@ -393,8 +393,8 @@ BraveBrowserProcessImpl::brave_referrals_service() {
brave_stats::BraveStatsUpdater* BraveBrowserProcessImpl::brave_stats_updater() {
if (!brave_stats_updater_)
brave_stats_updater_ =
std::make_unique<brave_stats::BraveStatsUpdater>(local_state());
brave_stats_updater_ = std::make_unique<brave_stats::BraveStatsUpdater>(
local_state(), g_browser_process->profile_manager());
return brave_stats_updater_.get();
}
+20 -3
View File
@@ -27,6 +27,7 @@
#include "brave/components/version_info/version_info.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/channel_info.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_registry_simple.h"
@@ -96,8 +97,11 @@ net::NetworkTrafficAnnotationTag AnonymousStatsAnnotation() {
} // anonymous namespace
BraveStatsUpdater::BraveStatsUpdater(PrefService* pref_service)
: pref_service_(pref_service), testing_url_loader_factory_(nullptr) {
BraveStatsUpdater::BraveStatsUpdater(PrefService* pref_service,
ProfileManager* profile_manager)
: pref_service_(pref_service),
profile_manager_(profile_manager),
testing_url_loader_factory_(nullptr) {
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(switches::kBraveStatsUpdaterServer)) {
@@ -112,10 +116,18 @@ BraveStatsUpdater::BraveStatsUpdater(PrefService* pref_service)
general_browser_usage_p3a_ =
std::make_unique<misc_metrics::GeneralBrowserUsage>(pref_service);
if (profile_manager != nullptr) {
g_browser_process->profile_manager()->AddObserver(this);
}
Start();
}
BraveStatsUpdater::~BraveStatsUpdater() = default;
BraveStatsUpdater::~BraveStatsUpdater() {
if (profile_manager_ != nullptr) {
g_browser_process->profile_manager()->RemoveObserver(this);
}
}
void BraveStatsUpdater::Start() {
// Startup timer, only initiated once we've checked for a promo
@@ -312,6 +324,11 @@ void BraveStatsUpdater::DisableThresholdPing() {
pref_service_->ClearPref(kThresholdQuery);
}
void BraveStatsUpdater::OnProfileAdded(Profile* profile) {
general_browser_usage_p3a_->ReportProfileCount(
g_browser_process->profile_manager()->GetNumberOfProfiles());
}
void BraveStatsUpdater::QueueServerPing() {
const bool referrals_initialized = IsReferralInitialized();
const bool ads_enabled = IsAdsEnabled();
+9 -3
View File
@@ -15,6 +15,7 @@
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "brave/components/brave_stats/browser/brave_stats_updater_util.h"
#include "chrome/browser/profiles/profile_manager_observer.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "url/gurl.h"
@@ -22,6 +23,7 @@ class BraveStatsUpdaterBrowserTest;
class PrefChangeRegistrar;
class PrefRegistrySimple;
class PrefService;
class ProfileManager;
namespace base {
class OneShotTimer;
@@ -47,12 +49,12 @@ extern const char kP3ADailyPingHistogramName[];
class BraveStatsUpdaterParams;
class BraveStatsUpdater {
class BraveStatsUpdater : public ProfileManagerObserver {
public:
explicit BraveStatsUpdater(PrefService* pref_service);
BraveStatsUpdater(PrefService* pref_service, ProfileManager* profile_manager);
BraveStatsUpdater(const BraveStatsUpdater&) = delete;
BraveStatsUpdater& operator=(const BraveStatsUpdater&) = delete;
~BraveStatsUpdater();
~BraveStatsUpdater() override;
void Start();
void Stop();
@@ -98,6 +100,9 @@ class BraveStatsUpdater {
bool HasDoneThresholdPing();
void DisableThresholdPing();
// ProfileManagerObserver:
void OnProfileAdded(Profile* profile) override;
network::mojom::URLLoaderFactory* GetURLLoaderFactory();
friend class ::BraveStatsUpdaterBrowserTest;
@@ -106,6 +111,7 @@ class BraveStatsUpdater {
ProcessArch arch_ = ProcessArch::kArchSkip;
bool stats_startup_complete_ = false;
raw_ptr<PrefService> pref_service_ = nullptr;
raw_ptr<ProfileManager> profile_manager_ = nullptr;
std::string usage_server_;
std::unique_ptr<network::SimpleURLLoader> simple_url_loader_;
std::unique_ptr<base::OneShotTimer> server_ping_startup_timer_;
@@ -687,7 +687,8 @@ TEST_F(BraveStatsUpdaterTest, UsagePingRequest) {
EXPECT_EQ(request.url.spec().find("https://localhost:8443"), (size_t)0);
}));
brave_stats::BraveStatsUpdater updater(GetLocalState());
brave_stats::BraveStatsUpdater updater(GetLocalState(),
/*profile_manager*/ nullptr);
updater.SetURLLoaderFactoryForTesting(shared_url_loader_factory_);
brave_stats::BraveStatsUpdater::StatsUpdatedCallback cb = base::BindRepeating(
[](int* ping_count, GURL* last_url, const GURL& url) {
@@ -719,7 +720,8 @@ TEST_F(BraveStatsUpdaterTest, UsagePingRequest) {
}
TEST_F(BraveStatsUpdaterTest, RecordP3APing) {
brave_stats::BraveStatsUpdater updater(GetLocalState());
brave_stats::BraveStatsUpdater updater(GetLocalState(),
/*profile_manager*/ nullptr);
updater.SetURLLoaderFactoryForTesting(shared_url_loader_factory_);
histogram_tester_.ExpectUniqueSample(
@@ -7,20 +7,29 @@
#include "base/metrics/histogram_macros.h"
#include "brave/components/misc_metrics/pref_names.h"
#include "brave/components/p3a_utils/bucket.h"
#include "brave/components/time_period_storage/iso_weekly_storage.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
namespace misc_metrics {
namespace {
const base::TimeDelta kReportInterval = base::Days(1);
#if !BUILDFLAG(IS_ANDROID)
constexpr int kProfileCountBuckets[] = {0, 1, 2, 3, 5};
#endif // !BUILDFLAG(IS_ANDROID)
} // namespace
const char kWeeklyUseHistogramName[] = "Brave.Core.WeeklyUsage";
const char kProfileCountHistogramName[] = "Brave.Core.ProfileCount";
GeneralBrowserUsage::GeneralBrowserUsage(PrefService* local_state) {
usage_storage_ = std::make_unique<ISOWeeklyStorage>(
local_state, kMiscMetricsBrowserUsageList);
report_timer_.Start(FROM_HERE, kReportInterval, this,
&GeneralBrowserUsage::Update);
Update();
}
@@ -30,10 +39,28 @@ void GeneralBrowserUsage::RegisterPrefs(PrefRegistrySimple* registry) {
registry->RegisterListPref(kMiscMetricsBrowserUsageList);
}
void GeneralBrowserUsage::Update() {
void GeneralBrowserUsage::ReportWeeklyUse() {
usage_storage_->ReplaceTodaysValueIfGreater(1);
UMA_HISTOGRAM_EXACT_LINEAR(kWeeklyUseHistogramName,
usage_storage_->GetLastISOWeekSum(), 8);
}
void GeneralBrowserUsage::ReportProfileCount(size_t count) {
#if !BUILDFLAG(IS_ANDROID)
p3a_utils::RecordToHistogramBucket(kProfileCountHistogramName,
kProfileCountBuckets, count);
#endif // !BUILDFLAG(IS_ANDROID)
}
void GeneralBrowserUsage::SetUpUpdateTimer() {
report_timer_.Start(FROM_HERE, base::Time::Now() + kReportInterval, this,
&GeneralBrowserUsage::Update);
}
void GeneralBrowserUsage::Update() {
ReportWeeklyUse();
SetUpUpdateTimer();
}
} // namespace misc_metrics
@@ -8,7 +8,7 @@
#include <memory>
#include "base/timer/timer.h"
#include "base/timer/wall_clock_timer.h"
class PrefRegistrySimple;
class PrefService;
@@ -17,6 +17,7 @@ class ISOWeeklyStorage;
namespace misc_metrics {
extern const char kWeeklyUseHistogramName[];
extern const char kProfileCountHistogramName[];
class GeneralBrowserUsage {
public:
@@ -28,12 +29,18 @@ class GeneralBrowserUsage {
static void RegisterPrefs(PrefRegistrySimple* registry);
void ReportProfileCount(size_t count);
private:
void ReportWeeklyUse();
void SetUpUpdateTimer();
void Update();
std::unique_ptr<ISOWeeklyStorage> usage_storage_;
base::RepeatingTimer report_timer_;
base::WallClockTimer report_timer_;
};
} // namespace misc_metrics
@@ -0,0 +1,76 @@
/* Copyright (c) 2023 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 https://mozilla.org/MPL/2.0/. */
#include <memory>
#include "base/test/metrics/histogram_tester.h"
#include "base/time/time.h"
#include "brave/components/misc_metrics/general_browser_usage.h"
#include "components/prefs/testing_pref_service.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace misc_metrics {
class GeneralBrowserUsageUnitTest : public testing::Test {
public:
GeneralBrowserUsageUnitTest()
: task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
void SetUp() override {
misc_metrics::GeneralBrowserUsage::RegisterPrefs(local_state_.registry());
// skip ahead to next monday if not on monday
base::Time now = base::Time::Now();
base::Time::Exploded exploded;
now.LocalMidnight().LocalExplode(&exploded);
int days_until_monday = 0;
if (exploded.day_of_week > 1) {
days_until_monday = 8 - exploded.day_of_week;
} else if (exploded.day_of_week == 0) {
days_until_monday = 1;
}
task_environment_.AdvanceClock(base::Days(days_until_monday));
general_browser_usage_ =
std::make_unique<GeneralBrowserUsage>(&local_state_);
}
protected:
content::BrowserTaskEnvironment task_environment_;
TestingPrefServiceSimple local_state_;
base::HistogramTester histogram_tester_;
std::unique_ptr<GeneralBrowserUsage> general_browser_usage_;
};
TEST_F(GeneralBrowserUsageUnitTest, WeeklyUsage) {
histogram_tester_.ExpectUniqueSample(kWeeklyUseHistogramName, 0, 1);
task_environment_.FastForwardBy(base::Days(1));
histogram_tester_.ExpectUniqueSample(kWeeklyUseHistogramName, 0, 2);
task_environment_.FastForwardBy(base::Days(3));
histogram_tester_.ExpectUniqueSample(kWeeklyUseHistogramName, 0, 5);
task_environment_.FastForwardBy(base::Days(3));
histogram_tester_.ExpectBucketCount(kWeeklyUseHistogramName, 7, 1);
}
#if !BUILDFLAG(IS_ANDROID)
TEST_F(GeneralBrowserUsageUnitTest, ProfileCount) {
histogram_tester_.ExpectTotalCount(kProfileCountHistogramName, 0);
general_browser_usage_->ReportProfileCount(1);
histogram_tester_.ExpectUniqueSample(kProfileCountHistogramName, 1, 1);
general_browser_usage_->ReportProfileCount(2);
histogram_tester_.ExpectBucketCount(kProfileCountHistogramName, 2, 1);
}
#endif // !BUILDFLAG(IS_ANDROID)
} // namespace misc_metrics
+1
View File
@@ -177,6 +177,7 @@ constexpr inline auto kCollectedTypicalHistograms =
constexpr inline auto kCollectedSlowHistograms =
base::MakeFixedFlatSet<base::StringPiece>({
"Brave.Core.ProfileCount",
"Brave.Core.UsageMonthly",
"Brave.P3A.TestSlowMetric",
"Brave.Rewards.TipsSent",
+1
View File
@@ -136,6 +136,7 @@ test("brave_unit_tests") {
"//brave/components/brave_sync/crypto/crypto_unittest.cc",
"//brave/components/content_settings/core/browser/brave_content_settings_pref_provider_unittest.cc",
"//brave/components/content_settings/core/browser/brave_content_settings_utils_unittest.cc",
"//brave/components/misc_metrics/general_browser_usage_unittest.cc",
"//brave/components/misc_metrics/menu_metrics_unittest.cc",
"//brave/components/ntp_background_images/browser/ntp_background_images_service_unittest.cc",
"//brave/components/ntp_background_images/browser/ntp_background_images_source_unittest.cc",