1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_FILEMGMT_BACKUP_B_RADAR_H
17 #define OHOS_FILEMGMT_BACKUP_B_RADAR_H
18 #include <chrono>
19 #include <string>
20 #include "i_service_reverse.h"
21 
22 namespace OHOS::FileManagement::Backup {
23 enum class BizStageBackup : int32_t {
24     BIZ_STAGE_DEFAULT = 0,
25     BIZ_STAGE_BOOT_BACKUP_SA_FAIL,
26     BIZ_STAGE_GET_LOCAL_CAPABILITIES_FAIL,
27     BIZ_STAGE_CREATE_BACKUP_SESSION_FAIL,
28     BIZ_STAGE_APPEND_BUNDLES_FAIL,
29     BIZ_STAGE_CONNECT_EXTENSION_FAIL,
30     BIZ_STAGE_START_DISPOSE,
31     BIZ_STAGE_EXTENSION_ABNORMAL_EXIT_CLEAR_FAIL,
32     BIZ_STAGE_GET_BACKUP_INFO_FAIL,
33     BIZ_STAGE_ON_BACKUP,
34     BIZ_STAGE_DO_BACKUP,
35     BIZ_STAGE_CHECK_DATA_FAIL,
36     BIZ_STAGE_END_DISPOSE,
37     BIZ_STAGE_PERMISSION_CHECK_FAIL,
38     BIZ_STAGE_EXECU_FAIL,
39     BIZ_STAGE_ACTIVE_SESSION,
40     BIZ_STAGE_DEACTIVE_SESSION,
41     BIZ_STAGE_RELEASE,
42     BIZ_STAGE_ONSTART_DISPOSE,
43     BIZ_STAGE_ONSTART_RESIDUAL
44 };
45 
46 enum class BizStageRestore : int32_t {
47     BIZ_STAGE_DEFAULT = 0,
48     BIZ_STAGE_BOOT_BACKUP_SA_FAIL,
49     BIZ_STAGE_GET_LOCAL_CAPABILITIES_FAIL,
50     BIZ_STAGE_CREATE_RESTORE_SESSION_FAIL,
51     BIZ_STAGE_APPEND_BUNDLES_FAIL,
52     BIZ_STAGE_CONNECT_EXTENSION_FAIL,
53     BIZ_STAGE_START_DISPOSE,
54     BIZ_STAGE_EXTENSION_ABNORMAL_EXIT_CLEAR_FAIL,
55     BIZ_STAGE_GET_FILE_HANDLE_FAIL,
56     BIZ_STAGE_DO_RESTORE,
57     BIZ_STAGE_CHECK_DATA_FAIL,
58     BIZ_STAGE_ON_RESTORE,
59     BIZ_STAGE_END_DISPOSE,
60     BIZ_STAGE_PERMISSION_CHECK_FAIL,
61     BIZ_STAGE_EXECU_FAIL,
62     BIZ_STAGE_ACTIVE_SESSION,
63     BIZ_STAGE_DEACTIVE_SESSION,
64     BIZ_STAGE_RELEASE,
65     BIZ_STAGE_ONSTART_DISPOSE,
66     BIZ_STAGE_ONSTART_RESIDUAL
67 };
68 
69 class AppRadar {
70 public:
GetInstance()71     static AppRadar &GetInstance()
72     {
73         static AppRadar instance;
74         return instance;
75     }
76 
77 public:
78     struct Info {
79         std::string bundleName;
80         std::string status;
81         std::string resInfo;
82 
InfoInfo83         Info(const std::string &bundleName, const std::string &status, const std::string &resInfo)
84             : bundleName(bundleName), status(status), resInfo(resInfo) {}
85     };
86 
87     struct DoRestoreInfo {
88         uint32_t bigFileNum;
89         uint64_t bigFileSize;
90         int64_t bigFileSpendTime;
91         uint32_t tarFileNum;
92         uint64_t tarFileSize;
93         int64_t tarFileSpendTime;
94         int64_t totalFileSpendTime;
95     };
96 
97     struct DoBackupInfo {
98         int64_t cost;
99         uint32_t allFileNum;
100         uint32_t smallFileNum;
101         uint32_t tarFileNum;
102         uint32_t includeNum;
103         uint32_t excludeNum;
104     };
105 
106     struct StatInfo {
107         std::string callerName;
108         std::string resInfo;
109 
StatInfoStatInfo110         StatInfo(const std::string &callerName, const std::string &resInfo)
111             : callerName(callerName), resInfo(resInfo) {}
112     };
113 
114 public:
115     int32_t GetUserId();
116     void RecordDefaultFuncRes(Info &info, const std::string &func, int32_t userId,
117                               enum BizStageBackup bizStage, int32_t resultCode);
118     void RecordBackupFuncRes(Info &info, const std::string &func, int32_t userId,
119                              enum BizStageBackup bizStage, int32_t resultCode);
120     void RecordRestoreFuncRes(Info &info, const std::string &func, int32_t userId,
121                               enum BizStageRestore bizStage, int32_t resultCode);
122     void RecordStatisticRes(StatInfo &statInfo, int32_t userId, enum IServiceReverse::Scenario scenario,
123                             int32_t succ_cnt, int32_t fail_cnt, int32_t resultCode);
124 private:
125     AppRadar() = default;
126     ~AppRadar() = default;
127     AppRadar(const AppRadar &) = delete;
128     AppRadar &operator=(const AppRadar &) = delete;
129     AppRadar(AppRadar &&) = delete;
130     AppRadar &operator=(AppRadar &&) = delete;
131 };
132 } // namespace OHOS::FileManagement::AppRadar
133 #endif // OHOS_FILEMGMT_BACKUP_B_RADAR_H
134