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

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 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 // Construct the histogram that is used for every request. The other histogram
Marc Treib 2016/07/20 12:58:27 s/Construct/Lookup
jkrcal 2016/07/20 14:20:17 Done.
62 // is used only once per day, construct it on the fly.
63 int status_count = static_cast<int>(RequestStatus::REQUEST_STATUS_COUNT);
64 histogram_request_status_ = base::LinearHistogram::FactoryGet(
65 base::StringPrintf(kUMAHistogramTotalFormat,
66 GetRequestTypeAsString().c_str()),
67 1, status_count, status_count + 1,
Marc Treib 2016/07/20 12:58:27 Is this correct? I think you're adding one more bi
jkrcal 2016/07/20 14:20:17 I emulate the behaviour of UMA_HISTOGRAM_ENUMERATI
Marc Treib 2016/07/20 15:13:57 Huh, indeed! I thought it was supposed to be "max"
68 base::HistogramBase::kUmaTargetedHistogramFlag);
69 }
70
71 // static
72 void RequestCounter::RegisterProfilePrefs(PrefRegistrySimple* registry) {
73 registry->RegisterIntegerPref(ntp_snippets::prefs::kSnippetFetcherQuotaDay,
74 0);
75 registry->RegisterIntegerPref(ntp_snippets::prefs::kSnippetFetcherQuotaCount,
76 0);
77 }
78
79 bool RequestCounter::DemandQuota() {
80 ResetCounterIfDayChanged();
81
82 std::string count_pref_key = GetCountPref();
83 int new_count = pref_service_->GetInteger(count_pref_key) + 1;
84 pref_service_->SetInteger(count_pref_key, new_count);
85
86 bool available = (new_count <= quota_);
87 histogram_request_status_->Add(
88 static_cast<int>(available ? RequestStatus::QUOTA_GRANTED
89 : RequestStatus::QUOTA_EXCEEDED));
90 return available;
91 }
92
93 void RequestCounter::ReportForcedRequest() const {
94 histogram_request_status_->Add(static_cast<int>(RequestStatus::FORCED));
95 }
96
97 void RequestCounter::ResetCounterIfDayChanged() {
98 // The count of days since a fixed reference (the Unix epoch).
99 int now_day = (base::Time::Now() - base::Time::UnixEpoch()).InDays();
100 std::string day_pref_key = GetDaysPref();
101
102 if (!pref_service_->HasPrefPath(day_pref_key)) {
103 // The counter starts from 0 for the first time in this profile.
104 pref_service_->SetInteger(day_pref_key, now_day);
105 } else if (now_day != pref_service_->GetInteger(day_pref_key)) {
106 // Day has changed - report the number of requests from the previous day.
107 base::HistogramBase* histogram_per_day_ = base::Histogram::FactoryGet(
108 base::StringPrintf(kUMAHistogramPerDayFormat,
109 GetRequestTypeAsString().c_str()),
110 1, 100, 50,
111 base::HistogramBase::kUmaTargetedHistogramFlag);
112
113 std::string count_pref_key = GetCountPref();
114 histogram_per_day_->Add(pref_service_->GetInteger(count_pref_key));
115 pref_service_->SetInteger(count_pref_key, 0);
116 pref_service_->SetInteger(day_pref_key, now_day);
117 }
118 }
119
120 std::string RequestCounter::GetRequestTypeAsString() const {
121 return kRequestTypeInfo[static_cast<int>(type_)].name;
122 }
123
124 std::string RequestCounter::GetCountPref() const {
125 return kRequestTypeInfo[static_cast<int>(type_)].count_pref;
126 }
127
128 std::string RequestCounter::GetDaysPref() const {
129 return kRequestTypeInfo[static_cast<int>(type_)].day_pref;
130 }
131
132 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698