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

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: Fix to compile unittests 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 ntp_snippets {
21
22 // Used internally for working with a RequestType.
23 struct RequestTypeInfo {
24 const char* name;
25 const char* count_pref;
26 const char* day_pref;
27 };
28
29 namespace {
30
31 const char kQuotaParamPrefix[] = "quota_";
32 const char kUMAHistogramRequestStatusFormat[] =
33 "NewTabPage.RequestCounter.RequestStatus_%s";
34 const char kUMAHistogramPerDayFormat[] = "NewTabPage.RequestCounter.PerDay_%s";
Alexei Svitkine (slow) 2016/07/21 14:13:29 Nit: If you're only using these once and in the sa
jkrcal 2016/07/21 18:17:57 Done.
35
36 // When adding a new type here, extend also the "RequestCounterTypes"
37 // <histogram_suffixes> in histograms.xml with the |name| string.
38 const RequestTypeInfo kRequestTypeInfo[] = {
39 // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER,
40 {"SuggestionFetcher", prefs::kSnippetFetcherQuotaCount,
41 prefs::kSnippetFetcherQuotaDay}};
42
43 } // namespace
44
45 RequestCounter::RequestCounter(PrefService* pref_service,
46 RequestType type,
47 int default_quota)
48 : pref_service_(pref_service),
49 type_info_(kRequestTypeInfo[static_cast<int>(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(
Alexei Svitkine (slow) 2016/07/21 14:13:29 For each of these, do you mind adding a comment ab
jkrcal 2016/07/21 18:17:57 Done.
64 base::StringPrintf(kUMAHistogramRequestStatusFormat,
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 (const RequestTypeInfo& info : kRequestTypeInfo) {
78 registry->RegisterIntegerPref(info.count_pref, 0);
79 registry->RegisterIntegerPref(info.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 type_info_.name;
118 }
119
120 int RequestCounter::GetCount() const {
121 return pref_service_->GetInteger(type_info_.count_pref);
122 }
123
124 void RequestCounter::SetCount(int count) {
125 pref_service_->SetInteger(type_info_.count_pref, count);
126 }
127
128 int RequestCounter::GetDay() const {
129 return pref_service_->GetInteger(type_info_.day_pref);
130 }
131
132 void RequestCounter::SetDay(int day) {
133 pref_service_->SetInteger(type_info_.day_pref, day);
134 }
135
136 bool RequestCounter::HasDay() const {
137 return pref_service_->HasPrefPath(type_info_.day_pref);
138 }
139
140 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698