Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/ntp_snippets/user_classifier.h" | 5 #include "components/ntp_snippets/user_classifier.h" |
| 6 | 6 |
| 7 #include <float.h> | 7 #include <float.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "components/ntp_snippets/features.h" | |
| 14 #include "components/ntp_snippets/pref_names.h" | 15 #include "components/ntp_snippets/pref_names.h" |
| 15 #include "components/prefs/pref_registry_simple.h" | 16 #include "components/prefs/pref_registry_simple.h" |
| 16 #include "components/prefs/pref_service.h" | 17 #include "components/prefs/pref_service.h" |
| 18 #include "components/variations/variations_associated_data.h" | |
| 17 | 19 |
| 18 namespace ntp_snippets { | 20 namespace ntp_snippets { |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 // TODO(jkrcal): Make all of this configurable via variations_service. | |
| 23 | |
| 24 // The discount factor for computing the discounted-average metrics. Must be | 24 // The discount factor for computing the discounted-average metrics. Must be |
| 25 // strictly larger than 0 and strictly smaller than 1! | 25 // strictly larger than 0 and strictly smaller than 1! |
| 26 const double kDiscountFactorPerDay = 0.25; | 26 const double kDiscountFactorPerDay = 0.25; |
| 27 const char kDiscountFactorPerDayParam[] = | |
| 28 "user_classifier_discount_factor_per_day"; | |
| 27 | 29 |
| 28 // Never consider any larger interval than this (so that extreme situations such | 30 // Never consider any larger interval than this (so that extreme situations such |
| 29 // as losing your phone or going for a long offline vacation do not skew the | 31 // as losing your phone or going for a long offline vacation do not skew the |
| 30 // average too much). | 32 // average too much). |
| 33 // When everriding via variation parameters, it is better to use smaller values | |
| 34 // than |kMaxHours| as this it the maximum value reported in the histograms. | |
| 31 const double kMaxHours = 7 * 24; | 35 const double kMaxHours = 7 * 24; |
| 36 const char kMaxHoursParam[] = "user_classifier_max_hours"; | |
| 32 | 37 |
| 33 // Ignore events within |kMinHours| hours since the last event (|kMinHours| is | 38 // Ignore events within |kMinHours| hours since the last event (|kMinHours| is |
| 34 // the length of the browsing session where subsequent events of the same type | 39 // the length of the browsing session where subsequent events of the same type |
| 35 // do not count again). | 40 // do not count again). |
| 36 const double kMinHours = 0.5; | 41 const double kMinHours = 0.5; |
| 42 const char kMinHoursParam[] = "user_classifier_min_hours"; | |
| 37 | 43 |
| 38 // Classification constants. | 44 // Classification constants. |
| 39 const double kFrequentUserScrollsAtLeastOncePerHours = 24; | 45 const double kActiveConsumerScrollsAtLeastOncePerHours = 24; |
| 40 const double kOccasionalUserOpensNTPAtMostOncePerHours = 72; | 46 const char kActiveConsumerScrollsAtLeastOncePerHoursParam[] = |
| 47 "user_classifier_active_consumer_scrolls_at_least_once_per_hours"; | |
| 41 | 48 |
| 49 const double kRareUserOpensNTPAtMostOncePerHours = 72; | |
| 50 const char kRareUserOpensNTPAtMostOncePerHoursParam[] = | |
| 51 "user_classifier_rare_user_opens_ntp_at_most_once_per_hours"; | |
| 52 | |
| 53 // Histograms for logging the estimated average hours to next event. | |
| 42 const char kHistogramAverageHoursToOpenNTP[] = | 54 const char kHistogramAverageHoursToOpenNTP[] = |
| 43 "NewTabPage.UserClassifier.AverageHoursToOpenNTP"; | 55 "NewTabPage.UserClassifier.AverageHoursToOpenNTP"; |
| 44 const char kHistogramAverageHoursToShowSuggestions[] = | 56 const char kHistogramAverageHoursToShowSuggestions[] = |
| 45 "NewTabPage.UserClassifier.AverageHoursToShowSuggestions"; | 57 "NewTabPage.UserClassifier.AverageHoursToShowSuggestions"; |
| 46 const char kHistogramAverageHoursToUseSuggestions[] = | 58 const char kHistogramAverageHoursToUseSuggestions[] = |
| 47 "NewTabPage.UserClassifier.AverageHoursToUseSuggestions"; | 59 "NewTabPage.UserClassifier.AverageHoursToUseSuggestions"; |
| 48 | 60 |
| 49 // The enum used for iteration. | 61 // The enum used for iteration. |
| 50 const UserClassifier::Metric kMetrics[] = { | 62 const UserClassifier::Metric kMetrics[] = { |
| 51 UserClassifier::Metric::NTP_OPENED, | 63 UserClassifier::Metric::NTP_OPENED, |
| 52 UserClassifier::Metric::SUGGESTIONS_SHOWN, | 64 UserClassifier::Metric::SUGGESTIONS_SHOWN, |
| 53 UserClassifier::Metric::SUGGESTIONS_USED}; | 65 UserClassifier::Metric::SUGGESTIONS_USED}; |
| 54 | 66 |
| 55 // The summary of the prefs. | 67 // The summary of the prefs. |
| 56 const char* kMetricKeys[] = { | 68 const char* kMetricKeys[] = { |
| 57 prefs::kUserClassifierAverageNTPOpenedPerHour, | 69 prefs::kUserClassifierAverageNTPOpenedPerHour, |
| 58 prefs::kUserClassifierAverageSuggestionsShownPerHour, | 70 prefs::kUserClassifierAverageSuggestionsShownPerHour, |
| 59 prefs::kUserClassifierAverageSuggestionsUsedPerHour}; | 71 prefs::kUserClassifierAverageSuggestionsUsedPerHour}; |
| 60 const char* kLastTimeKeys[] = {prefs::kUserClassifierLastTimeToOpenNTP, | 72 const char* kLastTimeKeys[] = {prefs::kUserClassifierLastTimeToOpenNTP, |
| 61 prefs::kUserClassifierLastTimeToShowSuggestions, | 73 prefs::kUserClassifierLastTimeToShowSuggestions, |
| 62 prefs::kUserClassifierLastTimeToUseSuggestions}; | 74 prefs::kUserClassifierLastTimeToUseSuggestions}; |
| 63 | 75 |
| 64 // Default lengths of the intervals for new users for the metrics. | 76 // Default lengths of the intervals for new users for the metrics. |
| 65 const double kDefaults[] = {24, 36, 48}; | 77 const double kInitialHoursBetweenEvents[] = {24, 36, 48}; |
| 78 const char* kInitialHoursBetweenEventsParams[] = { | |
| 79 "user_classifier_default_interval_ntp_opened", | |
| 80 "user_classifier_default_interval_suggestions_shown", | |
| 81 "user_classifier_default_interval_suggestions_used"}; | |
| 66 | 82 |
| 67 static_assert(arraysize(kMetrics) == | 83 static_assert(arraysize(kMetrics) == |
| 68 static_cast<int>(UserClassifier::Metric::COUNT) && | 84 static_cast<int>(UserClassifier::Metric::COUNT) && |
| 69 arraysize(kMetricKeys) == | 85 arraysize(kMetricKeys) == |
| 70 static_cast<int>(UserClassifier::Metric::COUNT) && | 86 static_cast<int>(UserClassifier::Metric::COUNT) && |
| 71 arraysize(kLastTimeKeys) == | 87 arraysize(kLastTimeKeys) == |
| 72 static_cast<int>(UserClassifier::Metric::COUNT) && | 88 static_cast<int>(UserClassifier::Metric::COUNT) && |
| 73 arraysize(kDefaults) == | 89 arraysize(kInitialHoursBetweenEvents) == |
| 90 static_cast<int>(UserClassifier::Metric::COUNT) && | |
| 91 arraysize(kInitialHoursBetweenEventsParams) == | |
| 74 static_cast<int>(UserClassifier::Metric::COUNT), | 92 static_cast<int>(UserClassifier::Metric::COUNT), |
| 75 "Fill in info for all metrics."); | 93 "Fill in info for all metrics."); |
| 76 | 94 |
| 95 double GetParamValue(const char* param_name, double default_value) { | |
| 96 std::string param_value_str = variations::GetVariationParamValueByFeature( | |
| 97 kArticleSuggestionsFeature, param_name); | |
| 98 double param_value = 0; | |
| 99 if (!base::StringToDouble(param_value_str, ¶m_value)) { | |
| 100 LOG_IF(WARNING, !param_value_str.empty()) | |
| 101 << "Invalid variation parameter for " << param_name; | |
| 102 return default_value; | |
| 103 } | |
| 104 return param_value; | |
| 105 } | |
| 106 | |
| 77 // Computes the discount rate. | 107 // Computes the discount rate. |
| 78 double GetDiscountRatePerHour() { | 108 double GetDiscountRatePerHour() { |
| 109 double discount_factor_per_day = | |
| 110 GetParamValue(kDiscountFactorPerDayParam, kDiscountFactorPerDay); | |
| 111 // Check for illegal values. | |
| 112 if (discount_factor_per_day <= 0 || discount_factor_per_day >= 1) | |
| 113 discount_factor_per_day = kDiscountFactorPerDay; | |
|
Marc Treib
2016/09/22 13:36:09
I guess this should be rare, but maybe add a DLOG
jkrcal
2016/09/22 14:03:20
Done.
| |
| 79 // Compute discount_rate_per_hour such that | 114 // Compute discount_rate_per_hour such that |
| 80 // kDiscountFactorPerDay = 1 - e^{-discount_rate_per_hour * 24}. | 115 // discount_factor_per_day = 1 - e^{-discount_rate_per_hour * 24}. |
| 81 return std::log(1.0 / (1.0 - kDiscountFactorPerDay)) / 24.0; | 116 return std::log(1.0 / (1.0 - discount_factor_per_day)) / 24.0; |
|
Marc Treib
2016/09/22 13:36:09
Pre-existing: Is there any fundamental difference
jkrcal
2016/09/22 14:03:20
The notion discount factor is usually used in disc
| |
| 117 } | |
| 118 | |
| 119 double GetInitialHoursBetweenEvents(UserClassifier::Metric metric) { | |
| 120 return GetParamValue( | |
| 121 kInitialHoursBetweenEventsParams[static_cast<int>(metric)], | |
| 122 kInitialHoursBetweenEvents[static_cast<int>(metric)]); | |
| 123 } | |
| 124 | |
| 125 double GetMinHours() { | |
| 126 return GetParamValue(kMinHoursParam, kMinHours); | |
| 127 } | |
| 128 | |
| 129 double GetMaxHours() { | |
| 130 return GetParamValue(kMaxHoursParam, kMaxHours); | |
| 82 } | 131 } |
| 83 | 132 |
| 84 // Returns the new value of the metric using its |old_value|, assuming | 133 // Returns the new value of the metric using its |old_value|, assuming |
| 85 // |hours_since_last_time| hours have passed since it was last discounted. | 134 // |hours_since_last_time| hours have passed since it was last discounted. |
| 86 double DiscountMetric(double old_value, | 135 double DiscountMetric(double old_value, |
| 87 double hours_since_last_time, | 136 double hours_since_last_time, |
| 88 double discount_rate_per_hour) { | 137 double discount_rate_per_hour) { |
| 89 // Compute the new discounted average according to the formula | 138 // Compute the new discounted average according to the formula |
| 90 // avg_events := e^{-discount_rate_per_hour * hours_since} * avg_events | 139 // avg_events := e^{-discount_rate_per_hour * hours_since} * avg_events |
| 91 return std::exp(-discount_rate_per_hour * hours_since_last_time) * old_value; | 140 return std::exp(-discount_rate_per_hour * hours_since_last_time) * old_value; |
| 92 } | 141 } |
| 93 | 142 |
| 94 // Compute the number of hours between two events for the given metric value | 143 // Compute the number of hours between two events for the given metric value |
| 95 // assuming the events were equally distributed. | 144 // assuming the events were equally distributed. |
| 96 double GetEstimateHoursBetweenEvents(double metric_value, | 145 double GetEstimateHoursBetweenEvents(double metric_value, |
| 97 double discount_rate_per_hour) { | 146 double discount_rate_per_hour, |
| 147 double min_hours, | |
| 148 double max_hours) { | |
| 98 // The computation below is well-defined only for |metric_value| > 1 (log of | 149 // The computation below is well-defined only for |metric_value| > 1 (log of |
| 99 // negative value or division by zero). When |metric_value| -> 1, the estimate | 150 // negative value or division by zero). When |metric_value| -> 1, the estimate |
| 100 // below -> infinity, so kMaxHours is a natural result, here. | 151 // below -> infinity, so max_hours is a natural result, here. |
| 101 if (metric_value <= 1) | 152 if (metric_value <= 1) |
| 102 return kMaxHours; | 153 return max_hours; |
| 103 | 154 |
| 104 // This is the estimate with the assumption that last event happened right | 155 // This is the estimate with the assumption that last event happened right |
| 105 // now and the system is in the steady-state. Solve estimate_hours in the | 156 // now and the system is in the steady-state. Solve estimate_hours in the |
| 106 // steady-state equation: | 157 // steady-state equation: |
| 107 // metric_value = 1 + e^{-discount_rate * estimate_hours} * metric_value, | 158 // metric_value = 1 + e^{-discount_rate * estimate_hours} * metric_value, |
| 108 // i.e. | 159 // i.e. |
| 109 // -discount_rate * estimate_hours = log((metric_value - 1) / metric_value), | 160 // -discount_rate * estimate_hours = log((metric_value - 1) / metric_value), |
| 110 // discount_rate * estimate_hours = log(metric_value / (metric_value - 1)), | 161 // discount_rate * estimate_hours = log(metric_value / (metric_value - 1)), |
| 111 // estimate_hours = log(metric_value / (metric_value - 1)) / discount_rate. | 162 // estimate_hours = log(metric_value / (metric_value - 1)) / discount_rate. |
| 112 double estimate_hours = | 163 double estimate_hours = |
| 113 std::log(metric_value / (metric_value - 1)) / discount_rate_per_hour; | 164 std::log(metric_value / (metric_value - 1)) / discount_rate_per_hour; |
| 114 return std::max(kMinHours, std::min(kMaxHours, estimate_hours)); | 165 return std::max(min_hours, std::min(max_hours, estimate_hours)); |
| 115 } | 166 } |
| 116 | 167 |
| 117 // The inverse of GetEstimateHoursBetweenEvents(). | 168 // The inverse of GetEstimateHoursBetweenEvents(). |
| 118 double GetMetricValueForEstimateHoursBetweenEvents( | 169 double GetMetricValueForEstimateHoursBetweenEvents( |
| 119 double estimate_hours, | 170 double estimate_hours, |
| 120 double discount_rate_per_hour) { | 171 double discount_rate_per_hour, |
| 121 // Keep the input value within [kMinHours, kMaxHours]. | 172 double min_hours, |
| 122 estimate_hours = std::max(kMinHours, std::min(kMaxHours, estimate_hours)); | 173 double max_hours) { |
| 123 | 174 // Keep the input value within [min_hours, max_hours]. |
| 175 estimate_hours = std::max(min_hours, std::min(max_hours, estimate_hours)); | |
| 124 // Return |metric_value| such that GetEstimateHoursBetweenEvents for | 176 // Return |metric_value| such that GetEstimateHoursBetweenEvents for |
| 125 // |metric_value| returns |estimate_hours|. Thus, solve |metric_value| in | 177 // |metric_value| returns |estimate_hours|. Thus, solve |metric_value| in |
| 126 // metric_value = 1 + e^{-discount_rate * estimate_hours} * metric_value, | 178 // metric_value = 1 + e^{-discount_rate * estimate_hours} * metric_value, |
| 127 // i.e. | 179 // i.e. |
| 128 // metric_value * (1 - e^{-discount_rate * estimate_hours}) = 1, | 180 // metric_value * (1 - e^{-discount_rate * estimate_hours}) = 1, |
| 129 // metric_value = 1 / (1 - e^{-discount_rate * estimate_hours}). | 181 // metric_value = 1 / (1 - e^{-discount_rate * estimate_hours}). |
| 130 return 1.0 / (1.0 - std::exp(-discount_rate_per_hour * estimate_hours)); | 182 return 1.0 / (1.0 - std::exp(-discount_rate_per_hour * estimate_hours)); |
| 131 } | 183 } |
| 132 | 184 |
| 133 } // namespace | 185 } // namespace |
| 134 | 186 |
| 135 UserClassifier::UserClassifier(PrefService* pref_service) | 187 UserClassifier::UserClassifier(PrefService* pref_service) |
| 136 : pref_service_(pref_service), | 188 : pref_service_(pref_service), |
| 137 discount_rate_per_hour_(GetDiscountRatePerHour()) { | 189 discount_rate_per_hour_(GetDiscountRatePerHour()), |
| 190 min_hours_(GetMinHours()), | |
| 191 max_hours_(GetMaxHours()), | |
| 192 active_consumer_scrolls_at_least_once_per_hours_( | |
| 193 GetParamValue(kActiveConsumerScrollsAtLeastOncePerHoursParam, | |
| 194 kActiveConsumerScrollsAtLeastOncePerHours)), | |
| 195 rare_user_opens_ntp_at_most_once_per_hours_( | |
| 196 GetParamValue(kRareUserOpensNTPAtMostOncePerHoursParam, | |
| 197 kRareUserOpensNTPAtMostOncePerHours)) { | |
| 138 // The pref_service_ can be null in tests. | 198 // The pref_service_ can be null in tests. |
| 139 if (!pref_service_) | 199 if (!pref_service_) |
| 140 return; | 200 return; |
| 141 | 201 |
| 202 // TODO(jkrcal): Store the current discount rate per hour into prefs. If it | |
| 203 // differs from the previous value, rescale the metric values so that the | |
| 204 // expectation does not change abruptly! | |
| 205 | |
| 142 // Initialize the prefs storing the last time: the counter has just started! | 206 // Initialize the prefs storing the last time: the counter has just started! |
| 143 for (const Metric metric : kMetrics) { | 207 for (const Metric metric : kMetrics) { |
| 144 if (!HasLastTime(metric)) | 208 if (!HasLastTime(metric)) |
| 145 SetLastTimeToNow(metric); | 209 SetLastTimeToNow(metric); |
| 146 } | 210 } |
| 147 } | 211 } |
| 148 | 212 |
| 149 UserClassifier::~UserClassifier() {} | 213 UserClassifier::~UserClassifier() {} |
| 150 | 214 |
| 151 // static | 215 // static |
| 152 void UserClassifier::RegisterProfilePrefs(PrefRegistrySimple* registry) { | 216 void UserClassifier::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
| 217 double discount_rate = GetDiscountRatePerHour(); | |
| 218 double min_hours = GetMinHours(); | |
| 219 double max_hours = GetMaxHours(); | |
| 220 | |
| 153 for (Metric metric : kMetrics) { | 221 for (Metric metric : kMetrics) { |
| 154 double default_metric_value = GetMetricValueForEstimateHoursBetweenEvents( | 222 double default_metric_value = GetMetricValueForEstimateHoursBetweenEvents( |
| 155 kDefaults[static_cast<int>(metric)], GetDiscountRatePerHour()); | 223 GetInitialHoursBetweenEvents(metric), discount_rate, min_hours, |
| 224 max_hours); | |
| 156 registry->RegisterDoublePref(kMetricKeys[static_cast<int>(metric)], | 225 registry->RegisterDoublePref(kMetricKeys[static_cast<int>(metric)], |
| 157 default_metric_value); | 226 default_metric_value); |
| 158 registry->RegisterInt64Pref(kLastTimeKeys[static_cast<int>(metric)], 0); | 227 registry->RegisterInt64Pref(kLastTimeKeys[static_cast<int>(metric)], 0); |
| 159 } | 228 } |
| 160 } | 229 } |
| 161 | 230 |
| 162 void UserClassifier::OnEvent(Metric metric) { | 231 void UserClassifier::OnEvent(Metric metric) { |
| 163 DCHECK_NE(metric, Metric::COUNT); | 232 DCHECK_NE(metric, Metric::COUNT); |
| 164 double metric_value = UpdateMetricOnEvent(metric); | 233 double metric_value = UpdateMetricOnEvent(metric); |
| 165 | 234 |
| 166 double avg = | 235 double avg = GetEstimateHoursBetweenEvents( |
| 167 GetEstimateHoursBetweenEvents(metric_value, discount_rate_per_hour_); | 236 metric_value, discount_rate_per_hour_, min_hours_, max_hours_); |
| 237 // We use kMaxHours as the max value below as this cannot change on the fly. | |
|
Marc Treib
2016/09/22 13:36:09
s/this cannot change on the fly/the maximum value
jkrcal
2016/09/22 14:03:20
Done.
| |
| 168 switch (metric) { | 238 switch (metric) { |
| 169 case Metric::NTP_OPENED: | 239 case Metric::NTP_OPENED: |
| 170 UMA_HISTOGRAM_CUSTOM_COUNTS(kHistogramAverageHoursToOpenNTP, avg, 1, | 240 UMA_HISTOGRAM_CUSTOM_COUNTS(kHistogramAverageHoursToOpenNTP, avg, 1, |
| 171 kMaxHours, 50); | 241 kMaxHours, 50); |
| 172 break; | 242 break; |
| 173 case Metric::SUGGESTIONS_SHOWN: | 243 case Metric::SUGGESTIONS_SHOWN: |
| 174 UMA_HISTOGRAM_CUSTOM_COUNTS(kHistogramAverageHoursToShowSuggestions, avg, | 244 UMA_HISTOGRAM_CUSTOM_COUNTS(kHistogramAverageHoursToShowSuggestions, avg, |
| 175 1, kMaxHours, 50); | 245 1, kMaxHours, 50); |
| 176 break; | 246 break; |
| 177 case Metric::SUGGESTIONS_USED: | 247 case Metric::SUGGESTIONS_USED: |
| 178 UMA_HISTOGRAM_CUSTOM_COUNTS(kHistogramAverageHoursToUseSuggestions, avg, | 248 UMA_HISTOGRAM_CUSTOM_COUNTS(kHistogramAverageHoursToUseSuggestions, avg, |
| 179 1, kMaxHours, 50); | 249 1, kMaxHours, 50); |
| 180 break; | 250 break; |
| 181 case Metric::COUNT: | 251 case Metric::COUNT: |
| 182 NOTREACHED(); | 252 NOTREACHED(); |
| 183 break; | 253 break; |
| 184 } | 254 } |
| 185 } | 255 } |
| 186 | 256 |
| 187 double UserClassifier::GetEstimatedAvgTime(Metric metric) const { | 257 double UserClassifier::GetEstimatedAvgTime(Metric metric) const { |
| 188 DCHECK_NE(metric, Metric::COUNT); | 258 DCHECK_NE(metric, Metric::COUNT); |
| 189 double metric_value = GetUpToDateMetricValue(metric); | 259 double metric_value = GetUpToDateMetricValue(metric); |
| 190 return GetEstimateHoursBetweenEvents(metric_value, discount_rate_per_hour_); | 260 return GetEstimateHoursBetweenEvents(metric_value, discount_rate_per_hour_, |
| 261 min_hours_, max_hours_); | |
| 191 } | 262 } |
| 192 | 263 |
| 193 UserClassifier::UserClass UserClassifier::GetUserClass() const { | 264 UserClassifier::UserClass UserClassifier::GetUserClass() const { |
| 194 if (GetEstimatedAvgTime(Metric::NTP_OPENED) >= | 265 if (GetEstimatedAvgTime(Metric::NTP_OPENED) >= |
| 195 kOccasionalUserOpensNTPAtMostOncePerHours) { | 266 rare_user_opens_ntp_at_most_once_per_hours_) { |
| 196 return UserClass::RARE_NTP_USER; | 267 return UserClass::RARE_NTP_USER; |
| 197 } | 268 } |
| 198 | 269 |
| 199 if (GetEstimatedAvgTime(Metric::SUGGESTIONS_SHOWN) <= | 270 if (GetEstimatedAvgTime(Metric::SUGGESTIONS_SHOWN) <= |
| 200 kFrequentUserScrollsAtLeastOncePerHours) { | 271 active_consumer_scrolls_at_least_once_per_hours_) { |
| 201 return UserClass::ACTIVE_SUGGESTIONS_CONSUMER; | 272 return UserClass::ACTIVE_SUGGESTIONS_CONSUMER; |
| 202 } | 273 } |
| 203 | 274 |
| 204 return UserClass::ACTIVE_NTP_USER; | 275 return UserClass::ACTIVE_NTP_USER; |
| 205 } | 276 } |
| 206 | 277 |
| 207 std::string UserClassifier::GetUserClassDescriptionForDebugging() const { | 278 std::string UserClassifier::GetUserClassDescriptionForDebugging() const { |
| 208 switch (GetUserClass()) { | 279 switch (GetUserClass()) { |
| 209 case UserClass::RARE_NTP_USER: | 280 case UserClass::RARE_NTP_USER: |
| 210 return "Rare user of the NTP"; | 281 return "Rare user of the NTP"; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 227 SetLastTimeToNow(metric); | 298 SetLastTimeToNow(metric); |
| 228 } | 299 } |
| 229 } | 300 } |
| 230 | 301 |
| 231 double UserClassifier::UpdateMetricOnEvent(Metric metric) { | 302 double UserClassifier::UpdateMetricOnEvent(Metric metric) { |
| 232 // The pref_service_ can be null in tests. | 303 // The pref_service_ can be null in tests. |
| 233 if (!pref_service_) | 304 if (!pref_service_) |
| 234 return 0; | 305 return 0; |
| 235 | 306 |
| 236 double hours_since_last_time = | 307 double hours_since_last_time = |
| 237 std::min(kMaxHours, GetHoursSinceLastTime(metric)); | 308 std::min(max_hours_, GetHoursSinceLastTime(metric)); |
| 238 // Ignore events within the same "browsing session". | 309 // Ignore events within the same "browsing session". |
| 239 if (hours_since_last_time < kMinHours) | 310 if (hours_since_last_time < min_hours_) |
| 240 return GetUpToDateMetricValue(metric); | 311 return GetUpToDateMetricValue(metric); |
| 241 | 312 |
| 242 SetLastTimeToNow(metric); | 313 SetLastTimeToNow(metric); |
| 243 | 314 |
| 244 double metric_value = GetMetricValue(metric); | 315 double metric_value = GetMetricValue(metric); |
| 245 // Add 1 to the discounted metric as the event has happened right now. | 316 // Add 1 to the discounted metric as the event has happened right now. |
| 246 double new_metric_value = | 317 double new_metric_value = |
| 247 1 + DiscountMetric(metric_value, hours_since_last_time, | 318 1 + DiscountMetric(metric_value, hours_since_last_time, |
| 248 discount_rate_per_hour_); | 319 discount_rate_per_hour_); |
| 249 SetMetricValue(metric, new_metric_value); | 320 SetMetricValue(metric, new_metric_value); |
| 250 return new_metric_value; | 321 return new_metric_value; |
| 251 } | 322 } |
| 252 | 323 |
| 253 double UserClassifier::GetUpToDateMetricValue(Metric metric) const { | 324 double UserClassifier::GetUpToDateMetricValue(Metric metric) const { |
| 254 // The pref_service_ can be null in tests. | 325 // The pref_service_ can be null in tests. |
| 255 if (!pref_service_) | 326 if (!pref_service_) |
| 256 return 0; | 327 return 0; |
| 257 | 328 |
| 258 double hours_since_last_time = | 329 double hours_since_last_time = |
| 259 std::min(kMaxHours, GetHoursSinceLastTime(metric)); | 330 std::min(max_hours_, GetHoursSinceLastTime(metric)); |
| 260 | 331 |
| 261 double metric_value = GetMetricValue(metric); | 332 double metric_value = GetMetricValue(metric); |
| 262 return DiscountMetric(metric_value, hours_since_last_time, | 333 return DiscountMetric(metric_value, hours_since_last_time, |
| 263 discount_rate_per_hour_); | 334 discount_rate_per_hour_); |
| 264 } | 335 } |
| 265 | 336 |
| 266 double UserClassifier::GetHoursSinceLastTime(Metric metric) const { | 337 double UserClassifier::GetHoursSinceLastTime(Metric metric) const { |
| 267 if (!HasLastTime(metric)) | 338 if (!HasLastTime(metric)) |
| 268 return 0; | 339 return 0; |
| 269 | 340 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 288 | 359 |
| 289 void UserClassifier::SetMetricValue(Metric metric, double metric_value) { | 360 void UserClassifier::SetMetricValue(Metric metric, double metric_value) { |
| 290 pref_service_->SetDouble(kMetricKeys[static_cast<int>(metric)], metric_value); | 361 pref_service_->SetDouble(kMetricKeys[static_cast<int>(metric)], metric_value); |
| 291 } | 362 } |
| 292 | 363 |
| 293 void UserClassifier::ClearMetricValue(Metric metric) { | 364 void UserClassifier::ClearMetricValue(Metric metric) { |
| 294 pref_service_->ClearPref(kMetricKeys[static_cast<int>(metric)]); | 365 pref_service_->ClearPref(kMetricKeys[static_cast<int>(metric)]); |
| 295 } | 366 } |
| 296 | 367 |
| 297 } // namespace ntp_snippets | 368 } // namespace ntp_snippets |
| OLD | NEW |