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, | |
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'); | |
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 scoped_ptr<MetricSample> MetricSample::CrashSample( | |
89 const std::string& crash_name) { | |
90 return scoped_ptr<MetricSample>( | |
91 new MetricSample(CRASH, crash_name, 0, 0, 0, 0)); | |
92 } | |
93 | |
94 scoped_ptr<MetricSample> MetricSample::HistogramSample( | |
95 const std::string& histogram_name, | |
96 int sample, | |
97 int min, | |
98 int max, | |
99 int bucket_count) { | |
100 return scoped_ptr<MetricSample>(new MetricSample( | |
101 HISTOGRAM, histogram_name, sample, min, max, bucket_count)); | |
102 } | |
103 | |
104 scoped_ptr<MetricSample> MetricSample::ReadHistogram( | |
105 const std::string& serialized_histogram) { | |
106 std::vector<std::string> parts; | |
107 base::SplitString(serialized_histogram, ' ', &parts); | |
108 | |
109 if (parts.size() != 5) | |
110 return scoped_ptr<MetricSample>(); | |
111 int sample, min, max, bucket_count; | |
112 if (parts[0].length() == 0 || !base::StringToInt(parts[1], &sample) || | |
113 !base::StringToInt(parts[2], &min) || | |
114 !base::StringToInt(parts[3], &max) || | |
115 !base::StringToInt(parts[4], &bucket_count)) { | |
116 return scoped_ptr<MetricSample>(); | |
117 } | |
118 | |
119 return HistogramSample(parts[0], sample, min, max, bucket_count); | |
120 } | |
121 | |
122 scoped_ptr<MetricSample> MetricSample::SparseHistogramSample( | |
123 const std::string& histogram_name, | |
124 int sample) { | |
125 return scoped_ptr<MetricSample>( | |
126 new MetricSample(SPARSE_HISTOGRAM, histogram_name, sample, 0, 0, 0)); | |
127 } | |
128 | |
129 scoped_ptr<MetricSample> MetricSample::ReadSparseHistogram( | |
130 const std::string& serialized_histogram) { | |
131 std::vector<std::string> parts; | |
132 base::SplitString(serialized_histogram, ' ', &parts); | |
133 if (parts.size() != 2) | |
134 return scoped_ptr<MetricSample>(); | |
135 int sample; | |
136 if (parts[0].length() == 0 || !base::StringToInt(parts[1], &sample)) | |
137 return scoped_ptr<MetricSample>(); | |
138 | |
139 return SparseHistogramSample(parts[0], sample); | |
140 } | |
141 | |
142 scoped_ptr<MetricSample> MetricSample::LinearHistogramSample( | |
143 const std::string& histogram_name, | |
144 int sample, | |
145 int max) { | |
146 return scoped_ptr<MetricSample>( | |
147 new MetricSample(LINEAR_HISTOGRAM, histogram_name, sample, 0, max, 0)); | |
148 } | |
149 | |
150 scoped_ptr<MetricSample> MetricSample::ReadLinearHistogram( | |
Alexei Svitkine (slow)
2014/05/13 15:36:26
Please add:
// static
Before all the static meth
bsimonnet
2014/05/13 17:59:14
Done.
| |
151 const std::string& serialized_histogram) { | |
152 std::vector<std::string> parts; | |
153 int sample, max; | |
154 base::SplitString(serialized_histogram, ' ', &parts); | |
155 if (parts.size() != 3) | |
156 return scoped_ptr<MetricSample>(); | |
157 if (parts[0].length() == 0 || !base::StringToInt(parts[1], &sample) || | |
158 !base::StringToInt(parts[2], &max)) | |
Alexei Svitkine (slow)
2014/05/13 15:36:26
Nit: {}'s
bsimonnet
2014/05/13 17:59:14
Done.
| |
159 return scoped_ptr<MetricSample>(); | |
160 | |
161 return LinearHistogramSample(parts[0], sample, max); | |
162 } | |
163 | |
164 scoped_ptr<MetricSample> MetricSample::UserActionSample( | |
165 const std::string& action_name) { | |
166 return scoped_ptr<MetricSample>( | |
167 new MetricSample(USER_ACTION, action_name, 0, 0, 0, 0)); | |
168 } | |
169 | |
170 bool MetricSample::IsEqual(const MetricSample* metric) { | |
171 CHECK(metric); | |
172 return type_ == metric->type_ && name_ == metric->name_ && | |
173 sample_ == metric->sample_ && min_ == metric->min_ && | |
174 max_ == metric->max_ && bucket_count_ == metric->bucket_count_; | |
175 } | |
176 | |
177 } // namespace metrics | |
OLD | NEW |