| 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <cfloat> | 8 #include <cfloat> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/time/clock.h" |
| 13 #include "components/ntp_snippets/features.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" |
| 17 #include "components/variations/variations_associated_data.h" | 18 #include "components/variations/variations_associated_data.h" |
| 18 | 19 |
| 19 namespace ntp_snippets { | 20 namespace ntp_snippets { |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 // |metric_value| returns |estimate_hours|. Thus, solve |metric_value| in | 175 // |metric_value| returns |estimate_hours|. Thus, solve |metric_value| in |
| 175 // metric_value = 1 + e^{-discount_rate * estimate_hours} * metric_value, | 176 // metric_value = 1 + e^{-discount_rate * estimate_hours} * metric_value, |
| 176 // i.e. | 177 // i.e. |
| 177 // metric_value * (1 - e^{-discount_rate * estimate_hours}) = 1, | 178 // metric_value * (1 - e^{-discount_rate * estimate_hours}) = 1, |
| 178 // metric_value = 1 / (1 - e^{-discount_rate * estimate_hours}). | 179 // metric_value = 1 / (1 - e^{-discount_rate * estimate_hours}). |
| 179 return 1.0 / (1.0 - std::exp(-discount_rate_per_hour * estimate_hours)); | 180 return 1.0 / (1.0 - std::exp(-discount_rate_per_hour * estimate_hours)); |
| 180 } | 181 } |
| 181 | 182 |
| 182 } // namespace | 183 } // namespace |
| 183 | 184 |
| 184 UserClassifier::UserClassifier(PrefService* pref_service) | 185 UserClassifier::UserClassifier(PrefService* pref_service, |
| 186 std::unique_ptr<base::Clock> clock) |
| 185 : pref_service_(pref_service), | 187 : pref_service_(pref_service), |
| 188 clock_(std::move(clock)), |
| 186 discount_rate_per_hour_(GetDiscountRatePerHour()), | 189 discount_rate_per_hour_(GetDiscountRatePerHour()), |
| 187 min_hours_(GetMinHours()), | 190 min_hours_(GetMinHours()), |
| 188 max_hours_(GetMaxHours()), | 191 max_hours_(GetMaxHours()), |
| 189 active_consumer_clicks_at_least_once_per_hours_( | 192 active_consumer_clicks_at_least_once_per_hours_( |
| 190 variations::GetVariationParamByFeatureAsDouble( | 193 variations::GetVariationParamByFeatureAsDouble( |
| 191 kArticleSuggestionsFeature, | 194 kArticleSuggestionsFeature, |
| 192 kActiveConsumerClicksAtLeastOncePerHoursParam, | 195 kActiveConsumerClicksAtLeastOncePerHoursParam, |
| 193 kActiveConsumerClicksAtLeastOncePerHours)), | 196 kActiveConsumerClicksAtLeastOncePerHours)), |
| 194 rare_user_opens_ntp_at_most_once_per_hours_( | 197 rare_user_opens_ntp_at_most_once_per_hours_( |
| 195 variations::GetVariationParamByFeatureAsDouble( | 198 variations::GetVariationParamByFeatureAsDouble( |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 return DiscountMetric(metric_value, hours_since_last_time, | 349 return DiscountMetric(metric_value, hours_since_last_time, |
| 347 discount_rate_per_hour_); | 350 discount_rate_per_hour_); |
| 348 } | 351 } |
| 349 | 352 |
| 350 double UserClassifier::GetHoursSinceLastTime(Metric metric) const { | 353 double UserClassifier::GetHoursSinceLastTime(Metric metric) const { |
| 351 if (!HasLastTime(metric)) { | 354 if (!HasLastTime(metric)) { |
| 352 return 0; | 355 return 0; |
| 353 } | 356 } |
| 354 | 357 |
| 355 base::TimeDelta since_last_time = | 358 base::TimeDelta since_last_time = |
| 356 base::Time::Now() - base::Time::FromInternalValue(pref_service_->GetInt64( | 359 clock_->Now() - base::Time::FromInternalValue(pref_service_->GetInt64( |
| 357 kLastTimeKeys[static_cast<int>(metric)])); | 360 kLastTimeKeys[static_cast<int>(metric)])); |
| 358 return since_last_time.InSecondsF() / 3600; | 361 return since_last_time.InSecondsF() / 3600; |
| 359 } | 362 } |
| 360 | 363 |
| 361 bool UserClassifier::HasLastTime(Metric metric) const { | 364 bool UserClassifier::HasLastTime(Metric metric) const { |
| 362 return pref_service_->HasPrefPath(kLastTimeKeys[static_cast<int>(metric)]); | 365 return pref_service_->HasPrefPath(kLastTimeKeys[static_cast<int>(metric)]); |
| 363 } | 366 } |
| 364 | 367 |
| 365 void UserClassifier::SetLastTimeToNow(Metric metric) { | 368 void UserClassifier::SetLastTimeToNow(Metric metric) { |
| 366 pref_service_->SetInt64(kLastTimeKeys[static_cast<int>(metric)], | 369 pref_service_->SetInt64(kLastTimeKeys[static_cast<int>(metric)], |
| 367 base::Time::Now().ToInternalValue()); | 370 clock_->Now().ToInternalValue()); |
| 368 } | 371 } |
| 369 | 372 |
| 370 double UserClassifier::GetMetricValue(Metric metric) const { | 373 double UserClassifier::GetMetricValue(Metric metric) const { |
| 371 return pref_service_->GetDouble(kMetricKeys[static_cast<int>(metric)]); | 374 return pref_service_->GetDouble(kMetricKeys[static_cast<int>(metric)]); |
| 372 } | 375 } |
| 373 | 376 |
| 374 void UserClassifier::SetMetricValue(Metric metric, double metric_value) { | 377 void UserClassifier::SetMetricValue(Metric metric, double metric_value) { |
| 375 pref_service_->SetDouble(kMetricKeys[static_cast<int>(metric)], metric_value); | 378 pref_service_->SetDouble(kMetricKeys[static_cast<int>(metric)], metric_value); |
| 376 } | 379 } |
| 377 | 380 |
| 378 void UserClassifier::ClearMetricValue(Metric metric) { | 381 void UserClassifier::ClearMetricValue(Metric metric) { |
| 379 pref_service_->ClearPref(kMetricKeys[static_cast<int>(metric)]); | 382 pref_service_->ClearPref(kMetricKeys[static_cast<int>(metric)]); |
| 380 } | 383 } |
| 381 | 384 |
| 382 } // namespace ntp_snippets | 385 } // namespace ntp_snippets |
| OLD | NEW |