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 
ReportError(const ErrorInfo & info)57 UpgradeRestoreTaskReport &UpgradeRestoreTaskReport::ReportError(const ErrorInfo &info)
58 {
59     std::string errorCode = std::to_string(info.error);
60     std::string errorInfo = BackupLogUtils::ErrorInfoToString(info);
61     MediaRestoreResultInfo resultInfo = UpgradeRestoreGalleryMediaTask()
62                                             .SetSceneCode(this->sceneCode_)
63                                             .SetTaskId(this->taskId_)
64                                             .Load("ErrorInfo", errorCode, errorInfo);
65     MEDIA_ERR_LOG("[Error] %{public}s: %{public}s", errorCode.c_str(), errorInfo.c_str());
66     PostInfoDfx(resultInfo);
67     PostErrorInfoAuditLog(info);
68     return *this;
69 }
70 
ReportProgress(const std::string & status,const std::string & progressInfo)71 UpgradeRestoreTaskReport &UpgradeRestoreTaskReport::ReportProgress(const std::string &status,
72     const std::string &progressInfo)
73 {
74     MediaRestoreResultInfo resultInfo = UpgradeRestoreGalleryMediaTask()
75                                             .SetSceneCode(this->sceneCode_)
76                                             .SetTaskId(this->taskId_)
77                                             .Load("ProgressInfo", status, progressInfo);
78     MEDIA_INFO_LOG("[Progress] %{public}s: %{public}s", status.c_str(), progressInfo.c_str());
79     PostInfoDfx(resultInfo);
80     PostProgressInfoAuditLog(status, progressInfo);
81     return *this;
82 }
83 
ReportProgress(const std::string & status,const std::string & progressInfo,uint64_t ongoingTotalNumber)84 UpgradeRestoreTaskReport &UpgradeRestoreTaskReport::ReportProgress(const std::string &status,
85     const std::string &progressInfo, uint64_t ongoingTotalNumber)
86 {
87     if (ongoingTotalNumber * ON_PROCESS_INTV % LOG_PROGRESS_INTV != 0) {
88         return *this;
89     }
90     return ReportProgress(status, progressInfo);
91 }
92 
ReportTimeout(uint64_t ongoingTotalNumber)93 UpgradeRestoreTaskReport &UpgradeRestoreTaskReport::ReportTimeout(uint64_t ongoingTotalNumber)
94 {
95     if (ongoingTotalNumber * ON_PROCESS_INTV % LOG_TIMEOUT_INTV != 0) {
96         return *this;
97     }
98     std::string status = "timeout";
99     std::string progressInfo = std::to_string(ongoingTotalNumber * ON_PROCESS_INTV);
100     return ReportProgress(status, progressInfo);
101 }
102 
ReportTotal(const std::string & errorCode,const std::string & totalInfo)103 UpgradeRestoreTaskReport &UpgradeRestoreTaskReport::ReportTotal(const std::string &errorCode,
104     const std::string &totalInfo)
105 {
106     return Report("TotalInfo", errorCode, totalInfo);
107 }
108 
ReportTimeCost()109 UpgradeRestoreTaskReport &UpgradeRestoreTaskReport::ReportTimeCost()
110 {
111     int64_t startTime = std::atoll(this->taskId_.c_str());
112     int64_t endTime = MediaFileUtils::UTCTimeSeconds();
113     int64_t timeCost = endTime - startTime;
114     if (timeCost < 0) {
115         MEDIA_ERR_LOG("Get timeCost < 0, startTime: %{public}lld, %{public}lld", (long long)startTime,
116             (long long)endTime);
117         return *this;
118     }
119     return Report("TimeCost", std::to_string(timeCost), "");
120 }
121 
PostInfoDfx(const MediaRestoreResultInfo & info)122 int32_t UpgradeRestoreTaskReport::PostInfoDfx(const MediaRestoreResultInfo &info)
123 {
124     int32_t ret = HiSysEventWrite(MEDIA_LIBRARY,
125         "MEDIALIB_BACKUP_RESTORE_RESULT",
126         HiviewDFX::HiSysEvent::EventType::STATISTIC,
127         "SCENE_CODE",
128         info.sceneCode,
129         "TASK_ID",
130         info.taskId,
131         "ERROR_CODE",
132         info.errorCode,
133         "ERROR_INFO",
134         info.errorInfo,
135         "TYPE",
136         info.type,
137         "BACKUP_INFO",
138         info.backupInfo,
139         "DUPLICATE_COUNT",
140         info.duplicateCount,
141         "FAILED_COUNT",
142         info.failedCount,
143         "SUCCESS_COUNT",
144         info.successCount);
145     if (ret != 0) {
146         MEDIA_ERR_LOG("PostInfoDfx error:%{public}d", ret);
147     }
148     return ret;
149 }
150 
PostInfoAuditLog(const MediaRestoreResultInfo & info)151 int32_t UpgradeRestoreTaskReport::PostInfoAuditLog(const MediaRestoreResultInfo &info)
152 {
153     BackupHiAuditHelper().SetSceneCode(this->sceneCode_).SetTaskId(this->taskId_).WriteReportAuditLog(info.ToString());
154     return 0;
155 }
156 
PostErrorInfoAuditLog(const ErrorInfo & info)157 int32_t UpgradeRestoreTaskReport::PostErrorInfoAuditLog(const ErrorInfo &info)
158 {
159     BackupHiAuditHelper().SetSceneCode(this->sceneCode_).SetTaskId(this->taskId_).WriteErrorAuditLog(info);
160     return 0;
161 }
162 
PostProgressInfoAuditLog(const std::string & status,const std::string & progressInfo)163 int32_t UpgradeRestoreTaskReport::PostProgressInfoAuditLog(const std::string &status, const std::string &progressInfo)
164 {
165     BackupHiAuditHelper()
166         .SetSceneCode(this->sceneCode_)
167         .SetTaskId(this->taskId_)
168         .WriteProgressAuditLog(status, progressInfo);
169     return 0;
170 }
171 }  // namespace OHOS::Media