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 "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:
BaseEventQueryWrapper(std::shared_ptr<EventStore::SysEventQuery> query)54     BaseEventQueryWrapper(std::shared_ptr<EventStore::SysEventQuery> query) : query_(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     virtual void BuildQuery() = 0;
77     virtual void Order() = 0;
78     void BuildCondition(const std::string& condition);
79 
80 private:
81     void ClearCachedEvents();
82     void FinishEventQuery(const OHOS::sptr<OHOS::HiviewDFX::IQueryBaseCallback>& callback, int32_t queryResult);
83     void TransportCachedEvents(const OHOS::sptr<OHOS::HiviewDFX::IQueryBaseCallback>& callback);
84 
85 protected:
86     QueryArgument argument_;
87     int32_t queryLimit_ = 0;
88     int64_t maxSeq_ = 0;
89     int64_t totalEventCnt_ = 0;
90     int64_t transportedEventCnt_ = 0;
91     int32_t ignoredEventCnt_ = 0;
92     std::shared_ptr<EventStore::SysEventQuery> query_ = nullptr;
93     std::vector<SysEventQueryRule> queryRules_;
94 
95 private:
96     bool isFirstPartialQuery_ = true;
97     ConditionParser parser_;
98     std::vector<std::u16string> cachedEvents_;
99     std::vector<int64_t> cachedSeqs_;
100     int64_t cachedEventTotalSize_ = 0;
101 };
102 
103 class TimeStampEventQueryWrapper final : public BaseEventQueryWrapper {
104 public:
TimeStampEventQueryWrapper(std::shared_ptr<EventStore::SysEventQuery> query)105     TimeStampEventQueryWrapper(std::shared_ptr<EventStore::SysEventQuery> query)
106         : BaseEventQueryWrapper(query) {}
107     virtual void SetMaxSequence(int64_t maxSeq);
108 
109 private:
110     virtual void BuildQuery();
111     virtual void Order();
112 };
113 
114 class SeqEventQueryWrapper final : public BaseEventQueryWrapper {
115 public:
SeqEventQueryWrapper(std::shared_ptr<EventStore::SysEventQuery> query)116     SeqEventQueryWrapper(std::shared_ptr<EventStore::SysEventQuery> query)
117         : BaseEventQueryWrapper(query) {}
118     virtual void SetMaxSequence(int64_t maxSeq);
119 
120 private:
121     virtual void BuildQuery();
122     virtual void Order();
123 };
124 
125 class EventQueryWrapperBuilder final : public std::enable_shared_from_this<EventQueryWrapperBuilder> {
126 public:
EventQueryWrapperBuilder(const QueryArgument & queryArgument)127     EventQueryWrapperBuilder(const QueryArgument& queryArgument)
128     {
129         InitQueryWrapper(queryArgument);
130     }
131 
132     std::shared_ptr<BaseEventQueryWrapper> Build() const;
133     EventQueryWrapperBuilder& Append(const std::string& domain, const std::string& eventName,
134         uint32_t eventType, const std::string& extraInfo);
135     bool IsValid() const;
136 
137 private:
138     std::shared_ptr<BaseEventQueryWrapper> CreateQueryWrapperByArgument(const QueryArgument& argument,
139         std::shared_ptr<EventStore::SysEventQuery> query);
140     void InitQueryWrapper(const QueryArgument& argument);
141 
142 private:
143    std::shared_ptr<BaseEventQueryWrapper> queryWrapper_ = nullptr;
144 };
145 } // namespace HiviewDFX
146 } // namespace OHOS
147 
148 #endif // OHOS_HIVIEWDFX_EVENT_QUERY_WRAPPER_BUILDER_H