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 #ifndef OHOS_CLOUD_SYNC_SERVICE_GALLERY_SYSEVENT_H 17 #define OHOS_CLOUD_SYNC_SERVICE_GALLERY_SYSEVENT_H 18 19 #include <atomic> 20 21 #include "hisysevent.h" 22 23 #include "dfs_error.h" 24 #include "sysevent.h" 25 #include "utils_log.h" 26 27 namespace OHOS { 28 namespace FileManagement { 29 namespace CloudSync { 30 31 /* stat length */ 32 #define UPLOAD_META_LEN 6 33 #define DOWNLOAD_META_LEN 5 34 #define DOWNLOAD_THUMB_LEN 5 35 #define DOWNLOAD_LCD_LEN 5 36 #define UPLOAD_ALBUM_LEN 4 37 #define DOWNLOAD_ALBUM_LEN 4 38 39 /* stat index */ 40 enum MetaStatIndex { 41 /* download */ 42 INDEX_DL_META_SUCCESS, 43 INDEX_DL_META_ERROR_SDK, 44 INDEX_DL_META_ERROR_DATA, 45 INDEX_DL_META_ERROR_IO, 46 INDEX_DL_META_ERROR_RDB, 47 48 /* upload */ 49 INDEX_UL_META_SUCCESS, 50 INDEX_UL_META_ERROR_SDK, 51 INDEX_UL_META_ERROR_DATA, 52 INDEX_UL_META_ERROR_IO, 53 INDEX_UL_META_ERROR_ATTACHMENT, 54 INDEX_UL_META_ERROR_RDB, 55 }; 56 57 enum AttachmentStatIndex { 58 /* thumb */ 59 INDEX_THUMB_SUCCESS, 60 INDEX_THUMB_ERROR_SDK, 61 INDEX_THUMB_ERROR_DATA, 62 INDEX_THUMB_ERROR_IO, 63 INDEX_THUMB_ERROR_RDB, 64 65 /* lcd */ 66 INDEX_LCD_SUCCESS, 67 INDEX_LCD_ERROR_SDK, 68 INDEX_LCD_ERROR_DATA, 69 INDEX_LCD_ERROR_IO, 70 INDEX_LCD_ERROR_RDB, 71 72 /* context */ 73 }; 74 75 enum AlbumStatIndex { 76 /* upload */ 77 INDEX_UL_ALBUM_SUCCESS, 78 INDEX_UL_ALBUM_ERROR_SDK, 79 INDEX_UL_ALBUM_ERROR_DATA, 80 INDEX_UL_ALBUM_ERROR_RDB, 81 82 /* download */ 83 INDEX_DL_ALBUM_SUCCESS, 84 INDEX_DL_ALBUM_ERROR_SDK, 85 INDEX_DL_ALBUM_ERROR_DATA, 86 INDEX_DL_ALBUM_ERROR_RDB, 87 }; 88 89 class GallerySyncStat : public SyncStat { 90 public: GallerySyncStat()91 GallerySyncStat() : uploadMeta_(UPLOAD_META_LEN), 92 downloadMeta_(DOWNLOAD_META_LEN), 93 downloadThumb_(DOWNLOAD_THUMB_LEN), 94 downloadLcd_(DOWNLOAD_LCD_LEN), 95 uploadAlbum_(UPLOAD_ALBUM_LEN), 96 downloadAlbum_(DOWNLOAD_ALBUM_LEN) 97 { 98 } 99 ~GallerySyncStat()100 virtual ~GallerySyncStat() {} 101 UpdateMetaStat(uint32_t index,uint64_t diff)102 void UpdateMetaStat(uint32_t index, uint64_t diff) 103 { 104 if (index < INDEX_UL_META_SUCCESS) { 105 downloadMeta_[index].fetch_add(diff); 106 } else { 107 uploadMeta_[index - INDEX_UL_META_SUCCESS].fetch_add(diff); 108 } 109 } 110 UpdateAttachmentStat(uint32_t index,uint64_t diff)111 void UpdateAttachmentStat(uint32_t index, uint64_t diff) 112 { 113 if (index < INDEX_LCD_SUCCESS) { 114 downloadThumb_[index].fetch_add(diff); 115 } else { 116 downloadLcd_[index - INDEX_LCD_SUCCESS].fetch_add(diff); 117 } 118 } 119 UpdateAlbumStat(uint32_t index,uint64_t diff)120 void UpdateAlbumStat(uint32_t index, uint64_t diff) 121 { 122 if (index < INDEX_DL_ALBUM_SUCCESS) { 123 downloadAlbum_[index].fetch_add(diff); 124 } else { 125 uploadAlbum_[index - INDEX_DL_ALBUM_SUCCESS].fetch_add(diff); 126 } 127 } 128 129 protected: 130 std::vector<std::atomic<uint64_t>> uploadMeta_; 131 std::vector<std::atomic<uint64_t>> downloadMeta_; 132 std::vector<std::atomic<uint64_t>> downloadThumb_; 133 std::vector<std::atomic<uint64_t>> downloadLcd_; 134 std::vector<std::atomic<uint64_t>> uploadAlbum_; 135 std::vector<std::atomic<uint64_t>> downloadAlbum_; 136 }; 137 138 class GalleryFullSyncStat : public GallerySyncStat { 139 public: GalleryFullSyncStat()140 GalleryFullSyncStat() 141 { 142 isFullSync_ = true; 143 } 144 ~GalleryFullSyncStat()145 ~GalleryFullSyncStat() {} 146 Report()147 void Report() override 148 { 149 int32_t ret = CLOUD_SYNC_SYS_EVENT("CLOUD_SYNC_FULL_STAT", 150 HiviewDFX::HiSysEvent::EventType::STATISTIC, 151 "sync_reason", syncReason_, 152 "stop_reason", stopReason_, 153 "start_time", startTime_, 154 "duration", duration_, 155 "upload_meta", vector_atomic_to_origin<uint64_t>(uploadMeta_), 156 "download_meta", vector_atomic_to_origin<uint64_t>(downloadMeta_), 157 "download_thm", vector_atomic_to_origin<uint64_t>(downloadThumb_), 158 "download_lcd", vector_atomic_to_origin<uint64_t>(downloadLcd_), 159 "upload_album", vector_atomic_to_origin<uint64_t>(uploadAlbum_), 160 "download_album", vector_atomic_to_origin<uint64_t>(downloadAlbum_), 161 "sync_info", sync_info_); 162 if (ret != E_OK) { 163 LOGE("report CLOUD_SYNC_FULL_STAT error %{public}d", ret); 164 } 165 } 166 }; 167 168 class GalleryIncSyncStat : public GalleryFullSyncStat { 169 public: GalleryIncSyncStat()170 GalleryIncSyncStat() 171 { 172 } 173 ~GalleryIncSyncStat()174 ~GalleryIncSyncStat() 175 { 176 Dump(); 177 } 178 Report()179 void Report() override 180 { 181 if (isFullSync_ == true) { 182 GalleryFullSyncStat::Report(); 183 return; 184 } 185 186 int32_t ret = CLOUD_SYNC_SYS_EVENT("CLOUD_SYNC_INC_STAT", 187 HiviewDFX::HiSysEvent::EventType::STATISTIC, 188 "upload_meta", vector_atomic_to_origin<uint64_t>(uploadMeta_), 189 "download_meta", vector_atomic_to_origin<uint64_t>(downloadMeta_), 190 "download_thm", vector_atomic_to_origin<uint64_t>(downloadThumb_), 191 "download_lcd", vector_atomic_to_origin<uint64_t>(downloadLcd_), 192 "upload_album", vector_atomic_to_origin<uint64_t>(uploadAlbum_), 193 "download_album", vector_atomic_to_origin<uint64_t>(downloadAlbum_), 194 "sync_info", sync_info_); 195 if (ret != E_OK) { 196 LOGE("report CLOUD_SYNC_INC_STAT error %{public}d", ret); 197 } 198 } 199 200 /* read from disk */ Load()201 void Load() 202 { 203 } 204 205 /* write into disk */ Dump()206 void Dump() 207 { 208 Load(); 209 } 210 }; 211 212 class GallerySyncStatContainer : public SyncStatContainer<GalleryIncSyncStat> { 213 public: UpdateMetaStat(uint32_t index,uint64_t diff)214 void UpdateMetaStat(uint32_t index, uint64_t diff) 215 { 216 std::shared_ptr<GalleryIncSyncStat> stat = GetSyncStat(); 217 /* for sake of no-crash, might delete later */ 218 if (stat != nullptr) { 219 stat->UpdateMetaStat(index, diff); 220 } 221 } 222 UpdateAttachmentStat(uint32_t index,uint64_t diff)223 void UpdateAttachmentStat(uint32_t index, uint64_t diff) 224 { 225 std::shared_ptr<GalleryIncSyncStat> stat = GetSyncStat(); 226 /* for sake of no-crash, might delete later */ 227 if (stat != nullptr) { 228 stat->UpdateAttachmentStat(index, diff); 229 } 230 } 231 UpdateAlbumStat(uint32_t index,uint64_t diff)232 void UpdateAlbumStat(uint32_t index, uint64_t diff) 233 { 234 std::shared_ptr<GalleryIncSyncStat> stat = GetSyncStat(); 235 /* for sake of no-crash, might delete later */ 236 if (stat != nullptr) { 237 stat->UpdateAlbumStat(index, diff); 238 } 239 } 240 }; 241 242 enum GalleryCheckStatIndex { 243 /* foud */ 244 INDEX_CHECK_FOUND, 245 /* fixed */ 246 INDEX_CHECK_FIXED, 247 }; 248 249 #define GALLERY_CHECK_STAT_LEN 2 250 251 class GalleryCheckSatat : public SyncStat { 252 public: GalleryCheckSatat()253 GalleryCheckSatat() : file_(GALLERY_CHECK_STAT_LEN), 254 album_(GALLERY_CHECK_STAT_LEN), 255 map_(GALLERY_CHECK_STAT_LEN), 256 attachment_(GALLERY_CHECK_STAT_LEN) 257 { 258 } 259 Report()260 void Report() override 261 { 262 int32_t ret = CLOUD_SYNC_SYS_EVENT("CLOUD_SYNC_CHECK", 263 HiviewDFX::HiSysEvent::EventType::STATISTIC, 264 "start_time", startTime_, 265 "duration", duration_, 266 "file", file_, 267 "album", album_, 268 "map", map_, 269 "attachment", attachment_, 270 "check_info", checkInfo_); 271 if (ret != E_OK) { 272 LOGE("report CLOUD_SYNC_CHECK error %{public}d", ret); 273 } 274 } 275 UpdateFile(uint32_t index,uint64_t diff)276 void UpdateFile(uint32_t index, uint64_t diff) 277 { 278 file_[index] += diff; 279 } 280 UpdateAlbum(uint32_t index,uint64_t diff)281 void UpdateAlbum(uint32_t index, uint64_t diff) 282 { 283 album_[index] += diff; 284 } 285 UpdateMap(uint32_t index,uint64_t diff)286 void UpdateMap(uint32_t index, uint64_t diff) 287 { 288 map_[index] += diff; 289 } 290 UpdateAttachment(uint32_t index,uint64_t diff)291 void UpdateAttachment(uint32_t index, uint64_t diff) 292 { 293 attachment_[index] += diff; 294 } 295 296 private: 297 std::vector<uint64_t> file_; 298 std::vector<uint64_t> album_; 299 std::vector<uint64_t> map_; 300 std::vector<uint64_t> attachment_; 301 std::string checkInfo_; 302 }; 303 304 class GalleryCheckStatContainer : public CheckStatContainer<GalleryCheckSatat> { 305 public: UpdateCheckFile(uint32_t index,uint64_t diff)306 void UpdateCheckFile(uint32_t index, uint64_t diff) 307 { 308 std::shared_ptr<GalleryCheckSatat> stat = GetCheckStat(); 309 if (stat != nullptr) { 310 stat->UpdateFile(index, diff); 311 } 312 } 313 UpdateCheckAlbum(uint32_t index,uint64_t diff)314 void UpdateCheckAlbum(uint32_t index, uint64_t diff) 315 { 316 std::shared_ptr<GalleryCheckSatat> stat = GetCheckStat(); 317 if (stat != nullptr) { 318 stat->UpdateAlbum(index, diff); 319 } 320 } 321 UpdateCheckMap(uint32_t index,uint64_t diff)322 void UpdateCheckMap(uint32_t index, uint64_t diff) 323 { 324 std::shared_ptr<GalleryCheckSatat> stat = GetCheckStat(); 325 if (stat != nullptr) { 326 stat->UpdateMap(index, diff); 327 } 328 } 329 UpdateCheckAttachment(uint32_t index,uint64_t diff)330 void UpdateCheckAttachment(uint32_t index, uint64_t diff) 331 { 332 std::shared_ptr<GalleryCheckSatat> stat = GetCheckStat(); 333 if (stat != nullptr) { 334 stat->UpdateAttachment(index, diff); 335 } 336 } 337 }; 338 } // namespace CloudSync 339 } // namespace FileManagement 340 } // namespace OHOS 341 #endif // OHOS_CLOUD_SYNC_SERVICE_GALLERY_SYSEVENT_H 342