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 "base/time/clock.h" |
14 #include "components/ntp_snippets/features.h" | 14 #include "components/ntp_snippets/features.h" |
15 #include "components/ntp_snippets/pref_names.h" | 15 #include "components/ntp_snippets/pref_names.h" |
16 #include "components/prefs/pref_registry_simple.h" | 16 #include "components/prefs/pref_registry_simple.h" |
17 #include "components/prefs/pref_service.h" | 17 #include "components/prefs/pref_service.h" |
18 #include "components/variations/variations_associated_data.h" | 18 #include "components/variations/variations_associated_data.h" |
19 | 19 |
20 namespace ntp_snippets { | 20 namespace ntp_snippets { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 // The discount rate for computing the discounted-average metrics. Must be | 24 // The discount rate 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 kDiscountRatePerDay = 0.25; | 26 const double kDiscountRatePerDay = 0.25; |
27 const char kDiscountRatePerDayParam[] = | 27 const char kDiscountRatePerDayParam[] = "user_classifier_discount_rate_per_day"; |
28 "user_classifier_discount_rate_per_day"; | |
29 | 28 |
30 // Never consider any larger interval than this (so that extreme situations such | 29 // Never consider any larger interval than this (so that extreme situations such |
31 // as losing your phone or going for a long offline vacation do not skew the | 30 // as losing your phone or going for a long offline vacation do not skew the |
32 // average too much). | 31 // average too much). |
33 // When everriding via variation parameters, it is better to use smaller values | 32 // 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. | 33 // than |kMaxHours| as this it the maximum value reported in the histograms. |
35 const double kMaxHours = 7 * 24; | 34 const double kMaxHours = 7 * 24; |
36 const char kMaxHoursParam[] = "user_classifier_max_hours"; | 35 const char kMaxHoursParam[] = "user_classifier_max_hours"; |
37 | 36 |
38 // Ignore events within |kMinHours| hours since the last event (|kMinHours| is | 37 // Ignore events within |kMinHours| hours since the last event (|kMinHours| is |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 if (hours_since_last_time < min_hours_) { | 323 if (hours_since_last_time < min_hours_) { |
325 return GetUpToDateMetricValue(metric); | 324 return GetUpToDateMetricValue(metric); |
326 } | 325 } |
327 | 326 |
328 SetLastTimeToNow(metric); | 327 SetLastTimeToNow(metric); |
329 | 328 |
330 double metric_value = GetMetricValue(metric); | 329 double metric_value = GetMetricValue(metric); |
331 // Add 1 to the discounted metric as the event has happened right now. | 330 // Add 1 to the discounted metric as the event has happened right now. |
332 double new_metric_value = | 331 double new_metric_value = |
333 1 + DiscountMetric(metric_value, hours_since_last_time, | 332 1 + DiscountMetric(metric_value, hours_since_last_time, |
334 discount_rate_per_hour_); | 333 discount_rate_per_hour_); |
335 SetMetricValue(metric, new_metric_value); | 334 SetMetricValue(metric, new_metric_value); |
336 return new_metric_value; | 335 return new_metric_value; |
337 } | 336 } |
338 | 337 |
339 double UserClassifier::GetUpToDateMetricValue(Metric metric) const { | 338 double UserClassifier::GetUpToDateMetricValue(Metric metric) const { |
340 // The pref_service_ can be null in tests. | 339 // The pref_service_ can be null in tests. |
341 if (!pref_service_) { | 340 if (!pref_service_) { |
342 return 0; | 341 return 0; |
343 } | 342 } |
344 | 343 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 | 375 |
377 void UserClassifier::SetMetricValue(Metric metric, double metric_value) { | 376 void UserClassifier::SetMetricValue(Metric metric, double metric_value) { |
378 pref_service_->SetDouble(kMetricKeys[static_cast<int>(metric)], metric_value); | 377 pref_service_->SetDouble(kMetricKeys[static_cast<int>(metric)], metric_value); |
379 } | 378 } |
380 | 379 |
381 void UserClassifier::ClearMetricValue(Metric metric) { | 380 void UserClassifier::ClearMetricValue(Metric metric) { |
382 pref_service_->ClearPref(kMetricKeys[static_cast<int>(metric)]); | 381 pref_service_->ClearPref(kMetricKeys[static_cast<int>(metric)]); |
383 } | 382 } |
384 | 383 |
385 } // namespace ntp_snippets | 384 } // namespace ntp_snippets |
OLD | NEW |