1 /* 2 * Copyright (c) 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 OHOS_HIVIEWDFX_EVENT_QUERY_WRAPPER_BUILDER_H 17 #define OHOS_HIVIEWDFX_EVENT_QUERY_WRAPPER_BUILDER_H 18 19 #include <unordered_map> 20 21 #include "iquery_sys_event_callback.h" 22 #include "json/json.h" 23 #include "query_argument.h" 24 #include "sys_event_dao.h" 25 #include "sys_event_query.h" 26 #include "sys_event_query_rule.h" 27 28 namespace OHOS { 29 namespace HiviewDFX { 30 using ExtraInfoConditionMap = std::unordered_map<std::string, EventStore::Cond>; 31 using EventNameExtraInfoMap = std::unordered_map<std::string, std::string>; 32 using DomainNameExtraInfoMap = std::unordered_map<std::string, EventNameExtraInfoMap>; 33 34 class ConditionParser { 35 public: 36 bool ParseCondition(const std::string& condStr, EventStore::Cond& condition); 37 38 private: 39 void SpliceConditionByLogic(EventStore::Cond& condition, const EventStore::Cond& subCond, 40 const std::string& logic); 41 bool ParseJsonString(const Json::Value& root, const std::string& key, std::string& value); 42 bool ParseLogicCondition(const Json::Value& root, const std::string& logic, EventStore::Cond& condition); 43 bool ParseOrCondition(const Json::Value& root, EventStore::Cond& condition); 44 bool ParseAndCondition(const Json::Value& root, EventStore::Cond& condition); 45 bool ParseQueryConditionJson(const Json::Value& root, EventStore::Cond& condition); 46 bool ParseQueryCondition(const std::string& condStr, EventStore::Cond& condition); 47 EventStore::Op GetOpEnum(const std::string& op); 48 49 private: 50 ExtraInfoConditionMap extraInfoCondCache; 51 }; 52 53 class BaseEventQueryWrapper { 54 public: BaseEventQueryWrapper(uint32_t eventType,std::shared_ptr<EventStore::SysEventQuery> query)55 BaseEventQueryWrapper(uint32_t eventType, std::shared_ptr<EventStore::SysEventQuery> query) 56 : query(query), eventType(eventType) {} ~BaseEventQueryWrapper()57 virtual ~BaseEventQueryWrapper() {} 58 59 public: 60 void Query(OHOS::sptr<OHOS::HiviewDFX::IQuerySysEventCallback> eventQueryCallback, int32_t& queryResult); 61 void SetQueryArgument(QueryArgument argument); 62 void SetIsFirstPartialQuery(bool isFirstPartialQuery); 63 void SetSysEventQuery(std::shared_ptr<EventStore::SysEventQuery> query); 64 void SetNext(std::shared_ptr<BaseEventQueryWrapper> next); 65 QueryArgument& GetQueryArgument(); 66 DomainNameExtraInfoMap& GetDomainNameExtraInfoMap(); 67 int64_t GetMaxSequence() const; 68 int64_t GetEventTotalCount() const; 69 void TransportSysEvent(OHOS::HiviewDFX::EventStore::ResultSet& result, 70 const OHOS::sptr<OHOS::HiviewDFX::IQuerySysEventCallback> callback, std::pair<int64_t, int32_t>& details); 71 72 public: 73 virtual void SetMaxSequence(int64_t maxSeq) = 0; 74 void SetEventTotalCount(int64_t totalCount); 75 76 public: 77 bool IsValid() const; 78 bool HasNext() const; 79 std::shared_ptr<BaseEventQueryWrapper> Next() const; 80 uint32_t GetEventType() const; 81 bool IsQueryComplete() const; 82 83 protected: 84 virtual bool NeedStartNextQuery() = 0; 85 virtual void SyncQueryArgument(const EventStore::ResultSet::RecordIter iter) = 0; 86 virtual void BuildQuery(std::shared_ptr<EventStore::SysEventQuery> query) = 0; 87 virtual void Order() = 0; 88 89 protected: 90 QueryArgument argument; 91 int32_t queryLimit = 0; 92 int64_t maxSeq = 0; 93 int64_t totalEventCnt = 0; 94 int64_t transportedEventCnt = 0; 95 int32_t ignoredEventCnt = 0; 96 std::shared_ptr<EventStore::SysEventQuery> query = nullptr; 97 98 private: 99 uint32_t eventType = -1; // invalid event type is -1 100 bool isFirstPartialQuery = true; 101 std::shared_ptr<BaseEventQueryWrapper> next = nullptr; 102 ConditionParser parser; 103 DomainNameExtraInfoMap domainNameExtraInfos; 104 105 private: 106 bool BuildConditonByDomainNameExtraInfo(EventStore::Cond& domainNameConds); 107 bool HasDomainNameExtraConditon(EventStore::Cond& domainNameConds, 108 const DomainNameExtraInfoMap::value_type& domainNameExtraInfo); 109 void HandleCurrentQueryDone(OHOS::sptr<OHOS::HiviewDFX::IQuerySysEventCallback> callback, 110 int32_t& queryResult); 111 }; 112 113 class TimeStampEventQueryWrapper final : public BaseEventQueryWrapper { 114 public: TimeStampEventQueryWrapper(uint32_t eventType,std::shared_ptr<EventStore::SysEventQuery> query)115 TimeStampEventQueryWrapper(uint32_t eventType, std::shared_ptr<EventStore::SysEventQuery> query) 116 : BaseEventQueryWrapper(eventType, query) {} 117 118 public: 119 virtual void SyncQueryArgument(const EventStore::ResultSet::RecordIter iter); 120 virtual bool NeedStartNextQuery(); 121 virtual void SetMaxSequence(int64_t maxSeq); 122 123 private: 124 virtual void BuildQuery(std::shared_ptr<EventStore::SysEventQuery> query); 125 virtual void Order(); 126 }; 127 128 class SeqEventQueryWrapper final : public BaseEventQueryWrapper { 129 public: SeqEventQueryWrapper(uint32_t eventType,std::shared_ptr<EventStore::SysEventQuery> query)130 SeqEventQueryWrapper(uint32_t eventType, std::shared_ptr<EventStore::SysEventQuery> query) 131 : BaseEventQueryWrapper(eventType, query) {} 132 133 public: 134 virtual void SyncQueryArgument(const EventStore::ResultSet::RecordIter iter); 135 virtual bool NeedStartNextQuery(); 136 virtual void SetMaxSequence(int64_t maxSeq); 137 138 private: 139 virtual void BuildQuery(std::shared_ptr<EventStore::SysEventQuery> query); 140 virtual void Order(); 141 }; 142 143 class EventQueryWrapperBuilder final : public std::enable_shared_from_this<EventQueryWrapperBuilder> { 144 public: EventQueryWrapperBuilder(const QueryArgument & queryArgument)145 EventQueryWrapperBuilder(const QueryArgument& queryArgument) 146 { 147 InitQueryWrappers(queryArgument); 148 } 149 150 public: 151 EventQueryWrapperBuilder& Append(uint32_t eventType); 152 EventQueryWrapperBuilder& Append(const std::string& domain, const std::string& eventName, uint32_t eventType, 153 const std::string& extraInfo); 154 std::shared_ptr<BaseEventQueryWrapper> Build() const; 155 bool IsValid() const; 156 157 private: 158 std::shared_ptr<BaseEventQueryWrapper> CreateQueryWrapperByArgument(const QueryArgument& argument, 159 uint32_t eventType, std::shared_ptr<EventStore::SysEventQuery> query); 160 void InitQueryWrappers(const QueryArgument& argument); 161 std::shared_ptr<BaseEventQueryWrapper> Find(uint32_t eventType = 0); 162 163 private: 164 std::shared_ptr<BaseEventQueryWrapper> queryWrapper = nullptr; 165 }; 166 } // namespace HiviewDFX 167 } // namespace OHOS 168 169 #endif // OHOS_HIVIEWDFX_EVENT_QUERY_WRAPPER_BUILDER_H