Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #include "components/metrics/metrics_data_use_measurements.h" | |
| 2 | |
| 3 #include <string> | |
| 4 | |
| 5 #include "base/strings/stringprintf.h" | |
| 6 #include "components/metrics/metrics_pref_names.h" | |
| 7 #include "components/prefs/scoped_user_pref_update.h" | |
| 8 #include "content/public/browser/browser_thread.h" | |
| 9 | |
| 10 namespace metrics{ | |
| 11 | |
| 12 namespace { | |
| 13 static scoped_ptr<metrics::MetricsDataUseMeasurements> instance_; | |
| 14 } | |
| 15 | |
| 16 //static | |
| 17 void MetricsDataUseMeasurements::Initialize(PrefService* local_state) { | |
| 18 if (!instance_) | |
| 19 instance_.reset(new MetricsDataUseMeasurements(local_state)); | |
| 20 } | |
| 21 | |
| 22 //static | |
| 23 MetricsDataUseMeasurements* MetricsDataUseMeasurements::GetInstance() { | |
| 24 return instance_.get(); | |
| 25 } | |
| 26 | |
| 27 MetricsDataUseMeasurements::MetricsDataUseMeasurements(PrefService* local_state) | |
| 28 :local_state_(local_state), | |
| 29 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.
| |
| 30 self_ptr_factory_(this) {} | |
| 31 | |
| 32 MetricsDataUseMeasurements::~MetricsDataUseMeasurements(){} | |
| 33 | |
| 34 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.
| |
| 35 date_for_testing_ = date; | |
| 36 } | |
| 37 | |
| 38 std::string MetricsDataUseMeasurements::GetCurrentMeasurementDate(){ | |
| 39 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.
| |
| 40 return date_for_testing_; | |
| 41 | |
| 42 base::Time::Exploded today_exploded; | |
| 43 base::Time::Now().LocalMidnight().LocalExplode(&today_exploded); | |
| 44 return base::StringPrintf("%04d-%02d-%02d", today_exploded.year, today_explode d.month, today_exploded.day_of_month); | |
| 45 } | |
| 46 | |
| 47 void MetricsDataUseMeasurements::UpdateMetricsUsagePrefs(const std::string& serv ice_name, int message_size) { | |
| 48 content::BrowserThread::PostTask( | |
| 49 content::BrowserThread::UI, | |
| 50 FROM_HERE, | |
| 51 base::Bind(&MetricsDataUseMeasurements::UpdateMetricsUsagePrefsOnUIThread, self_ptr_factory_.GetWeakPtr(), service_name, message_size)); | |
| 52 } | |
| 53 | |
| 54 void MetricsDataUseMeasurements::UpdateMetricsUsagePrefsOnUIThread(const std::st ring& service_name, int message_size) { | |
| 55 UpdateUsagePref(prefs::kUserCellDataUse, message_size); | |
| 56 if (service_name == "UMA") | |
| 57 UpdateUsagePref(prefs::kUmaCellDataUse, message_size); | |
| 58 } | |
| 59 | |
| 60 void MetricsDataUseMeasurements::RemoveExpiredEntries() { | |
| 61 RemoveExpiredEntriesForPref(prefs::kUmaCellDataUse); | |
| 62 RemoveExpiredEntriesForPref(prefs::kUserCellDataUse); | |
| 63 } | |
| 64 | |
| 65 void MetricsDataUseMeasurements::RemoveExpiredEntriesForPref(std::string pref_na me) { | |
| 66 const base::DictionaryValue* user_pref_dict = local_state_->GetDictionary(pref _name); | |
| 67 if(!user_pref_dict) | |
| 68 return; | |
| 69 base::DictionaryValue user_pref_new_dict; | |
| 70 for (base::DictionaryValue::Iterator it(*user_pref_dict); !it.IsAtEnd(); | |
| 71 it.Advance()) { | |
| 72 base::Time start_of_week = base::Time::Now().LocalMidnight() - base::TimeDel ta::FromDays(7); | |
| 73 base::Time key_date; | |
| 74 base::Time::FromString(it.key().c_str(), &key_date); | |
| 75 if ( key_date >= start_of_week) { | |
| 76 user_pref_new_dict.Set(it.key(), it.value().CreateDeepCopy()); | |
| 77 } | |
| 78 } | |
| 79 local_state_->Set(pref_name, user_pref_new_dict); | |
| 80 } | |
| 81 | |
| 82 void MetricsDataUseMeasurements::UpdateUsagePref(const std::string& pref_name, i nt message_size) { | |
| 83 DictionaryPrefUpdate pref_updater(local_state_, pref_name); | |
| 84 int todays_traffic = 0; | |
| 85 std::string todays_key = GetCurrentMeasurementDate(); | |
| 86 local_state_->GetDictionary(pref_name)->GetInteger(todays_key, &todays_traffic ); | |
| 87 pref_updater->SetInteger(todays_key, todays_traffic + message_size); | |
| 88 } | |
| 89 | |
| 90 bool MetricsDataUseMeasurements::CanUploadUMALog(int log_bytes) { | |
| 91 RemoveExpiredEntries(); | |
| 92 int uma_total_data_use = TotalDataUse(prefs::kUmaCellDataUse); | |
| 93 if (log_bytes + uma_total_data_use <= GetUmaQouta()) | |
| 94 return true; | |
| 95 | |
| 96 int user_total_data_use = TotalDataUse(prefs::kUserCellDataUse); | |
| 97 return (log_bytes + uma_total_data_use) | |
| 98 /(double)(log_bytes + user_total_data_use)<= GetUmaRatio(); | |
| 99 } | |
| 100 | |
| 101 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.
| |
| 102 int total_data_use = 0; | |
| 103 const base::DictionaryValue* pref_dict = local_state_->GetDictionary(pref_name ); | |
| 104 if(!pref_dict) | |
| 105 return total_data_use; | |
| 106 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
| |
| 107 it.Advance()) { | |
| 108 int value = 0; | |
| 109 it.value().GetAsInteger(&value); | |
| 110 total_data_use += value; | |
| 111 } | |
| 112 return total_data_use; | |
| 113 } | |
| 114 | |
| 115 int MetricsDataUseMeasurements::GetUmaQouta() { | |
| 116 // TODO: read from experiment | |
| 117 return 200; | |
| 118 } | |
| 119 | |
| 120 double MetricsDataUseMeasurements::GetUmaRatio() { | |
| 121 // TODO: read from experiment | |
| 122 return 0.05; | |
| 123 } | |
| 124 | |
| 125 }// namespace metrics | |
| OLD | NEW |