1/*
2 * Copyright (c) 2021-2022 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 HIVIEW_PLUGINS_EVENT_SERVICE_INCLUDE_SYS_EVENT_STAT_H
17#define HIVIEW_PLUGINS_EVENT_SERVICE_INCLUDE_SYS_EVENT_STAT_H
18
19#include <map>
20#include <mutex>
21#include <string>
22#include <vector>
23
24namespace OHOS {
25namespace HiviewDFX {
26class SysEventStat {
27public:
28    SysEventStat();
29    ~SysEventStat();
30
31public:
32    void AccumulateEvent(bool isValid = true);
33    void AccumulateEvent(const std::string &domain, const std::string &eventName, bool isValid = true);
34    void StatSummary(int fd);
35    void StatDetail(int fd);
36    void StatInvalidDetail(int fd);
37    void Clear(int fd);
38
39private:
40    void AddValidEvent(const std::string &domain, const std::string &eventName);
41    void AddInValidEvent(const std::string &domain, const std::string &eventName);
42
43private:
44    std::mutex mutex_;
45    int64_t inValidNum_;
46    int64_t validNum_;
47    std::map<std::string, std::map<std::string, int64_t>> d2es_;
48    std::map<std::string, std::string> d2ies_;
49}; // SysEventStat
50
51class BaseStat {
52public:
53    BaseStat(int fd, int64_t validNum, int64_t inValidNum);
54    virtual ~BaseStat();
55
56public:
57    void Stat() const;
58
59protected:
60    virtual void CalcStat() const = 0;
61
62protected:
63    int fd_;
64
65private:
66    void StatHead() const;
67    void StatDetail() const;
68
69private:
70    int64_t validNum_;
71    int64_t inValidNum_;
72}; // BaseStat
73
74class SysEventSummaryStat : public BaseStat {
75public:
76    SysEventSummaryStat(int fd, int64_t validNum, int64_t inValidNum,
77        std::map<std::string, std::map<std::string, int64_t>> &d2es);
78    ~SysEventSummaryStat();
79
80    void CalcStat() const override;
81
82protected:
83    virtual void StatDomainEvent(int fd, std::map<std::string, int64_t> &events) const;
84
85protected:
86    std::map<std::string, std::map<std::string, int64_t>> &d2es_;
87
88private:
89    void GetDomains(std::vector<std::string> &domains) const;
90    int64_t GetEventCount(std::map<std::string, int64_t> &events) const;
91}; // SysEventSummaryStat
92
93class SysEventDetailStat : public SysEventSummaryStat {
94public:
95    SysEventDetailStat(int fd, int64_t validNum, int64_t inValidNum,
96        std::map<std::string, std::map<std::string, int64_t>> &d2es);
97    ~SysEventDetailStat();
98
99protected:
100    void StatDomainEvent(int fd, std::map<std::string, int64_t> &events) const override;
101
102private:
103    void GetEventNames(std::map<std::string, int64_t> &events, std::vector<std::string> &eventNames) const;
104}; // SysEventDetailStat
105
106class SysEventInvalidStat : public BaseStat {
107public:
108    SysEventInvalidStat(int fd, int64_t validNum, int64_t inValidNum, std::map<std::string, std::string> &d2ies);
109    ~SysEventInvalidStat();
110
111public:
112    void CalcStat() const override;
113
114private:
115    std::map<std::string, std::string> &d2ies_;
116}; // SysEventInvalidStat
117} // namespace HiviewDFX
118} // namespace OHOS
119#endif // HIVIEW_PLUGINS_EVENT_SERVICE_INCLUDE_SYS_EVENT_STAT_H