• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef NETSTACK_INCLUDE_HISYSEVENT_H
17 #define NETSTACK_INCLUDE_HISYSEVENT_H
18 
19 #include <string>
20 #include <map>
21 #include <mutex>
22 #include <queue>
23 
24 #include "curl/curl.h"
25 
26 namespace OHOS::NetStack {
27 
28 struct EventInfo {
29     std::string packageName;
30     double totalTime;
31     double totalRate;
32     double totalDnsTime;
33     double totalTlsTime;
34     double totalTcpTime;
35     double totalFirstRecvTime;
36     uint32_t successCount;
37     uint32_t totalCount;
38     std::string version;
39 };
40 
41 struct HttpPerfInfo {
42     double totalTime = 0.0;
43     double dnsTime = 0.0;
44     double tlsTime = 0.0;
45     double firstRecvTime = 0.0;
46     double tcpTime = 0.0;
47     curl_off_t size = 0;
48     int64_t responseCode = 0;
49     std::string version = "";
50     int ipType = 0;
51     int64_t osErr = 0;
52     int32_t errCode = 0;
53 public:
54     bool IsSuccess() const;
55     bool IsError() const;
56 };
57 
58 class EventReport {
59 public:
60     void ProcessHttpPerfHiSysevent(const HttpPerfInfo &httpPerfInfo);
61     void SendHttpPerfEvent(const EventInfo &eventInfo);
62     static EventReport &GetInstance();
63     bool IsValid();
64 
65 private:
66     EventReport();
67     ~EventReport() = default;
68     EventReport(const EventReport &eventReport) = delete;
69     const EventReport &operator=(const EventReport &eventReport) = delete;
70     void InitPackageName();
71     void ResetCounters();
72     std::string GetPackageName();
73     std::string MapToJsonString(const std::map<std::string, uint32_t> mapPara);
74     void HandleHttpPerfEvents(const HttpPerfInfo &httpPerfInfo);
75     void HandleHttpResponseErrorEvents(const HttpPerfInfo &httpPerfInfo);
76     void SendHttpResponseErrorEvent(const std::deque<HttpPerfInfo> &httpPerfInfoQueue_,
77                                     const std::chrono::steady_clock::time_point now);
78     void ReportHiSysEventWrite(const std::deque<HttpPerfInfo> &httpPerfInfoQueue_);
79 
80 private:
81     time_t reportTime = 0;
82     std::chrono::steady_clock::time_point httpReponseRecordTime_ = std::chrono::steady_clock::time_point::min();
83     std::chrono::steady_clock::time_point hiviewReportFirstTime_ = std::chrono::steady_clock::time_point::min();
84     int sendHttpNetStackEventCount_ = 0;
85     uint32_t totalErrorCount_ = 0;
86     std::string packageName_;
87     EventInfo eventInfo;
88     std::map<std::string, uint32_t> versionMap;
89     std::deque<HttpPerfInfo> httpPerfInfoQueue_;
90     bool validFlag = true;
91     std::recursive_mutex mutex;
92 };
93 }
94 #endif