1 // Copyright 2023 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/debug/structured/structured_metrics_utils.h"
6
7 #include "base/i18n/number_formatting.h"
8 #include "components/metrics/structured/structured_metrics_service.h"
9
10 namespace metrics::structured {
11
12 namespace {
13
14 // Creates a dictionary that represents a key-value pair.
CreateKeyValue(base::StringPiece key,base::Value value)15 base::Value::Dict CreateKeyValue(base::StringPiece key, base::Value value) {
16 base::Value::Dict result;
17 result.Set("key", key);
18 result.Set("value", std::move(value));
19 return result;
20 }
21
22 // Creates a list of metrics represented by a key-value pair from the metrics of
23 // an event.
CreateMetricsList(const std::map<std::string,Event::MetricValue> & metrics)24 base::Value::List CreateMetricsList(
25 const std::map<std::string, Event::MetricValue>& metrics) {
26 base::Value::List result;
27 for (const auto& metric : metrics) {
28 result.Append(CreateKeyValue(metric.first, metric.second.value.Clone()));
29 }
30 return result;
31 }
32
33 // Creates an event metadata dictionary from an event.
CreateEventMetadataDict(const Event & event)34 base::Value::Dict CreateEventMetadataDict(const Event& event) {
35 base::Value::Dict metadata;
36 const auto& event_metadata = event.event_sequence_metadata();
37 metadata.Set(
38 "systemUptimeMs",
39 base::FormatNumber(event.recorded_time_since_boot().InMilliseconds()));
40 metadata.Set("id", event_metadata.event_unique_id);
41 metadata.Set("resetCounter", event_metadata.reset_counter);
42 return metadata;
43 }
44
45 // Creates a dictionary from an event.
CreateEventDict(const Event & event)46 base::Value::Dict CreateEventDict(const Event& event) {
47 base::Value::Dict result;
48
49 result.Set("project", event.project_name());
50 result.Set("event", event.event_name());
51 result.Set("metrics", CreateMetricsList(event.metric_values()));
52
53 if (event.IsEventSequenceType()) {
54 result.Set("type", "sequence");
55 result.Set("sequenceMetadata", CreateEventMetadataDict(event));
56 } else {
57 result.Set("type", "normal");
58 }
59
60 return result;
61 }
62
63 } // namespace
64
ConvertEventsIntoValue(const std::vector<Event> & events)65 base::Value ConvertEventsIntoValue(const std::vector<Event>& events) {
66 base::Value::List result;
67
68 for (const auto& event : events) {
69 result.Append(CreateEventDict(event));
70 }
71
72 return base::Value(std::move(result));
73 }
74
GetStructuredMetricsSummary(StructuredMetricsService * service)75 base::Value GetStructuredMetricsSummary(StructuredMetricsService* service) {
76 base::Value::Dict result;
77 result.Set("enabled", service->recording_enabled());
78 auto id =
79 service->recorder()->key_data_provider()->GetSecondaryId("CrOSEvents");
80 if (id.has_value()) {
81 result.Set("crosDeviceId", base::NumberToString(id.value()));
82 }
83 return base::Value(std::move(result));
84 }
85
86 } // namespace metrics::structured
87