• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ReportUpgradeEnh(const std::string & errorCode,const std::string & info)109 UpgradeRestoreTaskReport &UpgradeRestoreTaskReport::ReportUpgradeEnh(const std::string &errorCode,
110     const std::string &info)
111 {
112     return Report("UpgradeEnh", errorCode, info);
113 }
114 
ReportTimeCost()115 UpgradeRestoreTaskReport &UpgradeRestoreTaskReport::ReportTimeCost()
116 {
117     int64_t startTime = std::atoll(this->taskId_.c_str());
118     int64_t endTime = MediaFileUtils::UTCTimeSeconds();
119     int64_t timeCost = endTime - startTime;
120     if (timeCost < 0) {
121         MEDIA_ERR_LOG("Get timeCost < 0, startTime: %{public}lld, %{public}lld", (long long)startTime,
122             (long long)endTime);
123         return *this;
124     }
125     return Report("TimeCost", std::to_string(timeCost), "");
126 }
127 
ReportRestoreMode(int32_t restoreMode,uint64_t notFoundFileNum)128 UpgradeRestoreTaskReport &UpgradeRestoreTaskReport::ReportRestoreMode(int32_t restoreMode, uint64_t notFoundFileNum)
129 {
130     return Report("RestoreMode:NotFoundFileNum", std::to_string(restoreMode), std::to_string(notFoundFileNum));
131 }
132 
PostInfoDfx(const MediaRestoreResultInfo & info)133 int32_t UpgradeRestoreTaskReport::PostInfoDfx(const MediaRestoreResultInfo &info)
134 {
135     int32_t ret = HiSysEventWrite(MEDIA_LIBRARY,
136         "MEDIALIB_BACKUP_RESTORE_RESULT",
137         HiviewDFX::HiSysEvent::EventType::STATISTIC,
138         "SCENE_CODE",
139         info.sceneCode,
140         "TASK_ID",
141         info.taskId,
142         "ERROR_CODE",
143         info.errorCode,
144         "ERROR_INFO",
145         info.errorInfo,
146         "TYPE",
147         info.type,
148         "BACKUP_INFO",
149         info.backupInfo,
150         "DUPLICATE_COUNT",
151         info.duplicateCount,
152         "FAILED_COUNT",
153         info.failedCount,
154         "SUCCESS_COUNT",
155         info.successCount);
156     if (ret != 0) {
157         MEDIA_ERR_LOG("PostInfoDfx error:%{public}d", ret);
158     }
159     return ret;
160 }
161 
PostInfoAuditLog(const MediaRestoreResultInfo & info)162 int32_t UpgradeRestoreTaskReport::PostInfoAuditLog(const MediaRestoreResultInfo &info)
163 {
164     BackupHiAuditHelper().SetSceneCode(this->sceneCode_).SetTaskId(this->taskId_).WriteReportAuditLog(info.ToString());
165     return 0;
166 }
167 
PostErrorInfoAuditLog(const ErrorInfo & info)168 int32_t UpgradeRestoreTaskReport::PostErrorInfoAuditLog(const ErrorInfo &info)
169 {
170     BackupHiAuditHelper().SetSceneCode(this->sceneCode_).SetTaskId(this->taskId_).WriteErrorAuditLog(info);
171     return 0;
172 }
173 
PostProgressInfoAuditLog(const std::string & status,const std::string & progressInfo)174 int32_t UpgradeRestoreTaskReport::PostProgressInfoAuditLog(const std::string &status, const std::string &progressInfo)
175 {
176     BackupHiAuditHelper()
177         .SetSceneCode(this->sceneCode_)
178         .SetTaskId(this->taskId_)
179         .WriteProgressAuditLog(status, progressInfo);
180     return 0;
181 }
182 }  // namespace OHOS::Media