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 
16 #ifndef HIVIEW_BASE_EVENT_EXPORT_EVENT_READ_HANDLER_H
17 #define HIVIEW_BASE_EVENT_EXPORT_EVENT_READ_HANDLER_H
18 
19 #include <fstream>
20 #include <functional>
21 #include <tuple>
22 #include <unordered_map>
23 
24 #include "event_write_handler.h"
25 #include "export_base_handler.h"
26 #include "export_event_list_parser.h"
27 #include "sys_event_query.h"
28 
29 namespace OHOS {
30 namespace HiviewDFX {
31 struct EventReadRequest : public BaseRequest {
32     // the event sequence start to export
33     int64_t beginSeq = 0;
34 
35     // the event sequence end to sexport
36     int64_t endSeq = 0;
37 
38     // name of export module
39     std::string moduleName;
40 
41     // max size of a single event file
42     int64_t maxSize = 0;
43 
44     // event config list for query
45     ExportEventList eventList;
46 
47     // directory configured for export event file to store
48     std::string exportDir;
49 };
50 
51 class EventReadHandler : public ExportBaseHandler {
52 public:
53     using EventExportedListener = std::function<void(int64_t, int64_t)>;
54     void SetEventExportedListener(EventExportedListener listener);
55 
56     bool HandleRequest(RequestPtr request) override;
57 
58 private:
59     using QueryCallback = std::function<bool(bool)>;
60     bool QuerySysEventInRange(const std::pair<int64_t, int64_t>& queryRange, const ExportEventList& eventList,
61         QueryCallback queryCallback);
62     bool QuerySysEvent(const int64_t beginSeq, const int64_t endSeq, const ExportEventList& eventList,
63         QueryCallback queryCallback);
64     bool HandleQueryResult(EventStore::ResultSet& result, QueryCallback queryCallback, const int64_t queryLimit,
65         int64_t& totalQueryCnt);
66 
67 private:
68     EventExportedListener eventExportedListener_;
69     std::list<std::shared_ptr<CachedEvent>> cachedSysEvents_;
70 };
71 } // namespace HiviewDFX
72 } // namespace OHOS
73 
74 #endif // HIVIEW_BASE_EVENT_EXPORT_EVENT_READ_HANDLER_H