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/remote/request_throttler.h"
6 
7 #include <climits>
8 #include <set>
9 #include <vector>
10 
11 #include "base/logging.h"
12 #include "base/metrics/histogram.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/time/time.h"
17 #include "components/ntp_snippets/features.h"
18 #include "components/ntp_snippets/ntp_snippets_constants.h"
19 #include "components/ntp_snippets/pref_names.h"
20 #include "components/prefs/pref_registry_simple.h"
21 #include "components/prefs/pref_service.h"
22 #include "components/variations/variations_associated_data.h"
23 
24 namespace ntp_snippets {
25 
26 namespace {
27 
28 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA
29 // histogram, so do not change existing values. Insert new values at the end,
30 // and update the histogram definition.
31 enum class RequestStatus {
32   INTERACTIVE_QUOTA_GRANTED,
33   BACKGROUND_QUOTA_GRANTED,
34   BACKGROUND_QUOTA_EXCEEDED,
35   INTERACTIVE_QUOTA_EXCEEDED,
36   REQUEST_STATUS_COUNT
37 };
38 
39 // Quota value to use if no quota should be applied (by default).
40 const int kUnlimitedQuota = INT_MAX;
41 
42 }  // namespace
43 
44 struct RequestThrottler::RequestTypeInfo {
45   const char* name;
46   const char* count_pref;
47   const char* interactive_count_pref;
48   const char* day_pref;
49   const int default_quota;
50   const int default_interactive_quota;
51 };
52 
53 // When adding a new type here, extend also the "RequestThrottlerTypes"
54 // <histogram_suffixes> in histograms.xml with the |name| string.
55 const RequestThrottler::RequestTypeInfo RequestThrottler::kRequestTypeInfo[] = {
56     // The following three types share the same prefs. They differ in quota
57     // values (and UMA histograms).
58     // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER_RARE_NTP_USER,
59     {"SuggestionFetcherRareNTPUser", prefs::kSnippetFetcherRequestCount,
60      prefs::kSnippetFetcherInteractiveRequestCount,
61      prefs::kSnippetFetcherRequestsDay, 5, kUnlimitedQuota},
62     // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER_ACTIVE_NTP_USER,
63     {"SuggestionFetcherActiveNTPUser", prefs::kSnippetFetcherRequestCount,
64      prefs::kSnippetFetcherInteractiveRequestCount,
65      prefs::kSnippetFetcherRequestsDay, 20, kUnlimitedQuota},
66     // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER_ACTIVE_SUGGESTIONS_CONSUMER,
67     {"SuggestionFetcherActiveSuggestionsConsumer",
68      prefs::kSnippetFetcherRequestCount,
69      prefs::kSnippetFetcherInteractiveRequestCount,
70      prefs::kSnippetFetcherRequestsDay, 20, kUnlimitedQuota},
71     // RequestCounter::RequestType::CONTENT_SUGGESTION_THUMBNAIL,
72     {"SuggestionThumbnailFetcher", prefs::kSnippetThumbnailsRequestCount,
73      prefs::kSnippetThumbnailsInteractiveRequestCount,
74      prefs::kSnippetThumbnailsRequestsDay, kUnlimitedQuota, kUnlimitedQuota}};
75 
RequestThrottler(PrefService * pref_service,RequestType type)76 RequestThrottler::RequestThrottler(PrefService* pref_service, RequestType type)
77     : pref_service_(pref_service),
78       type_info_(kRequestTypeInfo[static_cast<int>(type)]) {
79   DCHECK(pref_service);
80 
81   std::string quota = variations::GetVariationParamValueByFeature(
82       ntp_snippets::kArticleSuggestionsFeature,
83       base::StringPrintf("quota_%s", GetRequestTypeName()));
84   if (!base::StringToInt(quota, &quota_)) {
85     LOG_IF(WARNING, !quota.empty())
86         << "Invalid variation parameter for quota for " << GetRequestTypeName();
87     quota_ = type_info_.default_quota;
88   }
89 
90   std::string interactive_quota = variations::GetVariationParamValueByFeature(
91       ntp_snippets::kArticleSuggestionsFeature,
92       base::StringPrintf("interactive_quota_%s", GetRequestTypeName()));
93   if (!base::StringToInt(interactive_quota, &interactive_quota_)) {
94     LOG_IF(WARNING, !interactive_quota.empty())
95         << "Invalid variation parameter for interactive quota for "
96         << GetRequestTypeName();
97     interactive_quota_ = type_info_.default_interactive_quota;
98   }
99 
100   // Since the histogram names are dynamic, we cannot use the standard macros
101   // and we need to lookup the histograms, instead.
102   int status_count = static_cast<int>(RequestStatus::REQUEST_STATUS_COUNT);
103   // Corresponds to UMA_HISTOGRAM_ENUMERATION(name, sample, |status_count|).
104   histogram_request_status_ = base::LinearHistogram::FactoryGet(
105       base::StringPrintf("NewTabPage.RequestThrottler.RequestStatus_%s",
106                          GetRequestTypeName()),
107       1, status_count, status_count + 1,
108       base::HistogramBase::kUmaTargetedHistogramFlag);
109   // Corresponds to UMA_HISTOGRAM_COUNTS_100(name, sample).
110   histogram_per_day_background_ = base::Histogram::FactoryGet(
111       base::StringPrintf("NewTabPage.RequestThrottler.PerDay_%s",
112                          GetRequestTypeName()),
113       1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag);
114   // Corresponds to UMA_HISTOGRAM_COUNTS_100(name, sample).
115   histogram_per_day_interactive_ = base::Histogram::FactoryGet(
116       base::StringPrintf("NewTabPage.RequestThrottler.PerDayInteractive_%s",
117                          GetRequestTypeName()),
118       1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag);
119 }
120 
121 // static
RegisterProfilePrefs(PrefRegistrySimple * registry)122 void RequestThrottler::RegisterProfilePrefs(PrefRegistrySimple* registry) {
123   // Collect all pref keys in a set to make sure we register each key exactly
124   // once, even if they repeat.
125   std::set<std::string> keys_to_register;
126   for (const RequestTypeInfo& info : kRequestTypeInfo) {
127     keys_to_register.insert(info.day_pref);
128     keys_to_register.insert(info.count_pref);
129     keys_to_register.insert(info.interactive_count_pref);
130   }
131 
132   for (const std::string& key : keys_to_register) {
133     registry->RegisterIntegerPref(key, 0);
134   }
135 }
136 
DemandQuotaForRequest(bool interactive_request)137 bool RequestThrottler::DemandQuotaForRequest(bool interactive_request) {
138   ResetCounterIfDayChanged();
139 
140   int new_count = GetCount(interactive_request) + 1;
141   SetCount(interactive_request, new_count);
142   bool available = (new_count <= GetQuota(interactive_request));
143 
144   if (interactive_request) {
145     histogram_request_status_->Add(static_cast<int>(
146         available ? RequestStatus::INTERACTIVE_QUOTA_GRANTED
147                   : RequestStatus::INTERACTIVE_QUOTA_EXCEEDED));
148   } else {
149     histogram_request_status_->Add(
150         static_cast<int>(available ? RequestStatus::BACKGROUND_QUOTA_GRANTED
151                                    : RequestStatus::BACKGROUND_QUOTA_EXCEEDED));
152   }
153   return available;
154 }
155 
ResetCounterIfDayChanged()156 void RequestThrottler::ResetCounterIfDayChanged() {
157   // Get the date, "concatenated" into an int in "YYYYMMDD" format.
158   base::Time::Exploded now_exploded{};
159   base::Time::Now().LocalExplode(&now_exploded);
160   int now_day = 10000 * now_exploded.year + 100 * now_exploded.month +
161                 now_exploded.day_of_month;
162 
163   if (!HasDay()) {
164     // The counter is used for the first time in this profile.
165     SetDay(now_day);
166   } else if (now_day != GetDay()) {
167     // Day has changed - report the number of requests from the previous day.
168     histogram_per_day_background_->Add(GetCount(/*interactive_request=*/false));
169     histogram_per_day_interactive_->Add(GetCount(/*interactive_request=*/true));
170     // Reset the counters.
171     SetCount(/*interactive_request=*/false, 0);
172     SetCount(/*interactive_request=*/true, 0);
173     SetDay(now_day);
174   }
175 }
176 
GetRequestTypeName() const177 const char* RequestThrottler::GetRequestTypeName() const {
178   return type_info_.name;
179 }
180 
181 // TODO(jkrcal): turn RequestTypeInfo into a proper class, move those methods
182 // onto the class and hide the members.
GetQuota(bool interactive_request) const183 int RequestThrottler::GetQuota(bool interactive_request) const {
184   return interactive_request ? interactive_quota_ : quota_;
185 }
186 
GetCount(bool interactive_request) const187 int RequestThrottler::GetCount(bool interactive_request) const {
188   return pref_service_->GetInteger(interactive_request
189                                        ? type_info_.interactive_count_pref
190                                        : type_info_.count_pref);
191 }
192 
SetCount(bool interactive_request,int count)193 void RequestThrottler::SetCount(bool interactive_request, int count) {
194   pref_service_->SetInteger(interactive_request
195                                 ? type_info_.interactive_count_pref
196                                 : type_info_.count_pref,
197                             count);
198 }
199 
GetDay() const200 int RequestThrottler::GetDay() const {
201   return pref_service_->GetInteger(type_info_.day_pref);
202 }
203 
SetDay(int day)204 void RequestThrottler::SetDay(int day) {
205   pref_service_->SetInteger(type_info_.day_pref, day);
206 }
207 
HasDay() const208 bool RequestThrottler::HasDay() const {
209   return pref_service_->HasPrefPath(type_info_.day_pref);
210 }
211 
212 }  // namespace ntp_snippets
213