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 "app_event_stat.h"
16
17 #include <random>
18
19 #include "ffrt.h"
20 #include "hiappevent_base.h"
21 #include "hiappevent_write.h"
22 #include "time_util.h"
23
24 namespace OHOS {
25 namespace HiviewDFX {
26 namespace AppEventStat {
27 namespace {
28 constexpr int BEHAVIOR = 4;
29
RandomNum()30 uint64_t RandomNum()
31 {
32 std::random_device seed;
33 std::mt19937_64 gen(seed());
34 std::uniform_int_distribution<uint64_t> dis(0, std::numeric_limits<uint64_t>::max());
35 return dis(gen);
36 }
37 }
38
WriteApiEndEventAsync(const std::string & apiName,uint64_t beginTime,int result,int errCode)39 void WriteApiEndEventAsync(const std::string& apiName, uint64_t beginTime, int result, int errCode)
40 {
41 auto appEventPack = std::make_shared<AppEventPack>("api_diagnostic", "api_exec_end", BEHAVIOR);
42 appEventPack->AddParam("trans_id", "transId_" + std::to_string(RandomNum()));
43 appEventPack->AddParam("api_name", apiName);
44 appEventPack->AddParam("sdk_name", "PerformanceAnalysisKit");
45 appEventPack->AddParam("begin_time", static_cast<int64_t>(beginTime));
46 appEventPack->AddParam("end_time", static_cast<int64_t>(TimeUtil::GetMilliseconds()));
47 appEventPack->AddParam("result", result);
48 appEventPack->AddParam("error_code", errCode);
49 ffrt::submit([appEventPack]() {
50 WriteEvent(appEventPack);
51 }, {}, {}, ffrt::task_attr().name("appevent_api_end"));
52 }
53 } // namespace AppEventStat
54 } // namespace HiviewDFX
55 } // namespace OHOS
56