blob: 5952ec34f64d96a102a3bef7bf4ac44b15020b7c [file] [log] [blame]
Alex Deymo38429cf2015-11-11 18:27:22 -08001//
2// Copyright (C) 2015 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#ifndef UPDATE_ENGINE_METRICS_UTILS_H_
18#define UPDATE_ENGINE_METRICS_UTILS_H_
19
Tianjie Xu90aaa102017-10-10 17:39:03 -070020#include <string>
21
Tianjie Xu282aa1f2017-09-05 13:42:45 -070022#include <base/time/time.h>
23
Tianjie Xu90aaa102017-10-10 17:39:03 -070024#include "update_engine/common/clock_interface.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070025#include "update_engine/common/connection_utils.h"
Tianjie Xu282aa1f2017-09-05 13:42:45 -070026#include "update_engine/common/error_code.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070027#include "update_engine/common/metrics_constants.h"
28#include "update_engine/common/metrics_reporter_interface.h"
Tianjie Xu90aaa102017-10-10 17:39:03 -070029#include "update_engine/common/prefs_interface.h"
Alex Deymo38429cf2015-11-11 18:27:22 -080030
31namespace chromeos_update_engine {
Alex Deymoa2591792015-11-17 00:39:40 -030032
33class SystemState;
34
Alex Deymo38429cf2015-11-11 18:27:22 -080035namespace metrics_utils {
36
37// Transforms a ErrorCode value into a metrics::DownloadErrorCode.
38// This obviously only works for errors related to downloading so if |code|
39// is e.g. |ErrorCode::kFilesystemCopierError| then
40// |kDownloadErrorCodeInputMalformed| is returned.
41metrics::DownloadErrorCode GetDownloadErrorCode(ErrorCode code);
42
43// Transforms a ErrorCode value into a metrics::AttemptResult.
44//
45// If metrics::AttemptResult::kPayloadDownloadError is returned, you
46// can use utils::GetDownloadError() to get more detail.
47metrics::AttemptResult GetAttemptResult(ErrorCode code);
48
49// Calculates the internet connection type given |type| and |tethering|.
Sen Jiang255e22b2016-05-20 16:15:29 -070050metrics::ConnectionType GetConnectionType(ConnectionType type,
51 ConnectionTethering tethering);
Alex Deymo38429cf2015-11-11 18:27:22 -080052
Tianjie Xu90aaa102017-10-10 17:39:03 -070053// Returns the persisted value from prefs for the given key. It also
54// validates that the value returned is non-negative.
55int64_t GetPersistedValue(const std::string& key, PrefsInterface* prefs);
56
57// Persists the reboot count of the update attempt to |kPrefsNumReboots|.
58void SetNumReboots(int64_t num_reboots, PrefsInterface* prefs);
59
60// Persists the payload attempt number to |kPrefsPayloadAttemptNumber|.
61void SetPayloadAttemptNumber(int64_t payload_attempt_number,
62 PrefsInterface* prefs);
63
64// Persists the finished time of an update to the |kPrefsSystemUpdatedMarker|.
65void SetSystemUpdatedMarker(ClockInterface* clock, PrefsInterface* prefs);
66
Tianjie Xu2a0ea632018-08-06 12:59:23 -070067// Persists the start monotonic time of an update to
68// |kPrefsUpdateTimestampStart|.
Tianjie Xu90aaa102017-10-10 17:39:03 -070069void SetUpdateTimestampStart(const base::Time& update_start_time,
70 PrefsInterface* prefs);
71
Tianjie Xu2a0ea632018-08-06 12:59:23 -070072// Persists the start boot time of an update to
73// |kPrefsUpdateBootTimestampStart|.
74void SetUpdateBootTimestampStart(const base::Time& update_start_boot_time,
75 PrefsInterface* prefs);
76
Tianjie Xu90aaa102017-10-10 17:39:03 -070077// Called at program startup if the device booted into a new update.
78// The |time_to_reboot| parameter contains the (monotonic-clock) duration
79// from when the update successfully completed (the value in
80// |kPrefsSystemUpdatedMarker|) until the device was booted into the update
81// (current monotonic-clock time).
82bool LoadAndReportTimeToReboot(MetricsReporterInterface* metrics_reporter,
83 PrefsInterface* prefs,
84 ClockInterface* clock);
85
Alex Deymo38429cf2015-11-11 18:27:22 -080086} // namespace metrics_utils
87} // namespace chromeos_update_engine
88
89#endif // UPDATE_ENGINE_METRICS_UTILS_H_