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