| 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. | 24 // The discount rate for computing the discounted-average metrics. Must be |
| 23 | |
| 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 kDiscountRatePerDay = 0.25; |
| 27 const char kDiscountRatePerDayParam[] = |
| 28 "user_classifier_discount_rate_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_rate_per_day = |
| 110 GetParamValue(kDiscountRatePerDayParam, kDiscountRatePerDay); |
| 111 // Check for illegal values. |
| 112 if (discount_rate_per_day <= 0 || discount_rate_per_day >= 1) { |
| 113 DLOG(WARNING) << "Illegal value " << discount_rate_per_day |
| 114 << " for the parameter " << kDiscountRatePerDayParam |
| 115 << " (must be strictly between 0 and 1; the default " |
| 116 << kDiscountRatePerDay << " is used, instead)."; |
| 117 discount_rate_per_day = kDiscountRatePerDay; |
| 118 } |
| 79 // Compute discount_rate_per_hour such that | 119 // Compute discount_rate_per_hour such that |
| 80 // kDiscountFactorPerDay = 1 - e^{-discount_rate_per_hour * 24}. | 120 // discount_rate_per_day = 1 - e^{-discount_rate_per_hour * 24}. |
| 81 return std::log(1.0 / (1.0 - kDiscountFactorPerDay)) / 24.0; | 121 return std::log(1.0 / (1.0 - discount_rate_per_day)) / 24.0; |
| 122 } |
| 123 |
| 124 double GetInitialHoursBetweenEvents(UserClassifier::Metric metric) { |
| 125 return GetParamValue( |
| 126 kInitialHoursBetweenEventsParams[static_cast<int>(metric)], |
| 127 kInitialHoursBetweenEvents[static_cast<int>(metric)]); |
| 128 } |
| 129 |
| 130 double GetMinHours() { |
| 131 return GetParamValue(kMinHoursParam, kMinHours); |
| 132 } |
| 133 |
| 134 double GetMaxHours() { |
| 135 return GetParamValue(kMaxHoursParam, kMaxHours); |
| 82 } | 136 } |
| 83 | 137 |
| 84 // Returns the new value of the metric using its |old_value|, assuming | 138 // 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. | 139 // |hours_since_last_time| hours have passed since it was last discounted. |
| 86 double DiscountMetric(double old_value, | 140 double DiscountMetric(double old_value, |
| 87 double hours_since_last_time, | 141 double hours_since_last_time, |
| 88 double discount_rate_per_hour) { | 142 double discount_rate_per_hour) { |
| 89 // Compute the new discounted average according to the formula | 143 // Compute the new discounted average according to the formula |
| 90 // avg_events := e^{-discount_rate_per_hour * hours_since} * avg_events | 144 // 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; | 145 return std::exp(-discount_rate_per_hour * hours_since_last_time) * old_value; |
| 92 } | 146 } |
| 93 | 147 |
| 94 // Compute the number of hours between two events for the given metric value | 148 // Compute the number of hours between two events for the given metric value |
| 95 // assuming the events were equally distributed. | 149 // assuming the events were equally distributed. |
| 96 double GetEstimateHoursBetweenEvents(double metric_value, | 150 double GetEstimateHoursBetweenEvents(double metric_value, |
| 97 double discount_rate_per_hour) { | 151 double discount_rate_per_hour, |
| 152 double min_hours, |
| 153 double max_hours) { |
| 98 // The computation below is well-defined only for |metric_value| > 1 (log of | 154 // 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 | 155 // negative value or division by zero). When |metric_value| -> 1, the estimate |
| 100 // below -> infinity, so kMaxHours is a natural result, here. | 156 // below -> infinity, so max_hours is a natural result, here. |
| 101 if (metric_value <= 1) | 157 if (metric_value <= 1) |
| 102 return kMaxHours; | 158 return max_hours; |
| 103 | 159 |
| 104 // This is the estimate with the assumption that last event happened right | 160 // 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 | 161 // now and the system is in the steady-state. Solve estimate_hours in the |
| 106 // steady-state equation: | 162 // steady-state equation: |
| 107 // metric_value = 1 + e^{-discount_rate * estimate_hours} * metric_value, | 163 // metric_value = 1 + e^{-discount_rate * estimate_hours} * metric_value, |
| 108 // i.e. | 164 // i.e. |
| 109 // -discount_rate * estimate_hours = log((metric_value - 1) / metric_value), | 165 // -discount_rate * estimate_hours = log((metric_value - 1) / metric_value), |
| 110 // discount_rate * estimate_hours = log(metric_value / (metric_value - 1)), | 166 // discount_rate * estimate_hours = log(metric_value / (metric_value - 1)), |
| 111 // estimate_hours = log(metric_value / (metric_value - 1)) / discount_rate. | 167 // estimate_hours = log(metric_value / (metric_value - 1)) / discount_rate. |
| 112 double estimate_hours = | 168 double estimate_hours = |
| 113 std::log(metric_value / (metric_value - 1)) / discount_rate_per_hour; | 169 std::log(metric_value / (metric_value - 1)) / discount_rate_per_hour; |
| 114 return std::max(kMinHours, std::min(kMaxHours, estimate_hours)); | 170 return std::max(min_hours, std::min(max_hours, estimate_hours)); |
| 115 } | 171 } |
| 116 | 172 |
| 117 // The inverse of GetEstimateHoursBetweenEvents(). | 173 // The inverse of GetEstimateHoursBetweenEvents(). |
| 118 double GetMetricValueForEstimateHoursBetweenEvents( | 174 double GetMetricValueForEstimateHoursBetweenEvents( |
| 119 double estimate_hours, | 175 double estimate_hours, |
| 120 double discount_rate_per_hour) { | 176 double discount_rate_per_hour, |
| 121 // Keep the input value within [kMinHours, kMaxHours]. | 177 double min_hours, |
| 122 estimate_hours = std::max(kMinHours, std::min(kMaxHours, estimate_hours)); | 178 double max_hours) { |
| 123 | 179 // Keep the input value within [min_hours, max_hours]. |
| 180 estimate_hours = std::max(min_hours, std::min(max_hours, estimate_hours)); |
| 124 // Return |metric_value| such that GetEstimateHoursBetweenEvents for | 181 // Return |metric_value| such that GetEstimateHoursBetweenEvents for |
| 125 // |metric_value| returns |estimate_hours|. Thus, solve |metric_value| in | 182 // |metric_value| returns |estimate_hours|. Thus, solve |metric_value| in |
| 126 // metric_value = 1 + e^{-discount_rate * estimate_hours} * metric_value, | 183 // metric_value = 1 + e^{-discount_rate * estimate_hours} * metric_value, |
| 127 // i.e. | 184 // i.e. |
| 128 // metric_value * (1 - e^{-discount_rate * estimate_hours}) = 1, | 185 // metric_value * (1 - e^{-discount_rate * estimate_hours}) = 1, |
| 129 // metric_value = 1 / (1 - e^{-discount_rate * estimate_hours}). | 186 // metric_value = 1 / (1 - e^{-discount_rate * estimate_hours}). |
| 130 return 1.0 / (1.0 - std::exp(-discount_rate_per_hour * estimate_hours)); | 187 return 1.0 / (1.0 - std::exp(-discount_rate_per_hour * estimate_hours)); |
| 131 } | 188 } |
| 132 | 189 |
| 133 } // namespace | 190 } // namespace |
| 134 | 191 |
| 135 UserClassifier::UserClassifier(PrefService* pref_service) | 192 UserClassifier::UserClassifier(PrefService* pref_service) |
| 136 : pref_service_(pref_service), | 193 : pref_service_(pref_service), |
| 137 discount_rate_per_hour_(GetDiscountRatePerHour()) { | 194 discount_rate_per_hour_(GetDiscountRatePerHour()), |
| 195 min_hours_(GetMinHours()), |
| 196 max_hours_(GetMaxHours()), |
| 197 active_consumer_scrolls_at_least_once_per_hours_( |
| 198 GetParamValue(kActiveConsumerScrollsAtLeastOncePerHoursParam, |
| 199 kActiveConsumerScrollsAtLeastOncePerHours)), |
| 200 rare_user_opens_ntp_at_most_once_per_hours_( |
| 201 GetParamValue(kRareUserOpensNTPAtMostOncePerHoursParam, |
| 202 kRareUserOpensNTPAtMostOncePerHours)) { |
| 138 // The pref_service_ can be null in tests. | 203 // The pref_service_ can be null in tests. |
| 139 if (!pref_service_) | 204 if (!pref_service_) |
| 140 return; | 205 return; |
| 141 | 206 |
| 207 // TODO(jkrcal): Store the current discount rate per hour into prefs. If it |
| 208 // differs from the previous value, rescale the metric values so that the |
| 209 // expectation does not change abruptly! |
| 210 |
| 142 // Initialize the prefs storing the last time: the counter has just started! | 211 // Initialize the prefs storing the last time: the counter has just started! |
| 143 for (const Metric metric : kMetrics) { | 212 for (const Metric metric : kMetrics) { |
| 144 if (!HasLastTime(metric)) | 213 if (!HasLastTime(metric)) |
| 145 SetLastTimeToNow(metric); | 214 SetLastTimeToNow(metric); |
| 146 } | 215 } |
| 147 } | 216 } |
| 148 | 217 |
| 149 UserClassifier::~UserClassifier() {} | 218 UserClassifier::~UserClassifier() {} |
| 150 | 219 |
| 151 // static | 220 // static |
| 152 void UserClassifier::RegisterProfilePrefs(PrefRegistrySimple* registry) { | 221 void UserClassifier::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
| 222 double discount_rate = GetDiscountRatePerHour(); |
| 223 double min_hours = GetMinHours(); |
| 224 double max_hours = GetMaxHours(); |
| 225 |
| 153 for (Metric metric : kMetrics) { | 226 for (Metric metric : kMetrics) { |
| 154 double default_metric_value = GetMetricValueForEstimateHoursBetweenEvents( | 227 double default_metric_value = GetMetricValueForEstimateHoursBetweenEvents( |
| 155 kDefaults[static_cast<int>(metric)], GetDiscountRatePerHour()); | 228 GetInitialHoursBetweenEvents(metric), discount_rate, min_hours, |
| 229 max_hours); |
| 156 registry->RegisterDoublePref(kMetricKeys[static_cast<int>(metric)], | 230 registry->RegisterDoublePref(kMetricKeys[static_cast<int>(metric)], |
| 157 default_metric_value); | 231 default_metric_value); |
| 158 registry->RegisterInt64Pref(kLastTimeKeys[static_cast<int>(metric)], 0); | 232 registry->RegisterInt64Pref(kLastTimeKeys[static_cast<int>(metric)], 0); |
| 159 } | 233 } |
| 160 } | 234 } |
| 161 | 235 |
| 162 void UserClassifier::OnEvent(Metric metric) { | 236 void UserClassifier::OnEvent(Metric metric) { |
| 163 DCHECK_NE(metric, Metric::COUNT); | 237 DCHECK_NE(metric, Metric::COUNT); |
| 164 double metric_value = UpdateMetricOnEvent(metric); | 238 double metric_value = UpdateMetricOnEvent(metric); |
| 165 | 239 |
| 166 double avg = | 240 double avg = GetEstimateHoursBetweenEvents( |
| 167 GetEstimateHoursBetweenEvents(metric_value, discount_rate_per_hour_); | 241 metric_value, discount_rate_per_hour_, min_hours_, max_hours_); |
| 242 // We use kMaxHours as the max value below as the maximum value for the |
| 243 // histograms must be constant. |
| 168 switch (metric) { | 244 switch (metric) { |
| 169 case Metric::NTP_OPENED: | 245 case Metric::NTP_OPENED: |
| 170 UMA_HISTOGRAM_CUSTOM_COUNTS(kHistogramAverageHoursToOpenNTP, avg, 1, | 246 UMA_HISTOGRAM_CUSTOM_COUNTS(kHistogramAverageHoursToOpenNTP, avg, 1, |
| 171 kMaxHours, 50); | 247 kMaxHours, 50); |
| 172 break; | 248 break; |
| 173 case Metric::SUGGESTIONS_SHOWN: | 249 case Metric::SUGGESTIONS_SHOWN: |
| 174 UMA_HISTOGRAM_CUSTOM_COUNTS(kHistogramAverageHoursToShowSuggestions, avg, | 250 UMA_HISTOGRAM_CUSTOM_COUNTS(kHistogramAverageHoursToShowSuggestions, avg, |
| 175 1, kMaxHours, 50); | 251 1, kMaxHours, 50); |
| 176 break; | 252 break; |
| 177 case Metric::SUGGESTIONS_USED: | 253 case Metric::SUGGESTIONS_USED: |
| 178 UMA_HISTOGRAM_CUSTOM_COUNTS(kHistogramAverageHoursToUseSuggestions, avg, | 254 UMA_HISTOGRAM_CUSTOM_COUNTS(kHistogramAverageHoursToUseSuggestions, avg, |
| 179 1, kMaxHours, 50); | 255 1, kMaxHours, 50); |
| 180 break; | 256 break; |
| 181 case Metric::COUNT: | 257 case Metric::COUNT: |
| 182 NOTREACHED(); | 258 NOTREACHED(); |
| 183 break; | 259 break; |
| 184 } | 260 } |
| 185 } | 261 } |
| 186 | 262 |
| 187 double UserClassifier::GetEstimatedAvgTime(Metric metric) const { | 263 double UserClassifier::GetEstimatedAvgTime(Metric metric) const { |
| 188 DCHECK_NE(metric, Metric::COUNT); | 264 DCHECK_NE(metric, Metric::COUNT); |
| 189 double metric_value = GetUpToDateMetricValue(metric); | 265 double metric_value = GetUpToDateMetricValue(metric); |
| 190 return GetEstimateHoursBetweenEvents(metric_value, discount_rate_per_hour_); | 266 return GetEstimateHoursBetweenEvents(metric_value, discount_rate_per_hour_, |
| 267 min_hours_, max_hours_); |
| 191 } | 268 } |
| 192 | 269 |
| 193 UserClassifier::UserClass UserClassifier::GetUserClass() const { | 270 UserClassifier::UserClass UserClassifier::GetUserClass() const { |
| 194 if (GetEstimatedAvgTime(Metric::NTP_OPENED) >= | 271 if (GetEstimatedAvgTime(Metric::NTP_OPENED) >= |
| 195 kOccasionalUserOpensNTPAtMostOncePerHours) { | 272 rare_user_opens_ntp_at_most_once_per_hours_) { |
| 196 return UserClass::RARE_NTP_USER; | 273 return UserClass::RARE_NTP_USER; |
| 197 } | 274 } |
| 198 | 275 |
| 199 if (GetEstimatedAvgTime(Metric::SUGGESTIONS_SHOWN) <= | 276 if (GetEstimatedAvgTime(Metric::SUGGESTIONS_SHOWN) <= |
| 200 kFrequentUserScrollsAtLeastOncePerHours) { | 277 active_consumer_scrolls_at_least_once_per_hours_) { |
| 201 return UserClass::ACTIVE_SUGGESTIONS_CONSUMER; | 278 return UserClass::ACTIVE_SUGGESTIONS_CONSUMER; |
| 202 } | 279 } |
| 203 | 280 |
| 204 return UserClass::ACTIVE_NTP_USER; | 281 return UserClass::ACTIVE_NTP_USER; |
| 205 } | 282 } |
| 206 | 283 |
| 207 std::string UserClassifier::GetUserClassDescriptionForDebugging() const { | 284 std::string UserClassifier::GetUserClassDescriptionForDebugging() const { |
| 208 switch (GetUserClass()) { | 285 switch (GetUserClass()) { |
| 209 case UserClass::RARE_NTP_USER: | 286 case UserClass::RARE_NTP_USER: |
| 210 return "Rare user of the NTP"; | 287 return "Rare user of the NTP"; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 227 SetLastTimeToNow(metric); | 304 SetLastTimeToNow(metric); |
| 228 } | 305 } |
| 229 } | 306 } |
| 230 | 307 |
| 231 double UserClassifier::UpdateMetricOnEvent(Metric metric) { | 308 double UserClassifier::UpdateMetricOnEvent(Metric metric) { |
| 232 // The pref_service_ can be null in tests. | 309 // The pref_service_ can be null in tests. |
| 233 if (!pref_service_) | 310 if (!pref_service_) |
| 234 return 0; | 311 return 0; |
| 235 | 312 |
| 236 double hours_since_last_time = | 313 double hours_since_last_time = |
| 237 std::min(kMaxHours, GetHoursSinceLastTime(metric)); | 314 std::min(max_hours_, GetHoursSinceLastTime(metric)); |
| 238 // Ignore events within the same "browsing session". | 315 // Ignore events within the same "browsing session". |
| 239 if (hours_since_last_time < kMinHours) | 316 if (hours_since_last_time < min_hours_) |
| 240 return GetUpToDateMetricValue(metric); | 317 return GetUpToDateMetricValue(metric); |
| 241 | 318 |
| 242 SetLastTimeToNow(metric); | 319 SetLastTimeToNow(metric); |
| 243 | 320 |
| 244 double metric_value = GetMetricValue(metric); | 321 double metric_value = GetMetricValue(metric); |
| 245 // Add 1 to the discounted metric as the event has happened right now. | 322 // Add 1 to the discounted metric as the event has happened right now. |
| 246 double new_metric_value = | 323 double new_metric_value = |
| 247 1 + DiscountMetric(metric_value, hours_since_last_time, | 324 1 + DiscountMetric(metric_value, hours_since_last_time, |
| 248 discount_rate_per_hour_); | 325 discount_rate_per_hour_); |
| 249 SetMetricValue(metric, new_metric_value); | 326 SetMetricValue(metric, new_metric_value); |
| 250 return new_metric_value; | 327 return new_metric_value; |
| 251 } | 328 } |
| 252 | 329 |
| 253 double UserClassifier::GetUpToDateMetricValue(Metric metric) const { | 330 double UserClassifier::GetUpToDateMetricValue(Metric metric) const { |
| 254 // The pref_service_ can be null in tests. | 331 // The pref_service_ can be null in tests. |
| 255 if (!pref_service_) | 332 if (!pref_service_) |
| 256 return 0; | 333 return 0; |
| 257 | 334 |
| 258 double hours_since_last_time = | 335 double hours_since_last_time = |
| 259 std::min(kMaxHours, GetHoursSinceLastTime(metric)); | 336 std::min(max_hours_, GetHoursSinceLastTime(metric)); |
| 260 | 337 |
| 261 double metric_value = GetMetricValue(metric); | 338 double metric_value = GetMetricValue(metric); |
| 262 return DiscountMetric(metric_value, hours_since_last_time, | 339 return DiscountMetric(metric_value, hours_since_last_time, |
| 263 discount_rate_per_hour_); | 340 discount_rate_per_hour_); |
| 264 } | 341 } |
| 265 | 342 |
| 266 double UserClassifier::GetHoursSinceLastTime(Metric metric) const { | 343 double UserClassifier::GetHoursSinceLastTime(Metric metric) const { |
| 267 if (!HasLastTime(metric)) | 344 if (!HasLastTime(metric)) |
| 268 return 0; | 345 return 0; |
| 269 | 346 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 288 | 365 |
| 289 void UserClassifier::SetMetricValue(Metric metric, double metric_value) { | 366 void UserClassifier::SetMetricValue(Metric metric, double metric_value) { |
| 290 pref_service_->SetDouble(kMetricKeys[static_cast<int>(metric)], metric_value); | 367 pref_service_->SetDouble(kMetricKeys[static_cast<int>(metric)], metric_value); |
| 291 } | 368 } |
| 292 | 369 |
| 293 void UserClassifier::ClearMetricValue(Metric metric) { | 370 void UserClassifier::ClearMetricValue(Metric metric) { |
| 294 pref_service_->ClearPref(kMetricKeys[static_cast<int>(metric)]); | 371 pref_service_->ClearPref(kMetricKeys[static_cast<int>(metric)]); |
| 295 } | 372 } |
| 296 | 373 |
| 297 } // namespace ntp_snippets | 374 } // namespace ntp_snippets |
| OLD | NEW |