1 // Copyright 2020 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 "chrome/browser/chromeos/power/smart_charging/smart_charging_ukm_logger.h"
6 
7 #include "chrome/browser/chromeos/power/smart_charging/user_charging_event.pb.h"
8 #include "services/metrics/public/cpp/ukm_builders.h"
9 #include "services/metrics/public/cpp/ukm_recorder.h"
10 #include "services/metrics/public/cpp/ukm_source_id.h"
11 
12 namespace chromeos {
13 namespace power {
14 
LogEvent(const UserChargingEvent & user_charging_event) const15 void SmartChargingUkmLogger::LogEvent(
16     const UserChargingEvent& user_charging_event) const {
17   const ukm::SourceId source_id = ukm::UkmRecorder::GetNewSourceID();
18   ukm::builders::SmartCharging ukm_smart_charging(source_id);
19 
20   ukm_smart_charging.SetEventId(user_charging_event.event().event_id());
21   ukm_smart_charging.SetReason(user_charging_event.event().reason());
22 
23   const UserChargingEvent::Features features = user_charging_event.features();
24 
25   if (features.has_battery_percentage()) {
26     ukm_smart_charging.SetBatteryPercentage(features.battery_percentage());
27   }
28 
29   if (features.has_time_since_last_charge()) {
30     ukm_smart_charging.SetTimeSinceLastCharge(
31         features.time_since_last_charge());
32   }
33 
34   if (features.has_duration_of_last_charge()) {
35     ukm_smart_charging.SetDurationOfLastCharge(
36         features.duration_of_last_charge());
37   }
38 
39   if (features.has_battery_percentage_of_last_charge()) {
40     ukm_smart_charging.SetBatteryPercentageOfLastCharge(
41         features.battery_percentage_of_last_charge());
42   }
43 
44   if (features.has_battery_percentage_before_last_charge()) {
45     ukm_smart_charging.SetBatteryPercentageBeforeLastCharge(
46         features.battery_percentage_before_last_charge());
47   }
48 
49   if (features.has_time_of_the_day()) {
50     ukm_smart_charging.SetTimeOfTheDay(features.time_of_the_day());
51   }
52 
53   if (features.has_day_of_week()) {
54     ukm_smart_charging.SetDayOfWeek(features.day_of_week());
55   }
56 
57   if (features.has_day_of_month()) {
58     ukm_smart_charging.SetDayOfMonth(features.day_of_month());
59   }
60 
61   if (features.has_month()) {
62     ukm_smart_charging.SetMonth(features.month());
63   }
64 
65   if (features.has_timezone_difference_from_last_charge()) {
66     ukm_smart_charging.SetTimezoneDifferenceFromLastCharge(
67         features.timezone_difference_from_last_charge());
68   }
69 
70   if (features.has_device_type()) {
71     ukm_smart_charging.SetDeviceType(features.device_type());
72   }
73 
74   if (features.has_device_mode()) {
75     ukm_smart_charging.SetDeviceMode(features.device_mode());
76   }
77 
78   if (features.has_num_recent_key_events()) {
79     ukm_smart_charging.SetNumRecentKeyEvents(features.num_recent_key_events());
80   }
81 
82   if (features.has_num_recent_mouse_events()) {
83     ukm_smart_charging.SetNumRecentMouseEvents(
84         features.num_recent_mouse_events());
85   }
86 
87   if (features.has_num_recent_touch_events()) {
88     ukm_smart_charging.SetNumRecentTouchEvents(
89         features.num_recent_touch_events());
90   }
91 
92   if (features.has_num_recent_stylus_events()) {
93     ukm_smart_charging.SetNumRecentStylusEvents(
94         features.num_recent_stylus_events());
95   }
96 
97   if (features.has_duration_recent_video_playing()) {
98     ukm_smart_charging.SetDurationRecentVideoPlaying(
99         features.duration_recent_video_playing());
100   }
101 
102   if (features.has_duration_recent_audio_playing()) {
103     ukm_smart_charging.SetDurationRecentAudioPlaying(
104         features.duration_recent_audio_playing());
105   }
106 
107   if (features.has_screen_brightness_percent()) {
108     ukm_smart_charging.SetScreenBrightnessPercent(
109         features.screen_brightness_percent());
110   }
111 
112   if (features.has_voltage()) {
113     ukm_smart_charging.SetVoltage(features.voltage());
114   }
115 
116   if (features.has_halt_from_last_charge()) {
117     ukm_smart_charging.SetHaltFromLastCharge(features.halt_from_last_charge());
118   }
119 
120   if (features.has_is_charging()) {
121     ukm_smart_charging.SetIsCharging(features.is_charging());
122   }
123 
124   ukm::UkmRecorder* const ukm_recorder = ukm::UkmRecorder::Get();
125   ukm_smart_charging.Record(ukm_recorder);
126 }
127 
128 }  // namespace power
129 }  // namespace chromeos
130