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
16 #include "rdb_radar_reporter.h"
17
18 #include "hisysevent_c.h"
19 #include "rdb_errno.h"
20
21 namespace OHOS::NativeRdb {
22
23 static constexpr const char *ORG_PKG_VALUE = "distributeddata";
24 static constexpr const char *EVENT_NAME = "DISTRIBUTED_RDB_BEHAVIOR";
25 static constexpr const char *UNKNOW = "unknow";
26 static constexpr const char *DISTRIBUTED_DATAMGR = "DISTDATAMGR";
27
28 std::string RdbRadar::hostPkg_{ "" };
29 std::mutex RdbRadar::mutex_;
30
RdbRadar(Scene scene,const char * funcName,std::string bundleName)31 RdbRadar::RdbRadar(Scene scene, const char *funcName, std::string bundleName)
32 : scene_(scene), funcName_(funcName), bundleName_(bundleName)
33 {
34 if (funcName_ == nullptr) {
35 funcName_ = UNKNOW;
36 }
37 LocalReport(scene_, funcName_, STATE_START);
38 }
39
~RdbRadar()40 RdbRadar::~RdbRadar()
41 {
42 LocalReport(scene_, funcName_, STATE_FINISH, errCode_);
43 }
44
operator =(int errCode)45 RdbRadar &RdbRadar::operator=(int errCode)
46 {
47 errCode_ = errCode;
48 return *this;
49 }
50
operator int() const51 RdbRadar::operator int() const
52 {
53 return errCode_;
54 }
55
LocalReport(int bizSence,const char * funcName,int state,int errCode)56 void RdbRadar::LocalReport(int bizSence, const char *funcName, int state, int errCode)
57 {
58 int stageRes = static_cast<int>(StageRes::RES_SUCCESS);
59 if (errCode != E_OK) {
60 stageRes = static_cast<int>(StageRes::RES_FAILED);
61 }
62
63 std::string hostPkg = GetHostPkgInfo();
64 char *hostPkgPtr = hostPkg.data();
65 if (hostPkgPtr == nullptr) {
66 return;
67 }
68 HiSysEventParam params[] = {
69 {.name = "ORG_PKG", .t = HISYSEVENT_STRING, .v = { .s = const_cast<char *>(ORG_PKG_VALUE) }, .arraySize = 0, },
70 {.name = "FUNC", .t = HISYSEVENT_STRING, .v = { .s = const_cast<char *>(funcName) }, .arraySize = 0, },
71 {.name = "BIZ_SCENE", .t = HISYSEVENT_INT32, .v = { .i32 = bizSence }, .arraySize = 0, },
72 {.name = "BIZ_STAGE", .t = HISYSEVENT_INT32, .v = { .i32 = SYNC_STAGE_RUN }, .arraySize = 0, },
73 {.name = "STAGE_RES", .t = HISYSEVENT_INT32, .v = { .i32 = stageRes }, .arraySize = 0, },
74 {.name = "ERROR_CODE", .t = HISYSEVENT_INT32, .v = { .i32 = errCode }, .arraySize = 0, },
75 {.name = "BIZ_STATE", .t = HISYSEVENT_INT32, .v = { .i32 = state }, .arraySize = 0, },
76 {.name = "HOST_PKG", .t = HISYSEVENT_STRING, .v = { .s = hostPkgPtr }, .arraySize = 0, },
77 };
78
79 OH_HiSysEvent_Write(
80 DISTRIBUTED_DATAMGR, EVENT_NAME, HISYSEVENT_BEHAVIOR, params, sizeof(params) / sizeof(params[0]));
81 return;
82 }
83
GetHostPkgInfo()84 std::string RdbRadar::GetHostPkgInfo()
85 {
86 std::lock_guard<std::mutex> lockGuard(mutex_);
87 if (!hostPkg_.empty()) {
88 return hostPkg_;
89 }
90 hostPkg_ = bundleName_;
91 return hostPkg_;
92 }
93 } // namespace OHOS::NativeRdb
94