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

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 #3 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_macros.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.%s.Total";
24 const char kUMAHistogramPerUserFormat[] =
25 "NewTabPage.RequestCounter.%s.PerUser";
26
27 } // namespace
28
29 namespace ntp_snippets {
30
31 RequestCounter::RequestCounter(PrefService* pref_service,
32 const std::string& id,
33 int default_quota)
34 : pref_service_(pref_service), id_(id), counter_(-1) {
35 DCHECK(!id_.empty());
36
37 std::string quota = variations::GetVariationParamValue(
38 ntp_snippets::kStudyName, kQuotaParamPrefix + id_);
39 if (!base::StringToInt(quota, &quota_)) {
Marc Treib 2016/07/19 09:06:17 Maybe warn if the param is non-empty, but fails to
jkrcal 2016/07/20 09:40:40 Done.
Marc Treib 2016/07/20 10:19:05 This should be LOG, not DCHECK - getting invalid i
jkrcal 2016/07/20 12:09:27 Done.
40 quota_ = default_quota;
41 }
42
43 // Create the CamelCase version of |id_|, i.e. "sample_id" -> "SampleId"
44 std::vector<std::string> words = base::SplitString(
45 id_, "_", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
46 for (std::string& word : words) {
47 word[0] = toupper(word[0]);
48 id_camel_case_ += word;
49 }
50 }
51
52 // static
53 void RequestCounter::RegisterProfilePrefs(PrefRegistrySimple* registry,
54 const std::string& id) {
55 registry->RegisterIntegerPref(GetCountPref(id), 0);
56 registry->RegisterIntegerPref(GetDaysPref(id), 0);
57 }
58
59 bool RequestCounter::IsQuotaAvailable() {
60 ResetCounterIfDayChanged();
61
62 counter_++;
63 pref_service_->SetInteger(GetCountPref(id_), counter_);
64
65 bool available = (counter_ <= quota_);
66 UMA_HISTOGRAM_ENUMERATION(
67 GetTotalHistogramName(),
Marc Treib 2016/07/19 09:06:17 This doesn't work - the UMA_HISTOGRAM macros requi
jkrcal 2016/07/20 09:40:40 Done.
68 static_cast<int>(available ? RequestType::QUOTA_GRANTED
69 : RequestType::QUOTA_EXCEEDED),
70 static_cast<int>(RequestType::REQUEST_TYPE_MAX));
71
72 return available;
73 }
74
75 void RequestCounter::ReportForcedRequest() {
76 UMA_HISTOGRAM_ENUMERATION(GetTotalHistogramName(),
77 static_cast<int>(RequestType::FORCED),
78 static_cast<int>(RequestType::REQUEST_TYPE_MAX));
79 }
80
81 void RequestCounter::ResetCounterIfDayChanged() {
82 // Load the counter from pref, if not done yet. Loading is not done in the
83 // constructor to simplify code in unittests.
84 if (counter_ == -1)
85 counter_ = pref_service_->GetInteger(GetCountPref(id_));
Marc Treib 2016/07/19 09:06:17 I assume the problem is that the perf service is n
jkrcal 2016/07/20 09:40:39 Done.
86
87 int now_day =
88 base::TimeDelta::FromMilliseconds(base::Time::Now().ToJavaTime())
Marc Treib 2016/07/19 09:06:17 Why not just Now().InDays()?
jkrcal 2016/07/20 09:40:40 Found something that looks much better to me.
89 .InDays();
90 std::string day_pref_key = GetDaysPref(id_);
91
92 if (!pref_service_->HasPrefPath(day_pref_key)) {
93 // The counter starts from 0 for the first time in this profile.
94 pref_service_->SetInteger(day_pref_key, now_day);
95 } else if (now_day != pref_service_->GetInteger(day_pref_key)) {
96 // Day has changed - report the no of requests from the previous day.
97 UMA_HISTOGRAM_COUNTS_100(GetPerUserHistogramName(), counter_);
98 pref_service_->SetInteger(day_pref_key, now_day);
99 counter_ = 0;
100 }
101 }
102
103 std::string RequestCounter::GetTotalHistogramName() {
104 return base::StringPrintf(kUMAHistogramTotalFormat, id_camel_case_.c_str());
105 }
106
107 std::string RequestCounter::GetPerUserHistogramName() {
108 return base::StringPrintf(kUMAHistogramPerUserFormat, id_camel_case_.c_str());
109 }
110
111 // static
112 std::string RequestCounter::GetDaysPref(const std::string& id) {
113 return base::StringPrintf(
114 ntp_snippets::prefs::kSnippetQuotaDayFormat, id.c_str());
115 }
116
117 // static
118 std::string RequestCounter::GetCountPref(const std::string& id) {
119 return base::StringPrintf(
120 ntp_snippets::prefs::kSnippetQuotaCountFormat, id.c_str());
121 }
122
123 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698