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 #define MLOG_TAG "BackupReport"
17
18 #include "upgrade_restore_task_report.h"
19
20 #include <string>
21
22 #include "backup_hi_audit_helper.h"
23 #include "backup_log_const.h"
24 #include "backup_log_utils.h"
25 #include "hisysevent.h"
26 #include "media_file_utils.h"
27 #include "media_log.h"
28 #include "upgrade_restore_gallery_media_task.h"
29
30 namespace OHOS::Media {
31 static constexpr char MEDIA_LIBRARY[] = "MEDIALIBRARY";
ReportTask(const std::string & taskInfo)32 UpgradeRestoreTaskReport &UpgradeRestoreTaskReport::ReportTask(const std::string &taskInfo)
33 {
34 std::vector<MediaRestoreResultInfo> resultInfos =
35 UpgradeRestoreGalleryMediaTask().SetSceneCode(this->sceneCode_).SetTaskId(this->taskId_).LoadTask(taskInfo);
36 for (const auto &info : resultInfos) {
37 MEDIA_INFO_LOG("[STAT] GET restoreExInfo: %{public}s", info.ToString().c_str());
38 PostInfoDfx(info);
39 PostInfoAuditLog(info);
40 }
41 return *this;
42 }
43
Report(const std::string & type,const std::string & errorCode,const std::string & errorInfo)44 UpgradeRestoreTaskReport &UpgradeRestoreTaskReport::Report(const std::string &type, const std::string &errorCode,
45 const std::string &errorInfo)
46 {
47 MediaRestoreResultInfo resultInfo = UpgradeRestoreGalleryMediaTask()
48 .SetSceneCode(this->sceneCode_)
49 .SetTaskId(this->taskId_)
50 .Load(type, errorCode, errorInfo);
51 MEDIA_INFO_LOG("[%{public}s] %{public}s: %{public}s", type.c_str(), errorCode.c_str(), errorInfo.c_str());
52 PostInfoDfx(resultInfo);
53 PostInfoAuditLog(resultInfo);
54 return *this;
55 }
56
Report(const RestoreTaskInfo & taskInfo)57 UpgradeRestoreTaskReport &UpgradeRestoreTaskReport::Report(const RestoreTaskInfo &taskInfo)
58 {
59 MediaRestoreResultInfo resultInfo = UpgradeRestoreGalleryMediaTask()
60 .SetSceneCode(this->sceneCode_)
61 .SetTaskId(this->taskId_)
62 .Load(taskInfo);
63 MEDIA_INFO_LOG("[%{public}s] %{public}s", taskInfo.type.c_str(), resultInfo.ToString().c_str());
64 PostInfoDfx(resultInfo);
65 PostInfoAuditLog(resultInfo);
66 return *this;
67 }
68
ReportInAudit(const std::string & type,const std::string & errorCode,const std::string & errorInfo)69 UpgradeRestoreTaskReport &UpgradeRestoreTaskReport::ReportInAudit(const std::string &type, const std::string &errorCode,
70 const std::string &errorInfo)
71 {
72 MediaRestoreResultInfo resultInfo = UpgradeRestoreGalleryMediaTask()
73 .SetSceneCode(this->sceneCode_)
74 .SetTaskId(this->taskId_)
75 .Load(type, errorCode, errorInfo);
76 MEDIA_INFO_LOG("[%{public}s] %{public}s: %{public}s", type.c_str(), errorCode.c_str(), errorInfo.c_str());
77 PostInfoAuditLog(resultInfo);
78 return *this;
79 }
80
ReportError(const ErrorInfo & info)81 UpgradeRestoreTaskReport &UpgradeRestoreTaskReport::ReportError(const ErrorInfo &info)
82 {
83 std::string errorCode = std::to_string(info.error);
84 std::string errorInfo = BackupLogUtils::ErrorInfoToString(info);
85 MediaRestoreResultInfo resultInfo = UpgradeRestoreGalleryMediaTask()
86 .SetSceneCode(this->sceneCode_)
87 .SetTaskId(this->taskId_)
88 .Load("ErrorInfo", errorCode, errorInfo);
89 MEDIA_ERR_LOG("[Error] %{public}s: %{public}s", errorCode.c_str(), errorInfo.c_str());
90 PostInfoDfx(resultInfo);
91 PostErrorInfoAuditLog(info);
92 return *this;
93 }
94
ReportProgress(const std::string & status,const std::string & progressInfo)95 UpgradeRestoreTaskReport &UpgradeRestoreTaskReport::ReportProgress(const std::string &status,
96 const std::string &progressInfo)
97 {
98 MediaRestoreResultInfo resultInfo = UpgradeRestoreGalleryMediaTask()
99 .SetSceneCode(this->sceneCode_)
100 .SetTaskId(this->taskId_)
101 .Load("ProgressInfo", status, progressInfo);
102 MEDIA_INFO_LOG("[Progress] %{public}s: %{public}s", status.c_str(), progressInfo.c_str());
103 PostInfoDfx(resultInfo);
104 PostProgressInfoAuditLog(status, progressInfo);
105 return *this;
106 }
107
ReportProgress(const std::string & status,const std::string & progressInfo,uint64_t ongoingTotalNumber)108 UpgradeRestoreTaskReport &UpgradeRestoreTaskReport::ReportProgress(const std::string &status,
109 const std::string &progressInfo, uint64_t ongoingTotalNumber)
110 {
111 if (ongoingTotalNumber * ON_PROCESS_INTV % LOG_PROGRESS_INTV != 0) {
112 return *this;
113 }
114 return ReportProgress(status, progressInfo);
115 }
116
ReportTimeout(uint64_t ongoingTotalNumber)117 UpgradeRestoreTaskReport &UpgradeRestoreTaskReport::ReportTimeout(uint64_t ongoingTotalNumber)
118 {
119 if (ongoingTotalNumber * ON_PROCESS_INTV % LOG_TIMEOUT_INTV != 0) {
120 return *this;
121 }
122 std::string status = "timeout";
123 std::string progressInfo = std::to_string(ongoingTotalNumber * ON_PROCESS_INTV);
124 return ReportProgress(status, progressInfo);
125 }
126
ReportTotal(const std::string & errorCode,const std::string & totalInfo)127 UpgradeRestoreTaskReport &UpgradeRestoreTaskReport::ReportTotal(const std::string &errorCode,
128 const std::string &totalInfo)
129 {
130 return Report("TotalInfo", errorCode, totalInfo);
131 }
132
ReportUpgradeEnh(const std::string & errorCode,const std::string & info)133 UpgradeRestoreTaskReport &UpgradeRestoreTaskReport::ReportUpgradeEnh(const std::string &errorCode,
134 const std::string &info)
135 {
136 return Report("UpgradeEnh", errorCode, info);
137 }
138
ReportTimeCost(const uint64_t successCount,const uint64_t duplicateCount,const size_t failCount)139 UpgradeRestoreTaskReport &UpgradeRestoreTaskReport::ReportTimeCost(const uint64_t successCount,
140 const uint64_t duplicateCount, const size_t failCount)
141 {
142 int64_t startTime = std::atoll(this->taskId_.c_str());
143 int64_t endTime = MediaFileUtils::UTCTimeSeconds();
144 int64_t timeCost = endTime - startTime;
145 if (timeCost < 0) {
146 MEDIA_ERR_LOG("Get timeCost < 0, startTime: %{public}lld, %{public}lld", (long long)startTime,
147 (long long)endTime);
148 return *this;
149 }
150 std::string type = "TimeCost";
151 std::string errorCode = std::to_string(timeCost);
152 std::string errorInfo = "";
153 MediaRestoreResultInfo resultInfo = UpgradeRestoreGalleryMediaTask()
154 .SetSceneCode(this->sceneCode_)
155 .SetTaskId(this->taskId_)
156 .Load(type, errorCode, errorInfo);
157 resultInfo.duplicateCount = static_cast<int>(duplicateCount);
158 resultInfo.failedCount = static_cast<int>(failCount);
159 resultInfo.successCount = static_cast<int>(successCount);
160 MEDIA_INFO_LOG("[%{public}s]: %{public}s, successCount: %{public}d, duplicateCount: %{public}d, "
161 "failCount: %{public}d", type.c_str(), errorCode.c_str(), resultInfo.successCount, resultInfo.duplicateCount,
162 resultInfo.failedCount);
163 PostInfoDfx(resultInfo);
164 PostInfoAuditLog(resultInfo);
165 return *this;
166 }
167
ReportRestoreMode(int32_t restoreMode,uint64_t notFoundFileNum)168 UpgradeRestoreTaskReport &UpgradeRestoreTaskReport::ReportRestoreMode(int32_t restoreMode, uint64_t notFoundFileNum)
169 {
170 return Report("RestoreMode:NotFoundFileNum", std::to_string(restoreMode), std::to_string(notFoundFileNum));
171 }
172
PostInfoDfx(const MediaRestoreResultInfo & info)173 int32_t UpgradeRestoreTaskReport::PostInfoDfx(const MediaRestoreResultInfo &info)
174 {
175 int32_t ret = HiSysEventWrite(MEDIA_LIBRARY,
176 "MEDIALIB_BACKUP_RESTORE_RESULT",
177 HiviewDFX::HiSysEvent::EventType::STATISTIC,
178 "SCENE_CODE",
179 info.sceneCode,
180 "TASK_ID",
181 info.taskId,
182 "ERROR_CODE",
183 info.errorCode,
184 "ERROR_INFO",
185 info.errorInfo,
186 "TYPE",
187 info.type,
188 "BACKUP_INFO",
189 info.backupInfo,
190 "DUPLICATE_COUNT",
191 info.duplicateCount,
192 "FAILED_COUNT",
193 info.failedCount,
194 "SUCCESS_COUNT",
195 info.successCount);
196 if (ret != 0) {
197 MEDIA_ERR_LOG("PostInfoDfx error:%{public}d", ret);
198 }
199 return ret;
200 }
201
PostInfoAuditLog(const MediaRestoreResultInfo & info)202 int32_t UpgradeRestoreTaskReport::PostInfoAuditLog(const MediaRestoreResultInfo &info)
203 {
204 BackupHiAuditHelper().SetSceneCode(this->sceneCode_).SetTaskId(this->taskId_).WriteReportAuditLog(info.ToString());
205 return 0;
206 }
207
PostErrorInfoAuditLog(const ErrorInfo & info)208 int32_t UpgradeRestoreTaskReport::PostErrorInfoAuditLog(const ErrorInfo &info)
209 {
210 BackupHiAuditHelper().SetSceneCode(this->sceneCode_).SetTaskId(this->taskId_).WriteErrorAuditLog(info);
211 return 0;
212 }
213
PostProgressInfoAuditLog(const std::string & status,const std::string & progressInfo)214 int32_t UpgradeRestoreTaskReport::PostProgressInfoAuditLog(const std::string &status, const std::string &progressInfo)
215 {
216 BackupHiAuditHelper()
217 .SetSceneCode(this->sceneCode_)
218 .SetTaskId(this->taskId_)
219 .WriteProgressAuditLog(status, progressInfo);
220 return 0;
221 }
222 } // namespace OHOS::Media