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
3 // found in the LICENSE file.
5 #include "components/metrics/serialization/metric_sample.h"
10 #include "base/logging.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/stringprintf.h"
17 MetricSample::MetricSample(MetricSample::SampleType sample_type,
18 const std::string& metric_name,
28 bucket_count_(bucket_count) {
31 MetricSample::~MetricSample() {
34 bool MetricSample::IsValid() const {
35 return name().find(' ') == std::string::npos &&
36 name().find('\0') == std::string::npos && !name().empty();
39 std::string MetricSample::ToString() const {
41 return base::StringPrintf("crash%c%s%c",
45 } else if (type_ == SPARSE_HISTOGRAM) {
46 return base::StringPrintf("sparsehistogram%c%s %d%c",
51 } else if (type_ == LINEAR_HISTOGRAM) {
52 return base::StringPrintf("linearhistogram%c%s %d %d%c",
58 } else if (type_ == HISTOGRAM) {
59 return base::StringPrintf("histogram%c%s %d %d %d %d%c",
68 // The type can only be USER_ACTION.
69 CHECK_EQ(type_, USER_ACTION);
70 return base::StringPrintf("useraction%c%s%c",
77 const int MetricSample::sample() const {
78 CHECK_NE(type_, USER_ACTION);
79 CHECK_NE(type_, CRASH);
83 const int MetricSample::min() const {
84 CHECK_EQ(type_, HISTOGRAM);
88 const int MetricSample::max() const {
89 CHECK_NE(type_, CRASH);
90 CHECK_NE(type_, USER_ACTION);
91 CHECK_NE(type_, SPARSE_HISTOGRAM);
95 const int MetricSample::bucket_count() const {
96 CHECK_EQ(type_, HISTOGRAM);
101 scoped_ptr<MetricSample> MetricSample::CrashSample(
102 const std::string& crash_name) {
103 return scoped_ptr<MetricSample>(
104 new MetricSample(CRASH, crash_name, 0, 0, 0, 0));
108 scoped_ptr<MetricSample> MetricSample::HistogramSample(
109 const std::string& histogram_name,
114 return scoped_ptr<MetricSample>(new MetricSample(
115 HISTOGRAM, histogram_name, sample, min, max, bucket_count));
119 scoped_ptr<MetricSample> MetricSample::ParseHistogram(
120 const std::string& serialized_histogram) {
121 std::vector<std::string> parts;
122 base::SplitString(serialized_histogram, ' ', &parts);
124 if (parts.size() != 5)
125 return scoped_ptr<MetricSample>();
126 int sample, min, max, bucket_count;
127 if (parts[0].empty() || !base::StringToInt(parts[1], &sample) ||
128 !base::StringToInt(parts[2], &min) ||
129 !base::StringToInt(parts[3], &max) ||
130 !base::StringToInt(parts[4], &bucket_count)) {
131 return scoped_ptr<MetricSample>();
134 return HistogramSample(parts[0], sample, min, max, bucket_count);
138 scoped_ptr<MetricSample> MetricSample::SparseHistogramSample(
139 const std::string& histogram_name,
141 return scoped_ptr<MetricSample>(
142 new MetricSample(SPARSE_HISTOGRAM, histogram_name, sample, 0, 0, 0));
146 scoped_ptr<MetricSample> MetricSample::ParseSparseHistogram(
147 const std::string& serialized_histogram) {
148 std::vector<std::string> parts;
149 base::SplitString(serialized_histogram, ' ', &parts);
150 if (parts.size() != 2)
151 return scoped_ptr<MetricSample>();
153 if (parts[0].empty() || !base::StringToInt(parts[1], &sample))
154 return scoped_ptr<MetricSample>();
156 return SparseHistogramSample(parts[0], sample);
160 scoped_ptr<MetricSample> MetricSample::LinearHistogramSample(
161 const std::string& histogram_name,
164 return scoped_ptr<MetricSample>(
165 new MetricSample(LINEAR_HISTOGRAM, histogram_name, sample, 0, max, 0));
169 scoped_ptr<MetricSample> MetricSample::ParseLinearHistogram(
170 const std::string& serialized_histogram) {
171 std::vector<std::string> parts;
173 base::SplitString(serialized_histogram, ' ', &parts);
174 if (parts.size() != 3)
175 return scoped_ptr<MetricSample>();
176 if (parts[0].empty() || !base::StringToInt(parts[1], &sample) ||
177 !base::StringToInt(parts[2], &max)) {
178 return scoped_ptr<MetricSample>();
181 return LinearHistogramSample(parts[0], sample, max);
185 scoped_ptr<MetricSample> MetricSample::UserActionSample(
186 const std::string& action_name) {
187 return scoped_ptr<MetricSample>(
188 new MetricSample(USER_ACTION, action_name, 0, 0, 0, 0));
191 bool MetricSample::IsEqual(const MetricSample& metric) {
192 return type_ == metric.type_ && name_ == metric.name_ &&
193 sample_ == metric.sample_ && min_ == metric.min_ &&
194 max_ == metric.max_ && bucket_count_ == metric.bucket_count_;
197 } // namespace metrics