1 /* 2 * Copyright (c) 2024-2025 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 void OnConfigUpdate(const std::string& configPath) override; 30 31 private: 32 typedef std::pair<std::string, std::string> CacheKey; 33 struct ControlInfo { 34 int32_t threshold = 0; 35 int32_t count = 0; 36 int64_t exceedTime = 0; 37 }; 38 39 void TryToUpdateCacheToDb(int64_t nowTime); 40 void TryToReportDb(int64_t nowTime); 41 bool CheckTimeOfCache(int64_t nowTime); 42 bool CheckSizeOfCache(); 43 void UpdateCacheToDb(); 44 int32_t GetThreshold(const CacheKey& cachekey, int32_t type); 45 int32_t GetCount(const CacheKey& cachekey); 46 void UpdateCacheAndDb(const CacheKey& cachekey, int32_t threshold, int32_t count); 47 void UpdateCache(const CacheKey& cachekey, int32_t threshold, int32_t count, int64_t exceedTime); 48 void UpdateDb(const CacheKey& cachekey, int32_t count, int64_t exceedTime); 49 50 private: 51 std::unique_ptr<DailyConfig> config_; 52 std::unique_ptr<DailyDbHelper> dbHelper_; 53 /* <<domain, name>, ControlInfo> */ 54 std::unordered_map<std::pair<std::string, std::string>, ControlInfo, EventPairHash> cache_; 55 }; 56 } // namespace HiviewDFX 57 } // namespace OHOS 58 #endif // HIVIEW_PLUGINS_SYS_EVENT_SOURCE_CONTROL_INCLUDE_DAILY_CONTROLLER_H 59