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 IMA_HISYSEVENT_REPORTER_H 17 #define IMA_HISYSEVENT_REPORTER_H 18 19 #include <cstdint> 20 #include <map> 21 #include <vector> 22 23 #include "global.h" 24 #include "imf_hisysevent_reporter.h" 25 26 namespace OHOS { 27 namespace MiscServices { 28 struct ImeCbTimeConsumeStatistics : public Serializable { 29 static constexpr uint32_t MAX_COUNT_DISTRIBUTIONS_INTERVAL = 10; ImeCbTimeConsumeStatisticsImeCbTimeConsumeStatistics30 explicit ImeCbTimeConsumeStatistics(uint32_t num) 31 { 32 if (num > MAX_COUNT_DISTRIBUTIONS_INTERVAL) { 33 num = MAX_COUNT_DISTRIBUTIONS_INTERVAL; 34 } 35 countDistributions.resize(num); 36 } MarshalImeCbTimeConsumeStatistics37 bool Marshal(cJSON *node) const override 38 { 39 auto ret = SetValue(node, GET_NAME(COUNT), count); 40 std::string countInfo; 41 for (size_t i = 0; i < countDistributions.size(); i++) { 42 countInfo.append(std::to_string(countDistributions[i])); 43 if (i != countDistributions.size() - 1) { 44 countInfo.append("/"); 45 } 46 } 47 return SetValue(node, GET_NAME(COUNT_DISTRIBUTION), countInfo) && ret; 48 } ModCountDistributionsImeCbTimeConsumeStatistics49 void ModCountDistributions(uint32_t intervalIndex) 50 { 51 count++; 52 if (intervalIndex >= countDistributions.size()) { 53 return; 54 } 55 countDistributions[intervalIndex]++; 56 } 57 int32_t count{ 0 }; 58 std::vector<uint32_t> countDistributions; 59 }; 60 61 struct ImeStartInputAllInfo : public Serializable { ImeStartInputAllInfoImeStartInputAllInfo62 ImeStartInputAllInfo(uint32_t succeedIntervalNum, uint32_t failedIntervalNum, uint32_t timeConsumeIntervalNum) 63 : succeedRateInfo(SuccessRateStatistics(succeedIntervalNum, failedIntervalNum)), 64 imeCbTimeConsumeInfo(ImeCbTimeConsumeStatistics(timeConsumeIntervalNum)) 65 { 66 } MarshalImeStartInputAllInfo67 bool Marshal(cJSON *node) const override 68 { 69 auto ret = succeedRateInfo.Marshal(node); 70 return SetValue(node, GET_NAME(CB_TIME_CONSUME), imeCbTimeConsumeInfo) && ret; 71 } 72 std::vector<std::string> appNames; 73 SuccessRateStatistics succeedRateInfo; 74 ImeCbTimeConsumeStatistics imeCbTimeConsumeInfo; 75 }; 76 77 struct BaseTextOperationAllInfo : public Serializable { BaseTextOperationAllInfoBaseTextOperationAllInfo78 BaseTextOperationAllInfo(uint32_t succeedIntervalNum, uint32_t failedIntervalNum) 79 : succeedRateInfo(SuccessRateStatistics(succeedIntervalNum, failedIntervalNum)) 80 { 81 } 82 std::vector<std::string> appNames; 83 SuccessRateStatistics succeedRateInfo; 84 }; 85 86 class ImaHiSysEventReporter : public ImfHiSysEventReporter { 87 public: 88 static ImaHiSysEventReporter &GetInstance(); 89 90 private: 91 ImaHiSysEventReporter(); 92 ~ImaHiSysEventReporter(); 93 static const std::vector<std::pair<int32_t, int32_t>> BASE_TEXT_OPERATION_TIME_INTERVAL; 94 static const std::vector<std::pair<int32_t, int32_t>> IME_CB_TIME_INTERVAL; 95 bool IsValidErrCode(int32_t errCode) override; 96 bool IsFault(int32_t errCode) override; 97 void RecordStatisticsEvent(ImfStatisticsEvent event, const HiSysOriginalInfo &info) override; 98 void ReportStatisticsEvent() override; 99 void RecordImeStartInputStatistics(const HiSysOriginalInfo &info); 100 void ModImeCbTimeConsumeInfo(int32_t imeCbTime); 101 void RecordBaseTextOperationStatistics(const HiSysOriginalInfo &info); 102 uint32_t GetBaseTextOperationSucceedIntervalIndex(int32_t baseTextOperationTime); 103 std::mutex statisticsEventLock_; 104 ImeStartInputAllInfo imeStartInputAllInfo_; 105 BaseTextOperationAllInfo baseTextOperationAllInfo_; 106 }; 107 } // namespace MiscServices 108 } // namespace OHOS 109 110 #endif // IMA_HISYSEVENT_REPORTER_H