OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/rappor/rappor_service.h" | 5 #include "components/rappor/rappor_service.h" |
6 | 6 |
7 #include "base/metrics/field_trial.h" | 7 #include "base/metrics/field_trial.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
10 #include "components/metrics/metrics_hashes.h" | 10 #include "components/metrics/metrics_hashes.h" |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 bool success = reports.SerializeToString(&log_text); | 177 bool success = reports.SerializeToString(&log_text); |
178 DCHECK(success); | 178 DCHECK(success); |
179 DVLOG(1) << "RapporService sending a report of " | 179 DVLOG(1) << "RapporService sending a report of " |
180 << reports.report_size() << " value(s)."; | 180 << reports.report_size() << " value(s)."; |
181 uploader_->QueueLog(log_text); | 181 uploader_->QueueLog(log_text); |
182 } | 182 } |
183 ScheduleNextLogRotation(base::TimeDelta::FromSeconds(kLogIntervalSeconds)); | 183 ScheduleNextLogRotation(base::TimeDelta::FromSeconds(kLogIntervalSeconds)); |
184 } | 184 } |
185 | 185 |
186 bool RapporService::ExportMetrics(RapporReports* reports) { | 186 bool RapporService::ExportMetrics(RapporReports* reports) { |
187 if (metrics_map_.empty()) { | |
188 DVLOG(2) << "metrics_map_ is empty."; | |
189 return false; | |
190 } | |
191 | |
192 DCHECK_GE(cohort_, 0); | 187 DCHECK_GE(cohort_, 0); |
193 reports->set_cohort(cohort_); | 188 reports->set_cohort(cohort_); |
194 | 189 |
195 for (std::map<std::string, RapporMetric*>::const_iterator it = | 190 for (const auto& kv : metrics_map_) { |
196 metrics_map_.begin(); | 191 const RapporMetric* metric = kv.second; |
197 it != metrics_map_.end(); | |
198 ++it) { | |
199 const RapporMetric* metric = it->second; | |
200 RapporReports::Report* report = reports->add_report(); | 192 RapporReports::Report* report = reports->add_report(); |
201 report->set_name_hash(metrics::HashMetricName(it->first)); | 193 report->set_name_hash(metrics::HashMetricName(kv.first)); |
202 ByteVector bytes = metric->GetReport(secret_); | 194 ByteVector bytes = metric->GetReport(secret_); |
203 report->set_bits(std::string(bytes.begin(), bytes.end())); | 195 report->set_bits(std::string(bytes.begin(), bytes.end())); |
204 } | 196 } |
205 STLDeleteValues(&metrics_map_); | 197 STLDeleteValues(&metrics_map_); |
206 return true; | 198 |
| 199 sampler_.ExportMetrics(secret_, reports); |
| 200 |
| 201 DVLOG(2) << "Generated a report with " << reports->report_size() |
| 202 << "metrics."; |
| 203 return reports->report_size() > 0; |
207 } | 204 } |
208 | 205 |
209 bool RapporService::IsInitialized() const { | 206 bool RapporService::IsInitialized() const { |
210 return cohort_ >= 0; | 207 return cohort_ >= 0; |
211 } | 208 } |
212 | 209 |
213 void RapporService::RecordSample(const std::string& metric_name, | 210 void RapporService::RecordSample(const std::string& metric_name, |
214 RapporType type, | 211 RapporType type, |
215 const std::string& sample) { | 212 const std::string& sample) { |
216 // Ignore the sample if the service hasn't started yet. | 213 // Ignore the sample if the service hasn't started yet. |
217 if (!IsInitialized()) | 214 if (!IsInitialized()) |
218 return; | 215 return; |
219 DCHECK_LT(type, NUM_RAPPOR_TYPES); | 216 DCHECK_LT(type, NUM_RAPPOR_TYPES); |
220 const RapporParameters& parameters = kRapporParametersForType[type]; | 217 const RapporParameters& parameters = kRapporParametersForType[type]; |
221 DVLOG(2) << "Recording sample \"" << sample | 218 DVLOG(2) << "Recording sample \"" << sample |
222 << "\" for metric \"" << metric_name | 219 << "\" for metric \"" << metric_name |
223 << "\" of type: " << type; | 220 << "\" of type: " << type; |
224 RecordSampleInternal(metric_name, parameters, sample); | 221 RecordSampleInternal(metric_name, parameters, sample); |
225 } | 222 } |
226 | 223 |
227 void RapporService::RecordSampleInternal(const std::string& metric_name, | 224 void RapporService::RecordSampleInternal(const std::string& metric_name, |
228 const RapporParameters& parameters, | 225 const RapporParameters& parameters, |
229 const std::string& sample) { | 226 const std::string& sample) { |
230 DCHECK(IsInitialized()); | 227 DCHECK(IsInitialized()); |
231 if (is_incognito_callback_.Run()) { | 228 if (is_incognito_callback_.Run()) { |
232 DVLOG(2) << "Metric not logged due to incognito mode."; | 229 DVLOG(2) << "Metric not logged due to incognito mode."; |
233 return; | 230 return; |
234 } | 231 } |
235 // Skip this metric if it's reporting level is less than the enabled | 232 // Skip this metric if its reporting level is less than the enabled |
236 // reporting level. | 233 // reporting level. |
237 if (recording_level_ < parameters.recording_level) { | 234 if (recording_level_ < parameters.recording_level) { |
238 DVLOG(2) << "Metric not logged due to recording_level " | 235 DVLOG(2) << "Metric not logged due to recording_level " |
239 << recording_level_ << " < " << parameters.recording_level; | 236 << recording_level_ << " < " << parameters.recording_level; |
240 return; | 237 return; |
241 } | 238 } |
242 RapporMetric* metric = LookUpMetric(metric_name, parameters); | 239 RapporMetric* metric = LookUpMetric(metric_name, parameters); |
243 metric->AddSample(sample); | 240 metric->AddSample(sample); |
244 } | 241 } |
245 | 242 |
246 RapporMetric* RapporService::LookUpMetric(const std::string& metric_name, | 243 RapporMetric* RapporService::LookUpMetric(const std::string& metric_name, |
247 const RapporParameters& parameters) { | 244 const RapporParameters& parameters) { |
248 DCHECK(IsInitialized()); | 245 DCHECK(IsInitialized()); |
249 std::map<std::string, RapporMetric*>::const_iterator it = | 246 std::map<std::string, RapporMetric*>::const_iterator it = |
250 metrics_map_.find(metric_name); | 247 metrics_map_.find(metric_name); |
251 if (it != metrics_map_.end()) { | 248 if (it != metrics_map_.end()) { |
252 RapporMetric* metric = it->second; | 249 RapporMetric* metric = it->second; |
253 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); | 250 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); |
254 return metric; | 251 return metric; |
255 } | 252 } |
256 | 253 |
257 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); | 254 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); |
258 metrics_map_[metric_name] = new_metric; | 255 metrics_map_[metric_name] = new_metric; |
259 return new_metric; | 256 return new_metric; |
260 } | 257 } |
261 | 258 |
| 259 scoped_ptr<Sample> RapporService::CreateSample(RapporType type) { |
| 260 DCHECK(IsInitialized()); |
| 261 return scoped_ptr<Sample>( |
| 262 new Sample(cohort_, kRapporParametersForType[type])); |
| 263 } |
| 264 |
| 265 void RapporService::RecordSampleObj(const std::string& metric_name, |
| 266 scoped_ptr<Sample> sample) { |
| 267 if (is_incognito_callback_.Run()) { |
| 268 DVLOG(2) << "Metric not logged due to incognito mode."; |
| 269 return; |
| 270 } |
| 271 // Skip this metric if its reporting level is less than the enabled |
| 272 // reporting level. |
| 273 if (recording_level_ < sample->parameters().recording_level) { |
| 274 DVLOG(2) << "Metric not logged due to recording_level " |
| 275 << recording_level_ << " < " |
| 276 << sample->parameters().recording_level; |
| 277 return; |
| 278 } |
| 279 sampler_.AddSample(metric_name, sample.Pass()); |
| 280 } |
| 281 |
262 } // namespace rappor | 282 } // namespace rappor |
OLD | NEW |