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