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

Unified Diff: components/metrics/data_use_tracker.cc

Issue 1818613002: Implement UMA log throttling for cellular connections (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: components/metrics/data_use_tracker.cc
diff --git a/components/metrics/data_use_tracker.cc b/components/metrics/data_use_tracker.cc
new file mode 100644
index 0000000000000000000000000000000000000000..2ab89c751732fa1dbdecbff6791e593610fbb47e
--- /dev/null
+++ b/components/metrics/data_use_tracker.cc
@@ -0,0 +1,183 @@
+// 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/metrics/data_use_tracker.h"
+
+#include <string>
+
+#include "base/strings/string_number_conversions.h"
+#include "base/strings/stringprintf.h"
+#include "components/metrics/metrics_pref_names.h"
+#include "components/prefs/scoped_user_pref_update.h"
+#include "components/variations/variations_associated_data.h"
+
+namespace metrics {
+
+namespace {
+
+// This function is for forwarding metrics usage pref changes to the appropriate
+// callback on the appropriate thread.
+void UpdateMetricsUsagePrefs(
+ const UpdateUsagePrefCallbackType& update_on_ui_callback,
+ scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
+ const std::string& service_name,
+ int message_size) {
+ ui_task_runner->PostTask(
+ FROM_HERE, base::Bind(update_on_ui_callback, service_name, message_size));
+}
+
+} // namespace
+
+DataUseTracker::DataUseTracker(PrefService* local_state)
+ : local_state_(local_state),
+ weak_ptr_factory_(this) {}
+
+DataUseTracker::~DataUseTracker() {}
+
+// static
+void DataUseTracker::RegisterPrefs(PrefRegistrySimple* registry) {
+ registry->RegisterDictionaryPref(metrics::prefs::kUserCellDataUse);
+ registry->RegisterDictionaryPref(metrics::prefs::kUmaCellDataUse);
+}
+
+UpdateUsagePrefCallbackType DataUseTracker::GetDataUseForwardingCallback(
+ scoped_refptr<base::SequencedTaskRunner> ui_task_runner) {
+ DCHECK(ui_task_runner->RunsTasksOnCurrentThread());
+
+ return base::Bind(
+ &UpdateMetricsUsagePrefs,
+ base::Bind(&DataUseTracker::UpdateMetricsUsagePrefsOnUIThread,
+ weak_ptr_factory_.GetWeakPtr()),
+ ui_task_runner);
+}
+
+bool DataUseTracker::ShouldUploadLogOnCellular(int log_bytes) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ RemoveExpiredEntries();
+
+ int uma_weekly_quota_bytes;
+ if (!GetUmaWeeaklyQuota(&uma_weekly_quota_bytes))
+ return true;
+
+ int uma_total_data_use = ComputeTotalDataUse(prefs::kUmaCellDataUse);
+ int new_uma_total_data_use = log_bytes + uma_total_data_use;
+ // If the new log doesn't increase the total UMA traffic to be above the
+ // allowed quota then the log should be uploaded.
+ if (new_uma_total_data_use <= uma_weekly_quota_bytes)
+ return true;
+
+ double uma_ratio;
+ if (!GetUmaRatio(&uma_ratio))
+ return true;
+
+ int user_total_data_use = ComputeTotalDataUse(prefs::kUserCellDataUse);
+ // If after adding the new log the uma ratio is still under the allowed ratio
+ // then the log should be uploaded and vice versa.
+ return new_uma_total_data_use /
+ static_cast<double>(log_bytes + user_total_data_use) <=
+ uma_ratio;
+}
+
+void DataUseTracker::UpdateMetricsUsagePrefsOnUIThread(
+ const std::string& service_name,
+ int message_size) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ UpdateUsagePref(prefs::kUserCellDataUse, message_size);
+ if (service_name == "UMA")
+ UpdateUsagePref(prefs::kUmaCellDataUse, message_size);
mmenke 2016/04/04 17:26:23 Recording UMA and non-UMA data use through a metho
gayane -on leave until 09-2017 2016/04/05 14:26:46 For now we would like to manage to get to M51 whic
mmenke 2016/04/05 20:18:32 Not going to block on this, but my general feeling
+}
+
+void DataUseTracker::UpdateUsagePref(const std::string& pref_name,
+ int message_size) {
mmenke 2016/04/04 17:26:23 All this does is write to a pref from another thre
gayane -on leave until 09-2017 2016/04/05 14:26:46 For using PrefMember we can have pass PrefMember o
mmenke 2016/04/05 20:18:32 Could DataUseTracker::GetDataUseForwardingCallback
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ DictionaryPrefUpdate pref_updater(local_state_, pref_name);
+ int todays_traffic = 0;
+ std::string todays_key = GetCurrentMeasurementDateAsString();
+
+ const base::DictionaryValue* user_pref_dict =
+ local_state_->GetDictionary(pref_name);
+ user_pref_dict->GetInteger(todays_key, &todays_traffic);
+ pref_updater->SetInteger(todays_key, todays_traffic + message_size);
+}
+
+void DataUseTracker::RemoveExpiredEntries() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ RemoveExpiredEntriesForPref(prefs::kUmaCellDataUse);
+ RemoveExpiredEntriesForPref(prefs::kUserCellDataUse);
+}
+
+void DataUseTracker::RemoveExpiredEntriesForPref(const std::string& pref_name) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ const base::DictionaryValue* user_pref_dict =
+ local_state_->GetDictionary(pref_name);
+ const base::Time current_date = GetCurrentMeasurementDate();
+ const base::Time week_ago = current_date - base::TimeDelta::FromDays(7);
+
+ base::DictionaryValue user_pref_new_dict;
+ for (base::DictionaryValue::Iterator it(*user_pref_dict); !it.IsAtEnd();
+ it.Advance()) {
+ base::Time key_date;
+ base::Time::FromUTCString(it.key().c_str(), &key_date);
+ if (key_date > week_ago)
+ user_pref_new_dict.Set(it.key(), it.value().CreateDeepCopy());
+ }
+ local_state_->Set(pref_name, user_pref_new_dict);
mmenke 2016/04/04 17:26:23 DataReductionProxyCompressionStats has its own mag
gayane -on leave until 09-2017 2016/04/05 14:26:46 I had a quick look but It didn't seem to me simila
mmenke 2016/04/05 20:18:32 Right...which means things would have to move arou
+}
+
+int DataUseTracker::ComputeTotalDataUse(std::string pref_name) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ int total_data_use = 0;
+ const base::DictionaryValue* pref_dict =
+ local_state_->GetDictionary(pref_name);
+ for (base::DictionaryValue::Iterator it(*pref_dict); !it.IsAtEnd();
+ it.Advance()) {
+ int value = 0;
+ it.value().GetAsInteger(&value);
+ total_data_use += value;
+ }
+ return total_data_use;
+}
+
+bool DataUseTracker::GetUmaWeeaklyQuota(int* uma_weekly_quota_bytes) {
mmenke 2016/04/04 17:26:23 Weeakly -> Weekly
gayane -on leave until 09-2017 2016/04/05 14:26:46 Done.
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ std::string param_value_str = variations::GetVariationParamValue(
+ "UMA_EnableCellularLogUpload", "Uma_Quota");
+ if (param_value_str.empty())
+ return false;
+
+ base::StringToInt(param_value_str, uma_weekly_quota_bytes);
+ return true;
+}
+
+bool DataUseTracker::GetUmaRatio(double* ratio) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ std::string param_value_str = variations::GetVariationParamValue(
+ "UMA_EnableCellularLogUpload", "Uma_Ratio");
+ if (param_value_str.empty())
+ return false;
+ base::StringToDouble(param_value_str, ratio);
+ return true;
+}
+
+base::Time DataUseTracker::GetCurrentMeasurementDate() {
+ return base::Time::Now().LocalMidnight();
+}
+
+std::string DataUseTracker::GetCurrentMeasurementDateAsString() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ base::Time::Exploded today_exploded;
+ GetCurrentMeasurementDate().LocalExplode(&today_exploded);
+ return base::StringPrintf("%04d-%02d-%02d", today_exploded.year,
+ today_exploded.month, today_exploded.day_of_month);
+}
+
+} // namespace metrics

Powered by Google App Engine
This is Rietveld 408576698