OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "components/metrics/chromeos/metric_sample.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
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" | |
14 | |
15 namespace metrics { | |
16 | |
17 MetricSample::MetricSample(MetricSample::SampleType sample_type, | |
18 const std::string& metric_name, | |
19 int sample, | |
20 int min, | |
21 int max, | |
22 int bucket_count) | |
23 : type_(sample_type), | |
24 name_(metric_name), | |
25 sample_(sample), | |
26 min_(min), | |
27 max_(max), | |
28 bucket_count_(bucket_count) { | |
29 } | |
30 | |
31 MetricSample::~MetricSample() { | |
32 } | |
33 | |
34 bool MetricSample::IsValid() const { | |
35 return name().find(' ') == std::string::npos && | |
36 name().find('\0') == std::string::npos && !name().empty(); | |
37 } | |
38 | |
39 std::string MetricSample::ToString() const { | |
40 if (type_ == CRASH) { | |
41 return base::StringPrintf("crash%c%s%c", | |
42 '\0', | |
43 name().c_str(), | |
44 '\0'); | |
45 } else if (type_ == SPARSE_HISTOGRAM) { | |
46 return base::StringPrintf("sparsehistogram%c%s %d%c", | |
47 '\0', | |
48 name().c_str(), | |
49 sample_, | |
50 '\0'); | |
51 } else if (type_ == LINEAR_HISTOGRAM) { | |
52 return base::StringPrintf("linearhistogram%c%s %d %d%c", | |
53 '\0', | |
54 name().c_str(), | |
55 sample_, | |
56 max_, | |
57 '\0'); | |
58 } else if (type_ == HISTOGRAM) { | |
59 return base::StringPrintf("histogram%c%s %d %d %d %d%c", | |
60 '\0', | |
61 name().c_str(), | |
62 sample_, | |
63 min_, | |
64 max_, | |
65 bucket_count_, | |
66 '\0'); | |
67 } else { | |
68 // The type can only be USER_ACTION. | |
69 CHECK(type_ == USER_ACTION); | |
Alexei Svitkine (slow)
2014/05/15 13:43:26
Nit: CHECK_EQ. Same for the ones below.
bsimonnet
2014/05/15 19:23:40
Done.
| |
70 return base::StringPrintf("useraction%c%s%c", | |
71 '\0', | |
72 name().c_str(), | |
73 '\0'); | |
74 } | |
75 } | |
76 | |
77 const int MetricSample::sample() const { | |
78 CHECK(type_ != USER_ACTION && type_ != CRASH); | |
Alexei Svitkine (slow)
2014/05/15 13:43:26
Nit: Split into two statements.
bsimonnet
2014/05/15 19:23:40
Done.
| |
79 return sample_; | |
80 } | |
81 | |
82 const int MetricSample::min() const { | |
83 CHECK(type_ == HISTOGRAM); | |
84 return min_; | |
85 } | |
86 | |
87 const int MetricSample::max() const { | |
88 CHECK(type_ == LINEAR_HISTOGRAM || type_ == HISTOGRAM); | |
89 return max_; | |
90 } | |
91 | |
92 const int MetricSample::bucket_count() const { | |
93 CHECK(type_ == HISTOGRAM); | |
94 return bucket_count_; | |
95 } | |
96 | |
97 // static | |
98 scoped_ptr<MetricSample> MetricSample::CrashSample( | |
99 const std::string& crash_name) { | |
100 return scoped_ptr<MetricSample>( | |
101 new MetricSample(CRASH, crash_name, 0, 0, 0, 0)); | |
102 } | |
103 | |
104 // static | |
105 scoped_ptr<MetricSample> MetricSample::HistogramSample( | |
106 const std::string& histogram_name, | |
107 int sample, | |
108 int min, | |
109 int max, | |
110 int bucket_count) { | |
111 return scoped_ptr<MetricSample>(new MetricSample( | |
112 HISTOGRAM, histogram_name, sample, min, max, bucket_count)); | |
113 } | |
114 | |
115 // static | |
116 scoped_ptr<MetricSample> MetricSample::ReadHistogram( | |
117 const std::string& serialized_histogram) { | |
118 std::vector<std::string> parts; | |
119 base::SplitString(serialized_histogram, ' ', &parts); | |
120 | |
121 if (parts.size() != 5) | |
122 return scoped_ptr<MetricSample>(); | |
123 int sample, min, max, bucket_count; | |
124 if (parts[0].empty() || !base::StringToInt(parts[1], &sample) || | |
125 !base::StringToInt(parts[2], &min) || | |
126 !base::StringToInt(parts[3], &max) || | |
127 !base::StringToInt(parts[4], &bucket_count)) { | |
128 return scoped_ptr<MetricSample>(); | |
129 } | |
130 | |
131 return HistogramSample(parts[0], sample, min, max, bucket_count); | |
132 } | |
133 | |
134 // static | |
135 scoped_ptr<MetricSample> MetricSample::SparseHistogramSample( | |
136 const std::string& histogram_name, | |
137 int sample) { | |
138 return scoped_ptr<MetricSample>( | |
139 new MetricSample(SPARSE_HISTOGRAM, histogram_name, sample, 0, 0, 0)); | |
140 } | |
141 | |
142 // static | |
143 scoped_ptr<MetricSample> MetricSample::ReadSparseHistogram( | |
144 const std::string& serialized_histogram) { | |
145 std::vector<std::string> parts; | |
146 base::SplitString(serialized_histogram, ' ', &parts); | |
147 if (parts.size() != 2) | |
148 return scoped_ptr<MetricSample>(); | |
149 int sample; | |
150 if (parts[0].empty() || !base::StringToInt(parts[1], &sample)) | |
151 return scoped_ptr<MetricSample>(); | |
152 | |
153 return SparseHistogramSample(parts[0], sample); | |
154 } | |
155 | |
156 // static | |
157 scoped_ptr<MetricSample> MetricSample::LinearHistogramSample( | |
158 const std::string& histogram_name, | |
159 int sample, | |
160 int max) { | |
161 return scoped_ptr<MetricSample>( | |
162 new MetricSample(LINEAR_HISTOGRAM, histogram_name, sample, 0, max, 0)); | |
163 } | |
164 | |
165 // static | |
166 scoped_ptr<MetricSample> MetricSample::ReadLinearHistogram( | |
167 const std::string& serialized_histogram) { | |
168 std::vector<std::string> parts; | |
169 int sample, max; | |
170 base::SplitString(serialized_histogram, ' ', &parts); | |
171 if (parts.size() != 3) | |
172 return scoped_ptr<MetricSample>(); | |
173 if (parts[0].empty() || !base::StringToInt(parts[1], &sample) || | |
174 !base::StringToInt(parts[2], &max)) { | |
175 return scoped_ptr<MetricSample>(); | |
176 } | |
177 | |
178 return LinearHistogramSample(parts[0], sample, max); | |
179 } | |
180 | |
181 // static | |
182 scoped_ptr<MetricSample> MetricSample::UserActionSample( | |
183 const std::string& action_name) { | |
184 return scoped_ptr<MetricSample>( | |
185 new MetricSample(USER_ACTION, action_name, 0, 0, 0, 0)); | |
186 } | |
187 | |
188 bool MetricSample::IsEqual(const MetricSample& metric) { | |
189 return type_ == metric.type_ && name_ == metric.name_ && | |
190 sample_ == metric.sample_ && min_ == metric.min_ && | |
191 max_ == metric.max_ && bucket_count_ == metric.bucket_count_; | |
192 } | |
193 | |
194 } // namespace metrics | |
OLD | NEW |