Index: components/ntp_snippets/request_counter.cc |
diff --git a/components/ntp_snippets/request_counter.cc b/components/ntp_snippets/request_counter.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c8d259da600a7a1bed1e43b3f765f556313cfcee |
--- /dev/null |
+++ b/components/ntp_snippets/request_counter.cc |
@@ -0,0 +1,140 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/ntp_snippets/request_counter.h" |
+ |
+#include <vector> |
+ |
+#include "base/metrics/histogram.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/strings/string_split.h" |
+#include "base/strings/stringprintf.h" |
+#include "base/time/time.h" |
+#include "components/ntp_snippets/ntp_snippets_constants.h" |
+#include "components/ntp_snippets/pref_names.h" |
+#include "components/prefs/pref_registry_simple.h" |
+#include "components/prefs/pref_service.h" |
+#include "components/variations/variations_associated_data.h" |
+ |
+namespace ntp_snippets { |
+ |
+// Used internally for working with a RequestType. |
+struct RequestTypeInfo { |
+ const char* name; |
+ const char* count_pref; |
+ const char* day_pref; |
+}; |
+ |
+namespace { |
+ |
+const char kQuotaParamPrefix[] = "quota_"; |
+const char kUMAHistogramRequestStatusFormat[] = |
+ "NewTabPage.RequestCounter.RequestStatus_%s"; |
+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.
|
+ |
+// When adding a new type here, extend also the "RequestCounterTypes" |
+// <histogram_suffixes> in histograms.xml with the |name| string. |
+const RequestTypeInfo kRequestTypeInfo[] = { |
+ // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER, |
+ {"SuggestionFetcher", prefs::kSnippetFetcherQuotaCount, |
+ prefs::kSnippetFetcherQuotaDay}}; |
+ |
+} // namespace |
+ |
+RequestCounter::RequestCounter(PrefService* pref_service, |
+ RequestType type, |
+ int default_quota) |
+ : pref_service_(pref_service), |
+ type_info_(kRequestTypeInfo[static_cast<int>(type)]) { |
+ DCHECK(pref_service); |
+ |
+ std::string quota = variations::GetVariationParamValue( |
+ ntp_snippets::kStudyName, kQuotaParamPrefix + GetRequestTypeAsString()); |
+ if (!base::StringToInt(quota, "a_)) { |
+ LOG_IF(WARNING, !quota.empty()) |
+ << "Invalid variation parameter for quota for " |
+ << GetRequestTypeAsString(); |
+ quota_ = default_quota; |
+ } |
+ |
+ // Lookup the histograms. |
+ int status_count = static_cast<int>(RequestStatus::REQUEST_STATUS_COUNT); |
+ 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.
|
+ base::StringPrintf(kUMAHistogramRequestStatusFormat, |
+ GetRequestTypeAsString().c_str()), |
+ 1, status_count, status_count + 1, |
+ base::HistogramBase::kUmaTargetedHistogramFlag); |
+ |
+ histogram_per_day_ = base::Histogram::FactoryGet( |
+ base::StringPrintf(kUMAHistogramPerDayFormat, |
+ GetRequestTypeAsString().c_str()), |
+ 1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag); |
+} |
+ |
+// static |
+void RequestCounter::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
+ for (const RequestTypeInfo& info : kRequestTypeInfo) { |
+ registry->RegisterIntegerPref(info.count_pref, 0); |
+ registry->RegisterIntegerPref(info.day_pref, 0); |
+ } |
+} |
+ |
+bool RequestCounter::DemandQuotaForRequest() { |
+ ResetCounterIfDayChanged(); |
+ |
+ int new_count = GetCount() + 1; |
+ SetCount(new_count); |
+ |
+ bool available = (new_count <= quota_); |
+ histogram_request_status_->Add( |
+ static_cast<int>(available ? RequestStatus::QUOTA_GRANTED |
+ : RequestStatus::QUOTA_EXCEEDED)); |
+ return available; |
+} |
+ |
+void RequestCounter::ReportForcedRequest() const { |
+ histogram_request_status_->Add(static_cast<int>(RequestStatus::FORCED)); |
+} |
+ |
+void RequestCounter::ResetCounterIfDayChanged() { |
+ // The count of days since a fixed reference (the Unix epoch). |
+ int now_day = (base::Time::Now() - base::Time::UnixEpoch()).InDays(); |
+ |
+ if (!HasDay()) { |
+ // The counter is used for the first time in this profile. |
+ SetDay(now_day); |
+ } else if (now_day != GetDay()) { |
+ // Day has changed - report the number of requests from the previous day. |
+ histogram_per_day_->Add(GetCount()); |
+ // Reset the counter. |
+ SetCount(0); |
+ SetDay(now_day); |
+ } |
+} |
+ |
+std::string RequestCounter::GetRequestTypeAsString() const { |
+ return type_info_.name; |
+} |
+ |
+int RequestCounter::GetCount() const { |
+ return pref_service_->GetInteger(type_info_.count_pref); |
+} |
+ |
+void RequestCounter::SetCount(int count) { |
+ pref_service_->SetInteger(type_info_.count_pref, count); |
+} |
+ |
+int RequestCounter::GetDay() const { |
+ return pref_service_->GetInteger(type_info_.day_pref); |
+} |
+ |
+void RequestCounter::SetDay(int day) { |
+ pref_service_->SetInteger(type_info_.day_pref, day); |
+} |
+ |
+bool RequestCounter::HasDay() const { |
+ return pref_service_->HasPrefPath(type_info_.day_pref); |
+} |
+ |
+} // namespace ntp_snippets |