1 /*
2 * Copyright (c) 2025-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 "hi_app_event_report.h"
17 #include <random>
18 #ifdef ENABLE_EVENT_HANDLER
19 #include "time_service_client.h"
20 #undef LOG_DOMAIN
21 #undef LOG_TAG
22 #include "netstack_log.h"
23 #endif
24
25 namespace OHOS {
26 namespace NetStack {
27 #ifdef ENABLE_EVENT_HANDLER
28 const int64_t TIMEOUT = 90;
29 const int64_t ROW = 30;
30 const int64_t MAXVALUE = 999999;
31 const int64_t PROCESSOR_ID_NOT_CREATE = -1;
32 static int64_t g_processorID = PROCESSOR_ID_NOT_CREATE;
33 #endif
34
HiAppEventReport(std::string sdk,std::string api)35 HiAppEventReport::HiAppEventReport(std::string sdk, std::string api)
36 {
37 #ifdef ENABLE_EVENT_HANDLER
38 apiName_ = api;
39 sdkName_ = sdk;
40 std::random_device randSeed;
41 std::mt19937 gen(randSeed());
42 std::uniform_int_distribution<> dis(0, MAXVALUE);
43 transId_ = std::string("transId_") + std::to_string(dis(gen));
44
45 beginTime_ = OHOS::MiscServices::TimeServiceClient::GetInstance()->GetBootTimeMs();
46 if (g_processorID == PROCESSOR_ID_NOT_CREATE) {
47 g_processorID = AddProcessor();
48 }
49 #endif
50 }
51
~HiAppEventReport()52 HiAppEventReport::~HiAppEventReport()
53 {
54 }
55
ReportSdkEvent(const int result,const int errCode)56 void HiAppEventReport::ReportSdkEvent(const int result, const int errCode)
57 {
58 #ifdef ENABLE_EVENT_HANDLER
59 int64_t endTime = OHOS::MiscServices::TimeServiceClient::GetInstance()->GetBootTimeMs();
60 OHOS::HiviewDFX::HiAppEvent::Event event("api_diagnostic", "api_exec_end", OHOS::HiviewDFX::HiAppEvent::BEHAVIOR);
61 event.AddParam("trans_id", this->transId_);
62 event.AddParam("api_name", this->apiName_);
63 event.AddParam("sdk_name", this->sdkName_);
64 event.AddParam("begin_time", this->beginTime_);
65 event.AddParam("end_time", endTime);
66 event.AddParam("result", result);
67 event.AddParam("error_code", errCode);
68 int ret = Write(event);
69 NETSTACK_LOGD("transId:%{public}s, apiName:%{public}s, sdkName:%{public}s, "
70 "startTime:%{public}ld, endTime:%{public}ld, result:%{public}d, errCode:%{public}d, ret:%{public}d",
71 this->transId_.c_str(), this->apiName_.c_str(), this->sdkName_.c_str(),
72 this->beginTime_, endTime, result, errCode, ret);
73 #endif
74 }
75
76 #ifdef ENABLE_EVENT_HANDLER
AddProcessor()77 int64_t HiAppEventReport::AddProcessor()
78 {
79 NETSTACK_LOGI("AddProcessor enter");
80 OHOS::HiviewDFX::HiAppEvent::ReportConfig config;
81 config.name = "ha_app_event";
82 config.appId = "com_hua" "wei_hmos_sdk_ocg";
83 config.routeInfo = "AUTO";
84 config.triggerCond.timeout = TIMEOUT;
85 config.triggerCond.row = ROW;
86 config.eventConfigs.clear();
87 {
88 OHOS::HiviewDFX::HiAppEvent::EventConfig event;
89 event.domain = "api_diagnostic";
90 event.name = "api_exec_end";
91 event.isRealTime = false;
92 config.eventConfigs.push_back(event);
93 }
94 {
95 OHOS::HiviewDFX::HiAppEvent::EventConfig event2;
96 event2.domain = "api_diagnostic";
97 event2.name = "api_called_stat";
98 event2.isRealTime = true;
99 config.eventConfigs.push_back(event2);
100 }
101 {
102 OHOS::HiviewDFX::HiAppEvent::EventConfig event3;
103 event3.domain = "api_diagnostic";
104 event3.name = "api_called_stat_cnt";
105 event3.isRealTime = true;
106 config.eventConfigs.push_back(event3);
107 }
108 return OHOS::HiviewDFX::HiAppEvent::AppEventProcessorMgr::AddProcessor(config);
109 }
110 #endif
111 } // namespace NetStack
112 } // namespace OHOS
113