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