Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / components / metrics / serialization / metric_sample.h
blob8a1a561d119576468ddd75156c425fb0ec03c8e6
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 #ifndef COMPONENTS_METRICS_SERIALIZATION_METRIC_SAMPLE_H_
6 #define COMPONENTS_METRICS_SERIALIZATION_METRIC_SAMPLE_H_
8 #include <string>
10 #include "base/gtest_prod_util.h"
11 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h"
14 namespace metrics {
16 // This class is used by libmetrics (ChromeOS) to serialize
17 // and deserialize measurements to send them to a metrics sending service.
18 // It is meant to be a simple container with serialization functions.
19 class MetricSample {
20 public:
21 // Types of metric sample used.
22 enum SampleType {
23 CRASH,
24 HISTOGRAM,
25 LINEAR_HISTOGRAM,
26 SPARSE_HISTOGRAM,
27 USER_ACTION
30 ~MetricSample();
32 // Returns true if the sample is valid (can be serialized without ambiguity).
34 // This function should be used to filter bad samples before serializing them.
35 bool IsValid() const;
37 // Getters for type and name. All types of metrics have these so we do not
38 // need to check the type.
39 SampleType type() const { return type_; }
40 const std::string& name() const { return name_; }
42 // Getters for sample, min, max, bucket_count.
43 // Check the metric type to make sure the request make sense. (ex: a crash
44 // sample does not have a bucket_count so we crash if we call bucket_count()
45 // on it.)
46 int sample() const;
47 int min() const;
48 int max() const;
49 int bucket_count() const;
51 // Returns a serialized version of the sample.
53 // The serialized message for each type is:
54 // crash: crash\0|name_|\0
55 // user action: useraction\0|name_|\0
56 // histogram: histogram\0|name_| |sample_| |min_| |max_| |bucket_count_|\0
57 // sparsehistogram: sparsehistogram\0|name_| |sample_|\0
58 // linearhistogram: linearhistogram\0|name_| |sample_| |max_|\0
59 std::string ToString() const;
61 // Builds a crash sample.
62 static scoped_ptr<MetricSample> CrashSample(const std::string& crash_name);
64 // Builds a histogram sample.
65 static scoped_ptr<MetricSample> HistogramSample(
66 const std::string& histogram_name,
67 int sample,
68 int min,
69 int max,
70 int bucket_count);
71 // Deserializes a histogram sample.
72 static scoped_ptr<MetricSample> ParseHistogram(const std::string& serialized);
74 // Builds a sparse histogram sample.
75 static scoped_ptr<MetricSample> SparseHistogramSample(
76 const std::string& histogram_name,
77 int sample);
78 // Deserializes a sparse histogram sample.
79 static scoped_ptr<MetricSample> ParseSparseHistogram(
80 const std::string& serialized);
82 // Builds a linear histogram sample.
83 static scoped_ptr<MetricSample> LinearHistogramSample(
84 const std::string& histogram_name,
85 int sample,
86 int max);
87 // Deserializes a linear histogram sample.
88 static scoped_ptr<MetricSample> ParseLinearHistogram(
89 const std::string& serialized);
91 // Builds a user action sample.
92 static scoped_ptr<MetricSample> UserActionSample(
93 const std::string& action_name);
95 // Returns true if sample and this object represent the same sample (type,
96 // name, sample, min, max, bucket_count match).
97 bool IsEqual(const MetricSample& sample);
99 private:
100 MetricSample(SampleType sample_type,
101 const std::string& metric_name,
102 const int sample,
103 const int min,
104 const int max,
105 const int bucket_count);
107 const SampleType type_;
108 const std::string name_;
109 const int sample_;
110 const int min_;
111 const int max_;
112 const int bucket_count_;
114 DISALLOW_COPY_AND_ASSIGN(MetricSample);
117 } // namespace metrics
119 #endif // COMPONENTS_METRICS_SERIALIZATION_METRIC_SAMPLE_H_