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

Unified Diff: components/metrics/metrics_data_use_measurements.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/metrics_data_use_measurements.cc
diff --git a/components/metrics/metrics_data_use_measurements.cc b/components/metrics/metrics_data_use_measurements.cc
new file mode 100644
index 0000000000000000000000000000000000000000..cb0772209e033a4e7e16af791f7e2220e9e91c04
--- /dev/null
+++ b/components/metrics/metrics_data_use_measurements.cc
@@ -0,0 +1,125 @@
+#include "components/metrics/metrics_data_use_measurements.h"
+
+#include <string>
+
+#include "base/strings/stringprintf.h"
+#include "components/metrics/metrics_pref_names.h"
+#include "components/prefs/scoped_user_pref_update.h"
+#include "content/public/browser/browser_thread.h"
+
+namespace metrics{
+
+namespace {
+ static scoped_ptr<metrics::MetricsDataUseMeasurements> instance_;
+}
+
+//static
+void MetricsDataUseMeasurements::Initialize(PrefService* local_state) {
+ if (!instance_)
+ instance_.reset(new MetricsDataUseMeasurements(local_state));
+}
+
+//static
+MetricsDataUseMeasurements* MetricsDataUseMeasurements::GetInstance() {
+ return instance_.get();
+}
+
+MetricsDataUseMeasurements::MetricsDataUseMeasurements(PrefService* local_state)
+ :local_state_(local_state),
+ date_for_testing_(""),
Alexei Svitkine (slow) 2016/03/18 22:23:30 Nit: Can remove - std::string has a default ctor.
gayane -on leave until 09-2017 2016/03/29 16:06:20 Done.
+ self_ptr_factory_(this) {}
+
+MetricsDataUseMeasurements::~MetricsDataUseMeasurements(){}
+
+void MetricsDataUseMeasurements::SetMeasurementDateForTesting(std::string date) {
Alexei Svitkine (slow) 2016/03/18 22:23:30 const std::string&
gayane -on leave until 09-2017 2016/03/29 16:06:20 Done.
+ date_for_testing_ = date;
+}
+
+std::string MetricsDataUseMeasurements::GetCurrentMeasurementDate(){
+ if(date_for_testing_ != "")
Alexei Svitkine (slow) 2016/03/18 22:23:30 Nit: !.empty()
gayane -on leave until 09-2017 2016/03/29 16:06:20 Done.
+ return date_for_testing_;
+
+ base::Time::Exploded today_exploded;
+ base::Time::Now().LocalMidnight().LocalExplode(&today_exploded);
+ return base::StringPrintf("%04d-%02d-%02d", today_exploded.year, today_exploded.month, today_exploded.day_of_month);
+}
+
+void MetricsDataUseMeasurements::UpdateMetricsUsagePrefs(const std::string& service_name, int message_size) {
+ content::BrowserThread::PostTask(
+ content::BrowserThread::UI,
+ FROM_HERE,
+ base::Bind(&MetricsDataUseMeasurements::UpdateMetricsUsagePrefsOnUIThread, self_ptr_factory_.GetWeakPtr(), service_name, message_size));
+}
+
+void MetricsDataUseMeasurements::UpdateMetricsUsagePrefsOnUIThread(const std::string& service_name, int message_size) {
+ UpdateUsagePref(prefs::kUserCellDataUse, message_size);
+ if (service_name == "UMA")
+ UpdateUsagePref(prefs::kUmaCellDataUse, message_size);
+}
+
+void MetricsDataUseMeasurements::RemoveExpiredEntries() {
+ RemoveExpiredEntriesForPref(prefs::kUmaCellDataUse);
+ RemoveExpiredEntriesForPref(prefs::kUserCellDataUse);
+}
+
+void MetricsDataUseMeasurements::RemoveExpiredEntriesForPref(std::string pref_name) {
+ const base::DictionaryValue* user_pref_dict = local_state_->GetDictionary(pref_name);
+ if(!user_pref_dict)
+ return;
+ base::DictionaryValue user_pref_new_dict;
+ for (base::DictionaryValue::Iterator it(*user_pref_dict); !it.IsAtEnd();
+ it.Advance()) {
+ base::Time start_of_week = base::Time::Now().LocalMidnight() - base::TimeDelta::FromDays(7);
+ base::Time key_date;
+ base::Time::FromString(it.key().c_str(), &key_date);
+ if ( key_date >= start_of_week) {
+ user_pref_new_dict.Set(it.key(), it.value().CreateDeepCopy());
+ }
+ }
+ local_state_->Set(pref_name, user_pref_new_dict);
+}
+
+void MetricsDataUseMeasurements::UpdateUsagePref(const std::string& pref_name, int message_size) {
+ DictionaryPrefUpdate pref_updater(local_state_, pref_name);
+ int todays_traffic = 0;
+ std::string todays_key = GetCurrentMeasurementDate();
+ local_state_->GetDictionary(pref_name)->GetInteger(todays_key, &todays_traffic);
+ pref_updater->SetInteger(todays_key, todays_traffic + message_size);
+}
+
+bool MetricsDataUseMeasurements::CanUploadUMALog(int log_bytes) {
+ RemoveExpiredEntries();
+ int uma_total_data_use = TotalDataUse(prefs::kUmaCellDataUse);
+ if (log_bytes + uma_total_data_use <= GetUmaQouta())
+ return true;
+
+ int user_total_data_use = TotalDataUse(prefs::kUserCellDataUse);
+ return (log_bytes + uma_total_data_use)
+ /(double)(log_bytes + user_total_data_use)<= GetUmaRatio();
+}
+
+int MetricsDataUseMeasurements::TotalDataUse(std::string pref_name) {
Alexei Svitkine (slow) 2016/03/18 22:23:30 Nit: Compute*
gayane -on leave until 09-2017 2016/03/29 16:06:20 Done.
+ int total_data_use = 0;
+ const base::DictionaryValue* pref_dict = local_state_->GetDictionary(pref_name);
+ if(!pref_dict)
+ return total_data_use;
+ for (base::DictionaryValue::Iterator it(*pref_dict); !it.IsAtEnd();
Alexei Svitkine (slow) 2016/03/18 22:23:30 What's stopping this from considering data outside
gayane -on leave until 09-2017 2016/03/29 16:06:20 nothing. its just this function should be called a
+ it.Advance()) {
+ int value = 0;
+ it.value().GetAsInteger(&value);
+ total_data_use += value;
+ }
+ return total_data_use;
+}
+
+int MetricsDataUseMeasurements::GetUmaQouta() {
+ // TODO: read from experiment
+ return 200;
+}
+
+double MetricsDataUseMeasurements::GetUmaRatio() {
+ // TODO: read from experiment
+ return 0.05;
+}
+
+}// namespace metrics

Powered by Google App Engine
This is Rietveld 408576698