xref: /aosp_15_r20/external/cronet/components/metrics/structured/event.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2021 The Chromium Authors
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/structured/event.h"
6 
7 #include <map>
8 #include <memory>
9 #include <string>
10 
11 #include "base/uuid.h"
12 #include "base/values.h"
13 
14 namespace metrics::structured {
15 
MetricValue(MetricType type,base::Value value)16 Event::MetricValue::MetricValue(MetricType type, base::Value value)
17     : type(type), value(std::move(value)) {}
18 
19 Event::MetricValue::MetricValue(Event::MetricValue&& other) = default;
20 Event::MetricValue& Event::MetricValue::operator=(Event::MetricValue&& other) =
21     default;
22 
operator ==(const Event::MetricValue & rhs) const23 bool Event::MetricValue::operator==(const Event::MetricValue& rhs) const {
24   return type == rhs.type && value == rhs.value;
25 }
26 
27 Event::MetricValue::~MetricValue() = default;
28 
EventSequenceMetadata(int reset_counter)29 Event::EventSequenceMetadata::EventSequenceMetadata(int reset_counter)
30     : reset_counter(reset_counter),
31       event_unique_id(base::Uuid::GenerateRandomV4().AsLowercaseString()) {}
32 
33 Event::EventSequenceMetadata::EventSequenceMetadata(
34     const Event::EventSequenceMetadata& other) = default;
35 Event::EventSequenceMetadata& Event::EventSequenceMetadata::operator=(
36     const Event::EventSequenceMetadata& other) = default;
37 
38 Event::EventSequenceMetadata::~EventSequenceMetadata() = default;
39 
40 Event::Event() = default;
Event(const std::string & project_name,const std::string & event_name)41 Event::Event(const std::string& project_name, const std::string& event_name)
42     : project_name_(project_name), event_name_(event_name) {}
Event(const std::string & project_name,const std::string & event_name,bool is_event_sequence)43 Event::Event(const std::string& project_name,
44              const std::string& event_name,
45              bool is_event_sequence)
46     : project_name_(project_name),
47       event_name_(event_name),
48       is_event_sequence_(is_event_sequence) {}
49 
50 Event::~Event() = default;
51 
Event(Event && other)52 Event::Event(Event&& other)
53     : project_name_(std::move(other.project_name_)),
54       event_name_(std::move(other.event_name_)),
55       metric_values_(std::move(other.metric_values_)),
56       recorded_time_since_boot_(std::move(other.recorded_time_since_boot_)),
57       event_sequence_metadata_(std::move(other.event_sequence_metadata_)),
58       is_event_sequence_(other.is_event_sequence_) {}
59 
operator =(Event && other)60 Event& Event::operator=(Event&& other) {
61   project_name_ = std::move(other.project_name_);
62   event_name_ = std::move(other.event_name_);
63   metric_values_ = std::move(other.metric_values_);
64   recorded_time_since_boot_ = std::move(other.recorded_time_since_boot_);
65   event_sequence_metadata_ = std::move(other.event_sequence_metadata_);
66   is_event_sequence_ = other.is_event_sequence_;
67   return *this;
68 }
69 
IsEventSequenceType() const70 bool Event::IsEventSequenceType() const {
71   return is_event_sequence_;
72 }
73 
Clone() const74 Event Event::Clone() const {
75   auto clone = Event(project_name_, event_name_, is_event_sequence_);
76   for (const auto& metric : metric_values()) {
77     const Event::MetricValue& metric_value = metric.second;
78     clone.AddMetric(metric.first, metric_value.type,
79                     metric_value.value.Clone());
80   }
81   clone.recorded_time_since_boot_ = recorded_time_since_boot_;
82   clone.event_sequence_metadata_ = event_sequence_metadata_;
83   return clone;
84 }
85 
event_sequence_metadata() const86 const Event::EventSequenceMetadata& Event::event_sequence_metadata() const {
87   CHECK(event_sequence_metadata_.has_value());
88   return event_sequence_metadata_.value();
89 }
90 
recorded_time_since_boot() const91 const base::TimeDelta Event::recorded_time_since_boot() const {
92   CHECK(recorded_time_since_boot_.has_value());
93   return recorded_time_since_boot_.value();
94 }
95 
AddMetric(const std::string & metric_name,Event::MetricType type,base::Value && value)96 bool Event::AddMetric(const std::string& metric_name,
97                       Event::MetricType type,
98                       base::Value&& value) {
99   bool valid = true;
100   switch (type) {
101     case MetricType::kHmac:
102       valid = value.is_string();
103       break;
104     // no base::LongValue so int64_t is encoded in a string.
105     case MetricType::kLong:
106       valid = value.is_string();
107       break;
108     case MetricType::kInt:
109       valid = value.is_int();
110       break;
111     case MetricType::kDouble:
112       valid = value.is_double();
113       break;
114     case MetricType::kRawString:
115       valid = value.is_string();
116       break;
117     case MetricType::kBoolean:
118       valid = value.is_bool();
119       break;
120   }
121   if (!valid) {
122     return false;
123   }
124 
125   auto pair =
126       metric_values_.emplace(metric_name, MetricValue(type, std::move(value)));
127   return pair.second;
128 }
129 
SetEventSequenceMetadata(const Event::EventSequenceMetadata & event_sequence_metadata)130 void Event::SetEventSequenceMetadata(
131     const Event::EventSequenceMetadata& event_sequence_metadata) {
132   event_sequence_metadata_ = event_sequence_metadata;
133 }
134 
SetRecordedTimeSinceBoot(base::TimeDelta recorded_time_since_boot)135 void Event::SetRecordedTimeSinceBoot(base::TimeDelta recorded_time_since_boot) {
136   recorded_time_since_boot_ = recorded_time_since_boot;
137 }
138 
139 }  // namespace metrics::structured
140