1 /* 2 * Copyright (c) 2022-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 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 "data_share_common.h" 22 #include "iquery_base_callback.h" 23 #include "json/json.h" 24 #include "query_argument.h" 25 #include "sys_event_dao.h" 26 #include "sys_event_query.h" 27 #include "sys_event_query_rule.h" 28 29 namespace OHOS { 30 namespace HiviewDFX { 31 32 using ExtraInfoConditionMap = std::unordered_map<std::string, EventStore::Cond>; 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 ParseAndCondition(const Json::Value& root, EventStore::Cond& condition); 44 bool ParseQueryConditionJson(const Json::Value& root, EventStore::Cond& condition); 45 bool ParseQueryCondition(const std::string& condStr, EventStore::Cond& condition); 46 EventStore::Op GetOpEnum(const std::string& op); 47 48 private: 49 ExtraInfoConditionMap extraInfoCondCache; 50 }; 51 52 class BaseEventQueryWrapper { 53 public: 54 BaseEventQueryWrapper(std::shared_ptr<EventStore::SysEventQuery> query); ~BaseEventQueryWrapper()55 virtual ~BaseEventQueryWrapper() {} 56 57 public: 58 void Query(const OHOS::sptr<OHOS::HiviewDFX::IQueryBaseCallback>& eventQueryCallback, int32_t& queryResult); 59 void SetQueryArgument(QueryArgument argument); 60 void SetIsFirstPartialQuery(bool isFirstPartialQuery); 61 void SetSysEventQuery(std::shared_ptr<EventStore::SysEventQuery> query); 62 void SetNext(std::shared_ptr<BaseEventQueryWrapper> next); 63 QueryArgument& GetQueryArgument(); 64 std::vector<SysEventQueryRule>& GetSysEventQueryRules(); 65 int64_t GetMaxSequence() const; 66 int64_t GetEventTotalCount() const; 67 void TransportSysEvent(OHOS::HiviewDFX::EventStore::ResultSet& result, 68 const OHOS::sptr<OHOS::HiviewDFX::IQueryBaseCallback>& callback, std::pair<int64_t, int32_t>& details); 69 virtual void SetMaxSequence(int64_t maxSeq) = 0; 70 void SetEventTotalCount(int64_t totalCount); 71 bool IsValid() const; 72 bool IsQueryComplete() const; 73 bool NeedStartNextQuery(); 74 75 protected: 76 struct QuerierInfo { 77 int32_t uid = 0; 78 int32_t pid = 0; 79 std::string processName; 80 }; 81 82 protected: 83 virtual void BuildQuery() = 0; 84 virtual void Order() = 0; 85 void BuildCondition(const std::string& condition); 86 87 private: 88 void ClearCachedEvents(); 89 void FinishEventQuery(const OHOS::sptr<OHOS::HiviewDFX::IQueryBaseCallback>& callback, int32_t queryResult); 90 void TransportCachedEvents(const OHOS::sptr<OHOS::HiviewDFX::IQueryBaseCallback>& callback); 91 92 protected: 93 QueryArgument argument_; 94 int32_t queryLimit_ = 0; 95 int64_t maxSeq_ = 0; 96 int64_t totalEventCnt_ = 0; 97 int64_t transportedEventCnt_ = 0; 98 int32_t ignoredEventCnt_ = 0; 99 std::shared_ptr<EventStore::SysEventQuery> query_ = nullptr; 100 std::vector<SysEventQueryRule> queryRules_; 101 QuerierInfo querierInfo_; 102 103 private: 104 bool isFirstPartialQuery_ = true; 105 ConditionParser parser_; 106 std::vector<std::u16string> cachedEvents_; 107 std::vector<int64_t> cachedSeqs_; 108 int64_t cachedEventTotalSize_ = 0; 109 }; 110 111 class TimeStampEventQueryWrapper final : public BaseEventQueryWrapper { 112 public: TimeStampEventQueryWrapper(std::shared_ptr<EventStore::SysEventQuery> query)113 TimeStampEventQueryWrapper(std::shared_ptr<EventStore::SysEventQuery> query) 114 : BaseEventQueryWrapper(query) {} 115 virtual void SetMaxSequence(int64_t maxSeq); 116 117 private: 118 virtual void BuildQuery(); 119 virtual void Order(); 120 }; 121 122 class SeqEventQueryWrapper final : public BaseEventQueryWrapper { 123 public: SeqEventQueryWrapper(std::shared_ptr<EventStore::SysEventQuery> query)124 SeqEventQueryWrapper(std::shared_ptr<EventStore::SysEventQuery> query) 125 : BaseEventQueryWrapper(query) {} 126 virtual void SetMaxSequence(int64_t maxSeq); 127 128 private: 129 virtual void BuildQuery(); 130 virtual void Order(); 131 }; 132 133 class EventQueryWrapperBuilder final : public std::enable_shared_from_this<EventQueryWrapperBuilder> { 134 public: EventQueryWrapperBuilder(const QueryArgument & queryArgument)135 EventQueryWrapperBuilder(const QueryArgument& queryArgument) 136 { 137 InitQueryWrapper(queryArgument); 138 } 139 140 std::shared_ptr<BaseEventQueryWrapper> Build() const; 141 EventQueryWrapperBuilder& Append(const std::string& domain, const std::string& eventName, 142 uint32_t eventType, const std::string& extraInfo); 143 bool IsValid() const; 144 145 private: 146 std::shared_ptr<BaseEventQueryWrapper> CreateQueryWrapperByArgument(const QueryArgument& argument, 147 std::shared_ptr<EventStore::SysEventQuery> query); 148 void InitQueryWrapper(const QueryArgument& argument); 149 150 private: 151 std::shared_ptr<BaseEventQueryWrapper> queryWrapper_ = nullptr; 152 }; 153 } // namespace HiviewDFX 154 } // namespace OHOS 155 156 #endif // OHOS_HIVIEWDFX_EVENT_QUERY_WRAPPER_BUILDER_H