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

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 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 (!GetUmaWeeklyQuota(&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 bool is_celllular) {
89 DCHECK(thread_checker_.CalledOnValidThread());
90
91 if (!is_celllular)
92 return;
93
94 UpdateUsagePref(prefs::kUserCellDataUse, message_size);
95 if (service_name == "UMA")
96 UpdateUsagePref(prefs::kUmaCellDataUse, message_size);
97 }
98
99 void DataUseTracker::UpdateUsagePref(const std::string& pref_name,
100 int message_size) {
101 DCHECK(thread_checker_.CalledOnValidThread());
102
103 DictionaryPrefUpdate pref_updater(local_state_, pref_name);
104 int todays_traffic = 0;
105 std::string todays_key = GetCurrentMeasurementDateAsString();
106
107 const base::DictionaryValue* user_pref_dict =
108 local_state_->GetDictionary(pref_name);
109 user_pref_dict->GetInteger(todays_key, &todays_traffic);
110 pref_updater->SetInteger(todays_key, todays_traffic + message_size);
111 }
112
113 void DataUseTracker::RemoveExpiredEntries() {
114 DCHECK(thread_checker_.CalledOnValidThread());
115 RemoveExpiredEntriesForPref(prefs::kUmaCellDataUse);
116 RemoveExpiredEntriesForPref(prefs::kUserCellDataUse);
117 }
118
119 void DataUseTracker::RemoveExpiredEntriesForPref(const std::string& pref_name) {
120 DCHECK(thread_checker_.CalledOnValidThread());
121
122 const base::DictionaryValue* user_pref_dict =
123 local_state_->GetDictionary(pref_name);
124 const base::Time current_date = GetCurrentMeasurementDate();
125 const base::Time week_ago = current_date - base::TimeDelta::FromDays(7);
126
127 base::DictionaryValue user_pref_new_dict;
128 for (base::DictionaryValue::Iterator it(*user_pref_dict); !it.IsAtEnd();
129 it.Advance()) {
130 base::Time key_date;
131 base::Time::FromUTCString(it.key().c_str(), &key_date);
132 if (key_date > week_ago)
133 user_pref_new_dict.Set(it.key(), it.value().CreateDeepCopy());
134 }
135 local_state_->Set(pref_name, user_pref_new_dict);
136 }
137
138 // Note: We compute total data use regardless of what is the current date. In
139 // scenario when user travels back in time zone and current date becomes earlier
140 // than latest registered date in perf, we still count that in total use as user
141 // actually used that data.
142 int DataUseTracker::ComputeTotalDataUse(const std::string& pref_name) {
143 DCHECK(thread_checker_.CalledOnValidThread());
144
145 int total_data_use = 0;
146 const base::DictionaryValue* pref_dict =
147 local_state_->GetDictionary(pref_name);
148 for (base::DictionaryValue::Iterator it(*pref_dict); !it.IsAtEnd();
149 it.Advance()) {
150 int value = 0;
151 it.value().GetAsInteger(&value);
152 total_data_use += value;
153 }
154 return total_data_use;
155 }
156
157 bool DataUseTracker::GetUmaWeeklyQuota(int* uma_weekly_quota_bytes) const {
158 DCHECK(thread_checker_.CalledOnValidThread());
159
160 std::string param_value_str = variations::GetVariationParamValue(
161 "UMA_EnableCellularLogUpload", "Uma_Quota");
162 if (param_value_str.empty())
163 return false;
164
165 base::StringToInt(param_value_str, uma_weekly_quota_bytes);
166 return true;
167 }
168
169 bool DataUseTracker::GetUmaRatio(double* ratio) const {
170 DCHECK(thread_checker_.CalledOnValidThread());
171
172 std::string param_value_str = variations::GetVariationParamValue(
173 "UMA_EnableCellularLogUpload", "Uma_Ratio");
174 if (param_value_str.empty())
175 return false;
176 base::StringToDouble(param_value_str, ratio);
177 return true;
178 }
179
180 base::Time DataUseTracker::GetCurrentMeasurementDate() const {
181 return base::Time::Now().LocalMidnight();
182 }
183
184 std::string DataUseTracker::GetCurrentMeasurementDateAsString() const {
185 DCHECK(thread_checker_.CalledOnValidThread());
186
187 base::Time::Exploded today_exploded;
188 GetCurrentMeasurementDate().LocalExplode(&today_exploded);
189 return base::StringPrintf("%04d-%02d-%02d", today_exploded.year,
190 today_exploded.month, today_exploded.day_of_month);
191 }
192
193 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698