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

Side by Side 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, 8 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/metrics/data_use_tracker.h"
6
7 #include <string>
8
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/stringprintf.h"
11 #include "components/metrics/metrics_pref_names.h"
12 #include "components/prefs/scoped_user_pref_update.h"
13 #include "components/variations/variations_associated_data.h"
14
15 namespace metrics {
16
17 namespace {
18
19 // This function is for forwarding metrics usage pref changes to the appropriate
20 // callback on the appropriate thread.
21 void UpdateMetricsUsagePrefs(
22 const UpdateUsagePrefCallbackType& update_on_ui_callback,
23 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
24 const std::string& service_name,
25 int message_size) {
26 ui_task_runner->PostTask(
27 FROM_HERE, base::Bind(update_on_ui_callback, service_name, message_size));
28 }
29
30 } // namespace
31
32 DataUseTracker::DataUseTracker(PrefService* local_state)
33 : local_state_(local_state),
34 uma_quota_for_testing_(0),
35 uma_ratio_for_testing_(0),
36 weak_ptr_factory_(this) {}
37
38 DataUseTracker::~DataUseTracker() {}
39
40 // static
41 void DataUseTracker::RegisterPrefs(PrefRegistrySimple* registry) {
42 registry->RegisterDictionaryPref(metrics::prefs::kUserCellDataUse);
43 registry->RegisterDictionaryPref(metrics::prefs::kUmaCellDataUse);
44 }
45
46 UpdateUsagePrefCallbackType DataUseTracker::GetDataUseForwardingCallback(
47 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) {
48 DCHECK(ui_task_runner->RunsTasksOnCurrentThread());
49
50 return base::Bind(
51 &UpdateMetricsUsagePrefs,
52 base::Bind(&DataUseTracker::UpdateMetricsUsagePrefsOnUIThread,
53 weak_ptr_factory_.GetWeakPtr()),
54 ui_task_runner);
55 }
56
57 bool DataUseTracker::ShouldUploadLogOnCellular(int log_bytes) {
58 DCHECK(thread_checker_.CalledOnValidThread());
59
60 RemoveExpiredEntries();
61
62 int uma_weekly_quota_bytes;
63 if (!GetUmaWeeaklyQuota(&uma_weekly_quota_bytes))
64 return true;
65
66 int uma_total_data_use = ComputeTotalDataUse(prefs::kUmaCellDataUse);
67 int new_uma_total_data_use = log_bytes + uma_total_data_use;
68 // If the new log doesn't increase the total UMA traffic to be above the
69 // allowed quota then the log should be uploaded.
70 if (new_uma_total_data_use <= uma_weekly_quota_bytes)
71 return true;
72
73 double uma_ratio;
74 if (!GetUmaRatio(&uma_ratio))
75 return true;
76
77 int user_total_data_use = ComputeTotalDataUse(prefs::kUserCellDataUse);
78 // If after adding the new log the uma ratio is still under the allowed ratio
79 // then the log should be uploaded and vice versa.
80 return new_uma_total_data_use /
81 static_cast<double>(log_bytes + user_total_data_use) <=
82 uma_ratio;
83 }
84
85 void DataUseTracker::UpdateMetricsUsagePrefsOnUIThread(
86 const std::string& service_name,
87 int message_size) {
88 DCHECK(thread_checker_.CalledOnValidThread());
89
90 UpdateUsagePref(prefs::kUserCellDataUse, message_size);
91 if (service_name == "UMA")
92 UpdateUsagePref(prefs::kUmaCellDataUse, message_size);
93 }
94
95 void DataUseTracker::UpdateUsagePref(const std::string& pref_name,
96 int message_size) {
97 DCHECK(thread_checker_.CalledOnValidThread());
98
99 DictionaryPrefUpdate pref_updater(local_state_, pref_name);
100 int todays_traffic = 0;
101 std::string todays_key = GetCurrentMeasurementDateAsString();
102
103 const base::DictionaryValue* user_pref_dict =
104 local_state_->GetDictionary(pref_name);
105 // The pref should exists as long as they are properly registered.
106 if (user_pref_dict)
107 user_pref_dict->GetInteger(todays_key, &todays_traffic);
108
109 pref_updater->SetInteger(todays_key, todays_traffic + message_size);
110 }
111
112 void DataUseTracker::RemoveExpiredEntries() {
113 DCHECK(thread_checker_.CalledOnValidThread());
114 RemoveExpiredEntriesForPref(prefs::kUmaCellDataUse);
115 RemoveExpiredEntriesForPref(prefs::kUserCellDataUse);
116 }
117
118 void DataUseTracker::RemoveExpiredEntriesForPref(const std::string& pref_name) {
119 DCHECK(thread_checker_.CalledOnValidThread());
120
121 const base::DictionaryValue* user_pref_dict =
122 local_state_->GetDictionary(pref_name);
123 // The pref should exists as long as they are properly registered.
124 if (!user_pref_dict)
125 return;
126
127 const base::Time current_date = GetCurrentMeasurementDate();
128 const base::Time week_ago = current_date - base::TimeDelta::FromDays(7);
129
130 base::DictionaryValue user_pref_new_dict;
131 for (base::DictionaryValue::Iterator it(*user_pref_dict); !it.IsAtEnd();
132 it.Advance()) {
133 base::Time key_date;
134 base::Time::FromUTCString(it.key().c_str(), &key_date);
135 if (key_date > week_ago)
136 user_pref_new_dict.Set(it.key(), it.value().CreateDeepCopy());
137 }
138 local_state_->Set(pref_name, user_pref_new_dict);
139 }
140
141 int DataUseTracker::ComputeTotalDataUse(std::string pref_name) {
142 DCHECK(thread_checker_.CalledOnValidThread());
143
144 int total_data_use = 0;
145 const base::DictionaryValue* pref_dict =
146 local_state_->GetDictionary(pref_name);
147 if (!pref_dict)
148 return total_data_use;
149 for (base::DictionaryValue::Iterator it(*pref_dict); !it.IsAtEnd();
150 it.Advance()) {
151 int value = 0;
152 it.value().GetAsInteger(&value);
153 total_data_use += value;
154 }
155 return total_data_use;
156 }
157
158 bool DataUseTracker::GetUmaWeeaklyQuota(int* uma_weekly_quota_bytes) {
159 DCHECK(thread_checker_.CalledOnValidThread());
160
161 std::string param_value_str = variations::GetVariationParamValue(
162 "UMA_EnableCellularLogUpload", "Uma_Quota");
163 if (param_value_str.empty())
164 return false;
165
166 base::StringToInt(param_value_str, uma_weekly_quota_bytes);
167 return true;
168 }
169
170 bool DataUseTracker::GetUmaRatio(double* ratio) {
171 DCHECK(thread_checker_.CalledOnValidThread());
172
173 std::string param_value_str = variations::GetVariationParamValue(
174 "UMA_EnableCellularLogUpload", "Uma_Ratio");
175 if (param_value_str.empty())
176 return false;
177 base::StringToDouble(param_value_str, ratio);
178 return true;
179 }
180
181 base::Time DataUseTracker::GetCurrentMeasurementDate() {
182 return base::Time::Now().LocalMidnight();
183 }
184
185 std::string DataUseTracker::GetCurrentMeasurementDateAsString() {
186 DCHECK(thread_checker_.CalledOnValidThread());
187
188 base::Time::Exploded today_exploded;
189 GetCurrentMeasurementDate().LocalExplode(&today_exploded);
190 return base::StringPrintf("%04d-%02d-%02d", today_exploded.year,
191 today_exploded.month, today_exploded.day_of_month);
192 }
193
194 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698