• 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 #define MLOG_TAG "DfxReporter"
16 
17 #include "dfx_reporter.h"
18 
19 #include <vector>
20 
21 #include "dfx_const.h"
22 #include "dfx_utils.h"
23 #include "dfx_database_utils.h"
24 #include "medialibrary_errno.h"
25 #include "media_file_utils.h"
26 #include "media_log.h"
27 #include "hisysevent.h"
28 #include "preferences.h"
29 #include "preferences_helper.h"
30 #include "medialibrary_data_manager_utils.h"
31 
32 using namespace std;
33 namespace OHOS {
34 namespace Media {
35 static constexpr char MEDIA_LIBRARY[] = "MEDIALIBRARY";
36 
DfxReporter()37 DfxReporter::DfxReporter()
38 {
39 }
40 
~DfxReporter()41 DfxReporter::~DfxReporter()
42 {
43 }
44 
ReportTimeOutOperation(std::string & bundleName,int32_t type,int32_t object,int32_t time)45 void DfxReporter::ReportTimeOutOperation(std::string &bundleName, int32_t type, int32_t object, int32_t time)
46 {
47     int ret = HiSysEventWrite(
48         MEDIA_LIBRARY,
49         "MEDIALIB_TIMEOUT_ERROR",
50         HiviewDFX::HiSysEvent::EventType::FAULT,
51         "BUNDLE_NAME", bundleName,
52         "OPERATION_TYPE", type,
53         "OPERATION_OBJECT", object,
54         "TIME", time);
55     if (ret != 0) {
56         MEDIA_ERR_LOG("ReportTimeoutOperation error:%{public}d", ret);
57     }
58 }
59 
ReportHighMemoryImageThumbnail(std::string & path,std::string & suffix,int32_t width,int32_t height)60 int32_t DfxReporter::ReportHighMemoryImageThumbnail(std::string &path, std::string &suffix, int32_t width,
61     int32_t height)
62 {
63     if (suffix != "jpg" && suffix != "jpeg" && suffix != "jpe") {
64         MEDIA_WARN_LOG("image %{public}s is %{public}s, width: %{public}d, height: %{public}d", path.c_str(),
65             suffix.c_str(), width, height);
66         return OTHER_FORMAT_IMAGE;
67     } else if (width > IMAGE_MIN && height > IMAGE_MIN) {
68         MEDIA_WARN_LOG("image %{public}s is too large, width: %{public}d, height: %{public}d", path.c_str(), width,
69             height);
70         return BIG_IMAGE;
71     }
72     return COMMON_IMAGE;
73 }
74 
ReportHighMemoryVideoThumbnail(std::string & path,std::string & suffix,int32_t width,int32_t height)75 int32_t DfxReporter::ReportHighMemoryVideoThumbnail(std::string &path, std::string &suffix, int32_t width,
76     int32_t height)
77 {
78     if (width >= VIDEO_8K_MIN || height >= VIDEO_8K_MIN) {
79         MEDIA_WARN_LOG("video %{public}s is too large, width: %{public}d, height: %{public}d", path.c_str(), width,
80             height);
81         return BIG_VIDEO;
82     }
83     return COMMON_VIDEO;
84 }
85 
ReportThumbnailError()86 void DfxReporter::ReportThumbnailError()
87 {
88     int32_t errCode;
89     shared_ptr<NativePreferences::Preferences> prefs =
90         NativePreferences::PreferencesHelper::GetPreferences(THUMBNAIL_ERROR_XML, errCode);
91     if (!prefs) {
92         MEDIA_ERR_LOG("get preferences error: %{public}d", errCode);
93         return;
94     }
95     map<string, NativePreferences::PreferencesValue> errorMap = prefs->GetAll();
96     for (auto &erroInfo : errorMap) {
97         string key = erroInfo.first;
98         string value = erroInfo.second;
99         vector<string> thumbnailInfo = DfxUtils::Split(key, SPLIT_CHAR);
100         if (thumbnailInfo.empty() || thumbnailInfo.size() < 3) { // 3 means length of key
101             continue;
102         }
103         // 0 means index of path
104         string path = thumbnailInfo[0];
105         // 1 means index of method
106         int32_t method = MediaLibraryDataManagerUtils::IsNumber(thumbnailInfo[1]) ? stoi(thumbnailInfo[1]) : 0;
107         // 2 means index of error code
108         int32_t errorCode = MediaLibraryDataManagerUtils::IsNumber(thumbnailInfo[2]) ? stoi(thumbnailInfo[2]) : 0;
109         int64_t time = MediaLibraryDataManagerUtils::IsNumber(value) ? stol(value) : 0;
110         int ret = HiSysEventWrite(
111             MEDIA_LIBRARY,
112             "MEDIALIB_THUMBNAIL_ERROR",
113             HiviewDFX::HiSysEvent::EventType::FAULT,
114             "PATH", path,
115             "METHOD", method,
116             "ERROR_CODE", errorCode,
117             "TIME", time);
118         if (ret != 0) {
119             MEDIA_ERR_LOG("ReportThumbnailError error:%{public}d", ret);
120         }
121     }
122     prefs->Clear();
123     prefs->FlushSync();
124 }
125 
ReportCommonBehavior()126 void DfxReporter::ReportCommonBehavior()
127 {
128     int32_t errCode;
129     shared_ptr<NativePreferences::Preferences> prefs =
130         NativePreferences::PreferencesHelper::GetPreferences(COMMON_BEHAVIOR_XML, errCode);
131     if (!prefs) {
132         MEDIA_ERR_LOG("get preferences error: %{public}d", errCode);
133         return;
134     }
135     map<string, NativePreferences::PreferencesValue> errorMap = prefs->GetAll();
136     for (auto &erroInfo : errorMap) {
137         string bundleName = erroInfo.first;
138         int32_t times = static_cast<int32_t>(erroInfo.second);
139         int ret = HiSysEventWrite(
140             MEDIA_LIBRARY,
141             "MEDIALIB_COMMON_STATISTIC",
142             HiviewDFX::HiSysEvent::EventType::STATISTIC,
143             "BUNDLE_NAME", bundleName,
144             "TIMES", times);
145         if (ret != 0) {
146             MEDIA_ERR_LOG("ReportCommonBehavior error:%{public}d", ret);
147         }
148     }
149     prefs->Clear();
150     prefs->FlushSync();
151 }
152 
ReportDeleteStatistic()153 void DfxReporter::ReportDeleteStatistic()
154 {
155     int32_t errCode;
156     shared_ptr<NativePreferences::Preferences> prefs =
157         NativePreferences::PreferencesHelper::GetPreferences(DELETE_BEHAVIOR_XML, errCode);
158     if (!prefs) {
159         MEDIA_ERR_LOG("get preferences error: %{public}d", errCode);
160         return;
161     }
162     map<string, NativePreferences::PreferencesValue> deleteMap = prefs->GetAll();
163     for (auto &info : deleteMap) {
164         string key = info.first;
165         vector<string> deleteInfo = DfxUtils::Split(key, SPLIT_CHAR);
166         if (deleteInfo.empty() || deleteInfo.size() < 2) { // 2 means length of key
167             continue;
168         }
169         // 0 means index of bundleName
170         string bundleName = deleteInfo[0];
171         // 1 means index of type
172         int32_t type = MediaLibraryDataManagerUtils::IsNumber(deleteInfo[1]) ? stoi(deleteInfo[1]) : 0;
173         int32_t times = static_cast<int32_t>(info.second);
174         int ret = HiSysEventWrite(
175             MEDIA_LIBRARY,
176             "MEDIALIB_DELETE_STATISTIC",
177             HiviewDFX::HiSysEvent::EventType::STATISTIC,
178             "BUNDLE_NAME", bundleName,
179             "TYPE", type,
180             "TIMES", times);
181         if (ret != 0) {
182             MEDIA_ERR_LOG("ReportDeleteBehavior error:%{public}d", ret);
183         }
184     }
185     prefs->Clear();
186     prefs->FlushSync();
187 }
188 
ReportDeleteBehavior(string bundleName,int32_t type,std::string path)189 void DfxReporter::ReportDeleteBehavior(string bundleName, int32_t type, std::string path)
190 {
191     if (bundleName == "" || path == "") {
192         return;
193     }
194     MEDIA_WARN_LOG("%{public}s do %{public}d on %{public}s", bundleName.c_str(), type, path.c_str());
195     int ret = HiSysEventWrite(
196         MEDIA_LIBRARY,
197         "MEDIALIB_DELETE_BEHAVIOR",
198         HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
199         "BUNDLE_NAME", bundleName,
200         "TYPE", type,
201         "PATH", path);
202     if (ret != 0) {
203         MEDIA_ERR_LOG("ReportDeleteBehavior error:%{public}d", ret);
204     }
205 }
206 
ReportThumbnailGeneration(const ThumbnailData::GenerateStats & stats)207 void DfxReporter::ReportThumbnailGeneration(const ThumbnailData::GenerateStats &stats)
208 {
209     int ret = HiSysEventWrite(
210         MEDIA_LIBRARY,
211         "MEDIALIB_THUMBNAIL_GENERATION",
212         HiviewDFX::HiSysEvent::EventType::STATISTIC,
213         "URI", stats.uri,
214         "SCENE", static_cast<int32_t>(stats.scene),
215         "OPEN_THUMB_COST", stats.openThumbCost,
216         "OPEN_LCD_COST", stats.openLcdCost,
217         "SOURCE_TYPE", static_cast<int32_t>(stats.sourceType),
218         "SOURCE_WIDTH", stats.sourceWidth,
219         "SOURCE_HEIGHT", stats.sourceHeight,
220         "TOTAL_COST", static_cast<int32_t>(stats.totalCost),
221         "ERROR_CODE", stats.errorCode);
222     if (ret != 0) {
223         MEDIA_ERR_LOG("ReportThumbnailGeneration error:%{public}d", ret);
224     }
225 }
226 
ReportPhotoInfo(int32_t localImageCount,int32_t localVideoCount,int32_t cloudImageCount,int32_t cloudVideoCount)227 void DfxReporter::ReportPhotoInfo(int32_t localImageCount, int32_t localVideoCount, int32_t cloudImageCount,
228     int32_t cloudVideoCount)
229 {
230     int ret = HiSysEventWrite(
231         MEDIA_LIBRARY,
232         "MEDIALIB_PHOTO_INFO",
233         HiviewDFX::HiSysEvent::EventType::STATISTIC,
234         "LOCAL_IMAGE_COUNT", localImageCount,
235         "LOCAL_VIDEO_COUNT", localVideoCount,
236         "CLOUD_IMAGE_COUNT", cloudImageCount,
237         "CLOUD_VIDEO_COUNT", cloudVideoCount);
238     if (ret != 0) {
239         MEDIA_ERR_LOG("ReportPhotoInfo error:%{public}d", ret);
240     }
241 }
242 
ReportAlbumInfo(const std::string & albumName,int32_t albumImageCount,int32_t albumVideoCount,bool isLocal)243 void DfxReporter::ReportAlbumInfo(const std::string &albumName, int32_t albumImageCount, int32_t albumVideoCount,
244     bool isLocal)
245 {
246     int ret = HiSysEventWrite(
247         MEDIA_LIBRARY,
248         "MEDIALIB_ALBUM_INFO",
249         HiviewDFX::HiSysEvent::EventType::STATISTIC,
250         "ALBUM_NAME", albumName,
251         "ALBUM_IMAGE_COUNT", albumImageCount,
252         "ALBUM_VIDEO_COUNT", albumVideoCount,
253         "IS_LOCAL", isLocal);
254     if (ret != 0) {
255         MEDIA_ERR_LOG("ReportAlbumInfo error:%{public}d", ret);
256     }
257 }
258 
ReportDirtyCloudPhoto(const std::string & data,int32_t dirty,int32_t cloudVersion)259 void DfxReporter::ReportDirtyCloudPhoto(const std::string &data, int32_t dirty, int32_t cloudVersion)
260 {
261     int ret = HiSysEventWrite(
262         MEDIA_LIBRARY,
263         "MEDIALIB_DIRTY_CLOUD_PHOTO",
264         HiviewDFX::HiSysEvent::EventType::STATISTIC,
265         "PATH", data,
266         "DIRTY", dirty,
267         "CLOUD_VERSION", cloudVersion);
268     if (ret != 0) {
269         MEDIA_ERR_LOG("ReportDirtyCloudPhoto error:%{public}d", ret);
270     }
271 }
272 
ReportCommonVersion(int32_t dbVersion)273 void DfxReporter::ReportCommonVersion(int32_t dbVersion)
274 {
275     MEDIA_INFO_LOG("dbVersion: %{public}d, thumbnailVersion: %{public}d", dbVersion, THUMBNAIL_VERSION);
276     int ret = HiSysEventWrite(
277         MEDIA_LIBRARY,
278         "MEDIALIB_COMMON_VERSION",
279         HiviewDFX::HiSysEvent::EventType::STATISTIC,
280         "DB_VERSION", dbVersion,
281         "THUMBNAIL_VERSION", THUMBNAIL_VERSION);
282     if (ret != 0) {
283         MEDIA_ERR_LOG("ReportCommonVersion error:%{public}d", ret);
284     }
285 }
286 
ReportAnalysisVersion(const std::string & analysisName,int32_t version)287 void DfxReporter::ReportAnalysisVersion(const std::string &analysisName, int32_t version)
288 {
289     int ret = HiSysEventWrite(
290         MEDIA_LIBRARY,
291         "MEDIALIB_ANALYSIS_VERSION",
292         HiviewDFX::HiSysEvent::EventType::STATISTIC,
293         "NAME", analysisName,
294         "VERSION", version);
295     if (ret != 0) {
296         MEDIA_ERR_LOG("ReportAnalysisVersion error:%{public}d", ret);
297     }
298 }
299 
ReportAdaptationToMovingPhoto()300 void DfxReporter::ReportAdaptationToMovingPhoto()
301 {
302     int32_t errCode;
303     shared_ptr<NativePreferences::Preferences> prefs =
304         NativePreferences::PreferencesHelper::GetPreferences(ADAPTATION_TO_MOVING_PHOTO_XML, errCode);
305     if (!prefs) {
306         MEDIA_ERR_LOG("get preferences error: %{public}d", errCode);
307         return;
308     }
309 
310     string date = DfxUtils::GetCurrentDate();
311     string unadaptedAppPackages = prefs->GetString(MOVING_PHOTO_KEY_UNADAPTED_PACKAGE);
312     string adaptedAppPackages = prefs->GetString(MOVING_PHOTO_KEY_ADAPTED_PACKAGE);
313     int32_t unadaptedAppNum = prefs->GetInt(MOVING_PHOTO_KEY_UNADAPTED_NUM);
314     int32_t adaptedAppNum = prefs->GetInt(MOVING_PHOTO_KEY_ADAPTED_NUM);
315 
316     int ret = HiSysEventWrite(
317         MEDIA_LIBRARY,
318         "MEDIALIB_MOVING_PHOTO_ADAPT",
319         HiviewDFX::HiSysEvent::EventType::STATISTIC,
320         "DATE", date,
321         "UNADAPTED_APP_NUM", unadaptedAppNum,
322         "UNADAPTED_APP_PACKAGE", unadaptedAppPackages,
323         "ADAPTED_APP_NUM", adaptedAppNum,
324         "ADAPTED_APP_PACKAGE", adaptedAppPackages);
325     if (ret != 0) {
326         MEDIA_ERR_LOG("Report adaptation to moving photo error:%{public}d", ret);
327     }
328 
329     prefs->Clear();
330     prefs->FlushSync();
331 }
332 
ReportStartResult(int32_t scene,int32_t error,int32_t start)333 void DfxReporter::ReportStartResult(int32_t scene, int32_t error, int32_t start)
334 {
335     int32_t cost = static_cast<int32_t>(MediaFileUtils::UTCTimeMilliSeconds() - start);
336     MEDIA_ERR_LOG("SCENE:%{public}d, ERROR:%{public}d, TIME:%{public}d", scene, error, cost);
337     int ret = HiSysEventWrite(
338         MEDIA_LIBRARY,
339         "MEDIALIB_START_RESULT",
340         HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
341         "SCENE", scene,
342         "ERROR", error,
343         "TIME", cost);
344     if (ret != 0) {
345         MEDIA_ERR_LOG("Report startResult error:%{public}d", ret);
346     }
347 }
348 
SecondsToTime(const int64_t & seconds)349 std::string SecondsToTime(const int64_t& seconds)
350 {
351     int32_t remain_seconds = seconds;
352     int32_t hour = seconds / ONE_HOUR;
353     remain_seconds = seconds - ONE_HOUR * hour;
354     int32_t minute = remain_seconds / ONE_MINUTE;
355     remain_seconds = remain_seconds - minute * ONE_MINUTE;
356     return std::to_string(hour) + "_h_" + std::to_string(minute) + "_m_" + std::to_string(remain_seconds) + "_s";
357 }
358 
ReportCloudSyncThumbGenerationStatus(const int32_t & downloadedThumb,const int32_t & generatedThumb,const int32_t & totalDownload)359 int32_t DfxReporter::ReportCloudSyncThumbGenerationStatus(const int32_t& downloadedThumb,
360     const int32_t& generatedThumb, const int32_t& totalDownload)
361 {
362     if (totalDownload == 0) {
363         return 0;
364     }
365     int32_t errCode;
366     shared_ptr<NativePreferences::Preferences> prefs =
367         NativePreferences::PreferencesHelper::GetPreferences(DFX_COMMON_XML, errCode);
368     if (!prefs) {
369         MEDIA_ERR_LOG("get dfx common preferences error: %{public}d", errCode);
370         return 0;
371     }
372     int64_t start = prefs->GetLong(CLOUD_SYNC_START_TIME, 0);
373     int64_t now = MediaFileUtils::UTCTimeSeconds();
374     int64_t cost = now - start;
375     time_t startTime = start + ONE_HOUR * 8;
376     std::string astcStartTime = asctime(gmtime(&startTime));
377     float total = static_cast<float>(totalDownload);
378     int ret = HiSysEventWrite(
379         MEDIA_LIBRARY,
380         "CLOUD_THUMB_GENERATE_STATISTIC",
381         HiviewDFX::HiSysEvent::EventType::STATISTIC,
382         "CLOUD_DOWN_START", astcStartTime,
383         "DOWNLOADED_THUMB_NUM", downloadedThumb,
384         "DOWNLOADED_THUMB_RATIO", downloadedThumb / total,
385         "GENERATED_THUMB_NUM", generatedThumb,
386         "GENERATED_THUMB_RATIO", generatedThumb / total,
387         "CLOUD_DOWN_TOTAL_DURATION", SecondsToTime(cost));
388     if (ret != 0) {
389         MEDIA_ERR_LOG("Report CloudSyncThumbGenerationStatus error:%{public}d", ret);
390     }
391     return ret;
392 }
393 
ReportPhotoRecordInfo()394 void DfxReporter::ReportPhotoRecordInfo()
395 {
396     PhotoRecordInfo photoRecordInfo;
397     int32_t result = DfxDatabaseUtils::QueryPhotoRecordInfo(photoRecordInfo);
398     if (result != 0) {
399         MEDIA_ERR_LOG("QueryPhotoRecordInfo error:%{public}d", result);
400         return;
401     }
402     int32_t imageCount = photoRecordInfo.imageCount;
403     int32_t videoCount = photoRecordInfo.videoCount;
404     int32_t abnormalSizeCount = photoRecordInfo.abnormalSizeCount;
405     int32_t abnormalWidthOrHeightCount = photoRecordInfo.abnormalWidthOrHeightCount;
406     int32_t abnormalVideoDurationCount = photoRecordInfo.abnormalVideoDurationCount;
407     int32_t toBeUpdatedRecordCount = photoRecordInfo.toBeUpdatedRecordCount;
408     int64_t dbFileSize = photoRecordInfo.dbFileSize;
409     int ret = HiSysEventWrite(
410         MEDIA_LIBRARY,
411         "MEDIALIB_DATABASE_INFO",
412         HiviewDFX::HiSysEvent::EventType::STATISTIC,
413         "DB_FILE_SIZE", dbFileSize,
414         "IMAGE_COUNT", imageCount,
415         "VIDEO_COUNT", videoCount,
416         "ABNORMAL_SIZE_COUNT", abnormalSizeCount,
417         "ABNORMAL_WIDTH_OR_HEIGHT_COUNT", abnormalWidthOrHeightCount,
418         "ABNORMAL_VIDEO_DURATION_COUNT", abnormalVideoDurationCount,
419         "ABNORMAL_COUNT_TO_UPDATE", toBeUpdatedRecordCount);
420     if (ret != 0) {
421         MEDIA_ERR_LOG("ReportPhotoRecordInfo error:%{public}d", ret);
422     }
423 }
424 
ReportMedialibraryAPI(const string & callerPackage,const string & saveUri)425 int32_t DfxReporter::ReportMedialibraryAPI(const string& callerPackage, const string& saveUri)
426 {
427     string currentDate = DfxUtils::GetCurrentDate();
428     int ret = HiSysEventWrite(
429         MEDIA_LIBRARY,
430         "MEDIALIB_DEPRECATED_API_USAGE",
431         HiviewDFX::HiSysEvent::EventType::STATISTIC,
432         "DATE", currentDate,
433         "CALLER_APP_PACKAGE", callerPackage,
434         "SAVE_URI", saveUri,
435         "READ_URI", "");
436     if (ret != 0) {
437         MEDIA_ERR_LOG("ReportMedialibraryAPI failed, ret: %{public}d", ret);
438         return E_FAIL;
439     }
440     return E_SUCCESS;
441 }
442 
ReportAlbumFusion(const AlbumFusionDfxDataPoint & reportData)443 int32_t DfxReporter::ReportAlbumFusion(const AlbumFusionDfxDataPoint& reportData)
444 {
445     int ret = HiSysEventWrite(
446         MEDIA_LIBRARY,
447         "MEDIALIB_ALBUM_FUSION_SINGLE",
448         HiviewDFX::HiSysEvent::EventType::STATISTIC,
449         "ALBUM_FUSION_TAG", reportData.albumFusionTag,
450         "REPORT_TIME_STAMP", reportData.reportTimeStamp,
451         "ALBUM_FUSION_STATE", reportData.albumFusionState,
452         "IMAGE_ASSET_COUNT", reportData.imageAssetCount,
453         "VIDEO_ASSET_COUNT", reportData.videoAssetCount,
454         "NUMBER_OF_SOURCE_ALBUM", reportData.numberOfSourceAlbum,
455         "NUMBER_OF_USER_ALBUM", reportData.numberOfUserAlbum,
456         "TOTAL_ASSETS_IN_SOURCE_ALBUMS", reportData.totalAssetsInSourceAlbums,
457         "TOTAL_ASSETS_IN_USER_ALBUMS", reportData.totalAssetsInUserAlbums,
458         "ALBUM_DETAILS", reportData.albumDetails,
459         "HIDDEN_ASSET_COUNT", reportData.hiddenAssetInfo);
460     if (ret != 0) {
461         MEDIA_ERR_LOG("ALBUM FUSION report data failed, ret: %{public}d", ret);
462         return E_FAIL;
463     }
464     return E_SUCCESS;
465 }
466 } // namespace Media
467 } // namespace OHOS