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 #ifndef HIVIEW_PLUGINS_SYS_EVENT_SOURCE_CONTROL_INCLUDE_DAILY_CONTROLLER_H
16 #define HIVIEW_PLUGINS_SYS_EVENT_SOURCE_CONTROL_INCLUDE_DAILY_CONTROLLER_H
17 
18 #include "i_controller.h"
19 #include "daily_config.h"
20 #include "daily_db_helper.h"
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 class DailyController : public IController {
25 public:
26     DailyController(const std::string& workPath, const std::string& configPath);
27     ~DailyController() = default;
28     bool CheckThreshold(std::shared_ptr<SysEvent> event) override;
29 
30 private:
31     typedef std::pair<std::string, std::string> CacheKey;
32     struct ControlInfo {
33         int32_t threshold = 0;
34         int32_t count = 0;
35         int64_t exceedTime = 0;
36     };
37 
38     void TryToUpdateCacheToDb(int64_t nowTime);
39     void TryToReportDb(int64_t nowTime);
40     bool CheckTimeOfCache(int64_t nowTime);
41     bool CheckSizeOfCache();
42     void UpdateCacheToDb();
43     int32_t GetThreshold(const CacheKey& cachekey, int32_t type);
44     int32_t GetCount(const CacheKey& cachekey);
45     void UpdateCacheAndDb(const CacheKey& cachekey, int32_t threshold, int32_t count);
46     void UpdateCache(const CacheKey& cachekey, int32_t threshold, int32_t count, int64_t exceedTime);
47     void UpdateDb(const CacheKey& cachekey, int32_t count, int64_t exceedTime);
48 
49 private:
50     std::unique_ptr<DailyConfig> config_;
51     std::unique_ptr<DailyDbHelper> dbHelper_;
52     /* <<domain, name>, ControlInfo> */
53     std::unordered_map<std::pair<std::string, std::string>, ControlInfo, EventPairHash> cache_;
54 };
55 } // namespace HiviewDFX
56 } // namespace OHOS
57 #endif // HIVIEW_PLUGINS_SYS_EVENT_SOURCE_CONTROL_INCLUDE_DAILY_CONTROLLER_H
58