1 /* 2 * Copyright (c) 2024-2025 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_config_parser.h" 27 #include "export_event_list_parser.h" 28 #include "sys_event_query.h" 29 30 namespace OHOS { 31 namespace HiviewDFX { 32 struct EventReadRequest : public BaseRequest { 33 // the event sequence start to export 34 int64_t beginSeq = 0; 35 36 // the event sequence end to sexport 37 int64_t endSeq = 0; 38 39 // name of export module 40 std::string moduleName; 41 42 // max size of a single event file 43 int64_t maxSize = 0; 44 45 // event config list for query 46 ExportEventList eventList; 47 48 // directory configured for export event file to store 49 std::string exportDir; 50 51 // task type 52 int64_t taskType; 53 }; 54 55 struct ExportPeriodInfo { 56 // format: YYYYMMDDHH 57 std::string timeStamp; 58 59 // count of event which has been exported in 1 hour 60 size_t exportedCnt = 0; 61 ExportPeriodInfoExportPeriodInfo62 ExportPeriodInfo(const std::string& timeStamp, size_t exportedCnt) 63 : timeStamp(timeStamp), exportedCnt(exportedCnt) {} 64 }; 65 66 class EventReadHandler : public ExportBaseHandler { 67 public: 68 using EventExportedListener = std::function<void(int64_t, int64_t)>; 69 void SetEventExportedListener(EventExportedListener listener); 70 71 bool HandleRequest(RequestPtr request) override; 72 73 private: 74 using QueryCallback = std::function<bool(bool)>; 75 bool QuerySysEventInRange(const std::pair<int64_t, int64_t>& queryRange, const ExportEventList& eventList, 76 QueryCallback queryCallback); 77 bool QuerySysEvent(const int64_t beginSeq, const int64_t endSeq, const ExportEventList& eventList, 78 QueryCallback queryCallback); 79 bool HandleQueryResult(EventStore::ResultSet& result, QueryCallback queryCallback, const int64_t queryLimit, 80 int64_t& totalQueryCnt); 81 82 private: 83 EventExportedListener eventExportedListener_; 84 std::list<std::shared_ptr<CachedEvent>> cachedSysEvents_; 85 std::unordered_map<std::string, ExportPeriodInfo> allPeriodInfo_; 86 std::unordered_map<std::string, ExportPeriodInfo> allPeriodInfoInOneQueryRange_; 87 std::shared_ptr<EventReadRequest> req_; 88 }; 89 } // namespace HiviewDFX 90 } // namespace OHOS 91 92 #endif // HIVIEW_BASE_EVENT_EXPORT_EVENT_READ_HANDLER_H