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

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: fix param order 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 void UpdateMetricsUsagePrefs(
Alexei Svitkine (slow) 2016/03/31 20:00:39 Add a 1-line comment. Also, put empty lines withi
gayane -on leave until 09-2017 2016/03/31 21:55:09 Done.
19 const UpdateUsagePrefCallbackType& update_on_ui_callback,
20 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
21 const std::string& service_name,
22 int message_size) {
23 ui_task_runner->PostTask(
24 FROM_HERE, base::Bind(update_on_ui_callback, service_name, message_size));
25 }
26 } // namespace
27
28 DataUseTracker::DataUseTracker(PrefService* local_state)
29 : local_state_(local_state),
30 uma_quota_for_testing_(0),
31 uma_ratio_for_testing_(0),
32 weak_ptr_factory_(this) {}
33
34 DataUseTracker::~DataUseTracker() {}
35
36 // static
37 void DataUseTracker::RegisterPrefs(PrefRegistrySimple* registry) {
38 registry->RegisterDictionaryPref(metrics::prefs::kUserCellDataUse);
39 registry->RegisterDictionaryPref(metrics::prefs::kUmaCellDataUse);
40 }
41
42 UpdateUsagePrefCallbackType DataUseTracker::GetDataUseForwardingCallback(
43 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) {
44 DCHECK(ui_task_runner->RunsTasksOnCurrentThread());
45 return base::Bind(
46 &UpdateMetricsUsagePrefs,
47 base::Bind(&DataUseTracker::UpdateMetricsUsagePrefsOnUIThread,
48 weak_ptr_factory_.GetWeakPtr()),
49 ui_task_runner);
50 }
51
52 bool DataUseTracker::CanUploadUMALog(int log_bytes) {
53 RemoveExpiredEntries();
Alexei Svitkine (slow) 2016/03/31 20:00:39 Nit: Add a blank line below.
gayane -on leave until 09-2017 2016/03/31 21:55:09 Done.
54 int uma_total_data_use = ComputeTotalDataUse(prefs::kUmaCellDataUse);
55 int uma_quota;
56 bool is_quota_specified = GetUmaQuota(&uma_quota);
Alexei Svitkine (slow) 2016/03/31 20:00:39 Nit: Inline this into the if below and have a sepa
gayane -on leave until 09-2017 2016/03/31 21:55:09 I am not sure I got it right.
57 if (!is_quota_specified || log_bytes + uma_total_data_use <= uma_quota)
58 return true;
59
60 double uma_ratio;
61 bool is_ratio_specified = GetUmaRatio(&uma_ratio);
Alexei Svitkine (slow) 2016/03/31 20:00:39 Nit: Just inline this into the if below.
gayane -on leave until 09-2017 2016/03/31 21:55:09 Done.
62 if (!is_ratio_specified)
63 return true;
64
65 int user_total_data_use = ComputeTotalDataUse(prefs::kUserCellDataUse);
66 return (log_bytes + uma_total_data_use) /
67 (double)(log_bytes + user_total_data_use) <=
Alexei Svitkine (slow) 2016/03/31 20:00:39 static_cast<double>
gayane -on leave until 09-2017 2016/03/31 21:55:09 Done.
68 uma_ratio;
69 }
70
71 void DataUseTracker::UpdateMetricsUsagePrefsOnUIThread(
72 const std::string& service_name,
73 int message_size) {
Alexei Svitkine (slow) 2016/03/31 20:00:39 Add a ThreadChecker member in this class and DCHEC
gayane -on leave until 09-2017 2016/03/31 21:55:09 Done.
74 UpdateUsagePref(prefs::kUserCellDataUse, message_size);
75 if (service_name == "UMA")
76 UpdateUsagePref(prefs::kUmaCellDataUse, message_size);
77 }
78
79 void DataUseTracker::UpdateUsagePref(const std::string& pref_name,
80 int message_size) {
81 DictionaryPrefUpdate pref_updater(local_state_, pref_name);
82 int todays_traffic = 0;
83 std::string todays_key = GetCurrentMeasurementDate();
84 local_state_->GetDictionary(pref_name)->GetInteger(todays_key,
Alexei Svitkine (slow) 2016/03/31 20:00:39 Is there any chance local_state_->GetDictionary(pr
gayane -on leave until 09-2017 2016/03/31 21:55:09 added the check
85 &todays_traffic);
86 pref_updater->SetInteger(todays_key, todays_traffic + message_size);
87 }
88
89 void DataUseTracker::RemoveExpiredEntries() {
90 RemoveExpiredEntriesForPref(prefs::kUmaCellDataUse);
91 RemoveExpiredEntriesForPref(prefs::kUserCellDataUse);
92 }
93
94 void DataUseTracker::RemoveExpiredEntriesForPref(const std::string& pref_name) {
95 const base::DictionaryValue* user_pref_dict =
96 local_state_->GetDictionary(pref_name);
97 if (!user_pref_dict)
98 return;
99
100 base::DictionaryValue user_pref_new_dict;
101 for (base::DictionaryValue::Iterator it(*user_pref_dict); !it.IsAtEnd();
102 it.Advance()) {
103 base::Time current_date;
104 if (!date_for_testing_.empty())
105 base::Time::FromUTCString(date_for_testing_.c_str(), &current_date);
106 else
107 current_date = base::Time::Now().LocalMidnight();
108
109 base::Time week_ago = current_date - base::TimeDelta::FromDays(7);
110 base::Time key_date;
111 base::Time::FromUTCString(it.key().c_str(), &key_date);
112
113 if (key_date > week_ago)
Alexei Svitkine (slow) 2016/03/31 20:59:31 Can you just add this logic to ComputeTotalDataUse
gayane -on leave until 09-2017 2016/03/31 21:55:09 I guess I can do that, but then I need to name the
Alexei Svitkine (slow) 2016/03/31 22:18:02 Okay, keeping them separate for tests makes sense.
114 user_pref_new_dict.Set(it.key(), it.value().CreateDeepCopy());
115 }
116 local_state_->Set(pref_name, user_pref_new_dict);
117 }
118
119 std::string DataUseTracker::GetCurrentMeasurementDate() {
120 if (!date_for_testing_.empty())
121 return date_for_testing_;
122
123 base::Time::Exploded today_exploded;
124 base::Time::Now().LocalMidnight().LocalExplode(&today_exploded);
125 return base::StringPrintf("%04d-%02d-%02d", today_exploded.year,
126 today_exploded.month, today_exploded.day_of_month);
127 }
128
129 int DataUseTracker::ComputeTotalDataUse(std::string pref_name) {
130 int total_data_use = 0;
131 const base::DictionaryValue* pref_dict =
132 local_state_->GetDictionary(pref_name);
133 if (!pref_dict)
134 return total_data_use;
135 for (base::DictionaryValue::Iterator it(*pref_dict); !it.IsAtEnd();
136 it.Advance()) {
137 int value = 0;
138 it.value().GetAsInteger(&value);
139 total_data_use += value;
140 }
141 return total_data_use;
142 }
143
144 bool DataUseTracker::GetUmaQuota(int* quota) {
145 if (uma_quota_for_testing_ > 0) {
146 *quota = uma_quota_for_testing_;
147 return true;
148 }
149
150 std::string param_value_str = variations::GetVariationParamValue(
151 "UMA_EnableCellularLogUpload", "Uma_Quota");
152 if (param_value_str.empty())
153 return false;
154
155 base::StringToInt(param_value_str, quota);
156 return true;
157 }
158
159 bool DataUseTracker::GetUmaRatio(double* ratio) {
160 if (uma_ratio_for_testing_ > 0) {
161 *ratio = uma_ratio_for_testing_;
162 return true;
163 }
164
165 std::string param_value_str = variations::GetVariationParamValue(
166 "UMA_EnableCellularLogUpload", "Uma_Ratio");
167 if (param_value_str.empty())
168 return false;
169 base::StringToDouble(param_value_str, ratio);
170 return true;
171 }
172
173 void DataUseTracker::SetMeasurementDateForTesting(const std::string& date) {
Alexei Svitkine (slow) 2016/03/31 20:00:39 Instead of these setters, I suggest just making th
gayane -on leave until 09-2017 2016/03/31 21:55:09 Done.
174 date_for_testing_ = date;
175 }
176
177 void DataUseTracker::SetUmaQuotaForTesting(int uma_quota) {
178 uma_quota_for_testing_ = uma_quota;
179 }
180
181 void DataUseTracker::SetUmaRatioForTesting(double uma_ratio) {
182 uma_ratio_for_testing_ = uma_ratio;
183 }
184
185 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698