OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/ntp_snippets/request_counter.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/metrics/histogram.h" | |
10 #include "base/strings/string_number_conversions.h" | |
11 #include "base/strings/string_split.h" | |
12 #include "base/strings/stringprintf.h" | |
13 #include "base/time/time.h" | |
14 #include "components/ntp_snippets/ntp_snippets_constants.h" | |
15 #include "components/ntp_snippets/pref_names.h" | |
16 #include "components/prefs/pref_registry_simple.h" | |
17 #include "components/prefs/pref_service.h" | |
18 #include "components/variations/variations_associated_data.h" | |
19 | |
20 namespace { | |
21 | |
22 const char kQuotaParamPrefix[] = "quota_"; | |
23 const char kUMAHistogramTotalFormat[] = "NewTabPage.RequestCounter.Total_%s"; | |
24 const char kUMAHistogramPerDayFormat[] = "NewTabPage.RequestCounter.PerDay_%s"; | |
25 | |
26 } // namespace | |
27 | |
28 namespace ntp_snippets { | |
29 | |
30 RequestCounter::RequestCounter(PrefService* pref_service, | |
31 RequestCounter::RequestType type, | |
Marc Treib
2016/07/20 10:19:05
nit: "RequestCounter::" not needed
jkrcal
2016/07/20 12:09:27
Done.
| |
32 int default_quota) | |
33 : pref_service_(pref_service), type_(type) { | |
34 DCHECK(pref_service); | |
35 | |
36 std::string quota = variations::GetVariationParamValue( | |
Marc Treib
2016/07/20 10:19:05
This should really be GetVariationParamValueByFeat
jkrcal
2016/07/20 12:09:27
This is exactly the reason. I do not think it is w
Marc Treib
2016/07/20 12:58:27
No, we should just move the feature definition int
jkrcal
2016/07/20 14:20:17
Okay, this would make sense.
| |
37 ntp_snippets::kStudyName, kQuotaParamPrefix + GetRequestTypeAsString()); | |
38 if (!base::StringToInt(quota, "a_)) { | |
39 DCHECK(quota.empty()) << "Invalid variation parameter for quota for " | |
40 << GetRequestTypeAsString(); | |
41 quota_ = default_quota; | |
42 } | |
43 | |
44 counter_ = pref_service_->GetInteger(GetCountPref()); | |
45 | |
46 int count = static_cast<int>(RequestStatus::REQUEST_STATUS_MAX); | |
47 histogram_total_ = base::LinearHistogram::FactoryGet( | |
48 base::StringPrintf(kUMAHistogramTotalFormat, | |
49 GetRequestTypeAsString().c_str()), | |
50 1, count, count + 1, base::HistogramBase::kUmaTargetedHistogramFlag); | |
51 } | |
52 | |
53 bool RequestCounter::GetQuota() { | |
54 ResetCounterIfDayChanged(); | |
55 | |
56 counter_++; | |
57 pref_service_->SetInteger(GetCountPref(), counter_); | |
58 | |
59 bool available = (counter_ <= quota_); | |
60 histogram_total_->Add( | |
61 static_cast<int>(available ? RequestStatus::QUOTA_GRANTED | |
62 : RequestStatus::QUOTA_EXCEEDED)); | |
63 return available; | |
64 } | |
65 | |
66 void RequestCounter::ReportForcedRequest() const { | |
67 histogram_total_->Add(static_cast<int>(RequestStatus::FORCED)); | |
68 } | |
69 | |
70 void RequestCounter::ResetCounterIfDayChanged() { | |
71 // The count of days since a fixed reference (the Unix epoch). | |
72 int now_day = (base::Time::Now() - base::Time::UnixEpoch()).InDays(); | |
73 DLOG(WARNING) << "Counter " << now_day; | |
Marc Treib
2016/07/20 10:19:05
This is too spammy - DVLOG?
jkrcal
2016/07/20 12:09:27
Oh, I am sorry, a debugging line leaked into the C
| |
74 std::string day_pref_key = GetDaysPref(); | |
75 | |
76 if (!pref_service_->HasPrefPath(day_pref_key)) { | |
77 // The counter starts from 0 for the first time in this profile. | |
78 pref_service_->SetInteger(day_pref_key, now_day); | |
79 } else if (now_day != pref_service_->GetInteger(day_pref_key)) { | |
80 // Day has changed - report the no of requests from the previous day. | |
Marc Treib
2016/07/20 10:19:05
s/no/number
jkrcal
2016/07/20 12:09:27
Done.
| |
81 base::HistogramBase* histogram_per_day_ = base::Histogram::FactoryGet( | |
82 base::StringPrintf(kUMAHistogramPerDayFormat, | |
83 GetRequestTypeAsString().c_str()), | |
84 1, 100, 50, | |
85 base::HistogramBase::kUmaTargetedHistogramFlag); | |
86 histogram_per_day_->Add(counter_); | |
87 | |
88 pref_service_->SetInteger(day_pref_key, now_day); | |
89 counter_ = 0; | |
Marc Treib
2016/07/20 10:19:05
Another argument for just having the pref as the s
jkrcal
2016/07/20 12:09:27
Not a bug, the value is set right after in GetQuot
| |
90 } | |
91 } | |
92 | |
93 std::string RequestCounter::GetRequestTypeAsString() const { | |
94 // When adding a new type here, extend also the "RequestCounterTypes" | |
95 // <histogram_suffixes> in histograms.xml. | |
96 switch (type_) { | |
Marc Treib
2016/07/20 10:19:05
Hm. Could we just have a global array mapping from
jkrcal
2016/07/20 12:09:27
Done.
| |
97 case RequestCounter::RequestType::ARTICLE_CONTENT_FETCHER: | |
98 return "Fetcher"; | |
99 default: | |
Marc Treib
2016/07/20 10:19:05
I would leave out the default case, then you shoul
jkrcal
2016/07/20 12:09:27
You are right. The code is not there, anymore.
| |
100 NOTREACHED(); | |
101 return ""; | |
102 } | |
103 } | |
104 | |
105 std::string RequestCounter::GetCountPref() const { | |
106 switch (type_) { | |
107 case RequestCounter::RequestType::ARTICLE_CONTENT_FETCHER: | |
108 return ntp_snippets::prefs::kSnippetFetcherQuotaCount; | |
109 default: | |
110 NOTREACHED(); | |
111 return ""; | |
112 } | |
113 } | |
114 | |
115 std::string RequestCounter::GetDaysPref() const { | |
116 switch (type_) { | |
117 case RequestCounter::RequestType::ARTICLE_CONTENT_FETCHER: | |
118 return ntp_snippets::prefs::kSnippetFetcherQuotaDay; | |
119 default: | |
120 NOTREACHED(); | |
121 return ""; | |
122 } | |
123 } | |
124 | |
125 // static | |
126 void RequestCounter::RegisterProfilePrefs(PrefRegistrySimple* registry) { | |
Marc Treib
2016/07/20 10:19:05
nit: move up, to match the order in the header
jkrcal
2016/07/20 12:09:27
Done.
| |
127 registry->RegisterIntegerPref(ntp_snippets::prefs::kSnippetFetcherQuotaDay, | |
128 0); | |
129 registry->RegisterIntegerPref(ntp_snippets::prefs::kSnippetFetcherQuotaCount, | |
130 0); | |
131 } | |
132 | |
133 } // namespace ntp_snippets | |
OLD | NEW |