106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci#include "hiappevent_util.h"
1606f6ba60Sopenharmony_ci
1706f6ba60Sopenharmony_ci#include <cinttypes>
1806f6ba60Sopenharmony_ci#include <utility>
1906f6ba60Sopenharmony_ci
2006f6ba60Sopenharmony_ci#include "app_event.h"
2106f6ba60Sopenharmony_ci#include "app_event_processor_mgr.h"
2206f6ba60Sopenharmony_ci#include "ffrt.h"
2306f6ba60Sopenharmony_ci#include "hidebug_util.h"
2406f6ba60Sopenharmony_ci#include "hilog/log.h"
2506f6ba60Sopenharmony_ci
2606f6ba60Sopenharmony_cinamespace OHOS {
2706f6ba60Sopenharmony_cinamespace HiviewDFX {
2806f6ba60Sopenharmony_ci
2906f6ba60Sopenharmony_ci#undef LOG_DOMAIN
3006f6ba60Sopenharmony_ci#define LOG_DOMAIN 0xD002D0A
3106f6ba60Sopenharmony_ci#undef LOG_TAG
3206f6ba60Sopenharmony_ci#define LOG_TAG "HIAPPEVENT_UTIL"
3306f6ba60Sopenharmony_ci
3406f6ba60Sopenharmony_ciint64_t ApiInvokeRecorder::processId_ = -1;
3506f6ba60Sopenharmony_ci
3606f6ba60Sopenharmony_ciApiInvokeRecorder::ApiInvokeRecorder(std::string apiName) : apiName_(std::move(apiName)),
3706f6ba60Sopenharmony_ci    beginTime_(GetNanoSecondsTimestamp()) {}
3806f6ba60Sopenharmony_ci
3906f6ba60Sopenharmony_ciApiInvokeRecorder::~ApiInvokeRecorder()
4006f6ba60Sopenharmony_ci{
4106f6ba60Sopenharmony_ci    if (processId_ < 0) {
4206f6ba60Sopenharmony_ci        return;
4306f6ba60Sopenharmony_ci    }
4406f6ba60Sopenharmony_ci    std::string apiName(std::move(apiName_));
4506f6ba60Sopenharmony_ci    int64_t beginTime(beginTime_);
4606f6ba60Sopenharmony_ci    int errorCode(errorCode_);
4706f6ba60Sopenharmony_ci    auto task = [apiName, beginTime, errorCode] {
4806f6ba60Sopenharmony_ci        HiAppEvent::Event event("api_diagnostic", "api_exec_end", HiAppEvent::BEHAVIOR);
4906f6ba60Sopenharmony_ci        event.AddParam("trans_id", std::string("transId_") + std::to_string(beginTime));
5006f6ba60Sopenharmony_ci        event.AddParam("api_name", apiName);
5106f6ba60Sopenharmony_ci        event.AddParam("sdk_name", std::string("PerformanceAnalysisKit"));
5206f6ba60Sopenharmony_ci        constexpr int milliSecondsToNanoseconds = 1000 * 1000;
5306f6ba60Sopenharmony_ci        event.AddParam("begin_time", beginTime / milliSecondsToNanoseconds);
5406f6ba60Sopenharmony_ci        event.AddParam("end_time", GetNanoSecondsTimestamp() / milliSecondsToNanoseconds);
5506f6ba60Sopenharmony_ci        event.AddParam("result", static_cast<int>(errorCode == 0));
5606f6ba60Sopenharmony_ci        event.AddParam("error_code", errorCode);
5706f6ba60Sopenharmony_ci        Write(event);
5806f6ba60Sopenharmony_ci    };
5906f6ba60Sopenharmony_ci    ffrt::submit(task, {}, {});
6006f6ba60Sopenharmony_ci}
6106f6ba60Sopenharmony_ci
6206f6ba60Sopenharmony_civoid ApiInvokeRecorder::SetErrorCode(int errorCode)
6306f6ba60Sopenharmony_ci{
6406f6ba60Sopenharmony_ci    errorCode_ = errorCode;
6506f6ba60Sopenharmony_ci}
6606f6ba60Sopenharmony_ci
6706f6ba60Sopenharmony_civoid ApiInvokeRecorder::InitProcessor()
6806f6ba60Sopenharmony_ci{
6906f6ba60Sopenharmony_ci    using namespace HiAppEvent;
7006f6ba60Sopenharmony_ci    ReportConfig config;
7106f6ba60Sopenharmony_ci    config.name = "ha_app_event";
7206f6ba60Sopenharmony_ci    config.appId = "com_huawei_hmos_sdk_ocg";
7306f6ba60Sopenharmony_ci    config.routeInfo = "AUTO";
7406f6ba60Sopenharmony_ci    constexpr int triggerTimeOut = 90;
7506f6ba60Sopenharmony_ci    config.triggerCond.timeout = triggerTimeOut;
7606f6ba60Sopenharmony_ci    constexpr int triggerRow = 30;
7706f6ba60Sopenharmony_ci    config.triggerCond.row = triggerRow;
7806f6ba60Sopenharmony_ci    config.eventConfigs.clear();
7906f6ba60Sopenharmony_ci    config.eventConfigs.push_back({
8006f6ba60Sopenharmony_ci        .domain = "api_diagnostic",
8106f6ba60Sopenharmony_ci        .name = "api_exec_end",
8206f6ba60Sopenharmony_ci        .isRealTime = false,
8306f6ba60Sopenharmony_ci    });
8406f6ba60Sopenharmony_ci    config.eventConfigs.push_back({
8506f6ba60Sopenharmony_ci        .domain = "api_diagnostic",
8606f6ba60Sopenharmony_ci        .name = "api_called_stat",
8706f6ba60Sopenharmony_ci        .isRealTime = true,
8806f6ba60Sopenharmony_ci    });
8906f6ba60Sopenharmony_ci    config.eventConfigs.push_back({
9006f6ba60Sopenharmony_ci        .domain = "api_diagnostic",
9106f6ba60Sopenharmony_ci        .name = "api_called_stat_cnt",
9206f6ba60Sopenharmony_ci        .isRealTime = true,
9306f6ba60Sopenharmony_ci    });
9406f6ba60Sopenharmony_ci    processId_ = AppEventProcessorMgr::AddProcessor(config);
9506f6ba60Sopenharmony_ci    if (processId_ < 0) {
9606f6ba60Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "failed to init processor and ret: %{public}" PRId64, processId_);
9706f6ba60Sopenharmony_ci    }
9806f6ba60Sopenharmony_ci}
9906f6ba60Sopenharmony_ci}
10006f6ba60Sopenharmony_ci}