• 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 #include "imsa_hisysevent_reporter.h"
17 
18 namespace OHOS {
19 namespace MiscServices {
20 using namespace std::chrono;
GetInstance()21 ImsaHiSysEventReporter &ImsaHiSysEventReporter::GetInstance()
22 {
23     static ImsaHiSysEventReporter instance;
24     return instance;
25 }
26 
ImsaHiSysEventReporter()27 ImsaHiSysEventReporter::ImsaHiSysEventReporter()
28     : clientAttachInfo_(ClientAttachAllInfo(COUNT_STATISTICS_INTERVAL_NUM, COUNT_STATISTICS_INTERVAL_NUM)),
29       clientShowInfo_(ClientShowAllInfo(COUNT_STATISTICS_INTERVAL_NUM, COUNT_STATISTICS_INTERVAL_NUM))
30 {
31 }
32 
~ImsaHiSysEventReporter()33 ImsaHiSysEventReporter::~ImsaHiSysEventReporter()
34 {
35 }
36 
IsValidErrCode(int32_t errCode)37 bool ImsaHiSysEventReporter::IsValidErrCode(int32_t errCode)
38 {
39     return ErrorCode::ERROR_IMSA_BEGIN < errCode && errCode < ErrorCode::ERROR_IMSA_END;
40 }
41 
IsFault(int32_t errCode)42 bool ImsaHiSysEventReporter::IsFault(int32_t errCode)
43 {
44     return errCode != ErrorCode::ERROR_STATUS_PERMISSION_DENIED && errCode != ErrorCode::ERROR_CLIENT_NOT_FOCUSED;
45 }
46 
RecordStatisticsEvent(ImfStatisticsEvent event,const HiSysOriginalInfo & info)47 void ImsaHiSysEventReporter::RecordStatisticsEvent(ImfStatisticsEvent event, const HiSysOriginalInfo &info)
48 {
49     std::lock_guard<std::mutex> lock(statisticsEventLock_);
50     switch (event) {
51         case ImfStatisticsEvent::CLIENT_ATTACH_STATISTICS: {
52             RecordClientAttachStatistics(info);
53             break;
54         }
55         case ImfStatisticsEvent::CLIENT_SHOW_STATISTICS: {
56             RecordClientShowStatistics(info);
57             break;
58         }
59         default:
60             break;
61     }
62 }
63 
ReportStatisticsEvent()64 void ImsaHiSysEventReporter::ReportStatisticsEvent()
65 {
66     ClientAttachAllInfo clientAttachInfo(COUNT_STATISTICS_INTERVAL_NUM, COUNT_STATISTICS_INTERVAL_NUM);
67     ClientShowAllInfo clientShowInfo(COUNT_STATISTICS_INTERVAL_NUM, COUNT_STATISTICS_INTERVAL_NUM);
68     {
69         std::lock_guard<std::mutex> lock(statisticsEventLock_);
70         auto time = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
71         ResetTimerStartTime(time);
72         clientAttachInfo = clientAttachInfo_;
73         clientShowInfo = clientShowInfo_;
74         clientAttachInfo_ = ClientAttachAllInfo(COUNT_STATISTICS_INTERVAL_NUM, COUNT_STATISTICS_INTERVAL_NUM);
75         clientShowInfo_ = ClientShowAllInfo(COUNT_STATISTICS_INTERVAL_NUM, COUNT_STATISTICS_INTERVAL_NUM);
76     }
77     if (!clientAttachInfo.appNames.empty()) {
78         std::string attachStatistics;
79         clientAttachInfo.succeedRateInfo.Marshall(attachStatistics);
80         ImfHiSysEventUtil::ReportStatisticsEvent(GET_NAME(CLIENT_ATTACH_STATISTICS), clientAttachInfo.imeNames,
81             clientAttachInfo.appNames, { attachStatistics });
82     }
83     if (!clientShowInfo.appNames.empty()) {
84         std::string showStatistics;
85         clientShowInfo.succeedRateInfo.Marshall(showStatistics);
86         ImfHiSysEventUtil::ReportStatisticsEvent(
87             GET_NAME(CLIENT_SHOW_STATISTICS), clientShowInfo.imeNames, clientShowInfo.appNames, { showStatistics });
88     }
89 }
90 
RecordClientAttachStatistics(const HiSysOriginalInfo & info)91 void ImsaHiSysEventReporter::RecordClientAttachStatistics(const HiSysOriginalInfo &info)
92 {
93     std::string appName = "*";
94     auto appIndex = ImfHiSysEventUtil::AddIfAbsent(appName, clientAttachInfo_.appNames);
95     auto imeIndex = ImfHiSysEventUtil::AddIfAbsent(info.imeName, clientAttachInfo_.imeNames);
96     auto intervalIndex = GetStatisticalIntervalIndex();
97     std::string key(appIndex);
98     if (info.errCode == ErrorCode::NO_ERROR) {
99         clientAttachInfo_.succeedRateInfo.succeedInfo.ModCountDistributions(intervalIndex, key);
100         return;
101     }
102     key.append("/")
103         .append(imeIndex)
104         .append("/")
105         .append(std::to_string(info.clientType))
106         .append("/")
107         .append(std::to_string(info.errCode));
108     clientAttachInfo_.succeedRateInfo.failedInfo.ModCountDistributions(intervalIndex, key);
109 }
110 
RecordClientShowStatistics(const HiSysOriginalInfo & info)111 void ImsaHiSysEventReporter::RecordClientShowStatistics(const HiSysOriginalInfo &info)
112 {
113     std::string appName = "*";
114     auto appIndex = ImfHiSysEventUtil::AddIfAbsent(appName, clientShowInfo_.appNames);
115     auto imeIndex = ImfHiSysEventUtil::AddIfAbsent(info.imeName, clientShowInfo_.imeNames);
116     auto intervalIndex = GetStatisticalIntervalIndex();
117     std::string key(appIndex);
118     if (info.errCode == ErrorCode::NO_ERROR) {
119         key.append("/").append(std::to_string(info.errCode));
120         clientShowInfo_.succeedRateInfo.succeedInfo.ModCountDistributions(intervalIndex, key);
121         return;
122     }
123     key.append("/")
124         .append(imeIndex)
125         .append("/")
126         .append(std::to_string(info.clientType))
127         .append("/")
128         .append(std::to_string(info.eventCode))
129         .append("/")
130         .append(std::to_string(info.errCode));
131     clientShowInfo_.succeedRateInfo.failedInfo.ModCountDistributions(intervalIndex, key);
132 }
133 } // namespace MiscServices
134 } // namespace OHOS