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/export/histogram_sample.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_split.h" |
| 12 #include "base/strings/stringprintf.h" |
| 13 #include "components/metrics/export/metric_sample.h" |
| 14 |
| 15 using base::SplitString; |
| 16 using base::StringPrintf; |
| 17 using base::StringToInt; |
| 18 using std::string; |
| 19 using std::vector; |
| 20 |
| 21 namespace metrics { |
| 22 |
| 23 HistogramSample::HistogramSample(const std::string& name, |
| 24 int sample, |
| 25 int min, |
| 26 int max, |
| 27 int nbucket) |
| 28 : MetricSample(MetricSample::HISTOGRAM, name), |
| 29 sample_(sample), |
| 30 min_(min), |
| 31 max_(max), |
| 32 nbucket_(nbucket) {} |
| 33 |
| 34 HistogramSample::~HistogramSample() {} |
| 35 |
| 36 string HistogramSample::ToString() const { |
| 37 return StringPrintf("histogram%c%s %d %d %d %d%c", |
| 38 '\0', |
| 39 name().c_str(), |
| 40 sample_, |
| 41 min_, |
| 42 max_, |
| 43 nbucket_, |
| 44 '\0'); |
| 45 } |
| 46 |
| 47 // static |
| 48 // Read a histogram from a string listing |name|, |sample|, |min|, |max|, |
| 49 // |nbucket| separated by a space. |
| 50 HistogramSample* HistogramSample::ReadHistogram( |
| 51 const std::string& histogram_serialized) { |
| 52 vector<string> parts; |
| 53 SplitString(histogram_serialized, ' ', &parts); |
| 54 if (parts.size() != 5) return NULL; |
| 55 int sample, min, max, nbucket; |
| 56 if (parts[0].length() == 0 || !StringToInt(parts[1], &sample) || |
| 57 !StringToInt(parts[2], &min) || !StringToInt(parts[3], &max) || |
| 58 !StringToInt(parts[4], &nbucket)) |
| 59 return NULL; |
| 60 |
| 61 return new HistogramSample(parts[0], sample, min, max, nbucket); |
| 62 } |
| 63 |
| 64 } // namespace metrics |
OLD | NEW |