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

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 // TODO(gayane): Reduce the frequency of posting tasks from IO to UI thread.
22 void UpdateMetricsUsagePrefs(
23 const UpdateUsagePrefCallbackType& update_on_ui_callback,
24 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
25 const std::string& service_name,
26 int message_size,
27 bool is_cellular) {
28 ui_task_runner->PostTask(
29 FROM_HERE, base::Bind(update_on_ui_callback, service_name, message_size,
30 is_cellular));
31 }
32
33 } // namespace
34
35 DataUseTracker::DataUseTracker(PrefService* local_state)
36 : local_state_(local_state), weak_ptr_factory_(this) {}
37
38 DataUseTracker::~DataUseTracker() {}
39
40 // static
41 scoped_ptr<DataUseTracker> DataUseTracker::Create(PrefService* local_state) {
42 scoped_ptr<DataUseTracker> data_use_tracker;
43 if (variations::GetVariationParamValue("UMA_EnableCellularLogUpload",
44 "Enabled") == "true") {
Alexei Svitkine (slow) 2016/04/07 15:43:29 Should it be based on having Uma_Quota & Uma_Ratio
gayane -on leave until 09-2017 2016/04/07 18:53:49 Done.
45 data_use_tracker.reset(new DataUseTracker(local_state));
46 }
47 return data_use_tracker;
48 }
49
50 // static
51 void DataUseTracker::RegisterPrefs(PrefRegistrySimple* registry) {
52 registry->RegisterDictionaryPref(metrics::prefs::kUserCellDataUse);
53 registry->RegisterDictionaryPref(metrics::prefs::kUmaCellDataUse);
54 }
55
56 UpdateUsagePrefCallbackType DataUseTracker::GetDataUseForwardingCallback(
57 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) {
58 DCHECK(ui_task_runner->RunsTasksOnCurrentThread());
59
60 return base::Bind(
61 &UpdateMetricsUsagePrefs,
62 base::Bind(&DataUseTracker::UpdateMetricsUsagePrefsOnUIThread,
63 weak_ptr_factory_.GetWeakPtr()),
64 ui_task_runner);
65 }
66
67 bool DataUseTracker::ShouldUploadLogOnCellular(int log_bytes) {
68 DCHECK(thread_checker_.CalledOnValidThread());
69
70 RemoveExpiredEntries();
71
72 int uma_weekly_quota_bytes;
73 if (!GetUmaWeeklyQuota(&uma_weekly_quota_bytes))
74 return true;
75
76 int uma_total_data_use = ComputeTotalDataUse(prefs::kUmaCellDataUse);
77 int new_uma_total_data_use = log_bytes + uma_total_data_use;
78 // If the new log doesn't increase the total UMA traffic to be above the
79 // allowed quota then the log should be uploaded.
80 if (new_uma_total_data_use <= uma_weekly_quota_bytes)
81 return true;
82
83 double uma_ratio;
84 if (!GetUmaRatio(&uma_ratio))
85 return true;
86
87 int user_total_data_use = ComputeTotalDataUse(prefs::kUserCellDataUse);
88 // If after adding the new log the uma ratio is still under the allowed ratio
89 // then the log should be uploaded and vice versa.
90 return new_uma_total_data_use /
91 static_cast<double>(log_bytes + user_total_data_use) <=
92 uma_ratio;
93 }
94
95 void DataUseTracker::UpdateMetricsUsagePrefsOnUIThread(
96 const std::string& service_name,
97 int message_size,
98 bool is_celllular) {
99 DCHECK(thread_checker_.CalledOnValidThread());
100
101 if (!is_celllular)
102 return;
103
104 UpdateUsagePref(prefs::kUserCellDataUse, message_size);
105 if (service_name == "UMA")
106 UpdateUsagePref(prefs::kUmaCellDataUse, message_size);
107 }
108
109 void DataUseTracker::UpdateUsagePref(const std::string& pref_name,
110 int message_size) {
111 DCHECK(thread_checker_.CalledOnValidThread());
112
113 DictionaryPrefUpdate pref_updater(local_state_, pref_name);
114 int todays_traffic = 0;
115 std::string todays_key = GetCurrentMeasurementDateAsString();
116
117 const base::DictionaryValue* user_pref_dict =
118 local_state_->GetDictionary(pref_name);
119 user_pref_dict->GetInteger(todays_key, &todays_traffic);
120 pref_updater->SetInteger(todays_key, todays_traffic + message_size);
121 }
122
123 void DataUseTracker::RemoveExpiredEntries() {
124 DCHECK(thread_checker_.CalledOnValidThread());
125 RemoveExpiredEntriesForPref(prefs::kUmaCellDataUse);
126 RemoveExpiredEntriesForPref(prefs::kUserCellDataUse);
127 }
128
129 void DataUseTracker::RemoveExpiredEntriesForPref(const std::string& pref_name) {
130 DCHECK(thread_checker_.CalledOnValidThread());
131
132 const base::DictionaryValue* user_pref_dict =
133 local_state_->GetDictionary(pref_name);
134 const base::Time current_date = GetCurrentMeasurementDate();
135 const base::Time week_ago = current_date - base::TimeDelta::FromDays(7);
136
137 base::DictionaryValue user_pref_new_dict;
138 for (base::DictionaryValue::Iterator it(*user_pref_dict); !it.IsAtEnd();
139 it.Advance()) {
140 base::Time key_date;
141 base::Time::FromUTCString(it.key().c_str(), &key_date);
142 if (key_date > week_ago)
143 user_pref_new_dict.Set(it.key(), it.value().CreateDeepCopy());
144 }
145 local_state_->Set(pref_name, user_pref_new_dict);
146 }
147
148 // Note: We compute total data use regardless of what is the current date. In
149 // scenario when user travels back in time zone and current date becomes earlier
150 // than latest registered date in perf, we still count that in total use as user
151 // actually used that data.
152 int DataUseTracker::ComputeTotalDataUse(const std::string& pref_name) {
153 DCHECK(thread_checker_.CalledOnValidThread());
154
155 int total_data_use = 0;
156 const base::DictionaryValue* pref_dict =
157 local_state_->GetDictionary(pref_name);
158 for (base::DictionaryValue::Iterator it(*pref_dict); !it.IsAtEnd();
159 it.Advance()) {
160 int value = 0;
161 it.value().GetAsInteger(&value);
162 total_data_use += value;
163 }
164 return total_data_use;
165 }
166
167 bool DataUseTracker::GetUmaWeeklyQuota(int* uma_weekly_quota_bytes) const {
168 DCHECK(thread_checker_.CalledOnValidThread());
169
170 std::string param_value_str = variations::GetVariationParamValue(
171 "UMA_EnableCellularLogUpload", "Uma_Quota");
172 if (param_value_str.empty())
173 return false;
174
175 base::StringToInt(param_value_str, uma_weekly_quota_bytes);
176 return true;
177 }
178
179 bool DataUseTracker::GetUmaRatio(double* ratio) const {
180 DCHECK(thread_checker_.CalledOnValidThread());
181
182 std::string param_value_str = variations::GetVariationParamValue(
183 "UMA_EnableCellularLogUpload", "Uma_Ratio");
184 if (param_value_str.empty())
185 return false;
186 base::StringToDouble(param_value_str, ratio);
187 return true;
188 }
189
190 base::Time DataUseTracker::GetCurrentMeasurementDate() const {
191 return base::Time::Now().LocalMidnight();
192 }
193
194 std::string DataUseTracker::GetCurrentMeasurementDateAsString() const {
195 DCHECK(thread_checker_.CalledOnValidThread());
196
197 base::Time::Exploded today_exploded;
198 GetCurrentMeasurementDate().LocalExplode(&today_exploded);
199 return base::StringPrintf("%04d-%02d-%02d", today_exploded.year,
200 today_exploded.month, today_exploded.day_of_month);
201 }
202
203 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698