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