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_CONFIG_INCLUDE_DAILY_CONFIG_H 16 #define HIVIEW_PLUGINS_SYS_EVENT_SOURCE_CONTROL_CONFIG_INCLUDE_DAILY_CONFIG_H 17 18 #include <unordered_map> 19 20 #include "cjson_util.h" 21 22 namespace OHOS { 23 namespace HiviewDFX { 24 struct EventPairHash { 25 template<class T1, class T2> operatorEventPairHash26 std::size_t operator()(const std::pair<T1, T2>& p) const 27 { 28 auto h1 = std::hash<T1>{}(p.first); 29 auto h2 = std::hash<T2>{}(p.second); 30 return h1 ^ h2; 31 } 32 }; 33 34 class DailyConfig { 35 public: 36 DailyConfig(const std::string& configPath); 37 ~DailyConfig() = default; 38 bool IsValid() const; 39 int32_t GetThreshold(const std::string& domain, const std::string name, int32_t type) const; 40 41 private: 42 bool Parse(const std::string& configPath); 43 bool ParseCommonThreshold(const cJSON* config); 44 bool ParseCustomThreshold(const cJSON* config); 45 bool ParseThresholdOfDomain(const cJSON* config); 46 bool ParseThresholdOfName(const cJSON* config, std::string& name, int32_t& threshold); 47 48 bool isValid_ = false; 49 /* <type, threshold> */ 50 std::unordered_map<int32_t, int32_t> commonThresholds_; 51 /* <<domain, name>, threshold> */ 52 std::unordered_map<std::pair<std::string, std::string>, int32_t, EventPairHash> customThresholds_; 53 }; 54 } // namespace HiviewDFX 55 } // namespace OHOS 56 #endif // HIVIEW_PLUGINS_SYS_EVENT_SOURCE_CONTROL_CONFIG_INCLUDE_DAILY_CONFIG_H 57