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_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 struct ExportPeriodInfo { 52 // format: YYYYMMDDHH 53 std::string timeStamp; 54 55 // count of event which has been exported in 1 hour 56 size_t exportedCnt = 0; 57 ExportPeriodInfoExportPeriodInfo58 ExportPeriodInfo(const std::string& timeStamp, size_t exportedCnt) 59 : timeStamp(timeStamp), exportedCnt(exportedCnt) {} 60 }; 61 62 class EventReadHandler : public ExportBaseHandler { 63 public: 64 using EventExportedListener = std::function<void(int64_t, int64_t)>; 65 void SetEventExportedListener(EventExportedListener listener); 66 67 bool HandleRequest(RequestPtr request) override; 68 69 private: 70 using QueryCallback = std::function<bool(bool)>; 71 bool QuerySysEventInRange(const std::pair<int64_t, int64_t>& queryRange, const ExportEventList& eventList, 72 QueryCallback queryCallback); 73 bool QuerySysEvent(const int64_t beginSeq, const int64_t endSeq, const ExportEventList& eventList, 74 QueryCallback queryCallback); 75 bool HandleQueryResult(EventStore::ResultSet& result, QueryCallback queryCallback, const int64_t queryLimit, 76 int64_t& totalQueryCnt); 77 78 private: 79 EventExportedListener eventExportedListener_; 80 std::list<std::shared_ptr<CachedEvent>> cachedSysEvents_; 81 std::unordered_map<std::string, ExportPeriodInfo> allPeriodInfo_; 82 std::unordered_map<std::string, ExportPeriodInfo> allPeriodInfoInOneQueryRange_; 83 }; 84 } // namespace HiviewDFX 85 } // namespace OHOS 86 87 #endif // HIVIEW_BASE_EVENT_EXPORT_EVENT_READ_HANDLER_H