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 #include "hiappevent_util.h"
16 
17 #include <cinttypes>
18 #include <utility>
19 
20 #include "app_event.h"
21 #include "app_event_processor_mgr.h"
22 #include "ffrt.h"
23 #include "hidebug_util.h"
24 #include "hilog/log.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 
29 #undef LOG_DOMAIN
30 #define LOG_DOMAIN 0xD002D0A
31 #undef LOG_TAG
32 #define LOG_TAG "HIAPPEVENT_UTIL"
33 
34 int64_t ApiInvokeRecorder::processId_ = -1;
35 
ApiInvokeRecorder(std::string apiName)36 ApiInvokeRecorder::ApiInvokeRecorder(std::string apiName) : apiName_(std::move(apiName)),
37     beginTime_(GetNanoSecondsTimestamp()) {}
38 
~ApiInvokeRecorder()39 ApiInvokeRecorder::~ApiInvokeRecorder()
40 {
41     if (processId_ < 0) {
42         return;
43     }
44     std::string apiName(std::move(apiName_));
45     int64_t beginTime(beginTime_);
46     int errorCode(errorCode_);
47     auto task = [apiName, beginTime, errorCode] {
48         HiAppEvent::Event event("api_diagnostic", "api_exec_end", HiAppEvent::BEHAVIOR);
49         event.AddParam("trans_id", std::string("transId_") + std::to_string(beginTime));
50         event.AddParam("api_name", apiName);
51         event.AddParam("sdk_name", std::string("PerformanceAnalysisKit"));
52         constexpr int milliSecondsToNanoseconds = 1000 * 1000;
53         event.AddParam("begin_time", beginTime / milliSecondsToNanoseconds);
54         event.AddParam("end_time", GetNanoSecondsTimestamp() / milliSecondsToNanoseconds);
55         event.AddParam("result", static_cast<int>(errorCode == 0));
56         event.AddParam("error_code", errorCode);
57         Write(event);
58     };
59     ffrt::submit(task, {}, {});
60 }
61 
SetErrorCode(int errorCode)62 void ApiInvokeRecorder::SetErrorCode(int errorCode)
63 {
64     errorCode_ = errorCode;
65 }
66 
InitProcessor()67 void ApiInvokeRecorder::InitProcessor()
68 {
69     using namespace HiAppEvent;
70     ReportConfig config;
71     config.name = "ha_app_event";
72     config.appId = "com_huawei_hmos_sdk_ocg";
73     config.routeInfo = "AUTO";
74     constexpr int triggerTimeOut = 90;
75     config.triggerCond.timeout = triggerTimeOut;
76     constexpr int triggerRow = 30;
77     config.triggerCond.row = triggerRow;
78     config.eventConfigs.clear();
79     config.eventConfigs.push_back({
80         .domain = "api_diagnostic",
81         .name = "api_exec_end",
82         .isRealTime = false,
83     });
84     config.eventConfigs.push_back({
85         .domain = "api_diagnostic",
86         .name = "api_called_stat",
87         .isRealTime = true,
88     });
89     config.eventConfigs.push_back({
90         .domain = "api_diagnostic",
91         .name = "api_called_stat_cnt",
92         .isRealTime = true,
93     });
94     processId_ = AppEventProcessorMgr::AddProcessor(config);
95     if (processId_ < 0) {
96         HILOG_ERROR(LOG_CORE, "failed to init processor and ret: %{public}" PRId64, processId_);
97     }
98 }
99 }
100 }