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 DURATION_STATISTIC_H
17 #define DURATION_STATISTIC_H
18 
19 #include <mutex>
20 #include <shared_mutex>
21 #include <functional>
22 #include <map>
23 #include <memory>
24 #include <string>
25 #include "wifi_direct_types.h"
26 
27 namespace OHOS::SoftBus {
28 static constexpr const char *TOTAL_START = "TotalStart";
29 static constexpr const char *TOTAL_END = "TotalEnd";
30 static constexpr const char *TOTAL_DURATION = "TotalDuration";
31 static constexpr const char *CREATE_GROUP_START = "CreateGroupStart";
32 static constexpr const char *CREATE_GROUP_END = "CreateGroupEnd";
33 static constexpr const char *CONNECT_GROUP_START = "ConnectGroupStart";
34 static constexpr const char *CONNECT_GROUP_END = "ConnectGroupEnd";
35 
36 class DurationStatisticCalculator {
37 public:
38     virtual void CalculateAllEvent(uint32_t requestId, std::map<std::string, uint64_t> records) = 0;
39 };
40 
41 class P2pCalculator : public DurationStatisticCalculator {
42 public:
43     virtual ~P2pCalculator() = default;
44 
GetInstance()45     static P2pCalculator &GetInstance()
46     {
47         static P2pCalculator instance;
48         return instance;
49     }
50 
51     void CalculateAllEvent(uint32_t requestId, std::map<std::string, uint64_t> records) override;
52 };
53 
54 class DurationStatisticCalculatorFactory {
55 public:
GetInstance()56     static DurationStatisticCalculatorFactory &GetInstance()
57     {
58         static DurationStatisticCalculatorFactory instance;
59         return instance;
60     }
61 
62     std::shared_ptr<DurationStatisticCalculator> NewInstance(enum WifiDirectConnectType type);
63 
64     using Creator = std::function<std::shared_ptr<DurationStatisticCalculator>(enum WifiDirectConnectType type)>;
65     void Register(const Creator &creator);
66 
67 private:
68     Creator creator_;
69 };
70 
71 class DurationStatistic {
72 public:
GetInstance()73     static DurationStatistic &GetInstance()
74     {
75         static DurationStatistic instance;
76         return instance;
77     }
78 
79     void Start(uint32_t requestId, const std::shared_ptr<DurationStatisticCalculator> &calculator);
80     void Record(uint32_t requestId, const std::string &event);
81     void RecordReNegotiate(uint32_t requestId, bool flag);
82     void End(uint32_t requestId);
83     void Clear(uint32_t requestId);
84     std::map<std::string, uint64_t> GetStateTimeMapElement(uint32_t requestId);
85     bool ReNegotiateFlag(uint32_t requestId);
86 
87 private:
88     static uint64_t GetTime();
89     std::map<uint32_t, std::map<std::string, uint64_t>> stateTimeMap_;
90     std::map<uint32_t, bool> reNegotiateFlagMap_;
91     std::map<uint32_t, std::shared_ptr<DurationStatisticCalculator>> calculators_;
92 
93     std::recursive_mutex mutex_;
94 };
95 
96 } // namespace OHOS::SoftBus
97 
98 #endif