Update mojo sdk to rev c02a28868825edfa57ab77947b8cb15e741c5598
[chromium-blink-merge.git] / components / rappor / sampler.cc
blob5d3e8e3c0e6c08471dcae07b414b674b3ed60e8a
1 // Copyright 2015 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.
5 #include "components/rappor/sampler.h"
7 #include <map>
8 #include <string>
10 #include "base/rand_util.h"
12 namespace rappor {
14 namespace internal {
16 Sampler::Sampler() {}
18 Sampler::~Sampler() {}
20 void Sampler::AddSample(const std::string& metric_name,
21 scoped_ptr<Sample> sample) {
22 ++sample_counts_[metric_name];
23 // Replace the previous sample with a 1 in sample_count_ chance so that each
24 // sample has equal probability of being reported.
25 if (base::RandGenerator(sample_counts_[metric_name]) == 0)
26 samples_.set(metric_name, sample.Pass());
29 void Sampler::ExportMetrics(const std::string& secret, RapporReports* reports) {
30 for (const auto& kv : samples_) {
31 kv.second->ExportMetrics(secret, kv.first, reports);
33 samples_.clear();
34 sample_counts_.clear();
37 } // namespace internal
39 } // namespace rappor