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 "radar_reporter.h"
17 #include "device_manager_adapter.h"
18 #include "hisysevent.h"
19 
20 namespace OHOS {
21 namespace DistributedDataDfx {
22 using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter;
RadarReporter(const char * eventName,int32_t scene,const char * bundleName,const char * funcName)23 RadarReporter::RadarReporter(const char *eventName, int32_t scene, const char *bundleName, const char *funcName)
24     : eventName_(eventName), funcName_(funcName)
25 {
26     radarParam_.scene_ = scene;
27     radarParam_.bundleName_ = bundleName;
28     radarParam_.res_ = RES_IDLE;
29     Report(radarParam_, funcName_, BEGIN, eventName_);
30 }
31 
~RadarReporter()32 RadarReporter::~RadarReporter()
33 {
34     Report(radarParam_, funcName_, END, eventName_);
35 }
36 
operator =(int32_t errCode)37 RadarReporter &RadarReporter::operator=(int32_t errCode)
38 {
39     radarParam_.errCode_ = errCode;
40     return *this;
41 }
42 
Report(const RadarParam & param,const char * funcName,int32_t state,const char * eventName)43 void RadarReporter::Report(const RadarParam &param, const char *funcName, int32_t state, const char *eventName)
44 {
45     int32_t res = state == BEGIN ? param.res_ : (param.errCode_ != NO_ERROR ? RES_FAILED : RES_SUCCESS);
46     if (state != 0) {
47         HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::DISTRIBUTED_DATAMGR, eventName,
48             OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, ORG_PKG_LABEL, ORG_PKG, FUNC_LABEL, funcName,
49             BIZ_SCENE_LABEL, param.scene_, BIZ_STAGE_LABEL, param.stage_, BIZ_STATE_LABEL, state, STAGE_RES_LABEL, res,
50             ERROR_CODE_LABEL, param.errCode_, HOST_PKG, param.bundleName_, LOCAL_UUID_LABEL,
51             AnonymousUuid(DmAdapter::GetInstance().GetLocalDevice().uuid), CONCURRENT_ID,
52             std::to_string(param.syncId_), TRIGGER_MODE, param.triggerMode_, WATER_VERSION, param.changeCount);
53     } else {
54         HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::DISTRIBUTED_DATAMGR, eventName,
55             OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, ORG_PKG_LABEL, ORG_PKG, FUNC_LABEL, funcName,
56             BIZ_SCENE_LABEL, param.scene_, BIZ_STAGE_LABEL, param.stage_, STAGE_RES_LABEL, res, ERROR_CODE_LABEL,
57             param.errCode_, HOST_PKG, param.bundleName_, LOCAL_UUID_LABEL,
58             AnonymousUuid(DmAdapter::GetInstance().GetLocalDevice().uuid), CONCURRENT_ID,
59             std::to_string(param.syncId_), TRIGGER_MODE, param.triggerMode_, WATER_VERSION, param.changeCount);
60     }
61     return;
62 }
63 
AnonymousUuid(const std::string & uuid)64 std::string RadarReporter::AnonymousUuid(const std::string &uuid)
65 {
66     if (uuid.length() < BASE_SIZE) {
67         return DEFAULT_ANONYMOUS;
68     }
69     return (uuid.substr(0, HEAD_SIZE) + REPLACE_CHAIN + uuid.substr(uuid.length() - END_SIZE, END_SIZE));
70 }
71 } // namespace DistributedDataDfx
72 } // namespace OHOS