• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #ifndef OHOS_FILEMGMT_BACKUP_RADAR_APP_STATISTIC_H
17 #define OHOS_FILEMGMT_BACKUP_RADAR_APP_STATISTIC_H
18 
19 #include <unordered_map>
20 #include "b_resources/b_constants.h"
21 #include "radar_const.h"
22 
23 namespace OHOS::FileManagement::Backup {
24 
25 struct ItemInfo {
26     uint32_t count = 0;
27     uint64_t size = 0;
28 };
29 
30 constexpr uint8_t TYPE_DEF_COUNT = 7;
31 constexpr uint8_t CATEGORY_TYPE_COUNT = 10;
32 
33 constexpr uint8_t SIZE_DEF_COUNT = 6;
34 
35 enum FileType : uint8_t {
36     TXT,
37     PIC,
38     AUDIO,
39     VEDIO,
40     COMPRESS,
41     BIN,
42     OTHER,
43 };
44 
45 constexpr uint64_t ONE_MB = 1024 * 1024;
46 constexpr uint64_t TWO_MB = 2 * ONE_MB;
47 constexpr uint64_t TEN_MB = 10 * ONE_MB;
48 constexpr uint64_t HUNDRED_MB = 100 * ONE_MB;
49 constexpr uint64_t ONE_GB = 1024 * ONE_MB;
50 
51 enum FileSize : uint8_t {
52     TINY, // [0, 1M)
53     SMALL, // [1M, 2M)
54     MEDIUM, // [2M, 10M)
55     BIG, // [10M, 100M)
56     GREAT_BIG, // [100M, 1G)
57     GIANT, // >=1G
58 };
59 
60 class FileStatList {
61 public:
62     std::string ToJsonString();
63     virtual ItemInfo* GetListPtr() = 0;
64     virtual uint32_t GetListSize() = 0;
65 };
66 
67 class FileTypeStat : public FileStatList {
68 public:
69     uint8_t GetIndexByType(std::string fileExtension);
70     void UpdateStat(std::string extension, uint64_t size);
GetListPtr()71     ItemInfo* GetListPtr() override
72     {
73         return typeInfoList_;
74     }
GetListSize()75     uint32_t GetListSize() override
76     {
77         return TYPE_DEF_COUNT;
78     }
79 private:
80     ItemInfo typeInfoList_[TYPE_DEF_COUNT] = {{0, 0}};
81 };
82 
83 class FileSizeStat : public FileStatList {
84 public:
85     void UpdateStat(uint64_t fileSize);
GetListPtr()86     ItemInfo* GetListPtr() override
87     {
88         return sizeInfoList_;
89     }
GetListSize()90     uint32_t GetListSize() override
91     {
92         return SIZE_DEF_COUNT;
93     }
94 private:
95     ItemInfo sizeInfoList_[SIZE_DEF_COUNT] = {{0, 0}};
96 };
97 
98 class RadarAppStatistic {
99 public:
100     std::string appCaller_; // tool app
101     uint32_t smallFileCount_ = 0;
102     uint64_t smallFileSize_ = 0;
103     uint32_t bigFileCount_ = 0;
104     uint64_t bigFileSize_ = 0;
105     uint32_t tarFileCount_ = 0;
106     uint64_t tarFileSize_ = 0;
107     uint32_t dirDepth_ = BConstants::APP_BASE_PATH_DEPTH;
108     uint64_t tarBoundSize_ = BConstants::BIG_FILE_BOUNDARY;
109     uint64_t manageJsonSize_ = 0;
110     Duration getExtInfoSpend_ = {0, 0};
111     uint32_t extConnectSpend_ = 0;
112 
113     Duration onBackupSpend_ = {0, 0};
114     Duration onBackupexSpend_ = {0, 0};
115     Duration scanFileSpend_ = {0, 0};
116     int64_t sendRateZeroStart_ = 0;
117     uint64_t sendRateZeroSpendUS_ = 0;
118     uint32_t tarSpend_ = 0;
119     uint64_t hashSpendUS_ = 0;
120     Duration doBackupSpend_ = {0, 0};
121 
122     Duration onRestoreSpend_ = {0, 0};
123     Duration onRestoreexSpend_ = {0, 0};
124     uint32_t untarSpend_ = 0;
125     uint32_t bigFileSpend_ = 0;
126     uint64_t doRestoreStart_ = 0;
127     uint32_t doRestoreSpend_ = 0;
128 
RadarAppStatistic()129     RadarAppStatistic() {};
RadarAppStatistic(std::string appCaller,int64_t uniqId,BizScene bizScene)130     RadarAppStatistic(std::string appCaller, int64_t uniqId, BizScene bizScene)
131         : appCaller_(appCaller), uniqId_(uniqId), bizScene_(bizScene) {}
132     ~RadarAppStatistic() = default;
133     RadarAppStatistic(const RadarAppStatistic &) = delete;
134     RadarAppStatistic &operator=(const RadarAppStatistic &) = delete;
135     RadarAppStatistic(RadarAppStatistic &&) = delete;
136     RadarAppStatistic &operator=(RadarAppStatistic &&) = delete;
137 
SetUniqId(int64_t uniqId)138     void SetUniqId(int64_t uniqId) { uniqId_ = uniqId; };
139     void UpdateSendRateZeroSpend();
140     void UpdateFileDist(std::string fileExtension, uint64_t fileSize);
141     void ReportBackup(const std::string &func, int32_t errorCode, std::string errMsg = "");
142     void ReportBackup(const std::string &func, BError errCode);
143     void ReportRestore(const std::string &func, int32_t errorCode, std::string errMsg = "");
144     void ReportRestore(const std::string &func, BError errCode);
145     void ReportError(const std::string &func, RadarError error);
146     void ReportSA(const std::string &func, RadarError error);
147 
148 private:
149     FileSizeStat fileSizeDist_;
150     FileTypeStat fileTypeDist_;
151     int64_t uniqId_ = 0;
152     BizScene bizScene_ = BizScene::UNKNOWN;
153 };
154 } // namespace OHOS::FileManagement::Backup
155 #endif // OHOS_FILEMGMT_BACKUP_RADAR_APP_STATISTIC_H
156