Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(364)

Side by Side Diff: components/ntp_snippets/request_counter.cc

Issue 2158843002: Introduce a request throttler for limiting requests in mobile NTP. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: A minor fix #2 Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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[] =
24 "NewTabPage.RequestCounter.RequestStatus_%s";
25 const char kUMAHistogramPerDayFormat[] = "NewTabPage.RequestCounter.PerDay_%s";
26
27 struct RequestTypeInfo {
28 const char* name;
29 const char* count_pref;
30 const char* day_pref;
31 };
32
33 // When adding a new type here, extend also the "RequestCounterTypes"
34 // <histogram_suffixes> in histograms.xml with the |name| string. Register the
35 // prefs below in RegisterProfilePrefs as well.
36 using ntp_snippets::RequestCounter;
37 const RequestTypeInfo kRequestTypeInfo[] = {
38 // RequestCounter::RequestType::ARTICLE_CONTENT_FETCHER,
39 {"Fetcher", ntp_snippets::prefs::kSnippetFetcherQuotaCount,
40 ntp_snippets::prefs::kSnippetFetcherQuotaDay}};
41
42 } // namespace
43
44 namespace ntp_snippets {
45
46 RequestCounter::RequestCounter(PrefService* pref_service,
47 RequestType type,
48 int default_quota)
49 : pref_service_(pref_service), type_(type) {
50 DCHECK(pref_service);
51
52 std::string quota = variations::GetVariationParamValue(
53 ntp_snippets::kStudyName, kQuotaParamPrefix + GetRequestTypeAsString());
54 if (!base::StringToInt(quota, &quota_)) {
55 LOG_IF(WARNING, !quota.empty())
56 << "Invalid variation parameter for quota for "
57 << GetRequestTypeAsString();
58 quota_ = default_quota;
59 }
60
61 // Lookup the histograms.
62 int status_count = static_cast<int>(RequestStatus::REQUEST_STATUS_COUNT);
63 histogram_request_status_ = base::LinearHistogram::FactoryGet(
64 base::StringPrintf(kUMAHistogramTotalFormat,
65 GetRequestTypeAsString().c_str()),
66 1, status_count, status_count + 1,
67 base::HistogramBase::kUmaTargetedHistogramFlag);
68
69 histogram_per_day_ = base::Histogram::FactoryGet(
70 base::StringPrintf(kUMAHistogramPerDayFormat,
71 GetRequestTypeAsString().c_str()),
72 1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag);
73 }
74
75 // static
76 void RequestCounter::RegisterProfilePrefs(PrefRegistrySimple* registry) {
77 for (unsigned int i = 0; i < arraysize(kRequestTypeInfo); i++) {
Marc Treib 2016/07/20 15:13:58 I think you can use a range-based for loop: for (c
jkrcal 2016/07/20 15:46:23 Indeed! Is it a general C++(11) feature or is the
Marc Treib 2016/07/20 16:15:14 It's a C++11 thing, in particular non-member begin
jkrcal 2016/07/21 07:44:27 Thanks!
78 registry->RegisterIntegerPref(kRequestTypeInfo[i].count_pref, 0);
79 registry->RegisterIntegerPref(kRequestTypeInfo[i].day_pref, 0);
80 }
81 }
82
83 bool RequestCounter::DemandQuotaForRequest() {
84 ResetCounterIfDayChanged();
85
86 int new_count = GetCount() + 1;
87 SetCount(new_count);
88
89 bool available = (new_count <= quota_);
90 histogram_request_status_->Add(
91 static_cast<int>(available ? RequestStatus::QUOTA_GRANTED
92 : RequestStatus::QUOTA_EXCEEDED));
93 return available;
94 }
95
96 void RequestCounter::ReportForcedRequest() const {
97 histogram_request_status_->Add(static_cast<int>(RequestStatus::FORCED));
98 }
99
100 void RequestCounter::ResetCounterIfDayChanged() {
101 // The count of days since a fixed reference (the Unix epoch).
102 int now_day = (base::Time::Now() - base::Time::UnixEpoch()).InDays();
103
104 if (!HasDay()) {
105 // The counter is used for the first time in this profile.
106 SetDay(now_day);
107 } else if (now_day != GetDay()) {
108 // Day has changed - report the number of requests from the previous day.
109 histogram_per_day_->Add(GetCount());
110 // Reset the counter.
111 SetCount(0);
112 SetDay(now_day);
113 }
114 }
115
116 std::string RequestCounter::GetRequestTypeAsString() const {
117 return kRequestTypeInfo[static_cast<int>(type_)].name;
118 }
119
120 int RequestCounter::GetCount() const {
121 return pref_service_->GetInteger(
122 kRequestTypeInfo[static_cast<int>(type_)].count_pref);
123 }
124
125 void RequestCounter::SetCount(int count) {
126 pref_service_->SetInteger(
127 kRequestTypeInfo[static_cast<int>(type_)].count_pref, count);
128 }
129
130 int RequestCounter::GetDay() const {
131 return pref_service_->GetInteger(
132 kRequestTypeInfo[static_cast<int>(type_)].day_pref);
133 }
134
135 void RequestCounter::SetDay(int day) {
136 pref_service_->SetInteger(
137 kRequestTypeInfo[static_cast<int>(type_)].day_pref, day);
138 }
139
140 bool RequestCounter::HasDay() const {
141 return pref_service_->HasPrefPath(
142 kRequestTypeInfo[static_cast<int>(type_)].day_pref);
Marc Treib 2016/07/20 15:13:57 optional: You could store a reference to the prope
jkrcal 2016/07/20 15:46:23 Done.
143 }
144
145 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698