1 /*
2  * Copyright (C) 2024-2025 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 "database_report.h"
19 
20 #include <sstream>
21 
22 #include "backup_const.h"
23 #include "backup_database_utils.h"
24 #include "backup_hi_audit_helper.h"
25 #include "external_files_count_statistic.h"
26 #include "hisysevent.h"
27 #include "gallery_media_count_statistic.h"
28 #include "media_log.h"
29 #include "photos_count_statistic.h"
30 
31 namespace OHOS::Media {
32 static constexpr char MEDIA_LIBRARY[] = "MEDIALIBRARY";
LoadGallery(std::shared_ptr<NativeRdb::RdbStore> galleryRdb,bool shouldIncludeSd)33 std::vector<AlbumMediaStatisticInfo> DatabaseReport::LoadGallery(
34     std::shared_ptr<NativeRdb::RdbStore> galleryRdb, bool shouldIncludeSd)
35 {
36     return GalleryMediaCountStatistic()
37         .SetGalleryRdb(galleryRdb)
38         .SetSceneCode(this->sceneCode_)
39         .SetTaskId(this->taskId_)
40         .SetShouldIncludeSd(shouldIncludeSd)
41         .Load();
42 }
43 
LoadExternal(std::shared_ptr<NativeRdb::RdbStore> externalRdb,std::shared_ptr<NativeRdb::RdbStore> galleryRdb)44 std::vector<AlbumMediaStatisticInfo> DatabaseReport::LoadExternal(std::shared_ptr<NativeRdb::RdbStore> externalRdb,
45     std::shared_ptr<NativeRdb::RdbStore> galleryRdb)
46 {
47     return ExternalFilesCountStatistic()
48         .SetExternalRdb(externalRdb)
49         .SetGalleryRdb(galleryRdb)
50         .SetSceneCode(this->sceneCode_)
51         .SetTaskId(this->taskId_)
52         .Load();
53 }
54 
LoadMedia(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb,int32_t period)55 std::vector<AlbumMediaStatisticInfo> DatabaseReport::LoadMedia(
56     std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb, int32_t period)
57 {
58     return PhotosCountStatistic()
59         .SetMediaLibraryRdb(mediaLibraryRdb)
60         .SetSceneCode(this->sceneCode_)
61         .SetTaskId(this->taskId_)
62         .SetPeriod(period)
63         .Load();
64 }
65 
Report(std::vector<AlbumMediaStatisticInfo> statisticInfos)66 int32_t DatabaseReport::Report(std::vector<AlbumMediaStatisticInfo> statisticInfos)
67 {
68     for (const auto &info : statisticInfos) {
69         MEDIA_INFO_LOG("[STAT] gallery analyze result: %{public}s", info.ToString().c_str());
70         PostInfoDfx(info);
71         PostInfoAuditLog(info);
72     }
73     return 0;
74 }
75 
ReportGallery(std::shared_ptr<NativeRdb::RdbStore> galleryRdb,bool shouldIncludeSd)76 DatabaseReport &DatabaseReport::ReportGallery(std::shared_ptr<NativeRdb::RdbStore> galleryRdb, bool shouldIncludeSd)
77 {
78     std::vector<AlbumMediaStatisticInfo> albumMediaStatisticInfos = this->LoadGallery(galleryRdb, shouldIncludeSd);
79     this->Report(albumMediaStatisticInfos);
80     return *this;
81 }
82 
ReportExternal(std::shared_ptr<NativeRdb::RdbStore> externalRdb,std::shared_ptr<NativeRdb::RdbStore> galleryRdb)83 DatabaseReport &DatabaseReport::ReportExternal(std::shared_ptr<NativeRdb::RdbStore> externalRdb,
84     std::shared_ptr<NativeRdb::RdbStore> galleryRdb)
85 {
86     std::vector<AlbumMediaStatisticInfo> albumMediaStatisticInfos = this->LoadExternal(externalRdb, galleryRdb);
87     this->Report(albumMediaStatisticInfos);
88     return *this;
89 }
90 
ReportMedia(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb,int32_t period)91 DatabaseReport &DatabaseReport::ReportMedia(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb, int32_t period)
92 {
93     std::vector<AlbumMediaStatisticInfo> albumMediaStatisticInfos = this->LoadMedia(mediaLibraryRdb, period);
94     this->Report(albumMediaStatisticInfos);
95     return *this;
96 }
97 
PostInfoDfx(const AlbumMediaStatisticInfo & info)98 int32_t DatabaseReport::PostInfoDfx(const AlbumMediaStatisticInfo &info)
99 {
100     int32_t ret = HiSysEventWrite(MEDIA_LIBRARY,
101         "MEDIALIB_BACKUP_MEDIA_STAT",
102         HiviewDFX::HiSysEvent::EventType::STATISTIC,
103         "SCENE_CODE",
104         info.sceneCode,
105         "TASK_ID",
106         info.taskId,
107         "ALBUM_NAME",
108         info.albumName,
109         "TOTAL_COUNT",
110         info.totalCount,
111         "IMAGE_COUNT",
112         info.imageCount,
113         "VIDEO_COUNT",
114         info.videoCount,
115         "HIDDEN_COUNT",
116         info.hiddenCount,
117         "TRASHED_COUNT",
118         info.trashedCount,
119         "FAVORITE_COUNT",
120         info.favoriteCount,
121         "CLOUD_COUNT",
122         info.cloudCount,
123         "BURST_COVER_COUNT",
124         info.burstCoverCount,
125         "BURST_TOTAL_COUNT",
126         info.burstTotalCount);
127     if (ret != 0) {
128         MEDIA_ERR_LOG("PostInfoDfx error:%{public}d", ret);
129     }
130     return ret;
131 }
132 
PostInfoAuditLog(const AlbumMediaStatisticInfo & info)133 int32_t DatabaseReport::PostInfoAuditLog(const AlbumMediaStatisticInfo &info)
134 {
135     BackupHiAuditHelper().SetSceneCode(this->sceneCode_).SetTaskId(this->taskId_).WriteReportAuditLog(info.ToString());
136     return 0;
137 }
138 }  // namespace OHOS::Media