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 const int sample, | |
Alexei Svitkine (slow)
2014/05/13 19:52:42
Nit: No need for primitive-typed params to be cons
bsimonnet
2014/05/13 21:28:00
Done.
| |
20 const int min, | |
21 const int max, | |
22 const 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().length() > 0; | |
37 } | |
38 | |
39 std::string MetricSample::ToString() const { | |
40 if (type_ == CRASH) { | |
41 return base::StringPrintf("crash%c%s%c", '\0', name().c_str(), '\0'); | |
42 } else if (type_ == SPARSE_HISTOGRAM) { | |
43 return base::StringPrintf( | |
44 "sparsehistogram%c%s %d%c", '\0', name().c_str(), sample_, '\0'); | |
Alexei Svitkine (slow)
2014/05/13 19:52:42
Nit: Standardize on wrapping - you're using a diff
bsimonnet
2014/05/13 21:28:00
Done.
This formatting was produced by git cl form
| |
45 } else if (type_ == LINEAR_HISTOGRAM) { | |
46 return base::StringPrintf("linearhistogram%c%s %d %d%c", | |
47 '\0', | |
48 name().c_str(), | |
49 sample_, | |
50 max_, | |
51 '\0'); | |
52 } else if (type_ == HISTOGRAM) { | |
53 return base::StringPrintf("histogram%c%s %d %d %d %d%c", | |
54 '\0', | |
55 name().c_str(), | |
56 sample_, | |
57 min_, | |
58 max_, | |
59 bucket_count_, | |
60 '\0'); | |
61 } else { | |
62 // The type can only be USER_ACTION. | |
63 CHECK(type_ == USER_ACTION); | |
64 return base::StringPrintf("useraction%c%s%c", '\0', name().c_str(), '\0'); | |
65 } | |
66 } | |
67 | |
68 const int MetricSample::sample() const { | |
69 CHECK(type_ != USER_ACTION && type_ != CRASH); | |
70 return sample_; | |
71 } | |
72 | |
73 const int MetricSample::min() const { | |
74 CHECK(type_ == HISTOGRAM); | |
75 return min_; | |
76 } | |
77 | |
78 const int MetricSample::max() const { | |
79 CHECK(type_ == LINEAR_HISTOGRAM || type_ == HISTOGRAM); | |
80 return max_; | |
81 } | |
82 | |
83 const int MetricSample::bucket_count() const { | |
84 CHECK(type_ == HISTOGRAM); | |
85 return bucket_count_; | |
86 } | |
87 | |
88 // static | |
89 scoped_ptr<MetricSample> MetricSample::CrashSample( | |
90 const std::string& crash_name) { | |
91 return scoped_ptr<MetricSample>( | |
92 new MetricSample(CRASH, crash_name, 0, 0, 0, 0)); | |
93 } | |
94 | |
95 // static | |
96 scoped_ptr<MetricSample> MetricSample::HistogramSample( | |
97 const std::string& histogram_name, | |
98 int sample, | |
99 int min, | |
100 int max, | |
101 int bucket_count) { | |
102 return scoped_ptr<MetricSample>(new MetricSample( | |
103 HISTOGRAM, histogram_name, sample, min, max, bucket_count)); | |
104 } | |
105 | |
106 // static | |
107 scoped_ptr<MetricSample> MetricSample::ReadHistogram( | |
108 const std::string& serialized_histogram) { | |
109 std::vector<std::string> parts; | |
110 base::SplitString(serialized_histogram, ' ', &parts); | |
111 | |
112 if (parts.size() != 5) | |
113 return scoped_ptr<MetricSample>(); | |
114 int sample, min, max, bucket_count; | |
115 if (parts[0].length() == 0 || !base::StringToInt(parts[1], &sample) || | |
116 !base::StringToInt(parts[2], &min) || | |
117 !base::StringToInt(parts[3], &max) || | |
118 !base::StringToInt(parts[4], &bucket_count)) { | |
119 return scoped_ptr<MetricSample>(); | |
120 } | |
121 | |
122 return HistogramSample(parts[0], sample, min, max, bucket_count); | |
123 } | |
124 | |
125 // static | |
126 scoped_ptr<MetricSample> MetricSample::SparseHistogramSample( | |
127 const std::string& histogram_name, | |
128 int sample) { | |
129 return scoped_ptr<MetricSample>( | |
130 new MetricSample(SPARSE_HISTOGRAM, histogram_name, sample, 0, 0, 0)); | |
131 } | |
132 | |
133 // static | |
134 scoped_ptr<MetricSample> MetricSample::ReadSparseHistogram( | |
135 const std::string& serialized_histogram) { | |
136 std::vector<std::string> parts; | |
137 base::SplitString(serialized_histogram, ' ', &parts); | |
138 if (parts.size() != 2) | |
139 return scoped_ptr<MetricSample>(); | |
140 int sample; | |
141 if (parts[0].length() == 0 || !base::StringToInt(parts[1], &sample)) | |
Alexei Svitkine (slow)
2014/05/13 19:52:42
Nit: .length() == 0 -> emtpty()
Change throughout
bsimonnet
2014/05/13 21:28:00
Done.
| |
142 return scoped_ptr<MetricSample>(); | |
143 | |
144 return SparseHistogramSample(parts[0], sample); | |
145 } | |
146 | |
147 // static | |
148 scoped_ptr<MetricSample> MetricSample::LinearHistogramSample( | |
149 const std::string& histogram_name, | |
150 int sample, | |
151 int max) { | |
152 return scoped_ptr<MetricSample>( | |
153 new MetricSample(LINEAR_HISTOGRAM, histogram_name, sample, 0, max, 0)); | |
154 } | |
155 | |
156 // static | |
157 scoped_ptr<MetricSample> MetricSample::ReadLinearHistogram( | |
158 const std::string& serialized_histogram) { | |
159 std::vector<std::string> parts; | |
160 int sample, max; | |
161 base::SplitString(serialized_histogram, ' ', &parts); | |
162 if (parts.size() != 3) | |
163 return scoped_ptr<MetricSample>(); | |
164 if (parts[0].length() == 0 || !base::StringToInt(parts[1], &sample) || | |
165 !base::StringToInt(parts[2], &max)) { | |
166 return scoped_ptr<MetricSample>(); | |
167 } | |
168 | |
169 return LinearHistogramSample(parts[0], sample, max); | |
170 } | |
171 | |
172 // static | |
173 scoped_ptr<MetricSample> MetricSample::UserActionSample( | |
174 const std::string& action_name) { | |
175 return scoped_ptr<MetricSample>( | |
176 new MetricSample(USER_ACTION, action_name, 0, 0, 0, 0)); | |
177 } | |
178 | |
179 bool MetricSample::IsEqual(const MetricSample* metric) { | |
180 CHECK(metric); | |
Alexei Svitkine (slow)
2014/05/13 19:52:42
Can the function take a const& to avoid needing to
bsimonnet
2014/05/13 21:28:00
Done.
| |
181 return type_ == metric->type_ && name_ == metric->name_ && | |
182 sample_ == metric->sample_ && min_ == metric->min_ && | |
183 max_ == metric->max_ && bucket_count_ == metric->bucket_count_; | |
184 } | |
185 | |
186 } // namespace metrics | |
OLD | NEW |