Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1338)

Unified Diff: components/ntp_snippets/user_classifier.cc

Issue 2804633003: Add base::FeatureParam<> struct (Closed)
Patch Set: Remove windows-incompatible constexpr Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/ntp_snippets/user_classifier.cc
diff --git a/components/ntp_snippets/user_classifier.cc b/components/ntp_snippets/user_classifier.cc
index 92b0312de491dbd848919aeeeb62298b9d35405c..32930b4d8fb6db621c02603ae7124ea7bdd6c9d1 100644
--- a/components/ntp_snippets/user_classifier.cc
+++ b/components/ntp_snippets/user_classifier.cc
@@ -24,30 +24,32 @@ namespace {
// The discount rate for computing the discounted-average metrics. Must be
// strictly larger than 0 and strictly smaller than 1!
const double kDiscountRatePerDay = 0.25;
-const char kDiscountRatePerDayParam[] = "user_classifier_discount_rate_per_day";
+constexpr base::FeatureParam<double> kDiscountRatePerDayParam{
+ &kArticleSuggestionsFeature, "user_classifier_discount_rate_per_day", 0.25};
// Never consider any larger interval than this (so that extreme situations such
// as losing your phone or going for a long offline vacation do not skew the
// average too much).
// When everriding via variation parameters, it is better to use smaller values
// than |kMaxHours| as this it the maximum value reported in the histograms.
-const double kMaxHours = 7 * 24;
-const char kMaxHoursParam[] = "user_classifier_max_hours";
+constexpr base::FeatureParam<double> kMaxHoursParam{
+ &kArticleSuggestionsFeature, "user_classifier_max_hours", 7 * 24};
// Ignore events within |kMinHours| hours since the last event (|kMinHours| is
// the length of the browsing session where subsequent events of the same type
// do not count again).
-const double kMinHours = 0.5;
-const char kMinHoursParam[] = "user_classifier_min_hours";
+constexpr base::FeatureParam<double> kMinHoursParam{
+ &kArticleSuggestionsFeature, "user_classifier_min_hours", 0.5};
// Classification constants.
-const double kActiveConsumerClicksAtLeastOncePerHours = 72;
-const char kActiveConsumerClicksAtLeastOncePerHoursParam[] =
- "user_classifier_active_consumer_clicks_at_least_once_per_hours";
+constexpr base::FeatureParam<double>
+ kActiveConsumerClicksAtLeastOncePerHoursParam{
+ &kArticleSuggestionsFeature,
+ "user_classifier_active_consumer_clicks_at_least_once_per_hours", 72};
-const double kRareUserOpensNTPAtMostOncePerHours = 96;
-const char kRareUserOpensNTPAtMostOncePerHoursParam[] =
- "user_classifier_rare_user_opens_ntp_at_most_once_per_hours";
+constexpr base::FeatureParam<double> kRareUserOpensNTPAtMostOncePerHoursParam{
+ &kArticleSuggestionsFeature,
+ "user_classifier_rare_user_opens_ntp_at_most_once_per_hours", 96};
// Histograms for logging the estimated average hours to next event.
const char kHistogramAverageHoursToOpenNTP[] =
@@ -73,11 +75,13 @@ const char* kLastTimeKeys[] = {prefs::kUserClassifierLastTimeToOpenNTP,
prefs::kUserClassifierLastTimeToUseSuggestions};
// Default lengths of the intervals for new users for the metrics.
-const double kInitialHoursBetweenEvents[] = {24, 48, 96};
-const char* kInitialHoursBetweenEventsParams[] = {
- "user_classifier_default_interval_ntp_opened",
- "user_classifier_default_interval_suggestions_shown",
- "user_classifier_default_interval_suggestions_used"};
+constexpr base::FeatureParam<double> kInitialHoursBetweenEventsParams[] = {
+ {&kArticleSuggestionsFeature, "user_classifier_default_interval_ntp_opened",
+ 24},
+ {&kArticleSuggestionsFeature,
+ "user_classifier_default_interval_suggestions_shown", 48},
+ {&kArticleSuggestionsFeature,
+ "user_classifier_default_interval_suggestions_used", 96}};
static_assert(arraysize(kMetrics) ==
static_cast<int>(UserClassifier::Metric::COUNT) &&
@@ -85,21 +89,17 @@ static_assert(arraysize(kMetrics) ==
static_cast<int>(UserClassifier::Metric::COUNT) &&
arraysize(kLastTimeKeys) ==
static_cast<int>(UserClassifier::Metric::COUNT) &&
- arraysize(kInitialHoursBetweenEvents) ==
- static_cast<int>(UserClassifier::Metric::COUNT) &&
arraysize(kInitialHoursBetweenEventsParams) ==
static_cast<int>(UserClassifier::Metric::COUNT),
"Fill in info for all metrics.");
// Computes the discount rate.
double GetDiscountRatePerHour() {
- double discount_rate_per_day = variations::GetVariationParamByFeatureAsDouble(
- kArticleSuggestionsFeature, kDiscountRatePerDayParam,
- kDiscountRatePerDay);
+ double discount_rate_per_day = kDiscountRatePerDayParam.Get();
// Check for illegal values.
if (discount_rate_per_day <= 0 || discount_rate_per_day >= 1) {
DLOG(WARNING) << "Illegal value " << discount_rate_per_day
- << " for the parameter " << kDiscountRatePerDayParam
+ << " for the parameter " << kDiscountRatePerDayParam.name
<< " (must be strictly between 0 and 1; the default "
<< kDiscountRatePerDay << " is used, instead).";
discount_rate_per_day = kDiscountRatePerDay;
@@ -110,20 +110,7 @@ double GetDiscountRatePerHour() {
}
double GetInitialHoursBetweenEvents(UserClassifier::Metric metric) {
- return variations::GetVariationParamByFeatureAsDouble(
- kArticleSuggestionsFeature,
- kInitialHoursBetweenEventsParams[static_cast<int>(metric)],
- kInitialHoursBetweenEvents[static_cast<int>(metric)]);
-}
-
-double GetMinHours() {
- return variations::GetVariationParamByFeatureAsDouble(
- kArticleSuggestionsFeature, kMinHoursParam, kMinHours);
-}
-
-double GetMaxHours() {
- return variations::GetVariationParamByFeatureAsDouble(
- kArticleSuggestionsFeature, kMaxHoursParam, kMaxHours);
+ return kInitialHoursBetweenEventsParams[static_cast<int>(metric)].Get();
}
// Returns the new value of the metric using its |old_value|, assuming
@@ -186,18 +173,12 @@ UserClassifier::UserClassifier(PrefService* pref_service,
: pref_service_(pref_service),
clock_(std::move(clock)),
discount_rate_per_hour_(GetDiscountRatePerHour()),
- min_hours_(GetMinHours()),
- max_hours_(GetMaxHours()),
+ min_hours_(kMinHoursParam.Get()),
+ max_hours_(kMaxHoursParam.Get()),
active_consumer_clicks_at_least_once_per_hours_(
- variations::GetVariationParamByFeatureAsDouble(
- kArticleSuggestionsFeature,
- kActiveConsumerClicksAtLeastOncePerHoursParam,
- kActiveConsumerClicksAtLeastOncePerHours)),
+ kActiveConsumerClicksAtLeastOncePerHoursParam.Get()),
rare_user_opens_ntp_at_most_once_per_hours_(
- variations::GetVariationParamByFeatureAsDouble(
- kArticleSuggestionsFeature,
- kRareUserOpensNTPAtMostOncePerHoursParam,
- kRareUserOpensNTPAtMostOncePerHours)) {
+ kRareUserOpensNTPAtMostOncePerHoursParam.Get()) {
// The pref_service_ can be null in tests.
if (!pref_service_) {
return;
@@ -220,8 +201,8 @@ UserClassifier::~UserClassifier() = default;
// static
void UserClassifier::RegisterProfilePrefs(PrefRegistrySimple* registry) {
double discount_rate = GetDiscountRatePerHour();
- double min_hours = GetMinHours();
- double max_hours = GetMaxHours();
+ double min_hours = kMinHoursParam.Get();
+ double max_hours = kMaxHoursParam.Get();
for (Metric metric : kMetrics) {
double default_metric_value = GetMetricValueForEstimateHoursBetweenEvents(
@@ -244,15 +225,15 @@ void UserClassifier::OnEvent(Metric metric) {
switch (metric) {
case Metric::NTP_OPENED:
UMA_HISTOGRAM_CUSTOM_COUNTS(kHistogramAverageHoursToOpenNTP, avg, 1,
- kMaxHours, 50);
+ kMaxHoursParam.default_value, 50);
break;
case Metric::SUGGESTIONS_SHOWN:
UMA_HISTOGRAM_CUSTOM_COUNTS(kHistogramAverageHoursToShowSuggestions, avg,
- 1, kMaxHours, 50);
+ 1, kMaxHoursParam.default_value, 50);
break;
case Metric::SUGGESTIONS_USED:
UMA_HISTOGRAM_CUSTOM_COUNTS(kHistogramAverageHoursToUseSuggestions, avg,
- 1, kMaxHours, 50);
+ 1, kMaxHoursParam.default_value, 50);
break;
case Metric::COUNT:
NOTREACHED();

Powered by Google App Engine
This is Rietveld 408576698